blob: d4db6ebb089d0dc4ba2abefd44df0123fa3148dd [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;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000336 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
337 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000338 return -EINVAL;
339 *props = algo->desc.sadb_alg_id;
340
341 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
342 if (!p)
343 return -ENOMEM;
344
345 strcpy(p->alg_name, algo->name);
346 if (!p->alg_trunc_len)
347 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
348
349 *algpp = p;
350 return 0;
351}
352
Herbert Xu1a6509d2008-01-28 19:37:29 -0800353static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
354 struct nlattr *rta)
355{
356 struct xfrm_algo_aead *p, *ualg;
357 struct xfrm_algo_desc *algo;
358
359 if (!rta)
360 return 0;
361
362 ualg = nla_data(rta);
363
364 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
365 if (!algo)
366 return -ENOSYS;
367 *props = algo->desc.sadb_alg_id;
368
369 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
370 if (!p)
371 return -ENOMEM;
372
373 strcpy(p->alg_name, algo->name);
374 *algpp = p;
375 return 0;
376}
377
Steffen Klasserte2b19122011-03-28 19:47:30 +0000378static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
379 struct nlattr *rp)
380{
381 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000382 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000383
384 if (!replay_esn || !rp)
385 return 0;
386
387 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000388 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000389
Mathias Krauseecd79182012-09-20 10:01:49 +0000390 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000391 return -EINVAL;
392
393 return 0;
394}
395
Steffen Klassertd8647b72011-03-08 00:10:27 +0000396static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
397 struct xfrm_replay_state_esn **preplay_esn,
398 struct nlattr *rta)
399{
400 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000401 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000402
403 if (!rta)
404 return 0;
405
406 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000407 klen = xfrm_replay_state_esn_len(up);
408 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000409
Mathias Krauseecd79182012-09-20 10:01:49 +0000410 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000411 if (!p)
412 return -ENOMEM;
413
Mathias Krauseecd79182012-09-20 10:01:49 +0000414 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000415 if (!pp) {
416 kfree(p);
417 return -ENOMEM;
418 }
419
Mathias Krauseecd79182012-09-20 10:01:49 +0000420 memcpy(p, up, ulen);
421 memcpy(pp, up, ulen);
422
Steffen Klassertd8647b72011-03-08 00:10:27 +0000423 *replay_esn = p;
424 *preplay_esn = pp;
425
426 return 0;
427}
428
Joy Latten661697f2007-04-13 16:14:35 -0700429static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800430{
Trent Jaegerdf718372005-12-13 23:12:27 -0800431 int len = 0;
432
433 if (xfrm_ctx) {
434 len += sizeof(struct xfrm_user_sec_ctx);
435 len += xfrm_ctx->ctx_len;
436 }
437 return len;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
441{
442 memcpy(&x->id, &p->id, sizeof(x->id));
443 memcpy(&x->sel, &p->sel, sizeof(x->sel));
444 memcpy(&x->lft, &p->lft, sizeof(x->lft));
445 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800446 x->props.replay_window = min_t(unsigned int, p->replay_window,
447 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 x->props.reqid = p->reqid;
449 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700450 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700452
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700453 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700454 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800457/*
458 * someday when pfkey also has support, we could have the code
459 * somehow made shareable and move it to xfrm_state.c - JHS
460 *
461*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000462static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
463 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800464{
Thomas Graf5424f322007-08-22 14:01:33 -0700465 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000466 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700467 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
468 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
469 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800470
Steffen Klassertd8647b72011-03-08 00:10:27 +0000471 if (re) {
472 struct xfrm_replay_state_esn *replay_esn;
473 replay_esn = nla_data(re);
474 memcpy(x->replay_esn, replay_esn,
475 xfrm_replay_state_esn_len(replay_esn));
476 memcpy(x->preplay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
478 }
479
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800480 if (rp) {
481 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700482 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800483 memcpy(&x->replay, replay, sizeof(*replay));
484 memcpy(&x->preplay, replay, sizeof(*replay));
485 }
486
487 if (lt) {
488 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700489 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800490 x->curlft.bytes = ltime->bytes;
491 x->curlft.packets = ltime->packets;
492 x->curlft.add_time = ltime->add_time;
493 x->curlft.use_time = ltime->use_time;
494 }
495
Thomas Grafcf5cb792007-08-22 13:59:04 -0700496 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700497 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800498
Thomas Grafcf5cb792007-08-22 13:59:04 -0700499 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700500 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800501}
502
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800503static struct xfrm_state *xfrm_state_construct(struct net *net,
504 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700505 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int *errp)
507{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800508 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int err = -ENOMEM;
510
511 if (!x)
512 goto error_no_put;
513
514 copy_from_user_state(x, p);
515
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100516 if (attrs[XFRMA_SA_EXTRA_FLAGS])
517 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
518
Herbert Xu1a6509d2008-01-28 19:37:29 -0800519 if ((err = attach_aead(&x->aead, &x->props.ealgo,
520 attrs[XFRMA_ALG_AEAD])))
521 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000522 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
523 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000525 if (!x->props.aalgo) {
526 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
527 attrs[XFRMA_ALG_AUTH])))
528 goto error;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
531 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700532 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto error;
534 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
535 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700536 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700538
539 if (attrs[XFRMA_ENCAP]) {
540 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
541 sizeof(*x->encap), GFP_KERNEL);
542 if (x->encap == NULL)
543 goto error;
544 }
545
Martin Willi35d28562010-12-08 04:37:49 +0000546 if (attrs[XFRMA_TFCPAD])
547 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
548
Thomas Graffd211502007-09-06 03:28:08 -0700549 if (attrs[XFRMA_COADDR]) {
550 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
551 sizeof(*x->coaddr), GFP_KERNEL);
552 if (x->coaddr == NULL)
553 goto error;
554 }
555
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000556 xfrm_mark_get(attrs, &x->mark);
557
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700558 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (err)
560 goto error;
561
Thomas Graffd211502007-09-06 03:28:08 -0700562 if (attrs[XFRMA_SEC_CTX] &&
563 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800564 goto error;
565
Steffen Klassertd8647b72011-03-08 00:10:27 +0000566 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
567 attrs[XFRMA_REPLAY_ESN_VAL])))
568 goto error;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800571 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800572 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800573 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800574
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000575 if ((err = xfrm_init_replay(x)))
576 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800577
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000578 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000579 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return x;
582
583error:
584 x->km.state = XFRM_STATE_DEAD;
585 xfrm_state_put(x);
586error_no_put:
587 *errp = err;
588 return NULL;
589}
590
Christoph Hellwig22e70052007-01-02 15:22:30 -0800591static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700592 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800594 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700595 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct xfrm_state *x;
597 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700598 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Thomas Graf35a7aa02007-08-22 14:00:40 -0700600 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (err)
602 return err;
603
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800604 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!x)
606 return err;
607
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
610 err = xfrm_state_add(x);
611 else
612 err = xfrm_state_update(x);
613
Tetsuo Handa2e710292014-04-22 21:48:30 +0900614 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (err < 0) {
617 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800618 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700619 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700622 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000623 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700624 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700625
626 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700627out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700628 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return err;
630}
631
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800632static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
633 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700634 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700635 int *errp)
636{
637 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000638 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700639 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000640 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700641
642 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
643 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000644 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700645 } else {
646 xfrm_address_t *saddr = NULL;
647
Thomas Graf35a7aa02007-08-22 14:00:40 -0700648 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700649 if (!saddr) {
650 err = -EINVAL;
651 goto out;
652 }
653
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800654 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000655 x = xfrm_state_lookup_byaddr(net, mark,
656 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800657 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700658 }
659
660 out:
661 if (!x && errp)
662 *errp = err;
663 return x;
664}
665
Christoph Hellwig22e70052007-01-02 15:22:30 -0800666static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700667 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800669 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700671 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700672 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700673 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800675 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
David S. Miller6f68dc32006-06-08 23:58:52 -0700679 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700680 goto out;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700683 err = -EPERM;
684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700687 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600688
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700689 if (err < 0)
690 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700691
692 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000693 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700694 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700695 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700697out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900698 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700699 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
704{
Mathias Krausef778a632012-09-19 11:33:39 +0000705 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 memcpy(&p->id, &x->id, sizeof(p->id));
707 memcpy(&p->sel, &x->sel, sizeof(p->sel));
708 memcpy(&p->lft, &x->lft, sizeof(p->lft));
709 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
710 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700711 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 p->mode = x->props.mode;
713 p->replay_window = x->props.replay_window;
714 p->reqid = x->props.reqid;
715 p->family = x->props.family;
716 p->flags = x->props.flags;
717 p->seq = x->km.seq;
718}
719
720struct xfrm_dump_info {
721 struct sk_buff *in_skb;
722 struct sk_buff *out_skb;
723 u32 nlmsg_seq;
724 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725};
726
Thomas Grafc0144be2007-08-22 13:55:43 -0700727static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
728{
Thomas Grafc0144be2007-08-22 13:55:43 -0700729 struct xfrm_user_sec_ctx *uctx;
730 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700731 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700732
733 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
734 if (attr == NULL)
735 return -EMSGSIZE;
736
737 uctx = nla_data(attr);
738 uctx->exttype = XFRMA_SEC_CTX;
739 uctx->len = ctx_size;
740 uctx->ctx_doi = s->ctx_doi;
741 uctx->ctx_alg = s->ctx_alg;
742 uctx->ctx_len = s->ctx_len;
743 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
744
745 return 0;
746}
747
Martin Willi4447bb32009-11-25 00:29:52 +0000748static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
749{
750 struct xfrm_algo *algo;
751 struct nlattr *nla;
752
753 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
754 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
755 if (!nla)
756 return -EMSGSIZE;
757
758 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000759 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000760 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
761 algo->alg_key_len = auth->alg_key_len;
762
763 return 0;
764}
765
Herbert Xu68325d32007-10-09 13:30:57 -0700766/* Don't change this without updating xfrm_sa_len! */
767static int copy_to_user_state_extra(struct xfrm_state *x,
768 struct xfrm_usersa_info *p,
769 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700771 int ret = 0;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 copy_to_user_state(x, p);
774
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100775 if (x->props.extra_flags) {
776 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
777 x->props.extra_flags);
778 if (ret)
779 goto out;
780 }
781
David S. Miller1d1e34d2012-06-27 21:57:03 -0700782 if (x->coaddr) {
783 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
784 if (ret)
785 goto out;
786 }
787 if (x->lastused) {
788 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
789 if (ret)
790 goto out;
791 }
792 if (x->aead) {
793 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
794 if (ret)
795 goto out;
796 }
797 if (x->aalg) {
798 ret = copy_to_user_auth(x->aalg, skb);
799 if (!ret)
800 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
801 xfrm_alg_auth_len(x->aalg), x->aalg);
802 if (ret)
803 goto out;
804 }
805 if (x->ealg) {
806 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
807 if (ret)
808 goto out;
809 }
810 if (x->calg) {
811 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
812 if (ret)
813 goto out;
814 }
815 if (x->encap) {
816 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
817 if (ret)
818 goto out;
819 }
820 if (x->tfcpad) {
821 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
822 if (ret)
823 goto out;
824 }
825 ret = xfrm_mark_put(skb, &x->mark);
826 if (ret)
827 goto out;
828 if (x->replay_esn) {
829 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
830 xfrm_replay_state_esn_len(x->replay_esn),
831 x->replay_esn);
832 if (ret)
833 goto out;
834 }
835 if (x->security)
836 ret = copy_sec_ctx(x->security, skb);
837out:
838 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700839}
840
841static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
842{
843 struct xfrm_dump_info *sp = ptr;
844 struct sk_buff *in_skb = sp->in_skb;
845 struct sk_buff *skb = sp->out_skb;
846 struct xfrm_usersa_info *p;
847 struct nlmsghdr *nlh;
848 int err;
849
Eric W. Biederman15e47302012-09-07 20:12:54 +0000850 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700851 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
852 if (nlh == NULL)
853 return -EMSGSIZE;
854
855 p = nlmsg_data(nlh);
856
857 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700858 if (err) {
859 nlmsg_cancel(skb, nlh);
860 return err;
861 }
Thomas Graf98250692007-08-22 12:47:26 -0700862 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Timo Teras4c563f72008-02-28 21:31:08 -0800866static int xfrm_dump_sa_done(struct netlink_callback *cb)
867{
868 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800869 struct sock *sk = cb->skb->sk;
870 struct net *net = sock_net(sk);
871
872 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800873 return 0;
874}
875
Nicolas Dichteld3623092014-02-14 15:30:36 +0100876static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
878{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800879 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800880 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct xfrm_dump_info info;
882
Timo Teras4c563f72008-02-28 21:31:08 -0800883 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
884 sizeof(cb->args) - sizeof(cb->args[0]));
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 info.in_skb = cb->skb;
887 info.out_skb = skb;
888 info.nlmsg_seq = cb->nlh->nlmsg_seq;
889 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800890
891 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100892 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100893 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100894 u8 proto = 0;
895 int err;
896
Timo Teras4c563f72008-02-28 21:31:08 -0800897 cb->args[0] = 1;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100898
899 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
900 xfrma_policy);
901 if (err < 0)
902 return err;
903
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100904 if (attrs[XFRMA_ADDRESS_FILTER]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100905 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
906 if (filter == NULL)
907 return -ENOMEM;
908
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100909 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
Nicolas Dichteld3623092014-02-14 15:30:36 +0100910 sizeof(*filter));
911 }
912
913 if (attrs[XFRMA_PROTO])
914 proto = nla_get_u8(attrs[XFRMA_PROTO]);
915
916 xfrm_state_walk_init(walk, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -0800917 }
918
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800919 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return skb->len;
922}
923
924static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
925 struct xfrm_state *x, u32 seq)
926{
927 struct xfrm_dump_info info;
928 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000929 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Thomas Graf7deb2262007-08-22 13:57:39 -0700931 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!skb)
933 return ERR_PTR(-ENOMEM);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 info.in_skb = in_skb;
936 info.out_skb = skb;
937 info.nlmsg_seq = seq;
938 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Mathias Krause864745d2012-09-13 11:41:26 +0000940 err = dump_one_state(x, 0, &info);
941 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000943 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946 return skb;
947}
948
Michal Kubecek21ee5432014-06-03 10:26:06 +0200949/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
950 * Must be called with RCU read lock.
951 */
952static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
953 u32 pid, unsigned int group)
954{
955 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
956
957 if (nlsk)
958 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
959 else
960 return -1;
961}
962
Thomas Graf7deb2262007-08-22 13:57:39 -0700963static inline size_t xfrm_spdinfo_msgsize(void)
964{
965 return NLMSG_ALIGN(4)
966 + nla_total_size(sizeof(struct xfrmu_spdinfo))
967 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
968}
969
Alexey Dobriyane0710412010-01-23 13:37:10 +0000970static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000971 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700972{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700973 struct xfrmk_spdinfo si;
974 struct xfrmu_spdinfo spc;
975 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700976 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700977 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700978 u32 *f;
979
Eric W. Biederman15e47302012-09-07 20:12:54 +0000980 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300981 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700982 return -EMSGSIZE;
983
984 f = nlmsg_data(nlh);
985 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000986 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700987 spc.incnt = si.incnt;
988 spc.outcnt = si.outcnt;
989 spc.fwdcnt = si.fwdcnt;
990 spc.inscnt = si.inscnt;
991 spc.outscnt = si.outscnt;
992 spc.fwdscnt = si.fwdscnt;
993 sph.spdhcnt = si.spdhcnt;
994 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700995
David S. Miller1d1e34d2012-06-27 21:57:03 -0700996 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
997 if (!err)
998 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
999 if (err) {
1000 nlmsg_cancel(skb, nlh);
1001 return err;
1002 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001003
1004 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001005}
1006
1007static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001008 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001009{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001010 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001011 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001012 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001013 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001014 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001015
Thomas Graf7deb2262007-08-22 13:57:39 -07001016 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001017 if (r_skb == NULL)
1018 return -ENOMEM;
1019
Eric W. Biederman15e47302012-09-07 20:12:54 +00001020 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001021 BUG();
1022
Eric W. Biederman15e47302012-09-07 20:12:54 +00001023 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001024}
1025
Thomas Graf7deb2262007-08-22 13:57:39 -07001026static inline size_t xfrm_sadinfo_msgsize(void)
1027{
1028 return NLMSG_ALIGN(4)
1029 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1030 + nla_total_size(4); /* XFRMA_SAD_CNT */
1031}
1032
Alexey Dobriyane0710412010-01-23 13:37:10 +00001033static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001034 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001035{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001036 struct xfrmk_sadinfo si;
1037 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001038 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001039 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001040 u32 *f;
1041
Eric W. Biederman15e47302012-09-07 20:12:54 +00001042 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001043 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001044 return -EMSGSIZE;
1045
1046 f = nlmsg_data(nlh);
1047 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001048 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001049
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001050 sh.sadhmcnt = si.sadhmcnt;
1051 sh.sadhcnt = si.sadhcnt;
1052
David S. Miller1d1e34d2012-06-27 21:57:03 -07001053 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1054 if (!err)
1055 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1056 if (err) {
1057 nlmsg_cancel(skb, nlh);
1058 return err;
1059 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001060
1061 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001062}
1063
1064static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001065 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001066{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001067 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001068 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001069 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001070 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001071 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001072
Thomas Graf7deb2262007-08-22 13:57:39 -07001073 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001074 if (r_skb == NULL)
1075 return -ENOMEM;
1076
Eric W. Biederman15e47302012-09-07 20:12:54 +00001077 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001078 BUG();
1079
Eric W. Biederman15e47302012-09-07 20:12:54 +00001080 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001081}
1082
Christoph Hellwig22e70052007-01-02 15:22:30 -08001083static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001084 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001086 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001087 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 struct xfrm_state *x;
1089 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001090 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001092 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (x == NULL)
1094 goto out_noput;
1095
1096 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1097 if (IS_ERR(resp_skb)) {
1098 err = PTR_ERR(resp_skb);
1099 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001100 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102 xfrm_state_put(x);
1103out_noput:
1104 return err;
1105}
1106
Christoph Hellwig22e70052007-01-02 15:22:30 -08001107static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001108 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001110 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct xfrm_state *x;
1112 struct xfrm_userspi_info *p;
1113 struct sk_buff *resp_skb;
1114 xfrm_address_t *daddr;
1115 int family;
1116 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001117 u32 mark;
1118 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Thomas Graf7b67c852007-08-22 13:53:52 -07001120 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001121 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (err)
1123 goto out_noput;
1124
1125 family = p->info.family;
1126 daddr = &p->info.id.daddr;
1127
1128 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001129
1130 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001132 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001133 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 xfrm_state_put(x);
1135 x = NULL;
1136 }
1137 }
1138
1139 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001140 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 p->info.id.proto, daddr,
1142 &p->info.saddr, 1,
1143 family);
1144 err = -ENOENT;
1145 if (x == NULL)
1146 goto out_noput;
1147
Herbert Xu658b2192007-10-09 13:29:52 -07001148 err = xfrm_alloc_spi(x, p->min, p->max);
1149 if (err)
1150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Herbert Xu658b2192007-10-09 13:29:52 -07001152 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (IS_ERR(resp_skb)) {
1154 err = PTR_ERR(resp_skb);
1155 goto out;
1156 }
1157
Eric W. Biederman15e47302012-09-07 20:12:54 +00001158 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160out:
1161 xfrm_state_put(x);
1162out_noput:
1163 return err;
1164}
1165
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001166static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 switch (dir) {
1169 case XFRM_POLICY_IN:
1170 case XFRM_POLICY_OUT:
1171 case XFRM_POLICY_FWD:
1172 break;
1173
1174 default:
1175 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 return 0;
1179}
1180
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001181static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001182{
1183 switch (type) {
1184 case XFRM_POLICY_TYPE_MAIN:
1185#ifdef CONFIG_XFRM_SUB_POLICY
1186 case XFRM_POLICY_TYPE_SUB:
1187#endif
1188 break;
1189
1190 default:
1191 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001192 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001193
1194 return 0;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1198{
Fan Due682adf2013-11-07 17:47:48 +08001199 int ret;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 switch (p->share) {
1202 case XFRM_SHARE_ANY:
1203 case XFRM_SHARE_SESSION:
1204 case XFRM_SHARE_USER:
1205 case XFRM_SHARE_UNIQUE:
1206 break;
1207
1208 default:
1209 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 switch (p->action) {
1213 case XFRM_POLICY_ALLOW:
1214 case XFRM_POLICY_BLOCK:
1215 break;
1216
1217 default:
1218 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 switch (p->sel.family) {
1222 case AF_INET:
1223 break;
1224
1225 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001226#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 break;
1228#else
1229 return -EAFNOSUPPORT;
1230#endif
1231
1232 default:
1233 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Fan Due682adf2013-11-07 17:47:48 +08001236 ret = verify_policy_dir(p->dir);
1237 if (ret)
1238 return ret;
1239 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1240 return -EINVAL;
1241
1242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Thomas Graf5424f322007-08-22 14:01:33 -07001245static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001246{
Thomas Graf5424f322007-08-22 14:01:33 -07001247 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001248 struct xfrm_user_sec_ctx *uctx;
1249
1250 if (!rt)
1251 return 0;
1252
Thomas Graf5424f322007-08-22 14:01:33 -07001253 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001254 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001255}
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1258 int nr)
1259{
1260 int i;
1261
1262 xp->xfrm_nr = nr;
1263 for (i = 0; i < nr; i++, ut++) {
1264 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1265
1266 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1267 memcpy(&t->saddr, &ut->saddr,
1268 sizeof(xfrm_address_t));
1269 t->reqid = ut->reqid;
1270 t->mode = ut->mode;
1271 t->share = ut->share;
1272 t->optional = ut->optional;
1273 t->aalgos = ut->aalgos;
1274 t->ealgos = ut->ealgos;
1275 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001276 /* If all masks are ~0, then we allow all algorithms. */
1277 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001278 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280}
1281
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001282static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1283{
1284 int i;
1285
1286 if (nr > XFRM_MAX_DEPTH)
1287 return -EINVAL;
1288
1289 for (i = 0; i < nr; i++) {
1290 /* We never validated the ut->family value, so many
1291 * applications simply leave it at zero. The check was
1292 * never made and ut->family was ignored because all
1293 * templates could be assumed to have the same family as
1294 * the policy itself. Now that we will have ipv4-in-ipv6
1295 * and ipv6-in-ipv4 tunnels, this is no longer true.
1296 */
1297 if (!ut[i].family)
1298 ut[i].family = family;
1299
1300 switch (ut[i].family) {
1301 case AF_INET:
1302 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001303#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001304 case AF_INET6:
1305 break;
1306#endif
1307 default:
1308 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001309 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001310 }
1311
1312 return 0;
1313}
1314
Thomas Graf5424f322007-08-22 14:01:33 -07001315static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Thomas Graf5424f322007-08-22 14:01:33 -07001317 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (!rt) {
1320 pol->xfrm_nr = 0;
1321 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001322 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1323 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001324 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001326 err = validate_tmpl(nr, utmpl, pol->family);
1327 if (err)
1328 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Thomas Graf5424f322007-08-22 14:01:33 -07001330 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 return 0;
1333}
1334
Thomas Graf5424f322007-08-22 14:01:33 -07001335static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001336{
Thomas Graf5424f322007-08-22 14:01:33 -07001337 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001338 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001339 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001340 int err;
1341
1342 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001343 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001344 type = upt->type;
1345 }
1346
1347 err = verify_policy_type(type);
1348 if (err)
1349 return err;
1350
1351 *tp = type;
1352 return 0;
1353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1356{
1357 xp->priority = p->priority;
1358 xp->index = p->index;
1359 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1360 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1361 xp->action = p->action;
1362 xp->flags = p->flags;
1363 xp->family = p->sel.family;
1364 /* XXX xp->share = p->share; */
1365}
1366
1367static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1368{
Mathias Krause7b789832012-09-19 11:33:40 +00001369 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1371 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1372 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1373 p->priority = xp->priority;
1374 p->index = xp->index;
1375 p->sel.family = xp->family;
1376 p->dir = dir;
1377 p->action = xp->action;
1378 p->flags = xp->flags;
1379 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1380}
1381
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001382static 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 -07001383{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001384 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int err;
1386
1387 if (!xp) {
1388 *errp = -ENOMEM;
1389 return NULL;
1390 }
1391
1392 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001393
Thomas Graf35a7aa02007-08-22 14:00:40 -07001394 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001395 if (err)
1396 goto error;
1397
Thomas Graf35a7aa02007-08-22 14:00:40 -07001398 if (!(err = copy_from_user_tmpl(xp, attrs)))
1399 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001400 if (err)
1401 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001403 xfrm_mark_get(attrs, &xp->mark);
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001406 error:
1407 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001408 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001409 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001410 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Christoph Hellwig22e70052007-01-02 15:22:30 -08001413static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001414 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001416 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001417 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001419 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int err;
1421 int excl;
1422
1423 err = verify_newpolicy_info(p);
1424 if (err)
1425 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001426 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001427 if (err)
1428 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001430 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (!xp)
1432 return err;
1433
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001434 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001435 * Aha! this is anti-netlink really i.e more pfkey derived
1436 * in netlink excl is a flag and you wouldnt need
1437 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1439 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001440 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001443 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 kfree(xp);
1445 return err;
1446 }
1447
Herbert Xuf60f6b82005-06-18 22:44:37 -07001448 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001449 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001450 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001451 km_policy_notify(xp, p->dir, &c);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 xfrm_pol_put(xp);
1454
1455 return 0;
1456}
1457
1458static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1459{
1460 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1461 int i;
1462
1463 if (xp->xfrm_nr == 0)
1464 return 0;
1465
1466 for (i = 0; i < xp->xfrm_nr; i++) {
1467 struct xfrm_user_tmpl *up = &vec[i];
1468 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1469
Mathias Krause1f868402012-09-19 11:33:41 +00001470 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001472 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1474 up->reqid = kp->reqid;
1475 up->mode = kp->mode;
1476 up->share = kp->share;
1477 up->optional = kp->optional;
1478 up->aalgos = kp->aalgos;
1479 up->ealgos = kp->ealgos;
1480 up->calgos = kp->calgos;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Thomas Grafc0144be2007-08-22 13:55:43 -07001483 return nla_put(skb, XFRMA_TMPL,
1484 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001485}
1486
Serge Hallyn0d681622006-07-24 23:30:44 -07001487static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1488{
1489 if (x->security) {
1490 return copy_sec_ctx(x->security, skb);
1491 }
1492 return 0;
1493}
1494
1495static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1496{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001497 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001498 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001499 return 0;
1500}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001501static inline size_t userpolicy_type_attrsize(void)
1502{
1503#ifdef CONFIG_XFRM_SUB_POLICY
1504 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1505#else
1506 return 0;
1507#endif
1508}
Serge Hallyn0d681622006-07-24 23:30:44 -07001509
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001510#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001511static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001512{
Thomas Grafc0144be2007-08-22 13:55:43 -07001513 struct xfrm_userpolicy_type upt = {
1514 .type = type,
1515 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001516
Thomas Grafc0144be2007-08-22 13:55:43 -07001517 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001518}
1519
1520#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001521static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001522{
1523 return 0;
1524}
1525#endif
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1528{
1529 struct xfrm_dump_info *sp = ptr;
1530 struct xfrm_userpolicy_info *p;
1531 struct sk_buff *in_skb = sp->in_skb;
1532 struct sk_buff *skb = sp->out_skb;
1533 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001534 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Eric W. Biederman15e47302012-09-07 20:12:54 +00001536 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001537 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1538 if (nlh == NULL)
1539 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Thomas Graf7b67c852007-08-22 13:53:52 -07001541 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001543 err = copy_to_user_tmpl(xp, skb);
1544 if (!err)
1545 err = copy_to_user_sec_ctx(xp, skb);
1546 if (!err)
1547 err = copy_to_user_policy_type(xp->type, skb);
1548 if (!err)
1549 err = xfrm_mark_put(skb, &xp->mark);
1550 if (err) {
1551 nlmsg_cancel(skb, nlh);
1552 return err;
1553 }
Thomas Graf98250692007-08-22 12:47:26 -07001554 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Timo Teras4c563f72008-02-28 21:31:08 -08001558static int xfrm_dump_policy_done(struct netlink_callback *cb)
1559{
1560 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001561 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001562
Fan Du283bc9f2013-11-07 17:47:50 +08001563 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001564 return 0;
1565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1568{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001569 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001570 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct xfrm_dump_info info;
1572
Timo Teras4c563f72008-02-28 21:31:08 -08001573 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1574 sizeof(cb->args) - sizeof(cb->args[0]));
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 info.in_skb = cb->skb;
1577 info.out_skb = skb;
1578 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1579 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001580
1581 if (!cb->args[0]) {
1582 cb->args[0] = 1;
1583 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1584 }
1585
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001586 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 return skb->len;
1589}
1590
1591static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1592 struct xfrm_policy *xp,
1593 int dir, u32 seq)
1594{
1595 struct xfrm_dump_info info;
1596 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Thomas Graf7deb2262007-08-22 13:57:39 -07001599 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (!skb)
1601 return ERR_PTR(-ENOMEM);
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 info.in_skb = in_skb;
1604 info.out_skb = skb;
1605 info.nlmsg_seq = seq;
1606 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Mathias Krausec2546372012-09-14 09:58:32 +00001608 err = dump_one_policy(xp, dir, 0, &info);
1609 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001611 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
1614 return skb;
1615}
1616
Christoph Hellwig22e70052007-01-02 15:22:30 -08001617static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001618 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001620 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 struct xfrm_policy *xp;
1622 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001623 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001625 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001627 struct xfrm_mark m;
1628 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Thomas Graf7b67c852007-08-22 13:53:52 -07001630 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1632
Thomas Graf35a7aa02007-08-22 14:00:40 -07001633 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001634 if (err)
1635 return err;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 err = verify_policy_dir(p->dir);
1638 if (err)
1639 return err;
1640
1641 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001642 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001643 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001644 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001645 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001646
Thomas Graf35a7aa02007-08-22 14:00:40 -07001647 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001648 if (err)
1649 return err;
1650
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001651 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001652 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001653 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001654
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001655 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001656 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001657 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001658 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001659 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001660 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001661 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (xp == NULL)
1664 return -ENOENT;
1665
1666 if (!delete) {
1667 struct sk_buff *resp_skb;
1668
1669 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1670 if (IS_ERR(resp_skb)) {
1671 err = PTR_ERR(resp_skb);
1672 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001673 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001674 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001676 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001677 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001678
1679 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001680 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001681
Herbert Xue7443892005-06-18 22:44:18 -07001682 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001683 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001684 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001685 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001686 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001689out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001690 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001691 if (delete && err == 0)
1692 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return err;
1694}
1695
Christoph Hellwig22e70052007-01-02 15:22:30 -08001696static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001697 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001699 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001700 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001701 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001702 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Tetsuo Handa2e710292014-04-22 21:48:30 +09001704 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001705 if (err) {
1706 if (err == -ESRCH) /* empty table */
1707 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001708 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001709 }
Herbert Xubf088672005-06-18 22:44:00 -07001710 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001711 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001712 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001713 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001714 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001715 km_state_notify(NULL, &c);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718}
1719
Steffen Klassertd8647b72011-03-08 00:10:27 +00001720static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001721{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001722 size_t replay_size = x->replay_esn ?
1723 xfrm_replay_state_esn_len(x->replay_esn) :
1724 sizeof(struct xfrm_replay_state);
1725
Thomas Graf7deb2262007-08-22 13:57:39 -07001726 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001727 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001728 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001729 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001730 + nla_total_size(4) /* XFRM_AE_RTHR */
1731 + nla_total_size(4); /* XFRM_AE_ETHR */
1732}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001733
David S. Miller214e0052011-02-24 00:02:38 -05001734static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001735{
1736 struct xfrm_aevent_id *id;
1737 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001738 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001739
Eric W. Biederman15e47302012-09-07 20:12:54 +00001740 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001741 if (nlh == NULL)
1742 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001743
Thomas Graf7b67c852007-08-22 13:53:52 -07001744 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001745 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001746 id->sa_id.spi = x->id.spi;
1747 id->sa_id.family = x->props.family;
1748 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001749 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001750 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001751 id->flags = c->data.aevent;
1752
David S. Millerd0fde792012-03-29 04:02:26 -04001753 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001754 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1755 xfrm_replay_state_esn_len(x->replay_esn),
1756 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001757 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001758 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1759 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001760 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001761 if (err)
1762 goto out_cancel;
1763 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1764 if (err)
1765 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001766
David S. Miller1d1e34d2012-06-27 21:57:03 -07001767 if (id->flags & XFRM_AE_RTHR) {
1768 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1769 if (err)
1770 goto out_cancel;
1771 }
1772 if (id->flags & XFRM_AE_ETHR) {
1773 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1774 x->replay_maxage * 10 / HZ);
1775 if (err)
1776 goto out_cancel;
1777 }
1778 err = xfrm_mark_put(skb, &x->mark);
1779 if (err)
1780 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001781
Thomas Graf98250692007-08-22 12:47:26 -07001782 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001783
David S. Miller1d1e34d2012-06-27 21:57:03 -07001784out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001785 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001786 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001787}
1788
Christoph Hellwig22e70052007-01-02 15:22:30 -08001789static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001790 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001791{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001792 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001793 struct xfrm_state *x;
1794 struct sk_buff *r_skb;
1795 int err;
1796 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001797 u32 mark;
1798 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001799 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001800 struct xfrm_usersa_id *id = &p->sa_id;
1801
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001802 mark = xfrm_mark_get(attrs, &m);
1803
1804 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001805 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001807
1808 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1809 if (r_skb == NULL) {
1810 xfrm_state_put(x);
1811 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001812 }
1813
1814 /*
1815 * XXX: is this lock really needed - none of the other
1816 * gets lock (the concern is things getting updated
1817 * while we are still reading) - jhs
1818 */
1819 spin_lock_bh(&x->lock);
1820 c.data.aevent = p->flags;
1821 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001822 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001823
1824 if (build_aevent(r_skb, x, &c) < 0)
1825 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001826 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001827 spin_unlock_bh(&x->lock);
1828 xfrm_state_put(x);
1829 return err;
1830}
1831
Christoph Hellwig22e70052007-01-02 15:22:30 -08001832static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001833 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001834{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001835 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001836 struct xfrm_state *x;
1837 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001838 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001839 u32 mark = 0;
1840 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001841 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001842 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001843 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001844 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001845
Steffen Klassertd8647b72011-03-08 00:10:27 +00001846 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001847 return err;
1848
1849 /* pedantic mode - thou shalt sayeth replaceth */
1850 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1851 return err;
1852
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001853 mark = xfrm_mark_get(attrs, &m);
1854
1855 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 -08001856 if (x == NULL)
1857 return -ESRCH;
1858
1859 if (x->km.state != XFRM_STATE_VALID)
1860 goto out;
1861
Steffen Klassert4479ff72013-09-09 09:39:01 +02001862 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001863 if (err)
1864 goto out;
1865
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001866 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001867 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001868 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001869
1870 c.event = nlh->nlmsg_type;
1871 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001872 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001873 c.data.aevent = XFRM_AE_CU;
1874 km_state_notify(x, &c);
1875 err = 0;
1876out:
1877 xfrm_state_put(x);
1878 return err;
1879}
1880
Christoph Hellwig22e70052007-01-02 15:22:30 -08001881static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001882 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001884 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001885 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001886 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001887 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001888
Thomas Graf35a7aa02007-08-22 14:00:40 -07001889 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001890 if (err)
1891 return err;
1892
Tetsuo Handa2e710292014-04-22 21:48:30 +09001893 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001894 if (err) {
1895 if (err == -ESRCH) /* empty table */
1896 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001897 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001898 }
1899
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001900 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001901 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001902 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001903 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001904 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001905 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return 0;
1907}
1908
Christoph Hellwig22e70052007-01-02 15:22:30 -08001909static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001910 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001911{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001912 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001913 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001914 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001915 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001916 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001917 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001918 struct xfrm_mark m;
1919 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001920
Thomas Graf35a7aa02007-08-22 14:00:40 -07001921 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001922 if (err)
1923 return err;
1924
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001925 err = verify_policy_dir(p->dir);
1926 if (err)
1927 return err;
1928
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001929 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001930 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001931 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001932 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001933 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001934
Thomas Graf35a7aa02007-08-22 14:00:40 -07001935 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001936 if (err)
1937 return err;
1938
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001939 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001940 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001941 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001942
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001943 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001944 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001945 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001946 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001947 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001948 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001949 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001950 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001951 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001952 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001953
Timo Teräsea2dea92010-03-31 00:17:05 +00001954 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001955 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001956
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001957 err = 0;
1958 if (up->hard) {
1959 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001960 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001961 } else {
1962 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001963 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001964 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001965 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001966
1967out:
1968 xfrm_pol_put(xp);
1969 return err;
1970}
1971
Christoph Hellwig22e70052007-01-02 15:22:30 -08001972static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001973 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001974{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001975 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001976 struct xfrm_state *x;
1977 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001978 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001979 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001980 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001981 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001982
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001983 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 -08001984
David S. Miller3a765aa2007-02-26 14:52:21 -08001985 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001986 if (x == NULL)
1987 return err;
1988
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001989 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001990 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001991 if (x->km.state != XFRM_STATE_VALID)
1992 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001993 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001994
Joy Latten161a09e2006-11-27 13:11:54 -06001995 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001996 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001997 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001998 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001999 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002000out:
2001 spin_unlock_bh(&x->lock);
2002 xfrm_state_put(x);
2003 return err;
2004}
2005
Christoph Hellwig22e70052007-01-02 15:22:30 -08002006static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002007 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002008{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002009 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002010 struct xfrm_policy *xp;
2011 struct xfrm_user_tmpl *ut;
2012 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002013 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002014 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002015
Thomas Graf7b67c852007-08-22 13:53:52 -07002016 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002017 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002018 int err = -ENOMEM;
2019
2020 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002021 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002022
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002023 xfrm_mark_get(attrs, &mark);
2024
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002025 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002026 if (err)
2027 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002028
2029 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002030 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002031 if (!xp)
2032 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002033
2034 memcpy(&x->id, &ua->id, sizeof(ua->id));
2035 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2036 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002037 xp->mark.m = x->mark.m = mark.m;
2038 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002039 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002040 /* extract the templates and for each call km_key */
2041 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2042 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2043 memcpy(&x->id, &t->id, sizeof(x->id));
2044 x->props.mode = t->mode;
2045 x->props.reqid = t->reqid;
2046 x->props.family = ut->family;
2047 t->aalgos = ua->aalgos;
2048 t->ealgos = ua->ealgos;
2049 t->calgos = ua->calgos;
2050 err = km_query(x, t, xp);
2051
2052 }
2053
2054 kfree(x);
2055 kfree(xp);
2056
2057 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002058
2059bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002060 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002061free_state:
2062 kfree(x);
2063nomem:
2064 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002065}
2066
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002068static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002069 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002070 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002071{
Thomas Graf5424f322007-08-22 14:01:33 -07002072 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002073 struct xfrm_user_migrate *um;
2074 int i, num_migrate;
2075
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002076 if (k != NULL) {
2077 struct xfrm_user_kmaddress *uk;
2078
2079 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2080 memcpy(&k->local, &uk->local, sizeof(k->local));
2081 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2082 k->family = uk->family;
2083 k->reserved = uk->reserved;
2084 }
2085
Thomas Graf5424f322007-08-22 14:01:33 -07002086 um = nla_data(rt);
2087 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002088
2089 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2090 return -EINVAL;
2091
2092 for (i = 0; i < num_migrate; i++, um++, ma++) {
2093 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2094 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2095 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2096 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2097
2098 ma->proto = um->proto;
2099 ma->mode = um->mode;
2100 ma->reqid = um->reqid;
2101
2102 ma->old_family = um->old_family;
2103 ma->new_family = um->new_family;
2104 }
2105
2106 *num = i;
2107 return 0;
2108}
2109
2110static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002111 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002112{
Thomas Graf7b67c852007-08-22 13:53:52 -07002113 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002114 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002115 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002116 u8 type;
2117 int err;
2118 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002119 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002120
Thomas Graf35a7aa02007-08-22 14:00:40 -07002121 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002122 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002123
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002124 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2125
Thomas Graf5424f322007-08-22 14:01:33 -07002126 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002127 if (err)
2128 return err;
2129
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002130 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002131 if (err)
2132 return err;
2133
2134 if (!n)
2135 return 0;
2136
Fan Du8d549c42013-11-07 17:47:49 +08002137 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002138
2139 return 0;
2140}
2141#else
2142static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002143 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002144{
2145 return -ENOPROTOOPT;
2146}
2147#endif
2148
2149#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002150static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002151{
2152 struct xfrm_user_migrate um;
2153
2154 memset(&um, 0, sizeof(um));
2155 um.proto = m->proto;
2156 um.mode = m->mode;
2157 um.reqid = m->reqid;
2158 um.old_family = m->old_family;
2159 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2160 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2161 um.new_family = m->new_family;
2162 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2163 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2164
Thomas Grafc0144be2007-08-22 13:55:43 -07002165 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002166}
2167
David S. Miller183cad12011-02-24 00:28:01 -05002168static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002169{
2170 struct xfrm_user_kmaddress uk;
2171
2172 memset(&uk, 0, sizeof(uk));
2173 uk.family = k->family;
2174 uk.reserved = k->reserved;
2175 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002176 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002177
2178 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2179}
2180
2181static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002182{
2183 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002184 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2185 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2186 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002187}
2188
David S. Miller183cad12011-02-24 00:28:01 -05002189static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2190 int num_migrate, const struct xfrm_kmaddress *k,
2191 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002192{
David S. Miller183cad12011-02-24 00:28:01 -05002193 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002194 struct xfrm_userpolicy_id *pol_id;
2195 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002196 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002197
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002198 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2199 if (nlh == NULL)
2200 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002201
Thomas Graf7b67c852007-08-22 13:53:52 -07002202 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002203 /* copy data from selector, dir, and type to the pol_id */
2204 memset(pol_id, 0, sizeof(*pol_id));
2205 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2206 pol_id->dir = dir;
2207
David S. Miller1d1e34d2012-06-27 21:57:03 -07002208 if (k != NULL) {
2209 err = copy_to_user_kmaddress(k, skb);
2210 if (err)
2211 goto out_cancel;
2212 }
2213 err = copy_to_user_policy_type(type, skb);
2214 if (err)
2215 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002216 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002217 err = copy_to_user_migrate(mp, skb);
2218 if (err)
2219 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002220 }
2221
Thomas Graf98250692007-08-22 12:47:26 -07002222 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002223
2224out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002225 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002226 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002227}
2228
David S. Miller183cad12011-02-24 00:28:01 -05002229static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2230 const struct xfrm_migrate *m, int num_migrate,
2231 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002233 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002234 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002236 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002237 if (skb == NULL)
2238 return -ENOMEM;
2239
2240 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002241 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002242 BUG();
2243
Michal Kubecek21ee5432014-06-03 10:26:06 +02002244 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002245}
2246#else
David S. Miller183cad12011-02-24 00:28:01 -05002247static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2248 const struct xfrm_migrate *m, int num_migrate,
2249 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002250{
2251 return -ENOPROTOOPT;
2252}
2253#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002254
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002255#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002256
2257static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002258 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002259 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2260 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2261 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2262 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2263 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2264 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002265 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002266 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002267 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002268 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002269 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002270 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002271 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002272 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2273 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002274 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002275 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002276 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2277 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278};
2279
Thomas Graf492b5582005-05-03 14:26:40 -07002280#undef XMSGSIZE
2281
Thomas Grafcf5cb792007-08-22 13:59:04 -07002282static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002283 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2284 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2285 [XFRMA_LASTUSED] = { .type = NLA_U64},
2286 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002287 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002288 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2291 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2292 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2293 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2294 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2295 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2296 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2297 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2298 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2299 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2300 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2301 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002302 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002303 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002304 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002305 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002306 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002307 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002308 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002309};
2310
Mathias Krause05600a72013-02-24 14:10:27 +01002311static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002312 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002314 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002315} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2316 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2317 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2318 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002319 .dump = xfrm_dump_sa,
2320 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002321 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2322 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2323 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002324 .dump = xfrm_dump_policy,
2325 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002326 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002327 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002328 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002329 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2330 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002331 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002332 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2333 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002334 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2335 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002336 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002337 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002338 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339};
2340
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002341static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002343 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002344 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002345 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002346 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 type -= XFRM_MSG_BASE;
2353 link = &xfrm_dispatch[type];
2354
2355 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002356 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002357 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Thomas Graf492b5582005-05-03 14:26:40 -07002359 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2360 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002361 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002365 {
2366 struct netlink_dump_control c = {
2367 .dump = link->dump,
2368 .done = link->done,
2369 };
2370 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373
Thomas Graf35a7aa02007-08-22 14:00:40 -07002374 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002375 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002376 if (err < 0)
2377 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002380 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Thomas Graf5424f322007-08-22 14:01:33 -07002382 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002385static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
Fan Du283bc9f2013-11-07 17:47:50 +08002387 struct net *net = sock_net(skb->sk);
2388
2389 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002390 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002391 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Thomas Graf7deb2262007-08-22 13:57:39 -07002394static inline size_t xfrm_expire_msgsize(void)
2395{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002396 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2397 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002398}
2399
David S. Miller214e0052011-02-24 00:02:38 -05002400static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
2402 struct xfrm_user_expire *ue;
2403 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002404 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Eric W. Biederman15e47302012-09-07 20:12:54 +00002406 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002407 if (nlh == NULL)
2408 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Thomas Graf7b67c852007-08-22 13:53:52 -07002410 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002412 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
David S. Miller1d1e34d2012-06-27 21:57:03 -07002414 err = xfrm_mark_put(skb, &x->mark);
2415 if (err)
2416 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002417
Thomas Graf98250692007-08-22 12:47:26 -07002418 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
David S. Miller214e0052011-02-24 00:02:38 -05002421static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002423 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 struct sk_buff *skb;
2425
Thomas Graf7deb2262007-08-22 13:57:39 -07002426 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (skb == NULL)
2428 return -ENOMEM;
2429
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002430 if (build_expire(skb, x, c) < 0) {
2431 kfree_skb(skb);
2432 return -EMSGSIZE;
2433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Michal Kubecek21ee5432014-06-03 10:26:06 +02002435 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
David S. Miller214e0052011-02-24 00:02:38 -05002438static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002439{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002440 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002441 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002442
Steffen Klassertd8647b72011-03-08 00:10:27 +00002443 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002444 if (skb == NULL)
2445 return -ENOMEM;
2446
2447 if (build_aevent(skb, x, c) < 0)
2448 BUG();
2449
Michal Kubecek21ee5432014-06-03 10:26:06 +02002450 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002451}
2452
David S. Miller214e0052011-02-24 00:02:38 -05002453static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002454{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002455 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456 struct xfrm_usersa_flush *p;
2457 struct nlmsghdr *nlh;
2458 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002459 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460
Thomas Graf7deb2262007-08-22 13:57:39 -07002461 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002462 if (skb == NULL)
2463 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002464
Eric W. Biederman15e47302012-09-07 20:12:54 +00002465 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002466 if (nlh == NULL) {
2467 kfree_skb(skb);
2468 return -EMSGSIZE;
2469 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470
Thomas Graf7b67c852007-08-22 13:53:52 -07002471 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002472 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002473
Thomas Graf98250692007-08-22 12:47:26 -07002474 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002475
Michal Kubecek21ee5432014-06-03 10:26:06 +02002476 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002477}
2478
Thomas Graf7deb2262007-08-22 13:57:39 -07002479static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480{
Thomas Graf7deb2262007-08-22 13:57:39 -07002481 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002482 if (x->aead)
2483 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002484 if (x->aalg) {
2485 l += nla_total_size(sizeof(struct xfrm_algo) +
2486 (x->aalg->alg_key_len + 7) / 8);
2487 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2488 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002489 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002490 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002491 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002492 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002494 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002495 if (x->tfcpad)
2496 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002497 if (x->replay_esn)
2498 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002499 if (x->security)
2500 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2501 x->security->ctx_len);
2502 if (x->coaddr)
2503 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002504 if (x->props.extra_flags)
2505 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002506
Herbert Xud26f3982007-11-13 21:47:08 -08002507 /* Must count x->lastused as it may become non-zero behind our back. */
2508 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002509
2510 return l;
2511}
2512
David S. Miller214e0052011-02-24 00:02:38 -05002513static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002514{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002515 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002516 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002517 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002518 struct nlmsghdr *nlh;
2519 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002520 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002521 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002522
2523 headlen = sizeof(*p);
2524 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002525 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002526 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002527 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002528 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002529 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002530
Thomas Graf7deb2262007-08-22 13:57:39 -07002531 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002532 if (skb == NULL)
2533 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002534
Eric W. Biederman15e47302012-09-07 20:12:54 +00002535 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002536 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002537 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002538 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002539
Thomas Graf7b67c852007-08-22 13:53:52 -07002540 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002541 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002542 struct nlattr *attr;
2543
Thomas Graf7b67c852007-08-22 13:53:52 -07002544 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002545 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2546 id->spi = x->id.spi;
2547 id->family = x->props.family;
2548 id->proto = x->id.proto;
2549
Thomas Grafc0144be2007-08-22 13:55:43 -07002550 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002551 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002552 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002553 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002554
2555 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002556 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002557 err = copy_to_user_state_extra(x, p, skb);
2558 if (err)
2559 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002560
Thomas Graf98250692007-08-22 12:47:26 -07002561 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002562
Michal Kubecek21ee5432014-06-03 10:26:06 +02002563 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564
David S. Miller1d1e34d2012-06-27 21:57:03 -07002565out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002566 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002567 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002568}
2569
David S. Miller214e0052011-02-24 00:02:38 -05002570static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002571{
2572
2573 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002574 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002575 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002576 case XFRM_MSG_NEWAE:
2577 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002578 case XFRM_MSG_DELSA:
2579 case XFRM_MSG_UPDSA:
2580 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002581 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002582 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583 return xfrm_notify_sa_flush(c);
2584 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002585 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2586 c->event);
2587 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002588 }
2589
2590 return 0;
2591
2592}
2593
Thomas Graf7deb2262007-08-22 13:57:39 -07002594static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2595 struct xfrm_policy *xp)
2596{
2597 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2598 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002599 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002600 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2601 + userpolicy_type_attrsize();
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002605 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002607 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct xfrm_user_acquire *ua;
2609 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002610 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002612 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2613 if (nlh == NULL)
2614 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Thomas Graf7b67c852007-08-22 13:53:52 -07002616 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 memcpy(&ua->id, &x->id, sizeof(ua->id));
2618 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2619 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002620 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 ua->aalgos = xt->aalgos;
2622 ua->ealgos = xt->ealgos;
2623 ua->calgos = xt->calgos;
2624 ua->seq = x->km.seq = seq;
2625
David S. Miller1d1e34d2012-06-27 21:57:03 -07002626 err = copy_to_user_tmpl(xp, skb);
2627 if (!err)
2628 err = copy_to_user_state_sec_ctx(x, skb);
2629 if (!err)
2630 err = copy_to_user_policy_type(xp->type, skb);
2631 if (!err)
2632 err = xfrm_mark_put(skb, &xp->mark);
2633 if (err) {
2634 nlmsg_cancel(skb, nlh);
2635 return err;
2636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Thomas Graf98250692007-08-22 12:47:26 -07002638 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
2641static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002642 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002644 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Thomas Graf7deb2262007-08-22 13:57:39 -07002647 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (skb == NULL)
2649 return -ENOMEM;
2650
Fan Du65e07362012-08-15 10:13:47 +08002651 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 BUG();
2653
Michal Kubecek21ee5432014-06-03 10:26:06 +02002654 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
2657/* User gives us xfrm_user_policy_info followed by an array of 0
2658 * or more templates.
2659 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002660static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 u8 *data, int len, int *dir)
2662{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002663 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2665 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2666 struct xfrm_policy *xp;
2667 int nr;
2668
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002669 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 case AF_INET:
2671 if (opt != IP_XFRM_POLICY) {
2672 *dir = -EOPNOTSUPP;
2673 return NULL;
2674 }
2675 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002676#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 case AF_INET6:
2678 if (opt != IPV6_XFRM_POLICY) {
2679 *dir = -EOPNOTSUPP;
2680 return NULL;
2681 }
2682 break;
2683#endif
2684 default:
2685 *dir = -EINVAL;
2686 return NULL;
2687 }
2688
2689 *dir = -EINVAL;
2690
2691 if (len < sizeof(*p) ||
2692 verify_newpolicy_info(p))
2693 return NULL;
2694
2695 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002696 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return NULL;
2698
Herbert Xua4f1bac2005-07-26 15:43:17 -07002699 if (p->dir > XFRM_POLICY_OUT)
2700 return NULL;
2701
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002702 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 if (xp == NULL) {
2704 *dir = -ENOBUFS;
2705 return NULL;
2706 }
2707
2708 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002709 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 copy_templates(xp, ut, nr);
2711
2712 *dir = p->dir;
2713
2714 return xp;
2715}
2716
Thomas Graf7deb2262007-08-22 13:57:39 -07002717static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2718{
2719 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2720 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2721 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002722 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002723 + userpolicy_type_attrsize();
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002727 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002730 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002731 struct nlmsghdr *nlh;
2732 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Eric W. Biederman15e47302012-09-07 20:12:54 +00002734 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002735 if (nlh == NULL)
2736 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Thomas Graf7b67c852007-08-22 13:53:52 -07002738 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002740 err = copy_to_user_tmpl(xp, skb);
2741 if (!err)
2742 err = copy_to_user_sec_ctx(xp, skb);
2743 if (!err)
2744 err = copy_to_user_policy_type(xp->type, skb);
2745 if (!err)
2746 err = xfrm_mark_put(skb, &xp->mark);
2747 if (err) {
2748 nlmsg_cancel(skb, nlh);
2749 return err;
2750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 upe->hard = !!hard;
2752
Thomas Graf98250692007-08-22 12:47:26 -07002753 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
David S. Miller214e0052011-02-24 00:02:38 -05002756static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002758 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Thomas Graf7deb2262007-08-22 13:57:39 -07002761 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (skb == NULL)
2763 return -ENOMEM;
2764
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002765 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 BUG();
2767
Michal Kubecek21ee5432014-06-03 10:26:06 +02002768 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
David S. Miller214e0052011-02-24 00:02:38 -05002771static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002773 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002774 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002775 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002776 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002777 struct nlmsghdr *nlh;
2778 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002779 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002780
2781 headlen = sizeof(*p);
2782 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002783 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002784 headlen = sizeof(*id);
2785 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002786 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002787 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002788 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002789
Thomas Graf7deb2262007-08-22 13:57:39 -07002790 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002791 if (skb == NULL)
2792 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002793
Eric W. Biederman15e47302012-09-07 20:12:54 +00002794 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002795 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002796 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002797 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002798
Thomas Graf7b67c852007-08-22 13:53:52 -07002799 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002800 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002801 struct nlattr *attr;
2802
Thomas Graf7b67c852007-08-22 13:53:52 -07002803 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002804 memset(id, 0, sizeof(*id));
2805 id->dir = dir;
2806 if (c->data.byid)
2807 id->index = xp->index;
2808 else
2809 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2810
Thomas Grafc0144be2007-08-22 13:55:43 -07002811 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002812 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002813 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002814 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002815
2816 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002817 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002818
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002819 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002820 err = copy_to_user_tmpl(xp, skb);
2821 if (!err)
2822 err = copy_to_user_policy_type(xp->type, skb);
2823 if (!err)
2824 err = xfrm_mark_put(skb, &xp->mark);
2825 if (err)
2826 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002827
Thomas Graf98250692007-08-22 12:47:26 -07002828 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002829
Michal Kubecek21ee5432014-06-03 10:26:06 +02002830 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002831
David S. Miller1d1e34d2012-06-27 21:57:03 -07002832out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002833 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002834 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002835}
2836
David S. Miller214e0052011-02-24 00:02:38 -05002837static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002838{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002839 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002840 struct nlmsghdr *nlh;
2841 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002842 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002843
Thomas Graf7deb2262007-08-22 13:57:39 -07002844 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002845 if (skb == NULL)
2846 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002847
Eric W. Biederman15e47302012-09-07 20:12:54 +00002848 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002849 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002850 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002851 goto out_free_skb;
2852 err = copy_to_user_policy_type(c->data.type, skb);
2853 if (err)
2854 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002855
Thomas Graf98250692007-08-22 12:47:26 -07002856 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002857
Michal Kubecek21ee5432014-06-03 10:26:06 +02002858 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002859
David S. Miller1d1e34d2012-06-27 21:57:03 -07002860out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002861 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002862 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002863}
2864
David S. Miller214e0052011-02-24 00:02:38 -05002865static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002866{
2867
2868 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002869 case XFRM_MSG_NEWPOLICY:
2870 case XFRM_MSG_UPDPOLICY:
2871 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002872 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002873 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002874 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002875 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002876 return xfrm_exp_policy_notify(xp, dir, c);
2877 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002878 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2879 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002880 }
2881
2882 return 0;
2883
2884}
2885
Thomas Graf7deb2262007-08-22 13:57:39 -07002886static inline size_t xfrm_report_msgsize(void)
2887{
2888 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2889}
2890
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002891static int build_report(struct sk_buff *skb, u8 proto,
2892 struct xfrm_selector *sel, xfrm_address_t *addr)
2893{
2894 struct xfrm_user_report *ur;
2895 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002896
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002897 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2898 if (nlh == NULL)
2899 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002900
Thomas Graf7b67c852007-08-22 13:53:52 -07002901 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002902 ur->proto = proto;
2903 memcpy(&ur->sel, sel, sizeof(ur->sel));
2904
David S. Miller1d1e34d2012-06-27 21:57:03 -07002905 if (addr) {
2906 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2907 if (err) {
2908 nlmsg_cancel(skb, nlh);
2909 return err;
2910 }
2911 }
Thomas Graf98250692007-08-22 12:47:26 -07002912 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002913}
2914
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002915static int xfrm_send_report(struct net *net, u8 proto,
2916 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002917{
2918 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002919
Thomas Graf7deb2262007-08-22 13:57:39 -07002920 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002921 if (skb == NULL)
2922 return -ENOMEM;
2923
2924 if (build_report(skb, proto, sel, addr) < 0)
2925 BUG();
2926
Michal Kubecek21ee5432014-06-03 10:26:06 +02002927 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002928}
2929
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002930static inline size_t xfrm_mapping_msgsize(void)
2931{
2932 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2933}
2934
2935static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2936 xfrm_address_t *new_saddr, __be16 new_sport)
2937{
2938 struct xfrm_user_mapping *um;
2939 struct nlmsghdr *nlh;
2940
2941 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2942 if (nlh == NULL)
2943 return -EMSGSIZE;
2944
2945 um = nlmsg_data(nlh);
2946
2947 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2948 um->id.spi = x->id.spi;
2949 um->id.family = x->props.family;
2950 um->id.proto = x->id.proto;
2951 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2952 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2953 um->new_sport = new_sport;
2954 um->old_sport = x->encap->encap_sport;
2955 um->reqid = x->props.reqid;
2956
2957 return nlmsg_end(skb, nlh);
2958}
2959
2960static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2961 __be16 sport)
2962{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002963 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002964 struct sk_buff *skb;
2965
2966 if (x->id.proto != IPPROTO_ESP)
2967 return -EINVAL;
2968
2969 if (!x->encap)
2970 return -EINVAL;
2971
2972 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2973 if (skb == NULL)
2974 return -ENOMEM;
2975
2976 if (build_mapping(skb, x, ipaddr, sport) < 0)
2977 BUG();
2978
Michal Kubecek21ee5432014-06-03 10:26:06 +02002979 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002980}
2981
Horia Geanta0f245582014-02-12 16:20:06 +02002982static bool xfrm_is_alive(const struct km_event *c)
2983{
2984 return (bool)xfrm_acquire_is_on(c->net);
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987static struct xfrm_mgr netlink_mgr = {
2988 .id = "netlink",
2989 .notify = xfrm_send_state_notify,
2990 .acquire = xfrm_send_acquire,
2991 .compile_policy = xfrm_compile_policy,
2992 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002993 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002994 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002995 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02002996 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997};
2998
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002999static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Patrick McHardybe336902006-03-20 22:40:54 -08003001 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003002 struct netlink_kernel_cfg cfg = {
3003 .groups = XFRMNLGRP_MAX,
3004 .input = xfrm_netlink_rcv,
3005 };
Patrick McHardybe336902006-03-20 22:40:54 -08003006
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003007 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003008 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003010 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003011 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return 0;
3013}
3014
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003015static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003016{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003017 struct net *net;
3018 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003019 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003020 synchronize_net();
3021 list_for_each_entry(net, net_exit_list, exit_list)
3022 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003023}
3024
3025static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003026 .init = xfrm_user_net_init,
3027 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003028};
3029
3030static int __init xfrm_user_init(void)
3031{
3032 int rv;
3033
3034 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3035
3036 rv = register_pernet_subsys(&xfrm_user_net_ops);
3037 if (rv < 0)
3038 return rv;
3039 rv = xfrm_register_km(&netlink_mgr);
3040 if (rv < 0)
3041 unregister_pernet_subsys(&xfrm_user_net_ops);
3042 return rv;
3043}
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045static void __exit xfrm_user_exit(void)
3046{
3047 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003048 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
3051module_init(xfrm_user_init);
3052module_exit(xfrm_user_exit);
3053MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003054MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003055