blob: 903725b8cc70b8cf72af5aa341376ad8f36de466 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Herbert Xu1a6509d2008-01-28 19:37:29 -080035static inline int aead_len(struct xfrm_algo_aead *alg)
36{
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38}
39
Thomas Graf5424f322007-08-22 14:01:33 -070040static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Thomas Graf5424f322007-08-22 14:01:33 -070042 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_algo *algp;
44
45 if (!rt)
46 return 0;
47
Thomas Graf5424f322007-08-22 14:01:33 -070048 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080049 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57
58 default:
59 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63 return 0;
64}
65
Martin Willi4447bb32009-11-25 00:29:52 +000066static int verify_auth_trunc(struct nlattr **attrs)
67{
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
70
71 if (!rt)
72 return 0;
73
74 algp = nla_data(rt);
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
76 return -EINVAL;
77
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 return 0;
80}
81
Herbert Xu1a6509d2008-01-28 19:37:29 -080082static int verify_aead(struct nlattr **attrs)
83{
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
86
87 if (!rt)
88 return 0;
89
90 algp = nla_data(rt);
91 if (nla_len(rt) < aead_len(algp))
92 return -EINVAL;
93
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95 return 0;
96}
97
Thomas Graf5424f322007-08-22 14:01:33 -070098static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099 xfrm_address_t **addrp)
100{
Thomas Graf5424f322007-08-22 14:01:33 -0700101 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700102
Thomas Grafcf5cb792007-08-22 13:59:04 -0700103 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700104 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700105}
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
Thomas Graf5424f322007-08-22 14:01:33 -0700107static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800108{
Thomas Graf5424f322007-08-22 14:01:33 -0700109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800110 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800111
112 if (!rt)
113 return 0;
114
Thomas Graf5424f322007-08-22 14:01:33 -0700115 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800117 return -EINVAL;
118
119 return 0;
120}
121
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
124{
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000126 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000127
Mathias Krauseecd79182012-09-20 10:01:49 +0000128 if (p->flags & XFRM_STATE_ESN) {
129 if (!rt)
130 return -EINVAL;
131
132 rs = nla_data(rt);
133
134 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135 return -EINVAL;
136
137 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138 nla_len(rt) != sizeof(*rs))
139 return -EINVAL;
140 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000141
Steffen Klassertd8647b72011-03-08 00:10:27 +0000142 if (!rt)
143 return 0;
144
Fan Du01714102014-01-18 09:54:28 +0800145 /* As only ESP and AH support ESN feature. */
146 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000147 return -EINVAL;
148
Steffen Klassertd8647b72011-03-08 00:10:27 +0000149 if (p->replay_window != 0)
150 return -EINVAL;
151
152 return 0;
153}
Trent Jaegerdf718372005-12-13 23:12:27 -0800154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700156 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 int err;
159
160 err = -EINVAL;
161 switch (p->family) {
162 case AF_INET:
163 break;
164
165 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000166#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168#else
169 err = -EAFNOSUPPORT;
170 goto out;
171#endif
172
173 default:
174 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 err = -EINVAL;
178 switch (p->id.proto) {
179 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000180 if ((!attrs[XFRMA_ALG_AUTH] &&
181 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800182 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700183 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000184 attrs[XFRMA_ALG_COMP] ||
Fan Duea9884b2013-12-16 18:47:48 +0800185 attrs[XFRMA_TFCPAD] ||
186 (ntohl(p->id.spi) >= 0x10000))
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
189 break;
190
191 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800192 if (attrs[XFRMA_ALG_COMP])
193 goto out;
194 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000195 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800196 !attrs[XFRMA_ALG_CRYPT] &&
197 !attrs[XFRMA_ALG_AEAD])
198 goto out;
199 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000200 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800201 attrs[XFRMA_ALG_CRYPT]) &&
202 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000204 if (attrs[XFRMA_TFCPAD] &&
205 p->mode != XFRM_MODE_TUNNEL)
206 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208
209 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700210 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800211 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700212 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000213 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000214 attrs[XFRMA_ALG_CRYPT] ||
215 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 goto out;
217 break;
218
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000219#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700220 case IPPROTO_DSTOPTS:
221 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700222 if (attrs[XFRMA_ALG_COMP] ||
223 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000224 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800225 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700226 attrs[XFRMA_ALG_CRYPT] ||
227 attrs[XFRMA_ENCAP] ||
228 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000229 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700230 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700231 goto out;
232 break;
233#endif
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 default:
236 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Herbert Xu1a6509d2008-01-28 19:37:29 -0800239 if ((err = verify_aead(attrs)))
240 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000241 if ((err = verify_auth_trunc(attrs)))
242 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700243 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700245 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700247 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700249 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800250 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000251 if ((err = verify_replay(p, attrs)))
252 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 err = -EINVAL;
255 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700256 case XFRM_MODE_TRANSPORT:
257 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700258 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700259 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
261
262 default:
263 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 err = 0;
267
268out:
269 return err;
270}
271
272static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800273 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700274 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct xfrm_algo *p, *ualg;
277 struct xfrm_algo_desc *algo;
278
279 if (!rta)
280 return 0;
281
Thomas Graf5424f322007-08-22 14:01:33 -0700282 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 algo = get_byname(ualg->alg_name, 1);
285 if (!algo)
286 return -ENOSYS;
287 *props = algo->desc.sadb_alg_id;
288
Eric Dumazet0f99be02008-01-08 23:39:06 -0800289 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!p)
291 return -ENOMEM;
292
Herbert Xu04ff1262006-08-13 08:50:00 +1000293 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 *algpp = p;
295 return 0;
296}
297
Martin Willi4447bb32009-11-25 00:29:52 +0000298static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
299 struct nlattr *rta)
300{
301 struct xfrm_algo *ualg;
302 struct xfrm_algo_auth *p;
303 struct xfrm_algo_desc *algo;
304
305 if (!rta)
306 return 0;
307
308 ualg = nla_data(rta);
309
310 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
311 if (!algo)
312 return -ENOSYS;
313 *props = algo->desc.sadb_alg_id;
314
315 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
316 if (!p)
317 return -ENOMEM;
318
319 strcpy(p->alg_name, algo->name);
320 p->alg_key_len = ualg->alg_key_len;
321 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
322 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
323
324 *algpp = p;
325 return 0;
326}
327
328static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
329 struct nlattr *rta)
330{
331 struct xfrm_algo_auth *p, *ualg;
332 struct xfrm_algo_desc *algo;
333
334 if (!rta)
335 return 0;
336
337 ualg = nla_data(rta);
338
339 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
340 if (!algo)
341 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000342 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
343 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000344 return -EINVAL;
345 *props = algo->desc.sadb_alg_id;
346
347 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
348 if (!p)
349 return -ENOMEM;
350
351 strcpy(p->alg_name, algo->name);
352 if (!p->alg_trunc_len)
353 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
354
355 *algpp = p;
356 return 0;
357}
358
Herbert Xu1a6509d2008-01-28 19:37:29 -0800359static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
360 struct nlattr *rta)
361{
362 struct xfrm_algo_aead *p, *ualg;
363 struct xfrm_algo_desc *algo;
364
365 if (!rta)
366 return 0;
367
368 ualg = nla_data(rta);
369
370 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
371 if (!algo)
372 return -ENOSYS;
373 *props = algo->desc.sadb_alg_id;
374
375 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
376 if (!p)
377 return -ENOMEM;
378
379 strcpy(p->alg_name, algo->name);
380 *algpp = p;
381 return 0;
382}
383
Steffen Klasserte2b19122011-03-28 19:47:30 +0000384static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
385 struct nlattr *rp)
386{
387 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000388 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000389
390 if (!replay_esn || !rp)
391 return 0;
392
393 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000394 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000395
Mathias Krauseecd79182012-09-20 10:01:49 +0000396 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000397 return -EINVAL;
398
399 return 0;
400}
401
Steffen Klassertd8647b72011-03-08 00:10:27 +0000402static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
403 struct xfrm_replay_state_esn **preplay_esn,
404 struct nlattr *rta)
405{
406 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000407 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000408
409 if (!rta)
410 return 0;
411
412 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 klen = xfrm_replay_state_esn_len(up);
414 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000415
Mathias Krauseecd79182012-09-20 10:01:49 +0000416 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000417 if (!p)
418 return -ENOMEM;
419
Mathias Krauseecd79182012-09-20 10:01:49 +0000420 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000421 if (!pp) {
422 kfree(p);
423 return -ENOMEM;
424 }
425
Mathias Krauseecd79182012-09-20 10:01:49 +0000426 memcpy(p, up, ulen);
427 memcpy(pp, up, ulen);
428
Steffen Klassertd8647b72011-03-08 00:10:27 +0000429 *replay_esn = p;
430 *preplay_esn = pp;
431
432 return 0;
433}
434
Joy Latten661697f2007-04-13 16:14:35 -0700435static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800436{
Trent Jaegerdf718372005-12-13 23:12:27 -0800437 int len = 0;
438
439 if (xfrm_ctx) {
440 len += sizeof(struct xfrm_user_sec_ctx);
441 len += xfrm_ctx->ctx_len;
442 }
443 return len;
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
447{
448 memcpy(&x->id, &p->id, sizeof(x->id));
449 memcpy(&x->sel, &p->sel, sizeof(x->sel));
450 memcpy(&x->lft, &p->lft, sizeof(x->lft));
451 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800452 x->props.replay_window = min_t(unsigned int, p->replay_window,
453 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 x->props.reqid = p->reqid;
455 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700456 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700458
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700459 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700460 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800463/*
464 * someday when pfkey also has support, we could have the code
465 * somehow made shareable and move it to xfrm_state.c - JHS
466 *
467*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000468static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
469 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800470{
Thomas Graf5424f322007-08-22 14:01:33 -0700471 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000472 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700473 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
474 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
475 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800476
Steffen Klassertd8647b72011-03-08 00:10:27 +0000477 if (re) {
478 struct xfrm_replay_state_esn *replay_esn;
479 replay_esn = nla_data(re);
480 memcpy(x->replay_esn, replay_esn,
481 xfrm_replay_state_esn_len(replay_esn));
482 memcpy(x->preplay_esn, replay_esn,
483 xfrm_replay_state_esn_len(replay_esn));
484 }
485
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800486 if (rp) {
487 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700488 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800489 memcpy(&x->replay, replay, sizeof(*replay));
490 memcpy(&x->preplay, replay, sizeof(*replay));
491 }
492
493 if (lt) {
494 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700495 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800496 x->curlft.bytes = ltime->bytes;
497 x->curlft.packets = ltime->packets;
498 x->curlft.add_time = ltime->add_time;
499 x->curlft.use_time = ltime->use_time;
500 }
501
Thomas Grafcf5cb792007-08-22 13:59:04 -0700502 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700503 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800504
Thomas Grafcf5cb792007-08-22 13:59:04 -0700505 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700506 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800507}
508
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800509static struct xfrm_state *xfrm_state_construct(struct net *net,
510 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700511 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int *errp)
513{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800514 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int err = -ENOMEM;
516
517 if (!x)
518 goto error_no_put;
519
520 copy_from_user_state(x, p);
521
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100522 if (attrs[XFRMA_SA_EXTRA_FLAGS])
523 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
524
Herbert Xu1a6509d2008-01-28 19:37:29 -0800525 if ((err = attach_aead(&x->aead, &x->props.ealgo,
526 attrs[XFRMA_ALG_AEAD])))
527 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000528 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
529 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000531 if (!x->props.aalgo) {
532 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
533 attrs[XFRMA_ALG_AUTH])))
534 goto error;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
537 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700538 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto error;
540 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
541 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700542 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700544
545 if (attrs[XFRMA_ENCAP]) {
546 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
547 sizeof(*x->encap), GFP_KERNEL);
548 if (x->encap == NULL)
549 goto error;
550 }
551
Martin Willi35d28562010-12-08 04:37:49 +0000552 if (attrs[XFRMA_TFCPAD])
553 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
554
Thomas Graffd211502007-09-06 03:28:08 -0700555 if (attrs[XFRMA_COADDR]) {
556 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
557 sizeof(*x->coaddr), GFP_KERNEL);
558 if (x->coaddr == NULL)
559 goto error;
560 }
561
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000562 xfrm_mark_get(attrs, &x->mark);
563
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700564 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (err)
566 goto error;
567
Thomas Graffd211502007-09-06 03:28:08 -0700568 if (attrs[XFRMA_SEC_CTX] &&
569 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800570 goto error;
571
Steffen Klassertd8647b72011-03-08 00:10:27 +0000572 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
573 attrs[XFRMA_REPLAY_ESN_VAL])))
574 goto error;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800577 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800578 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800579 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800580
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000581 if ((err = xfrm_init_replay(x)))
582 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800583
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000584 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000585 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 return x;
588
589error:
590 x->km.state = XFRM_STATE_DEAD;
591 xfrm_state_put(x);
592error_no_put:
593 *errp = err;
594 return NULL;
595}
596
Christoph Hellwig22e70052007-01-02 15:22:30 -0800597static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700598 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800600 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700601 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct xfrm_state *x;
603 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700604 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700605 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500606 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800607 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Thomas Graf35a7aa02007-08-22 14:00:40 -0700609 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (err)
611 return err;
612
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800613 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!x)
615 return err;
616
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700617 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
619 err = xfrm_state_add(x);
620 else
621 err = xfrm_state_update(x);
622
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800623 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400624 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (err < 0) {
627 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800628 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700629 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700632 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000633 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700634 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700635
636 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700637out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700638 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return err;
640}
641
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800642static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
643 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700644 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700645 int *errp)
646{
647 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000648 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700649 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000650 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700651
652 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
653 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000654 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700655 } else {
656 xfrm_address_t *saddr = NULL;
657
Thomas Graf35a7aa02007-08-22 14:00:40 -0700658 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700659 if (!saddr) {
660 err = -EINVAL;
661 goto out;
662 }
663
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800664 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000665 x = xfrm_state_lookup_byaddr(net, mark,
666 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800667 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700668 }
669
670 out:
671 if (!x && errp)
672 *errp = err;
673 return x;
674}
675
Christoph Hellwig22e70052007-01-02 15:22:30 -0800676static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700677 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800679 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700681 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700682 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700683 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700684 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500685 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800686 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800688 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
David S. Miller6f68dc32006-06-08 23:58:52 -0700692 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700693 goto out;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700696 err = -EPERM;
697 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700700 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600701
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700702 if (err < 0)
703 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700704
705 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000706 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700707 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700708 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700710out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800711 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400712 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700713 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
717static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
718{
Mathias Krausef778a632012-09-19 11:33:39 +0000719 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 memcpy(&p->id, &x->id, sizeof(p->id));
721 memcpy(&p->sel, &x->sel, sizeof(p->sel));
722 memcpy(&p->lft, &x->lft, sizeof(p->lft));
723 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
724 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700725 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 p->mode = x->props.mode;
727 p->replay_window = x->props.replay_window;
728 p->reqid = x->props.reqid;
729 p->family = x->props.family;
730 p->flags = x->props.flags;
731 p->seq = x->km.seq;
732}
733
734struct xfrm_dump_info {
735 struct sk_buff *in_skb;
736 struct sk_buff *out_skb;
737 u32 nlmsg_seq;
738 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739};
740
Thomas Grafc0144be2007-08-22 13:55:43 -0700741static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
742{
Thomas Grafc0144be2007-08-22 13:55:43 -0700743 struct xfrm_user_sec_ctx *uctx;
744 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700745 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700746
747 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
748 if (attr == NULL)
749 return -EMSGSIZE;
750
751 uctx = nla_data(attr);
752 uctx->exttype = XFRMA_SEC_CTX;
753 uctx->len = ctx_size;
754 uctx->ctx_doi = s->ctx_doi;
755 uctx->ctx_alg = s->ctx_alg;
756 uctx->ctx_len = s->ctx_len;
757 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
758
759 return 0;
760}
761
Martin Willi4447bb32009-11-25 00:29:52 +0000762static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
763{
764 struct xfrm_algo *algo;
765 struct nlattr *nla;
766
767 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
768 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
769 if (!nla)
770 return -EMSGSIZE;
771
772 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000773 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000774 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
775 algo->alg_key_len = auth->alg_key_len;
776
777 return 0;
778}
779
Herbert Xu68325d32007-10-09 13:30:57 -0700780/* Don't change this without updating xfrm_sa_len! */
781static int copy_to_user_state_extra(struct xfrm_state *x,
782 struct xfrm_usersa_info *p,
783 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700785 int ret = 0;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 copy_to_user_state(x, p);
788
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100789 if (x->props.extra_flags) {
790 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
791 x->props.extra_flags);
792 if (ret)
793 goto out;
794 }
795
David S. Miller1d1e34d2012-06-27 21:57:03 -0700796 if (x->coaddr) {
797 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
798 if (ret)
799 goto out;
800 }
801 if (x->lastused) {
802 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
803 if (ret)
804 goto out;
805 }
806 if (x->aead) {
807 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
808 if (ret)
809 goto out;
810 }
811 if (x->aalg) {
812 ret = copy_to_user_auth(x->aalg, skb);
813 if (!ret)
814 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
815 xfrm_alg_auth_len(x->aalg), x->aalg);
816 if (ret)
817 goto out;
818 }
819 if (x->ealg) {
820 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
821 if (ret)
822 goto out;
823 }
824 if (x->calg) {
825 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
826 if (ret)
827 goto out;
828 }
829 if (x->encap) {
830 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
831 if (ret)
832 goto out;
833 }
834 if (x->tfcpad) {
835 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
836 if (ret)
837 goto out;
838 }
839 ret = xfrm_mark_put(skb, &x->mark);
840 if (ret)
841 goto out;
842 if (x->replay_esn) {
843 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
844 xfrm_replay_state_esn_len(x->replay_esn),
845 x->replay_esn);
846 if (ret)
847 goto out;
848 }
849 if (x->security)
850 ret = copy_sec_ctx(x->security, skb);
851out:
852 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700853}
854
855static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
856{
857 struct xfrm_dump_info *sp = ptr;
858 struct sk_buff *in_skb = sp->in_skb;
859 struct sk_buff *skb = sp->out_skb;
860 struct xfrm_usersa_info *p;
861 struct nlmsghdr *nlh;
862 int err;
863
Eric W. Biederman15e47302012-09-07 20:12:54 +0000864 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700865 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
866 if (nlh == NULL)
867 return -EMSGSIZE;
868
869 p = nlmsg_data(nlh);
870
871 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700872 if (err) {
873 nlmsg_cancel(skb, nlh);
874 return err;
875 }
Thomas Graf98250692007-08-22 12:47:26 -0700876 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Timo Teras4c563f72008-02-28 21:31:08 -0800880static int xfrm_dump_sa_done(struct netlink_callback *cb)
881{
882 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800883 struct sock *sk = cb->skb->sk;
884 struct net *net = sock_net(sk);
885
886 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800887 return 0;
888}
889
Nicolas Dichteld3623092014-02-14 15:30:36 +0100890static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
892{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800893 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800894 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct xfrm_dump_info info;
896
Timo Teras4c563f72008-02-28 21:31:08 -0800897 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
898 sizeof(cb->args) - sizeof(cb->args[0]));
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 info.in_skb = cb->skb;
901 info.out_skb = skb;
902 info.nlmsg_seq = cb->nlh->nlmsg_seq;
903 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800904
905 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100906 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100907 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100908 u8 proto = 0;
909 int err;
910
Timo Teras4c563f72008-02-28 21:31:08 -0800911 cb->args[0] = 1;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100912
913 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
914 xfrma_policy);
915 if (err < 0)
916 return err;
917
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100918 if (attrs[XFRMA_ADDRESS_FILTER]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100919 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
920 if (filter == NULL)
921 return -ENOMEM;
922
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100923 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
Nicolas Dichteld3623092014-02-14 15:30:36 +0100924 sizeof(*filter));
925 }
926
927 if (attrs[XFRMA_PROTO])
928 proto = nla_get_u8(attrs[XFRMA_PROTO]);
929
930 xfrm_state_walk_init(walk, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -0800931 }
932
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800933 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 return skb->len;
936}
937
938static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
939 struct xfrm_state *x, u32 seq)
940{
941 struct xfrm_dump_info info;
942 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000943 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Thomas Graf7deb2262007-08-22 13:57:39 -0700945 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (!skb)
947 return ERR_PTR(-ENOMEM);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 info.in_skb = in_skb;
950 info.out_skb = skb;
951 info.nlmsg_seq = seq;
952 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Mathias Krause864745d2012-09-13 11:41:26 +0000954 err = dump_one_state(x, 0, &info);
955 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000957 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
960 return skb;
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);
Paul Moore03e1ad72008-04-12 19:07:52 -07001254 return security_xfrm_policy_alloc(&pol->security, uctx);
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;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001422 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001423 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001424 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 err = verify_newpolicy_info(p);
1427 if (err)
1428 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001429 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001430 if (err)
1431 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001433 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!xp)
1435 return err;
1436
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001437 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001438 * Aha! this is anti-netlink really i.e more pfkey derived
1439 * in netlink excl is a flag and you wouldnt need
1440 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1442 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001443 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001444 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001447 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 kfree(xp);
1449 return err;
1450 }
1451
Herbert Xuf60f6b82005-06-18 22:44:37 -07001452 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001453 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001454 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001455 km_policy_notify(xp, p->dir, &c);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 xfrm_pol_put(xp);
1458
1459 return 0;
1460}
1461
1462static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1463{
1464 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1465 int i;
1466
1467 if (xp->xfrm_nr == 0)
1468 return 0;
1469
1470 for (i = 0; i < xp->xfrm_nr; i++) {
1471 struct xfrm_user_tmpl *up = &vec[i];
1472 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1473
Mathias Krause1f868402012-09-19 11:33:41 +00001474 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001476 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1478 up->reqid = kp->reqid;
1479 up->mode = kp->mode;
1480 up->share = kp->share;
1481 up->optional = kp->optional;
1482 up->aalgos = kp->aalgos;
1483 up->ealgos = kp->ealgos;
1484 up->calgos = kp->calgos;
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Thomas Grafc0144be2007-08-22 13:55:43 -07001487 return nla_put(skb, XFRMA_TMPL,
1488 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001489}
1490
Serge Hallyn0d681622006-07-24 23:30:44 -07001491static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1492{
1493 if (x->security) {
1494 return copy_sec_ctx(x->security, skb);
1495 }
1496 return 0;
1497}
1498
1499static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1500{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001501 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001502 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001503 return 0;
1504}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001505static inline size_t userpolicy_type_attrsize(void)
1506{
1507#ifdef CONFIG_XFRM_SUB_POLICY
1508 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1509#else
1510 return 0;
1511#endif
1512}
Serge Hallyn0d681622006-07-24 23:30:44 -07001513
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001514#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001515static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001516{
Thomas Grafc0144be2007-08-22 13:55:43 -07001517 struct xfrm_userpolicy_type upt = {
1518 .type = type,
1519 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001520
Thomas Grafc0144be2007-08-22 13:55:43 -07001521 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001522}
1523
1524#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001525static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001526{
1527 return 0;
1528}
1529#endif
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1532{
1533 struct xfrm_dump_info *sp = ptr;
1534 struct xfrm_userpolicy_info *p;
1535 struct sk_buff *in_skb = sp->in_skb;
1536 struct sk_buff *skb = sp->out_skb;
1537 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001538 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Eric W. Biederman15e47302012-09-07 20:12:54 +00001540 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001541 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1542 if (nlh == NULL)
1543 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Thomas Graf7b67c852007-08-22 13:53:52 -07001545 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001547 err = copy_to_user_tmpl(xp, skb);
1548 if (!err)
1549 err = copy_to_user_sec_ctx(xp, skb);
1550 if (!err)
1551 err = copy_to_user_policy_type(xp->type, skb);
1552 if (!err)
1553 err = xfrm_mark_put(skb, &xp->mark);
1554 if (err) {
1555 nlmsg_cancel(skb, nlh);
1556 return err;
1557 }
Thomas Graf98250692007-08-22 12:47:26 -07001558 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Timo Teras4c563f72008-02-28 21:31:08 -08001562static int xfrm_dump_policy_done(struct netlink_callback *cb)
1563{
1564 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001565 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001566
Fan Du283bc9f2013-11-07 17:47:50 +08001567 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001568 return 0;
1569}
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1572{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001573 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001574 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct xfrm_dump_info info;
1576
Timo Teras4c563f72008-02-28 21:31:08 -08001577 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1578 sizeof(cb->args) - sizeof(cb->args[0]));
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 info.in_skb = cb->skb;
1581 info.out_skb = skb;
1582 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1583 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001584
1585 if (!cb->args[0]) {
1586 cb->args[0] = 1;
1587 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1588 }
1589
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001590 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 return skb->len;
1593}
1594
1595static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1596 struct xfrm_policy *xp,
1597 int dir, u32 seq)
1598{
1599 struct xfrm_dump_info info;
1600 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Thomas Graf7deb2262007-08-22 13:57:39 -07001603 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (!skb)
1605 return ERR_PTR(-ENOMEM);
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 info.in_skb = in_skb;
1608 info.out_skb = skb;
1609 info.nlmsg_seq = seq;
1610 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Mathias Krausec2546372012-09-14 09:58:32 +00001612 err = dump_one_policy(xp, dir, 0, &info);
1613 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001615 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 return skb;
1619}
1620
Christoph Hellwig22e70052007-01-02 15:22:30 -08001621static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001622 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001624 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct xfrm_policy *xp;
1626 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001627 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001629 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001631 struct xfrm_mark m;
1632 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Thomas Graf7b67c852007-08-22 13:53:52 -07001634 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1636
Thomas Graf35a7aa02007-08-22 14:00:40 -07001637 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001638 if (err)
1639 return err;
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 err = verify_policy_dir(p->dir);
1642 if (err)
1643 return err;
1644
1645 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001646 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001647 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001648 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001649 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001650
Thomas Graf35a7aa02007-08-22 14:00:40 -07001651 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001652 if (err)
1653 return err;
1654
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001655 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001656 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001657 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001658
Paul Moore03e1ad72008-04-12 19:07:52 -07001659 err = security_xfrm_policy_alloc(&ctx, uctx);
1660 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001661 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001662 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001663 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001664 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001665 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (xp == NULL)
1668 return -ENOENT;
1669
1670 if (!delete) {
1671 struct sk_buff *resp_skb;
1672
1673 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1674 if (IS_ERR(resp_skb)) {
1675 err = PTR_ERR(resp_skb);
1676 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001677 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001678 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001681 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001682 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001683 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001684
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001685 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001686 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1687 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001688
1689 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001690 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001691
Herbert Xue7443892005-06-18 22:44:18 -07001692 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001693 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001694 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001695 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001699out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001700 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001701 if (delete && err == 0)
1702 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return err;
1704}
1705
Christoph Hellwig22e70052007-01-02 15:22:30 -08001706static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001707 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001709 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001710 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001711 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001712 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001713 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001715 audit_info.loginuid = audit_get_loginuid(current);
1716 audit_info.sessionid = audit_get_sessionid(current);
1717 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001718 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001719 if (err) {
1720 if (err == -ESRCH) /* empty table */
1721 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001722 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001723 }
Herbert Xubf088672005-06-18 22:44:00 -07001724 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001725 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001726 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001727 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001728 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001729 km_state_notify(NULL, &c);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return 0;
1732}
1733
Steffen Klassertd8647b72011-03-08 00:10:27 +00001734static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001735{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001736 size_t replay_size = x->replay_esn ?
1737 xfrm_replay_state_esn_len(x->replay_esn) :
1738 sizeof(struct xfrm_replay_state);
1739
Thomas Graf7deb2262007-08-22 13:57:39 -07001740 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001741 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001742 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001743 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001744 + nla_total_size(4) /* XFRM_AE_RTHR */
1745 + nla_total_size(4); /* XFRM_AE_ETHR */
1746}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001747
David S. Miller214e0052011-02-24 00:02:38 -05001748static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001749{
1750 struct xfrm_aevent_id *id;
1751 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001752 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001753
Eric W. Biederman15e47302012-09-07 20:12:54 +00001754 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001755 if (nlh == NULL)
1756 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001757
Thomas Graf7b67c852007-08-22 13:53:52 -07001758 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001759 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001760 id->sa_id.spi = x->id.spi;
1761 id->sa_id.family = x->props.family;
1762 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001763 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001764 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001765 id->flags = c->data.aevent;
1766
David S. Millerd0fde792012-03-29 04:02:26 -04001767 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001768 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1769 xfrm_replay_state_esn_len(x->replay_esn),
1770 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001771 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001772 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1773 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001774 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001775 if (err)
1776 goto out_cancel;
1777 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1778 if (err)
1779 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001780
David S. Miller1d1e34d2012-06-27 21:57:03 -07001781 if (id->flags & XFRM_AE_RTHR) {
1782 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1783 if (err)
1784 goto out_cancel;
1785 }
1786 if (id->flags & XFRM_AE_ETHR) {
1787 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1788 x->replay_maxage * 10 / HZ);
1789 if (err)
1790 goto out_cancel;
1791 }
1792 err = xfrm_mark_put(skb, &x->mark);
1793 if (err)
1794 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001795
Thomas Graf98250692007-08-22 12:47:26 -07001796 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001797
David S. Miller1d1e34d2012-06-27 21:57:03 -07001798out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001799 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001800 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001801}
1802
Christoph Hellwig22e70052007-01-02 15:22:30 -08001803static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001804 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001805{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001806 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001807 struct xfrm_state *x;
1808 struct sk_buff *r_skb;
1809 int err;
1810 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001811 u32 mark;
1812 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001813 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001814 struct xfrm_usersa_id *id = &p->sa_id;
1815
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001816 mark = xfrm_mark_get(attrs, &m);
1817
1818 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001819 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001820 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001821
1822 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1823 if (r_skb == NULL) {
1824 xfrm_state_put(x);
1825 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001826 }
1827
1828 /*
1829 * XXX: is this lock really needed - none of the other
1830 * gets lock (the concern is things getting updated
1831 * while we are still reading) - jhs
1832 */
1833 spin_lock_bh(&x->lock);
1834 c.data.aevent = p->flags;
1835 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001836 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001837
1838 if (build_aevent(r_skb, x, &c) < 0)
1839 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001840 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001841 spin_unlock_bh(&x->lock);
1842 xfrm_state_put(x);
1843 return err;
1844}
1845
Christoph Hellwig22e70052007-01-02 15:22:30 -08001846static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001847 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001848{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001849 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001850 struct xfrm_state *x;
1851 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001852 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001853 u32 mark = 0;
1854 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001855 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001856 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001857 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001858 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001859
Steffen Klassertd8647b72011-03-08 00:10:27 +00001860 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001861 return err;
1862
1863 /* pedantic mode - thou shalt sayeth replaceth */
1864 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1865 return err;
1866
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001867 mark = xfrm_mark_get(attrs, &m);
1868
1869 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 -08001870 if (x == NULL)
1871 return -ESRCH;
1872
1873 if (x->km.state != XFRM_STATE_VALID)
1874 goto out;
1875
Steffen Klassert4479ff72013-09-09 09:39:01 +02001876 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001877 if (err)
1878 goto out;
1879
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001880 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001881 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001882 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001883
1884 c.event = nlh->nlmsg_type;
1885 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001886 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001887 c.data.aevent = XFRM_AE_CU;
1888 km_state_notify(x, &c);
1889 err = 0;
1890out:
1891 xfrm_state_put(x);
1892 return err;
1893}
1894
Christoph Hellwig22e70052007-01-02 15:22:30 -08001895static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001896 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001898 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001899 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001900 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001901 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001902 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001903
Thomas Graf35a7aa02007-08-22 14:00:40 -07001904 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001905 if (err)
1906 return err;
1907
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001908 audit_info.loginuid = audit_get_loginuid(current);
1909 audit_info.sessionid = audit_get_sessionid(current);
1910 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001911 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001912 if (err) {
1913 if (err == -ESRCH) /* empty table */
1914 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001915 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001916 }
1917
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001918 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001919 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001920 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001921 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001922 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001923 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
Christoph Hellwig22e70052007-01-02 15:22:30 -08001927static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001928 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001929{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001930 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001931 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001932 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001933 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001934 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001935 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001936 struct xfrm_mark m;
1937 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938
Thomas Graf35a7aa02007-08-22 14:00:40 -07001939 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001940 if (err)
1941 return err;
1942
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001943 err = verify_policy_dir(p->dir);
1944 if (err)
1945 return err;
1946
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001947 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001948 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001949 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001950 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001951 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001952
Thomas Graf35a7aa02007-08-22 14:00:40 -07001953 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001954 if (err)
1955 return err;
1956
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001957 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001959 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001960
Paul Moore03e1ad72008-04-12 19:07:52 -07001961 err = security_xfrm_policy_alloc(&ctx, uctx);
1962 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001963 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001964 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001965 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001966 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001967 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001968 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001969 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001970 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001971
Timo Teräsea2dea92010-03-31 00:17:05 +00001972 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001973 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001974
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001975 err = 0;
1976 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001977 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001978 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001979 u32 sid;
1980
1981 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001982 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001983 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001984
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001985 } else {
1986 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001987 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001988 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001989 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001990
1991out:
1992 xfrm_pol_put(xp);
1993 return err;
1994}
1995
Christoph Hellwig22e70052007-01-02 15:22:30 -08001996static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001997 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001998{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001999 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002000 struct xfrm_state *x;
2001 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002002 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002003 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002004 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002005 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002006
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002007 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 -08002008
David S. Miller3a765aa2007-02-26 14:52:21 -08002009 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002010 if (x == NULL)
2011 return err;
2012
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002013 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002014 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002015 if (x->km.state != XFRM_STATE_VALID)
2016 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002017 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002018
Joy Latten161a09e2006-11-27 13:11:54 -06002019 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002020 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05002021 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08002022 u32 sid;
2023
2024 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002025 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002026 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002027 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002028 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002029out:
2030 spin_unlock_bh(&x->lock);
2031 xfrm_state_put(x);
2032 return err;
2033}
2034
Christoph Hellwig22e70052007-01-02 15:22:30 -08002035static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002036 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002037{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002038 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002039 struct xfrm_policy *xp;
2040 struct xfrm_user_tmpl *ut;
2041 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002042 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002043 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002044
Thomas Graf7b67c852007-08-22 13:53:52 -07002045 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002046 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002047 int err = -ENOMEM;
2048
2049 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002050 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002051
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002052 xfrm_mark_get(attrs, &mark);
2053
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002054 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002055 if (err)
2056 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002057
2058 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002059 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002060 if (!xp)
2061 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002062
2063 memcpy(&x->id, &ua->id, sizeof(ua->id));
2064 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2065 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002066 xp->mark.m = x->mark.m = mark.m;
2067 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002068 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002069 /* extract the templates and for each call km_key */
2070 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2071 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2072 memcpy(&x->id, &t->id, sizeof(x->id));
2073 x->props.mode = t->mode;
2074 x->props.reqid = t->reqid;
2075 x->props.family = ut->family;
2076 t->aalgos = ua->aalgos;
2077 t->ealgos = ua->ealgos;
2078 t->calgos = ua->calgos;
2079 err = km_query(x, t, xp);
2080
2081 }
2082
2083 kfree(x);
2084 kfree(xp);
2085
2086 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002087
2088bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002089 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002090free_state:
2091 kfree(x);
2092nomem:
2093 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002094}
2095
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002096#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002097static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002098 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002099 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002100{
Thomas Graf5424f322007-08-22 14:01:33 -07002101 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002102 struct xfrm_user_migrate *um;
2103 int i, num_migrate;
2104
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002105 if (k != NULL) {
2106 struct xfrm_user_kmaddress *uk;
2107
2108 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2109 memcpy(&k->local, &uk->local, sizeof(k->local));
2110 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2111 k->family = uk->family;
2112 k->reserved = uk->reserved;
2113 }
2114
Thomas Graf5424f322007-08-22 14:01:33 -07002115 um = nla_data(rt);
2116 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002117
2118 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2119 return -EINVAL;
2120
2121 for (i = 0; i < num_migrate; i++, um++, ma++) {
2122 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2123 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2124 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2125 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2126
2127 ma->proto = um->proto;
2128 ma->mode = um->mode;
2129 ma->reqid = um->reqid;
2130
2131 ma->old_family = um->old_family;
2132 ma->new_family = um->new_family;
2133 }
2134
2135 *num = i;
2136 return 0;
2137}
2138
2139static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002140 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002141{
Thomas Graf7b67c852007-08-22 13:53:52 -07002142 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002143 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002144 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002145 u8 type;
2146 int err;
2147 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002148 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149
Thomas Graf35a7aa02007-08-22 14:00:40 -07002150 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002151 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002153 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2154
Thomas Graf5424f322007-08-22 14:01:33 -07002155 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002156 if (err)
2157 return err;
2158
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002159 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002160 if (err)
2161 return err;
2162
2163 if (!n)
2164 return 0;
2165
Fan Du8d549c42013-11-07 17:47:49 +08002166 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002167
2168 return 0;
2169}
2170#else
2171static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002172 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002173{
2174 return -ENOPROTOOPT;
2175}
2176#endif
2177
2178#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002179static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002180{
2181 struct xfrm_user_migrate um;
2182
2183 memset(&um, 0, sizeof(um));
2184 um.proto = m->proto;
2185 um.mode = m->mode;
2186 um.reqid = m->reqid;
2187 um.old_family = m->old_family;
2188 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2189 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2190 um.new_family = m->new_family;
2191 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2192 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2193
Thomas Grafc0144be2007-08-22 13:55:43 -07002194 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002195}
2196
David S. Miller183cad12011-02-24 00:28:01 -05002197static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002198{
2199 struct xfrm_user_kmaddress uk;
2200
2201 memset(&uk, 0, sizeof(uk));
2202 uk.family = k->family;
2203 uk.reserved = k->reserved;
2204 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002205 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002206
2207 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2208}
2209
2210static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002211{
2212 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002213 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2214 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2215 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002216}
2217
David S. Miller183cad12011-02-24 00:28:01 -05002218static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2219 int num_migrate, const struct xfrm_kmaddress *k,
2220 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221{
David S. Miller183cad12011-02-24 00:28:01 -05002222 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002223 struct xfrm_userpolicy_id *pol_id;
2224 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002225 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002226
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002227 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2228 if (nlh == NULL)
2229 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002230
Thomas Graf7b67c852007-08-22 13:53:52 -07002231 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232 /* copy data from selector, dir, and type to the pol_id */
2233 memset(pol_id, 0, sizeof(*pol_id));
2234 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2235 pol_id->dir = dir;
2236
David S. Miller1d1e34d2012-06-27 21:57:03 -07002237 if (k != NULL) {
2238 err = copy_to_user_kmaddress(k, skb);
2239 if (err)
2240 goto out_cancel;
2241 }
2242 err = copy_to_user_policy_type(type, skb);
2243 if (err)
2244 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002245 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002246 err = copy_to_user_migrate(mp, skb);
2247 if (err)
2248 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002249 }
2250
Thomas Graf98250692007-08-22 12:47:26 -07002251 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002252
2253out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002254 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002255 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002256}
2257
David S. Miller183cad12011-02-24 00:28:01 -05002258static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2259 const struct xfrm_migrate *m, int num_migrate,
2260 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002261{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002262 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002263 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002264
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002265 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002266 if (skb == NULL)
2267 return -ENOMEM;
2268
2269 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002270 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002271 BUG();
2272
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002273 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002274}
2275#else
David S. Miller183cad12011-02-24 00:28:01 -05002276static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2277 const struct xfrm_migrate *m, int num_migrate,
2278 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002279{
2280 return -ENOPROTOOPT;
2281}
2282#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002283
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002284#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002285
2286static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002287 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002288 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2289 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2290 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2291 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2292 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2293 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002294 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002295 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002296 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002297 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002298 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002299 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002300 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002301 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2302 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002303 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002304 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002305 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2306 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
Thomas Graf492b5582005-05-03 14:26:40 -07002309#undef XMSGSIZE
2310
Thomas Grafcf5cb792007-08-22 13:59:04 -07002311static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002312 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2313 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2314 [XFRMA_LASTUSED] = { .type = NLA_U64},
2315 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002316 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002317 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2318 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2319 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2320 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2321 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2322 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2323 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2324 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2325 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2326 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2327 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2328 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2329 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2330 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002331 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002332 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002333 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002334 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002335 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002336 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002337 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002338};
2339
Mathias Krause05600a72013-02-24 14:10:27 +01002340static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002341 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002343 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002344} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2345 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2346 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2347 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002348 .dump = xfrm_dump_sa,
2349 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002350 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2351 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2352 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002353 .dump = xfrm_dump_policy,
2354 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002355 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002356 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002357 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002358 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2359 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002360 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002361 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2362 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002363 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2364 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002365 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002366 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002367 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368};
2369
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002370static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002372 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002373 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002374 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002375 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 type -= XFRM_MSG_BASE;
2382 link = &xfrm_dispatch[type];
2383
2384 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002385 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002386 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Thomas Graf492b5582005-05-03 14:26:40 -07002388 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2389 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002390 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002394 {
2395 struct netlink_dump_control c = {
2396 .dump = link->dump,
2397 .done = link->done,
2398 };
2399 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
Thomas Graf35a7aa02007-08-22 14:00:40 -07002403 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002404 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002405 if (err < 0)
2406 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Thomas Graf5424f322007-08-22 14:01:33 -07002411 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002414static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Fan Du283bc9f2013-11-07 17:47:50 +08002416 struct net *net = sock_net(skb->sk);
2417
2418 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002419 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002420 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Thomas Graf7deb2262007-08-22 13:57:39 -07002423static inline size_t xfrm_expire_msgsize(void)
2424{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002425 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2426 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002427}
2428
David S. Miller214e0052011-02-24 00:02:38 -05002429static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 struct xfrm_user_expire *ue;
2432 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002433 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Eric W. Biederman15e47302012-09-07 20:12:54 +00002435 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002436 if (nlh == NULL)
2437 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Thomas Graf7b67c852007-08-22 13:53:52 -07002439 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002441 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
David S. Miller1d1e34d2012-06-27 21:57:03 -07002443 err = xfrm_mark_put(skb, &x->mark);
2444 if (err)
2445 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002446
Thomas Graf98250692007-08-22 12:47:26 -07002447 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
David S. Miller214e0052011-02-24 00:02:38 -05002450static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002452 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct sk_buff *skb;
2454
Thomas Graf7deb2262007-08-22 13:57:39 -07002455 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (skb == NULL)
2457 return -ENOMEM;
2458
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002459 if (build_expire(skb, x, c) < 0) {
2460 kfree_skb(skb);
2461 return -EMSGSIZE;
2462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002464 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
David S. Miller214e0052011-02-24 00:02:38 -05002467static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002468{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002469 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002470 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002471
Steffen Klassertd8647b72011-03-08 00:10:27 +00002472 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002473 if (skb == NULL)
2474 return -ENOMEM;
2475
2476 if (build_aevent(skb, x, c) < 0)
2477 BUG();
2478
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002479 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002480}
2481
David S. Miller214e0052011-02-24 00:02:38 -05002482static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002483{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002484 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002485 struct xfrm_usersa_flush *p;
2486 struct nlmsghdr *nlh;
2487 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002488 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002489
Thomas Graf7deb2262007-08-22 13:57:39 -07002490 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002491 if (skb == NULL)
2492 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493
Eric W. Biederman15e47302012-09-07 20:12:54 +00002494 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002495 if (nlh == NULL) {
2496 kfree_skb(skb);
2497 return -EMSGSIZE;
2498 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002499
Thomas Graf7b67c852007-08-22 13:53:52 -07002500 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002501 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002502
Thomas Graf98250692007-08-22 12:47:26 -07002503 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002504
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002505 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002506}
2507
Thomas Graf7deb2262007-08-22 13:57:39 -07002508static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002509{
Thomas Graf7deb2262007-08-22 13:57:39 -07002510 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002511 if (x->aead)
2512 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002513 if (x->aalg) {
2514 l += nla_total_size(sizeof(struct xfrm_algo) +
2515 (x->aalg->alg_key_len + 7) / 8);
2516 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2517 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002518 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002519 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002520 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002521 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002522 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002523 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002524 if (x->tfcpad)
2525 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002526 if (x->replay_esn)
2527 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002528 if (x->security)
2529 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2530 x->security->ctx_len);
2531 if (x->coaddr)
2532 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002533 if (x->props.extra_flags)
2534 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002535
Herbert Xud26f3982007-11-13 21:47:08 -08002536 /* Must count x->lastused as it may become non-zero behind our back. */
2537 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002538
2539 return l;
2540}
2541
David S. Miller214e0052011-02-24 00:02:38 -05002542static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002543{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002544 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002545 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002546 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002547 struct nlmsghdr *nlh;
2548 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002549 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002550 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002551
2552 headlen = sizeof(*p);
2553 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002554 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002555 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002556 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002557 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002558 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002559
Thomas Graf7deb2262007-08-22 13:57:39 -07002560 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002561 if (skb == NULL)
2562 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002563
Eric W. Biederman15e47302012-09-07 20:12:54 +00002564 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002565 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002566 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002567 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002568
Thomas Graf7b67c852007-08-22 13:53:52 -07002569 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002570 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002571 struct nlattr *attr;
2572
Thomas Graf7b67c852007-08-22 13:53:52 -07002573 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002574 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2575 id->spi = x->id.spi;
2576 id->family = x->props.family;
2577 id->proto = x->id.proto;
2578
Thomas Grafc0144be2007-08-22 13:55:43 -07002579 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002580 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002581 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002582 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002583
2584 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002585 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002586 err = copy_to_user_state_extra(x, p, skb);
2587 if (err)
2588 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002589
Thomas Graf98250692007-08-22 12:47:26 -07002590 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002591
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002592 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002593
David S. Miller1d1e34d2012-06-27 21:57:03 -07002594out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002596 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597}
2598
David S. Miller214e0052011-02-24 00:02:38 -05002599static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002600{
2601
2602 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002603 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002604 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002605 case XFRM_MSG_NEWAE:
2606 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002607 case XFRM_MSG_DELSA:
2608 case XFRM_MSG_UPDSA:
2609 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002610 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002611 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612 return xfrm_notify_sa_flush(c);
2613 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002614 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2615 c->event);
2616 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002617 }
2618
2619 return 0;
2620
2621}
2622
Thomas Graf7deb2262007-08-22 13:57:39 -07002623static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2624 struct xfrm_policy *xp)
2625{
2626 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2627 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002628 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002629 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2630 + userpolicy_type_attrsize();
2631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002634 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002636 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 struct xfrm_user_acquire *ua;
2638 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002639 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002641 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2642 if (nlh == NULL)
2643 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Thomas Graf7b67c852007-08-22 13:53:52 -07002645 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 memcpy(&ua->id, &x->id, sizeof(ua->id));
2647 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2648 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002649 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 ua->aalgos = xt->aalgos;
2651 ua->ealgos = xt->ealgos;
2652 ua->calgos = xt->calgos;
2653 ua->seq = x->km.seq = seq;
2654
David S. Miller1d1e34d2012-06-27 21:57:03 -07002655 err = copy_to_user_tmpl(xp, skb);
2656 if (!err)
2657 err = copy_to_user_state_sec_ctx(x, skb);
2658 if (!err)
2659 err = copy_to_user_policy_type(xp->type, skb);
2660 if (!err)
2661 err = xfrm_mark_put(skb, &xp->mark);
2662 if (err) {
2663 nlmsg_cancel(skb, nlh);
2664 return err;
2665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Thomas Graf98250692007-08-22 12:47:26 -07002667 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
2670static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002671 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002673 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Thomas Graf7deb2262007-08-22 13:57:39 -07002676 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if (skb == NULL)
2678 return -ENOMEM;
2679
Fan Du65e07362012-08-15 10:13:47 +08002680 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 BUG();
2682
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002683 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
2686/* User gives us xfrm_user_policy_info followed by an array of 0
2687 * or more templates.
2688 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002689static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 u8 *data, int len, int *dir)
2691{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002692 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2694 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2695 struct xfrm_policy *xp;
2696 int nr;
2697
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002698 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 case AF_INET:
2700 if (opt != IP_XFRM_POLICY) {
2701 *dir = -EOPNOTSUPP;
2702 return NULL;
2703 }
2704 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002705#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 case AF_INET6:
2707 if (opt != IPV6_XFRM_POLICY) {
2708 *dir = -EOPNOTSUPP;
2709 return NULL;
2710 }
2711 break;
2712#endif
2713 default:
2714 *dir = -EINVAL;
2715 return NULL;
2716 }
2717
2718 *dir = -EINVAL;
2719
2720 if (len < sizeof(*p) ||
2721 verify_newpolicy_info(p))
2722 return NULL;
2723
2724 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002725 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return NULL;
2727
Herbert Xua4f1bac2005-07-26 15:43:17 -07002728 if (p->dir > XFRM_POLICY_OUT)
2729 return NULL;
2730
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002731 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (xp == NULL) {
2733 *dir = -ENOBUFS;
2734 return NULL;
2735 }
2736
2737 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002738 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 copy_templates(xp, ut, nr);
2740
2741 *dir = p->dir;
2742
2743 return xp;
2744}
2745
Thomas Graf7deb2262007-08-22 13:57:39 -07002746static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2747{
2748 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2749 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2750 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002751 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002752 + userpolicy_type_attrsize();
2753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002756 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
2758 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002759 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002760 struct nlmsghdr *nlh;
2761 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Eric W. Biederman15e47302012-09-07 20:12:54 +00002763 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002764 if (nlh == NULL)
2765 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Thomas Graf7b67c852007-08-22 13:53:52 -07002767 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002769 err = copy_to_user_tmpl(xp, skb);
2770 if (!err)
2771 err = copy_to_user_sec_ctx(xp, skb);
2772 if (!err)
2773 err = copy_to_user_policy_type(xp->type, skb);
2774 if (!err)
2775 err = xfrm_mark_put(skb, &xp->mark);
2776 if (err) {
2777 nlmsg_cancel(skb, nlh);
2778 return err;
2779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 upe->hard = !!hard;
2781
Thomas Graf98250692007-08-22 12:47:26 -07002782 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
David S. Miller214e0052011-02-24 00:02:38 -05002785static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002787 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Thomas Graf7deb2262007-08-22 13:57:39 -07002790 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (skb == NULL)
2792 return -ENOMEM;
2793
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002794 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 BUG();
2796
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002797 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
David S. Miller214e0052011-02-24 00:02:38 -05002800static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002801{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002802 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002803 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002804 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002805 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002806 struct nlmsghdr *nlh;
2807 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002808 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002809
2810 headlen = sizeof(*p);
2811 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002812 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002813 headlen = sizeof(*id);
2814 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002815 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002816 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002817 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002818
Thomas Graf7deb2262007-08-22 13:57:39 -07002819 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002820 if (skb == NULL)
2821 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002822
Eric W. Biederman15e47302012-09-07 20:12:54 +00002823 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002824 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002825 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002826 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002827
Thomas Graf7b67c852007-08-22 13:53:52 -07002828 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002829 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002830 struct nlattr *attr;
2831
Thomas Graf7b67c852007-08-22 13:53:52 -07002832 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002833 memset(id, 0, sizeof(*id));
2834 id->dir = dir;
2835 if (c->data.byid)
2836 id->index = xp->index;
2837 else
2838 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2839
Thomas Grafc0144be2007-08-22 13:55:43 -07002840 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002841 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002842 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002843 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002844
2845 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002846 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002847
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002848 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002849 err = copy_to_user_tmpl(xp, skb);
2850 if (!err)
2851 err = copy_to_user_policy_type(xp->type, skb);
2852 if (!err)
2853 err = xfrm_mark_put(skb, &xp->mark);
2854 if (err)
2855 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002856
Thomas Graf98250692007-08-22 12:47:26 -07002857 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002858
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002859 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002860
David S. Miller1d1e34d2012-06-27 21:57:03 -07002861out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002862 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002863 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002864}
2865
David S. Miller214e0052011-02-24 00:02:38 -05002866static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002867{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002868 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002869 struct nlmsghdr *nlh;
2870 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002871 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002872
Thomas Graf7deb2262007-08-22 13:57:39 -07002873 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002874 if (skb == NULL)
2875 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002876
Eric W. Biederman15e47302012-09-07 20:12:54 +00002877 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002878 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002879 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002880 goto out_free_skb;
2881 err = copy_to_user_policy_type(c->data.type, skb);
2882 if (err)
2883 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002884
Thomas Graf98250692007-08-22 12:47:26 -07002885 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002886
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002887 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002888
David S. Miller1d1e34d2012-06-27 21:57:03 -07002889out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002890 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002891 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002892}
2893
David S. Miller214e0052011-02-24 00:02:38 -05002894static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002895{
2896
2897 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002898 case XFRM_MSG_NEWPOLICY:
2899 case XFRM_MSG_UPDPOLICY:
2900 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002901 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002902 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002903 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002904 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002905 return xfrm_exp_policy_notify(xp, dir, c);
2906 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002907 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2908 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002909 }
2910
2911 return 0;
2912
2913}
2914
Thomas Graf7deb2262007-08-22 13:57:39 -07002915static inline size_t xfrm_report_msgsize(void)
2916{
2917 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2918}
2919
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002920static int build_report(struct sk_buff *skb, u8 proto,
2921 struct xfrm_selector *sel, xfrm_address_t *addr)
2922{
2923 struct xfrm_user_report *ur;
2924 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002925
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002926 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2927 if (nlh == NULL)
2928 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002929
Thomas Graf7b67c852007-08-22 13:53:52 -07002930 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002931 ur->proto = proto;
2932 memcpy(&ur->sel, sel, sizeof(ur->sel));
2933
David S. Miller1d1e34d2012-06-27 21:57:03 -07002934 if (addr) {
2935 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2936 if (err) {
2937 nlmsg_cancel(skb, nlh);
2938 return err;
2939 }
2940 }
Thomas Graf98250692007-08-22 12:47:26 -07002941 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002942}
2943
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002944static int xfrm_send_report(struct net *net, u8 proto,
2945 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002946{
2947 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002948
Thomas Graf7deb2262007-08-22 13:57:39 -07002949 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002950 if (skb == NULL)
2951 return -ENOMEM;
2952
2953 if (build_report(skb, proto, sel, addr) < 0)
2954 BUG();
2955
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002956 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002957}
2958
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002959static inline size_t xfrm_mapping_msgsize(void)
2960{
2961 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2962}
2963
2964static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2965 xfrm_address_t *new_saddr, __be16 new_sport)
2966{
2967 struct xfrm_user_mapping *um;
2968 struct nlmsghdr *nlh;
2969
2970 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2971 if (nlh == NULL)
2972 return -EMSGSIZE;
2973
2974 um = nlmsg_data(nlh);
2975
2976 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2977 um->id.spi = x->id.spi;
2978 um->id.family = x->props.family;
2979 um->id.proto = x->id.proto;
2980 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2981 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2982 um->new_sport = new_sport;
2983 um->old_sport = x->encap->encap_sport;
2984 um->reqid = x->props.reqid;
2985
2986 return nlmsg_end(skb, nlh);
2987}
2988
2989static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2990 __be16 sport)
2991{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002992 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002993 struct sk_buff *skb;
2994
2995 if (x->id.proto != IPPROTO_ESP)
2996 return -EINVAL;
2997
2998 if (!x->encap)
2999 return -EINVAL;
3000
3001 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3002 if (skb == NULL)
3003 return -ENOMEM;
3004
3005 if (build_mapping(skb, x, ipaddr, sport) < 0)
3006 BUG();
3007
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003008 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003009}
3010
Horia Geanta0f245582014-02-12 16:20:06 +02003011static bool xfrm_is_alive(const struct km_event *c)
3012{
3013 return (bool)xfrm_acquire_is_on(c->net);
3014}
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016static struct xfrm_mgr netlink_mgr = {
3017 .id = "netlink",
3018 .notify = xfrm_send_state_notify,
3019 .acquire = xfrm_send_acquire,
3020 .compile_policy = xfrm_compile_policy,
3021 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003022 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003023 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003024 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003025 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026};
3027
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003028static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Patrick McHardybe336902006-03-20 22:40:54 -08003030 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003031 struct netlink_kernel_cfg cfg = {
3032 .groups = XFRMNLGRP_MAX,
3033 .input = xfrm_netlink_rcv,
3034 };
Patrick McHardybe336902006-03-20 22:40:54 -08003035
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003036 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003037 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003039 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003040 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return 0;
3042}
3043
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003044static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003045{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003046 struct net *net;
3047 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003048 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003049 synchronize_net();
3050 list_for_each_entry(net, net_exit_list, exit_list)
3051 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003052}
3053
3054static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003055 .init = xfrm_user_net_init,
3056 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003057};
3058
3059static int __init xfrm_user_init(void)
3060{
3061 int rv;
3062
3063 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3064
3065 rv = register_pernet_subsys(&xfrm_user_net_ops);
3066 if (rv < 0)
3067 return rv;
3068 rv = xfrm_register_km(&netlink_mgr);
3069 if (rv < 0)
3070 unregister_pernet_subsys(&xfrm_user_net_ops);
3071 return rv;
3072}
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074static void __exit xfrm_user_exit(void)
3075{
3076 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003077 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
3080module_init(xfrm_user_init);
3081module_exit(xfrm_user_exit);
3082MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003083MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003084