blob: d7694f25829460870ec6c92f4feb26a3c23d5740 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
891{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800892 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800893 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct xfrm_dump_info info;
895
Timo Teras4c563f72008-02-28 21:31:08 -0800896 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
897 sizeof(cb->args) - sizeof(cb->args[0]));
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 info.in_skb = cb->skb;
900 info.out_skb = skb;
901 info.nlmsg_seq = cb->nlh->nlmsg_seq;
902 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800903
904 if (!cb->args[0]) {
905 cb->args[0] = 1;
906 xfrm_state_walk_init(walk, 0);
907 }
908
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800909 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 return skb->len;
912}
913
914static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
915 struct xfrm_state *x, u32 seq)
916{
917 struct xfrm_dump_info info;
918 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000919 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Thomas Graf7deb2262007-08-22 13:57:39 -0700921 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!skb)
923 return ERR_PTR(-ENOMEM);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 info.in_skb = in_skb;
926 info.out_skb = skb;
927 info.nlmsg_seq = seq;
928 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Mathias Krause864745d2012-09-13 11:41:26 +0000930 err = dump_one_state(x, 0, &info);
931 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000933 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 return skb;
937}
938
Thomas Graf7deb2262007-08-22 13:57:39 -0700939static inline size_t xfrm_spdinfo_msgsize(void)
940{
941 return NLMSG_ALIGN(4)
942 + nla_total_size(sizeof(struct xfrmu_spdinfo))
943 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
944}
945
Alexey Dobriyane0710412010-01-23 13:37:10 +0000946static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000947 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700948{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700949 struct xfrmk_spdinfo si;
950 struct xfrmu_spdinfo spc;
951 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700952 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700953 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700954 u32 *f;
955
Eric W. Biederman15e47302012-09-07 20:12:54 +0000956 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300957 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700958 return -EMSGSIZE;
959
960 f = nlmsg_data(nlh);
961 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000962 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700963 spc.incnt = si.incnt;
964 spc.outcnt = si.outcnt;
965 spc.fwdcnt = si.fwdcnt;
966 spc.inscnt = si.inscnt;
967 spc.outscnt = si.outscnt;
968 spc.fwdscnt = si.fwdscnt;
969 sph.spdhcnt = si.spdhcnt;
970 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700971
David S. Miller1d1e34d2012-06-27 21:57:03 -0700972 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
973 if (!err)
974 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
975 if (err) {
976 nlmsg_cancel(skb, nlh);
977 return err;
978 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700979
980 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700981}
982
983static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700984 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700985{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800986 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700987 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700988 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700990 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700991
Thomas Graf7deb2262007-08-22 13:57:39 -0700992 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700993 if (r_skb == NULL)
994 return -ENOMEM;
995
Eric W. Biederman15e47302012-09-07 20:12:54 +0000996 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700997 BUG();
998
Eric W. Biederman15e47302012-09-07 20:12:54 +0000999 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001000}
1001
Thomas Graf7deb2262007-08-22 13:57:39 -07001002static inline size_t xfrm_sadinfo_msgsize(void)
1003{
1004 return NLMSG_ALIGN(4)
1005 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1006 + nla_total_size(4); /* XFRMA_SAD_CNT */
1007}
1008
Alexey Dobriyane0710412010-01-23 13:37:10 +00001009static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001010 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001011{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001012 struct xfrmk_sadinfo si;
1013 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001014 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001015 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001016 u32 *f;
1017
Eric W. Biederman15e47302012-09-07 20:12:54 +00001018 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001019 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001020 return -EMSGSIZE;
1021
1022 f = nlmsg_data(nlh);
1023 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001024 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001025
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001026 sh.sadhmcnt = si.sadhmcnt;
1027 sh.sadhcnt = si.sadhcnt;
1028
David S. Miller1d1e34d2012-06-27 21:57:03 -07001029 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1030 if (!err)
1031 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1032 if (err) {
1033 nlmsg_cancel(skb, nlh);
1034 return err;
1035 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001036
1037 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001038}
1039
1040static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001041 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001042{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001043 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001044 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001045 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001046 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001047 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001048
Thomas Graf7deb2262007-08-22 13:57:39 -07001049 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001050 if (r_skb == NULL)
1051 return -ENOMEM;
1052
Eric W. Biederman15e47302012-09-07 20:12:54 +00001053 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001054 BUG();
1055
Eric W. Biederman15e47302012-09-07 20:12:54 +00001056 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001057}
1058
Christoph Hellwig22e70052007-01-02 15:22:30 -08001059static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001060 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001062 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001063 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct xfrm_state *x;
1065 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001066 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001068 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (x == NULL)
1070 goto out_noput;
1071
1072 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1073 if (IS_ERR(resp_skb)) {
1074 err = PTR_ERR(resp_skb);
1075 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078 xfrm_state_put(x);
1079out_noput:
1080 return err;
1081}
1082
Christoph Hellwig22e70052007-01-02 15:22:30 -08001083static int xfrm_alloc_userspi(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct xfrm_state *x;
1088 struct xfrm_userspi_info *p;
1089 struct sk_buff *resp_skb;
1090 xfrm_address_t *daddr;
1091 int family;
1092 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001093 u32 mark;
1094 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Thomas Graf7b67c852007-08-22 13:53:52 -07001096 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001097 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (err)
1099 goto out_noput;
1100
1101 family = p->info.family;
1102 daddr = &p->info.id.daddr;
1103
1104 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001105
1106 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001108 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001109 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 xfrm_state_put(x);
1111 x = NULL;
1112 }
1113 }
1114
1115 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001116 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 p->info.id.proto, daddr,
1118 &p->info.saddr, 1,
1119 family);
1120 err = -ENOENT;
1121 if (x == NULL)
1122 goto out_noput;
1123
Herbert Xu658b2192007-10-09 13:29:52 -07001124 err = xfrm_alloc_spi(x, p->min, p->max);
1125 if (err)
1126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Herbert Xu658b2192007-10-09 13:29:52 -07001128 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (IS_ERR(resp_skb)) {
1130 err = PTR_ERR(resp_skb);
1131 goto out;
1132 }
1133
Eric W. Biederman15e47302012-09-07 20:12:54 +00001134 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136out:
1137 xfrm_state_put(x);
1138out_noput:
1139 return err;
1140}
1141
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001142static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 switch (dir) {
1145 case XFRM_POLICY_IN:
1146 case XFRM_POLICY_OUT:
1147 case XFRM_POLICY_FWD:
1148 break;
1149
1150 default:
1151 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 return 0;
1155}
1156
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001157static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001158{
1159 switch (type) {
1160 case XFRM_POLICY_TYPE_MAIN:
1161#ifdef CONFIG_XFRM_SUB_POLICY
1162 case XFRM_POLICY_TYPE_SUB:
1163#endif
1164 break;
1165
1166 default:
1167 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001168 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001169
1170 return 0;
1171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1174{
Fan Due682adf2013-11-07 17:47:48 +08001175 int ret;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 switch (p->share) {
1178 case XFRM_SHARE_ANY:
1179 case XFRM_SHARE_SESSION:
1180 case XFRM_SHARE_USER:
1181 case XFRM_SHARE_UNIQUE:
1182 break;
1183
1184 default:
1185 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 switch (p->action) {
1189 case XFRM_POLICY_ALLOW:
1190 case XFRM_POLICY_BLOCK:
1191 break;
1192
1193 default:
1194 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 switch (p->sel.family) {
1198 case AF_INET:
1199 break;
1200
1201 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001202#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 break;
1204#else
1205 return -EAFNOSUPPORT;
1206#endif
1207
1208 default:
1209 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Fan Due682adf2013-11-07 17:47:48 +08001212 ret = verify_policy_dir(p->dir);
1213 if (ret)
1214 return ret;
1215 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1216 return -EINVAL;
1217
1218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Thomas Graf5424f322007-08-22 14:01:33 -07001221static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001222{
Thomas Graf5424f322007-08-22 14:01:33 -07001223 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001224 struct xfrm_user_sec_ctx *uctx;
1225
1226 if (!rt)
1227 return 0;
1228
Thomas Graf5424f322007-08-22 14:01:33 -07001229 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001230 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1234 int nr)
1235{
1236 int i;
1237
1238 xp->xfrm_nr = nr;
1239 for (i = 0; i < nr; i++, ut++) {
1240 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1241
1242 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1243 memcpy(&t->saddr, &ut->saddr,
1244 sizeof(xfrm_address_t));
1245 t->reqid = ut->reqid;
1246 t->mode = ut->mode;
1247 t->share = ut->share;
1248 t->optional = ut->optional;
1249 t->aalgos = ut->aalgos;
1250 t->ealgos = ut->ealgos;
1251 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001252 /* If all masks are ~0, then we allow all algorithms. */
1253 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001254 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256}
1257
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001258static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1259{
1260 int i;
1261
1262 if (nr > XFRM_MAX_DEPTH)
1263 return -EINVAL;
1264
1265 for (i = 0; i < nr; i++) {
1266 /* We never validated the ut->family value, so many
1267 * applications simply leave it at zero. The check was
1268 * never made and ut->family was ignored because all
1269 * templates could be assumed to have the same family as
1270 * the policy itself. Now that we will have ipv4-in-ipv6
1271 * and ipv6-in-ipv4 tunnels, this is no longer true.
1272 */
1273 if (!ut[i].family)
1274 ut[i].family = family;
1275
1276 switch (ut[i].family) {
1277 case AF_INET:
1278 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001279#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001280 case AF_INET6:
1281 break;
1282#endif
1283 default:
1284 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001285 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001286 }
1287
1288 return 0;
1289}
1290
Thomas Graf5424f322007-08-22 14:01:33 -07001291static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Thomas Graf5424f322007-08-22 14:01:33 -07001293 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (!rt) {
1296 pol->xfrm_nr = 0;
1297 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001298 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1299 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001300 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001302 err = validate_tmpl(nr, utmpl, pol->family);
1303 if (err)
1304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Thomas Graf5424f322007-08-22 14:01:33 -07001306 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 return 0;
1309}
1310
Thomas Graf5424f322007-08-22 14:01:33 -07001311static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001312{
Thomas Graf5424f322007-08-22 14:01:33 -07001313 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001314 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001315 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001316 int err;
1317
1318 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001319 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001320 type = upt->type;
1321 }
1322
1323 err = verify_policy_type(type);
1324 if (err)
1325 return err;
1326
1327 *tp = type;
1328 return 0;
1329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1332{
1333 xp->priority = p->priority;
1334 xp->index = p->index;
1335 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1336 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1337 xp->action = p->action;
1338 xp->flags = p->flags;
1339 xp->family = p->sel.family;
1340 /* XXX xp->share = p->share; */
1341}
1342
1343static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1344{
Mathias Krause7b789832012-09-19 11:33:40 +00001345 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1347 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1348 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1349 p->priority = xp->priority;
1350 p->index = xp->index;
1351 p->sel.family = xp->family;
1352 p->dir = dir;
1353 p->action = xp->action;
1354 p->flags = xp->flags;
1355 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1356}
1357
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001358static 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 -07001359{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001360 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 int err;
1362
1363 if (!xp) {
1364 *errp = -ENOMEM;
1365 return NULL;
1366 }
1367
1368 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001369
Thomas Graf35a7aa02007-08-22 14:00:40 -07001370 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001371 if (err)
1372 goto error;
1373
Thomas Graf35a7aa02007-08-22 14:00:40 -07001374 if (!(err = copy_from_user_tmpl(xp, attrs)))
1375 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001376 if (err)
1377 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001379 xfrm_mark_get(attrs, &xp->mark);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001382 error:
1383 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001384 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001385 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001386 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Christoph Hellwig22e70052007-01-02 15:22:30 -08001389static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001390 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001392 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001393 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001395 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int err;
1397 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001398 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001399 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001400 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 err = verify_newpolicy_info(p);
1403 if (err)
1404 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001405 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001406 if (err)
1407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001409 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (!xp)
1411 return err;
1412
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001413 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001414 * Aha! this is anti-netlink really i.e more pfkey derived
1415 * in netlink excl is a flag and you wouldnt need
1416 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1418 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001419 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001420 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001423 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 kfree(xp);
1425 return err;
1426 }
1427
Herbert Xuf60f6b82005-06-18 22:44:37 -07001428 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001429 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001430 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 km_policy_notify(xp, p->dir, &c);
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 xfrm_pol_put(xp);
1434
1435 return 0;
1436}
1437
1438static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1439{
1440 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1441 int i;
1442
1443 if (xp->xfrm_nr == 0)
1444 return 0;
1445
1446 for (i = 0; i < xp->xfrm_nr; i++) {
1447 struct xfrm_user_tmpl *up = &vec[i];
1448 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1449
Mathias Krause1f868402012-09-19 11:33:41 +00001450 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001452 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1454 up->reqid = kp->reqid;
1455 up->mode = kp->mode;
1456 up->share = kp->share;
1457 up->optional = kp->optional;
1458 up->aalgos = kp->aalgos;
1459 up->ealgos = kp->ealgos;
1460 up->calgos = kp->calgos;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Grafc0144be2007-08-22 13:55:43 -07001463 return nla_put(skb, XFRMA_TMPL,
1464 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001465}
1466
Serge Hallyn0d681622006-07-24 23:30:44 -07001467static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1468{
1469 if (x->security) {
1470 return copy_sec_ctx(x->security, skb);
1471 }
1472 return 0;
1473}
1474
1475static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1476{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001477 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001478 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001479 return 0;
1480}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001481static inline size_t userpolicy_type_attrsize(void)
1482{
1483#ifdef CONFIG_XFRM_SUB_POLICY
1484 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1485#else
1486 return 0;
1487#endif
1488}
Serge Hallyn0d681622006-07-24 23:30:44 -07001489
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001490#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001491static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001492{
Thomas Grafc0144be2007-08-22 13:55:43 -07001493 struct xfrm_userpolicy_type upt = {
1494 .type = type,
1495 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001496
Thomas Grafc0144be2007-08-22 13:55:43 -07001497 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001498}
1499
1500#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001501static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001502{
1503 return 0;
1504}
1505#endif
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1508{
1509 struct xfrm_dump_info *sp = ptr;
1510 struct xfrm_userpolicy_info *p;
1511 struct sk_buff *in_skb = sp->in_skb;
1512 struct sk_buff *skb = sp->out_skb;
1513 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001514 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Eric W. Biederman15e47302012-09-07 20:12:54 +00001516 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001517 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1518 if (nlh == NULL)
1519 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Thomas Graf7b67c852007-08-22 13:53:52 -07001521 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001523 err = copy_to_user_tmpl(xp, skb);
1524 if (!err)
1525 err = copy_to_user_sec_ctx(xp, skb);
1526 if (!err)
1527 err = copy_to_user_policy_type(xp->type, skb);
1528 if (!err)
1529 err = xfrm_mark_put(skb, &xp->mark);
1530 if (err) {
1531 nlmsg_cancel(skb, nlh);
1532 return err;
1533 }
Thomas Graf98250692007-08-22 12:47:26 -07001534 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Timo Teras4c563f72008-02-28 21:31:08 -08001538static int xfrm_dump_policy_done(struct netlink_callback *cb)
1539{
1540 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001541 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001542
Fan Du283bc9f2013-11-07 17:47:50 +08001543 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001544 return 0;
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1548{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001549 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001550 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct xfrm_dump_info info;
1552
Timo Teras4c563f72008-02-28 21:31:08 -08001553 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1554 sizeof(cb->args) - sizeof(cb->args[0]));
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 info.in_skb = cb->skb;
1557 info.out_skb = skb;
1558 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1559 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001560
1561 if (!cb->args[0]) {
1562 cb->args[0] = 1;
1563 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1564 }
1565
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001566 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 return skb->len;
1569}
1570
1571static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1572 struct xfrm_policy *xp,
1573 int dir, u32 seq)
1574{
1575 struct xfrm_dump_info info;
1576 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001577 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Thomas Graf7deb2262007-08-22 13:57:39 -07001579 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (!skb)
1581 return ERR_PTR(-ENOMEM);
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 info.in_skb = in_skb;
1584 info.out_skb = skb;
1585 info.nlmsg_seq = seq;
1586 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Mathias Krausec2546372012-09-14 09:58:32 +00001588 err = dump_one_policy(xp, dir, 0, &info);
1589 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001591 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
1594 return skb;
1595}
1596
Christoph Hellwig22e70052007-01-02 15:22:30 -08001597static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001598 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001600 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 struct xfrm_policy *xp;
1602 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001603 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001605 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001607 struct xfrm_mark m;
1608 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Thomas Graf7b67c852007-08-22 13:53:52 -07001610 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1612
Thomas Graf35a7aa02007-08-22 14:00:40 -07001613 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001614 if (err)
1615 return err;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 err = verify_policy_dir(p->dir);
1618 if (err)
1619 return err;
1620
1621 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001622 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001623 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001624 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001625 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001626
Thomas Graf35a7aa02007-08-22 14:00:40 -07001627 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001628 if (err)
1629 return err;
1630
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001631 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001632 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001633 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001634
Paul Moore03e1ad72008-04-12 19:07:52 -07001635 err = security_xfrm_policy_alloc(&ctx, uctx);
1636 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001637 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001638 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001639 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001640 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001641 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (xp == NULL)
1644 return -ENOENT;
1645
1646 if (!delete) {
1647 struct sk_buff *resp_skb;
1648
1649 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1650 if (IS_ERR(resp_skb)) {
1651 err = PTR_ERR(resp_skb);
1652 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001653 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001654 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001656 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001657 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001658 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001659 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001660
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001661 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001662 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1663 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001664
1665 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001666 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001667
Herbert Xue7443892005-06-18 22:44:18 -07001668 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001669 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001670 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001671 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001672 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001675out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001676 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001677 if (delete && err == 0)
1678 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return err;
1680}
1681
Christoph Hellwig22e70052007-01-02 15:22:30 -08001682static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001683 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001685 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001686 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001687 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001688 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001689 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001691 audit_info.loginuid = audit_get_loginuid(current);
1692 audit_info.sessionid = audit_get_sessionid(current);
1693 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001694 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001695 if (err) {
1696 if (err == -ESRCH) /* empty table */
1697 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001698 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001699 }
Herbert Xubf088672005-06-18 22:44:00 -07001700 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001701 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001702 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001703 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001704 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001705 km_state_notify(NULL, &c);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return 0;
1708}
1709
Steffen Klassertd8647b72011-03-08 00:10:27 +00001710static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001711{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001712 size_t replay_size = x->replay_esn ?
1713 xfrm_replay_state_esn_len(x->replay_esn) :
1714 sizeof(struct xfrm_replay_state);
1715
Thomas Graf7deb2262007-08-22 13:57:39 -07001716 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001717 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001718 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001719 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001720 + nla_total_size(4) /* XFRM_AE_RTHR */
1721 + nla_total_size(4); /* XFRM_AE_ETHR */
1722}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001723
David S. Miller214e0052011-02-24 00:02:38 -05001724static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001725{
1726 struct xfrm_aevent_id *id;
1727 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001728 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001729
Eric W. Biederman15e47302012-09-07 20:12:54 +00001730 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001731 if (nlh == NULL)
1732 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001733
Thomas Graf7b67c852007-08-22 13:53:52 -07001734 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001735 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001736 id->sa_id.spi = x->id.spi;
1737 id->sa_id.family = x->props.family;
1738 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001739 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001740 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001741 id->flags = c->data.aevent;
1742
David S. Millerd0fde792012-03-29 04:02:26 -04001743 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001744 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1745 xfrm_replay_state_esn_len(x->replay_esn),
1746 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001747 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001748 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1749 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001750 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001751 if (err)
1752 goto out_cancel;
1753 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1754 if (err)
1755 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001756
David S. Miller1d1e34d2012-06-27 21:57:03 -07001757 if (id->flags & XFRM_AE_RTHR) {
1758 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1759 if (err)
1760 goto out_cancel;
1761 }
1762 if (id->flags & XFRM_AE_ETHR) {
1763 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1764 x->replay_maxage * 10 / HZ);
1765 if (err)
1766 goto out_cancel;
1767 }
1768 err = xfrm_mark_put(skb, &x->mark);
1769 if (err)
1770 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001771
Thomas Graf98250692007-08-22 12:47:26 -07001772 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001773
David S. Miller1d1e34d2012-06-27 21:57:03 -07001774out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001775 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001776 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001777}
1778
Christoph Hellwig22e70052007-01-02 15:22:30 -08001779static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001780 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001781{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001782 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001783 struct xfrm_state *x;
1784 struct sk_buff *r_skb;
1785 int err;
1786 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001787 u32 mark;
1788 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001789 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001790 struct xfrm_usersa_id *id = &p->sa_id;
1791
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001792 mark = xfrm_mark_get(attrs, &m);
1793
1794 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001795 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001796 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001797
1798 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1799 if (r_skb == NULL) {
1800 xfrm_state_put(x);
1801 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001802 }
1803
1804 /*
1805 * XXX: is this lock really needed - none of the other
1806 * gets lock (the concern is things getting updated
1807 * while we are still reading) - jhs
1808 */
1809 spin_lock_bh(&x->lock);
1810 c.data.aevent = p->flags;
1811 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001812 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001813
1814 if (build_aevent(r_skb, x, &c) < 0)
1815 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001816 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001817 spin_unlock_bh(&x->lock);
1818 xfrm_state_put(x);
1819 return err;
1820}
1821
Christoph Hellwig22e70052007-01-02 15:22:30 -08001822static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001823 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001824{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001825 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001826 struct xfrm_state *x;
1827 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001828 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001829 u32 mark = 0;
1830 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001831 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001832 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001833 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001834 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001835
Steffen Klassertd8647b72011-03-08 00:10:27 +00001836 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001837 return err;
1838
1839 /* pedantic mode - thou shalt sayeth replaceth */
1840 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1841 return err;
1842
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001843 mark = xfrm_mark_get(attrs, &m);
1844
1845 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 -08001846 if (x == NULL)
1847 return -ESRCH;
1848
1849 if (x->km.state != XFRM_STATE_VALID)
1850 goto out;
1851
Steffen Klassert4479ff72013-09-09 09:39:01 +02001852 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001853 if (err)
1854 goto out;
1855
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001856 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001857 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001858 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001859
1860 c.event = nlh->nlmsg_type;
1861 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001862 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001863 c.data.aevent = XFRM_AE_CU;
1864 km_state_notify(x, &c);
1865 err = 0;
1866out:
1867 xfrm_state_put(x);
1868 return err;
1869}
1870
Christoph Hellwig22e70052007-01-02 15:22:30 -08001871static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001872 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001874 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001875 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001876 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001877 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001878 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001879
Thomas Graf35a7aa02007-08-22 14:00:40 -07001880 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001881 if (err)
1882 return err;
1883
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001884 audit_info.loginuid = audit_get_loginuid(current);
1885 audit_info.sessionid = audit_get_sessionid(current);
1886 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001887 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001888 if (err) {
1889 if (err == -ESRCH) /* empty table */
1890 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001891 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001892 }
1893
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001894 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001895 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001896 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001897 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001898 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001899 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 0;
1901}
1902
Christoph Hellwig22e70052007-01-02 15:22:30 -08001903static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001904 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001905{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001906 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001907 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001908 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001909 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001910 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001911 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001912 struct xfrm_mark m;
1913 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001914
Thomas Graf35a7aa02007-08-22 14:00:40 -07001915 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001916 if (err)
1917 return err;
1918
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001919 err = verify_policy_dir(p->dir);
1920 if (err)
1921 return err;
1922
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001923 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001924 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001925 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001926 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001927 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001928
Thomas Graf35a7aa02007-08-22 14:00:40 -07001929 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930 if (err)
1931 return err;
1932
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001933 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001934 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001935 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001936
Paul Moore03e1ad72008-04-12 19:07:52 -07001937 err = security_xfrm_policy_alloc(&ctx, uctx);
1938 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001939 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001940 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001941 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001942 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001943 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001945 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001946 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001947
Timo Teräsea2dea92010-03-31 00:17:05 +00001948 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001949 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001950
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001951 err = 0;
1952 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001953 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001954 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001955 u32 sid;
1956
1957 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001959 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001960
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001961 } else {
1962 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001963 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001964 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001965 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001966
1967out:
1968 xfrm_pol_put(xp);
1969 return err;
1970}
1971
Christoph Hellwig22e70052007-01-02 15:22:30 -08001972static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001973 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001974{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001975 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001976 struct xfrm_state *x;
1977 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001978 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001979 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001980 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001981 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001982
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001983 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001984
David S. Miller3a765aa2007-02-26 14:52:21 -08001985 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001986 if (x == NULL)
1987 return err;
1988
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001989 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001990 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001991 if (x->km.state != XFRM_STATE_VALID)
1992 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001993 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001994
Joy Latten161a09e2006-11-27 13:11:54 -06001995 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001996 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001997 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001998 u32 sid;
1999
2000 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002001 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002002 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002003 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002004 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002005out:
2006 spin_unlock_bh(&x->lock);
2007 xfrm_state_put(x);
2008 return err;
2009}
2010
Christoph Hellwig22e70052007-01-02 15:22:30 -08002011static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002012 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002013{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002014 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002015 struct xfrm_policy *xp;
2016 struct xfrm_user_tmpl *ut;
2017 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002018 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002019 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002020
Thomas Graf7b67c852007-08-22 13:53:52 -07002021 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002022 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002023 int err = -ENOMEM;
2024
2025 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002026 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002027
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002028 xfrm_mark_get(attrs, &mark);
2029
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002030 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002031 if (err)
2032 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002033
2034 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002035 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002036 if (!xp)
2037 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002038
2039 memcpy(&x->id, &ua->id, sizeof(ua->id));
2040 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2041 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002042 xp->mark.m = x->mark.m = mark.m;
2043 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002044 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002045 /* extract the templates and for each call km_key */
2046 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2047 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2048 memcpy(&x->id, &t->id, sizeof(x->id));
2049 x->props.mode = t->mode;
2050 x->props.reqid = t->reqid;
2051 x->props.family = ut->family;
2052 t->aalgos = ua->aalgos;
2053 t->ealgos = ua->ealgos;
2054 t->calgos = ua->calgos;
2055 err = km_query(x, t, xp);
2056
2057 }
2058
2059 kfree(x);
2060 kfree(xp);
2061
2062 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002063
2064bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002065 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002066free_state:
2067 kfree(x);
2068nomem:
2069 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002070}
2071
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002073static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002074 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002075 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002076{
Thomas Graf5424f322007-08-22 14:01:33 -07002077 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002078 struct xfrm_user_migrate *um;
2079 int i, num_migrate;
2080
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002081 if (k != NULL) {
2082 struct xfrm_user_kmaddress *uk;
2083
2084 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2085 memcpy(&k->local, &uk->local, sizeof(k->local));
2086 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2087 k->family = uk->family;
2088 k->reserved = uk->reserved;
2089 }
2090
Thomas Graf5424f322007-08-22 14:01:33 -07002091 um = nla_data(rt);
2092 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002093
2094 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2095 return -EINVAL;
2096
2097 for (i = 0; i < num_migrate; i++, um++, ma++) {
2098 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2099 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2100 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2101 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2102
2103 ma->proto = um->proto;
2104 ma->mode = um->mode;
2105 ma->reqid = um->reqid;
2106
2107 ma->old_family = um->old_family;
2108 ma->new_family = um->new_family;
2109 }
2110
2111 *num = i;
2112 return 0;
2113}
2114
2115static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002116 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002117{
Thomas Graf7b67c852007-08-22 13:53:52 -07002118 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002119 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002120 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002121 u8 type;
2122 int err;
2123 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002124 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002125
Thomas Graf35a7aa02007-08-22 14:00:40 -07002126 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002127 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002128
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002129 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2130
Thomas Graf5424f322007-08-22 14:01:33 -07002131 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002132 if (err)
2133 return err;
2134
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002135 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002136 if (err)
2137 return err;
2138
2139 if (!n)
2140 return 0;
2141
Fan Du8d549c42013-11-07 17:47:49 +08002142 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002143
2144 return 0;
2145}
2146#else
2147static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002148 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149{
2150 return -ENOPROTOOPT;
2151}
2152#endif
2153
2154#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002155static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002156{
2157 struct xfrm_user_migrate um;
2158
2159 memset(&um, 0, sizeof(um));
2160 um.proto = m->proto;
2161 um.mode = m->mode;
2162 um.reqid = m->reqid;
2163 um.old_family = m->old_family;
2164 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2165 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2166 um.new_family = m->new_family;
2167 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2168 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2169
Thomas Grafc0144be2007-08-22 13:55:43 -07002170 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002171}
2172
David S. Miller183cad12011-02-24 00:28:01 -05002173static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002174{
2175 struct xfrm_user_kmaddress uk;
2176
2177 memset(&uk, 0, sizeof(uk));
2178 uk.family = k->family;
2179 uk.reserved = k->reserved;
2180 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002181 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002182
2183 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2184}
2185
2186static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002187{
2188 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002189 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2190 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2191 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002192}
2193
David S. Miller183cad12011-02-24 00:28:01 -05002194static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2195 int num_migrate, const struct xfrm_kmaddress *k,
2196 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002197{
David S. Miller183cad12011-02-24 00:28:01 -05002198 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002199 struct xfrm_userpolicy_id *pol_id;
2200 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002201 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002202
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002203 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2204 if (nlh == NULL)
2205 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002206
Thomas Graf7b67c852007-08-22 13:53:52 -07002207 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002208 /* copy data from selector, dir, and type to the pol_id */
2209 memset(pol_id, 0, sizeof(*pol_id));
2210 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2211 pol_id->dir = dir;
2212
David S. Miller1d1e34d2012-06-27 21:57:03 -07002213 if (k != NULL) {
2214 err = copy_to_user_kmaddress(k, skb);
2215 if (err)
2216 goto out_cancel;
2217 }
2218 err = copy_to_user_policy_type(type, skb);
2219 if (err)
2220 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002222 err = copy_to_user_migrate(mp, skb);
2223 if (err)
2224 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002225 }
2226
Thomas Graf98250692007-08-22 12:47:26 -07002227 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002228
2229out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002230 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002231 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232}
2233
David S. Miller183cad12011-02-24 00:28:01 -05002234static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2235 const struct xfrm_migrate *m, int num_migrate,
2236 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002237{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002238 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002239 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002240
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002241 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002242 if (skb == NULL)
2243 return -ENOMEM;
2244
2245 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002246 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002247 BUG();
2248
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002249 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002250}
2251#else
David S. Miller183cad12011-02-24 00:28:01 -05002252static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2253 const struct xfrm_migrate *m, int num_migrate,
2254 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002255{
2256 return -ENOPROTOOPT;
2257}
2258#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002259
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002260#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002261
2262static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002263 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002264 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2265 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2266 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2267 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2268 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2269 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002270 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002271 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002272 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002273 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002274 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002275 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002276 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002277 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2278 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002279 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002280 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002281 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2282 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283};
2284
Thomas Graf492b5582005-05-03 14:26:40 -07002285#undef XMSGSIZE
2286
Thomas Grafcf5cb792007-08-22 13:59:04 -07002287static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002288 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2289 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2290 [XFRMA_LASTUSED] = { .type = NLA_U64},
2291 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002292 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002293 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2294 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2295 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2296 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2297 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2298 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2299 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2300 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2301 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2302 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2303 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2304 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2305 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2306 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002307 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002308 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002309 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002310 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002311 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002312};
2313
Mathias Krause05600a72013-02-24 14:10:27 +01002314static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002315 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002317 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002318} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2319 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2320 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2321 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002322 .dump = xfrm_dump_sa,
2323 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002324 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2325 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2326 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002327 .dump = xfrm_dump_policy,
2328 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002329 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002330 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002331 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002332 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2333 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002334 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002335 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2336 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002337 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2338 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002339 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002340 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002341 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342};
2343
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002344static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002346 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002347 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002348 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002349 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 type -= XFRM_MSG_BASE;
2356 link = &xfrm_dispatch[type];
2357
2358 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002359 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002360 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Thomas Graf492b5582005-05-03 14:26:40 -07002362 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2363 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002364 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002368 {
2369 struct netlink_dump_control c = {
2370 .dump = link->dump,
2371 .done = link->done,
2372 };
2373 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Thomas Graf35a7aa02007-08-22 14:00:40 -07002377 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002378 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002379 if (err < 0)
2380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Thomas Graf5424f322007-08-22 14:01:33 -07002385 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002388static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Fan Du283bc9f2013-11-07 17:47:50 +08002390 struct net *net = sock_net(skb->sk);
2391
2392 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002393 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002394 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
Thomas Graf7deb2262007-08-22 13:57:39 -07002397static inline size_t xfrm_expire_msgsize(void)
2398{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002399 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2400 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002401}
2402
David S. Miller214e0052011-02-24 00:02:38 -05002403static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 struct xfrm_user_expire *ue;
2406 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002407 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Eric W. Biederman15e47302012-09-07 20:12:54 +00002409 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002410 if (nlh == NULL)
2411 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Thomas Graf7b67c852007-08-22 13:53:52 -07002413 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002415 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
David S. Miller1d1e34d2012-06-27 21:57:03 -07002417 err = xfrm_mark_put(skb, &x->mark);
2418 if (err)
2419 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002420
Thomas Graf98250692007-08-22 12:47:26 -07002421 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
David S. Miller214e0052011-02-24 00:02:38 -05002424static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002426 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 struct sk_buff *skb;
2428
Thomas Graf7deb2262007-08-22 13:57:39 -07002429 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (skb == NULL)
2431 return -ENOMEM;
2432
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002433 if (build_expire(skb, x, c) < 0) {
2434 kfree_skb(skb);
2435 return -EMSGSIZE;
2436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002438 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
David S. Miller214e0052011-02-24 00:02:38 -05002441static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002442{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002443 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002444 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002445
Steffen Klassertd8647b72011-03-08 00:10:27 +00002446 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002447 if (skb == NULL)
2448 return -ENOMEM;
2449
2450 if (build_aevent(skb, x, c) < 0)
2451 BUG();
2452
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002453 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002454}
2455
David S. Miller214e0052011-02-24 00:02:38 -05002456static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002458 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002459 struct xfrm_usersa_flush *p;
2460 struct nlmsghdr *nlh;
2461 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002462 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463
Thomas Graf7deb2262007-08-22 13:57:39 -07002464 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002465 if (skb == NULL)
2466 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002467
Eric W. Biederman15e47302012-09-07 20:12:54 +00002468 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002469 if (nlh == NULL) {
2470 kfree_skb(skb);
2471 return -EMSGSIZE;
2472 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002473
Thomas Graf7b67c852007-08-22 13:53:52 -07002474 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002475 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002476
Thomas Graf98250692007-08-22 12:47:26 -07002477 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002478
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002479 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480}
2481
Thomas Graf7deb2262007-08-22 13:57:39 -07002482static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002483{
Thomas Graf7deb2262007-08-22 13:57:39 -07002484 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002485 if (x->aead)
2486 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002487 if (x->aalg) {
2488 l += nla_total_size(sizeof(struct xfrm_algo) +
2489 (x->aalg->alg_key_len + 7) / 8);
2490 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2491 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002492 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002493 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002494 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002495 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002496 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002497 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002498 if (x->tfcpad)
2499 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002500 if (x->replay_esn)
2501 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002502 if (x->security)
2503 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2504 x->security->ctx_len);
2505 if (x->coaddr)
2506 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002507 if (x->props.extra_flags)
2508 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002509
Herbert Xud26f3982007-11-13 21:47:08 -08002510 /* Must count x->lastused as it may become non-zero behind our back. */
2511 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002512
2513 return l;
2514}
2515
David S. Miller214e0052011-02-24 00:02:38 -05002516static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002517{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002518 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002519 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002520 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521 struct nlmsghdr *nlh;
2522 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002524 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002525
2526 headlen = sizeof(*p);
2527 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002528 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002529 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002530 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002531 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002532 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002533
Thomas Graf7deb2262007-08-22 13:57:39 -07002534 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002535 if (skb == NULL)
2536 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002537
Eric W. Biederman15e47302012-09-07 20:12:54 +00002538 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002539 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002540 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002541 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002542
Thomas Graf7b67c852007-08-22 13:53:52 -07002543 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002544 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002545 struct nlattr *attr;
2546
Thomas Graf7b67c852007-08-22 13:53:52 -07002547 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002548 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2549 id->spi = x->id.spi;
2550 id->family = x->props.family;
2551 id->proto = x->id.proto;
2552
Thomas Grafc0144be2007-08-22 13:55:43 -07002553 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002554 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002555 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002556 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002557
2558 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002559 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002560 err = copy_to_user_state_extra(x, p, skb);
2561 if (err)
2562 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002563
Thomas Graf98250692007-08-22 12:47:26 -07002564 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002565
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002566 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567
David S. Miller1d1e34d2012-06-27 21:57:03 -07002568out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002570 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002571}
2572
David S. Miller214e0052011-02-24 00:02:38 -05002573static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002574{
2575
2576 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002577 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002578 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002579 case XFRM_MSG_NEWAE:
2580 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002581 case XFRM_MSG_DELSA:
2582 case XFRM_MSG_UPDSA:
2583 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002584 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002585 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002586 return xfrm_notify_sa_flush(c);
2587 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002588 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2589 c->event);
2590 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002591 }
2592
2593 return 0;
2594
2595}
2596
Thomas Graf7deb2262007-08-22 13:57:39 -07002597static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2598 struct xfrm_policy *xp)
2599{
2600 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2601 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002602 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002603 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2604 + userpolicy_type_attrsize();
2605}
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002608 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002610 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 struct xfrm_user_acquire *ua;
2612 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002613 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002615 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2616 if (nlh == NULL)
2617 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Thomas Graf7b67c852007-08-22 13:53:52 -07002619 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 memcpy(&ua->id, &x->id, sizeof(ua->id));
2621 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2622 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002623 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 ua->aalgos = xt->aalgos;
2625 ua->ealgos = xt->ealgos;
2626 ua->calgos = xt->calgos;
2627 ua->seq = x->km.seq = seq;
2628
David S. Miller1d1e34d2012-06-27 21:57:03 -07002629 err = copy_to_user_tmpl(xp, skb);
2630 if (!err)
2631 err = copy_to_user_state_sec_ctx(x, skb);
2632 if (!err)
2633 err = copy_to_user_policy_type(xp->type, skb);
2634 if (!err)
2635 err = xfrm_mark_put(skb, &xp->mark);
2636 if (err) {
2637 nlmsg_cancel(skb, nlh);
2638 return err;
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Thomas Graf98250692007-08-22 12:47:26 -07002641 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
2644static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002645 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002647 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Thomas Graf7deb2262007-08-22 13:57:39 -07002650 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if (skb == NULL)
2652 return -ENOMEM;
2653
Fan Du65e07362012-08-15 10:13:47 +08002654 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 BUG();
2656
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002657 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660/* User gives us xfrm_user_policy_info followed by an array of 0
2661 * or more templates.
2662 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002663static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 u8 *data, int len, int *dir)
2665{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002666 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2668 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2669 struct xfrm_policy *xp;
2670 int nr;
2671
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002672 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 case AF_INET:
2674 if (opt != IP_XFRM_POLICY) {
2675 *dir = -EOPNOTSUPP;
2676 return NULL;
2677 }
2678 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002679#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 case AF_INET6:
2681 if (opt != IPV6_XFRM_POLICY) {
2682 *dir = -EOPNOTSUPP;
2683 return NULL;
2684 }
2685 break;
2686#endif
2687 default:
2688 *dir = -EINVAL;
2689 return NULL;
2690 }
2691
2692 *dir = -EINVAL;
2693
2694 if (len < sizeof(*p) ||
2695 verify_newpolicy_info(p))
2696 return NULL;
2697
2698 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002699 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return NULL;
2701
Herbert Xua4f1bac2005-07-26 15:43:17 -07002702 if (p->dir > XFRM_POLICY_OUT)
2703 return NULL;
2704
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002705 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (xp == NULL) {
2707 *dir = -ENOBUFS;
2708 return NULL;
2709 }
2710
2711 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002712 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 copy_templates(xp, ut, nr);
2714
2715 *dir = p->dir;
2716
2717 return xp;
2718}
2719
Thomas Graf7deb2262007-08-22 13:57:39 -07002720static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2721{
2722 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2723 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2724 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002725 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002726 + userpolicy_type_attrsize();
2727}
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002730 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
2732 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002733 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002734 struct nlmsghdr *nlh;
2735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Eric W. Biederman15e47302012-09-07 20:12:54 +00002737 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002738 if (nlh == NULL)
2739 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Thomas Graf7b67c852007-08-22 13:53:52 -07002741 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002743 err = copy_to_user_tmpl(xp, skb);
2744 if (!err)
2745 err = copy_to_user_sec_ctx(xp, skb);
2746 if (!err)
2747 err = copy_to_user_policy_type(xp->type, skb);
2748 if (!err)
2749 err = xfrm_mark_put(skb, &xp->mark);
2750 if (err) {
2751 nlmsg_cancel(skb, nlh);
2752 return err;
2753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 upe->hard = !!hard;
2755
Thomas Graf98250692007-08-22 12:47:26 -07002756 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
David S. Miller214e0052011-02-24 00:02:38 -05002759static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002761 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Thomas Graf7deb2262007-08-22 13:57:39 -07002764 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (skb == NULL)
2766 return -ENOMEM;
2767
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002768 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 BUG();
2770
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002771 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
David S. Miller214e0052011-02-24 00:02:38 -05002774static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002775{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002776 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002777 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002778 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002779 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002780 struct nlmsghdr *nlh;
2781 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002782 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002783
2784 headlen = sizeof(*p);
2785 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002786 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002787 headlen = sizeof(*id);
2788 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002789 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002790 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002791 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002792
Thomas Graf7deb2262007-08-22 13:57:39 -07002793 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002794 if (skb == NULL)
2795 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002796
Eric W. Biederman15e47302012-09-07 20:12:54 +00002797 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002798 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002799 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002800 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002801
Thomas Graf7b67c852007-08-22 13:53:52 -07002802 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002803 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002804 struct nlattr *attr;
2805
Thomas Graf7b67c852007-08-22 13:53:52 -07002806 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002807 memset(id, 0, sizeof(*id));
2808 id->dir = dir;
2809 if (c->data.byid)
2810 id->index = xp->index;
2811 else
2812 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2813
Thomas Grafc0144be2007-08-22 13:55:43 -07002814 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002815 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002816 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002817 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002818
2819 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002820 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002821
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002822 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002823 err = copy_to_user_tmpl(xp, skb);
2824 if (!err)
2825 err = copy_to_user_policy_type(xp->type, skb);
2826 if (!err)
2827 err = xfrm_mark_put(skb, &xp->mark);
2828 if (err)
2829 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002830
Thomas Graf98250692007-08-22 12:47:26 -07002831 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002832
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002833 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002834
David S. Miller1d1e34d2012-06-27 21:57:03 -07002835out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002836 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002837 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002838}
2839
David S. Miller214e0052011-02-24 00:02:38 -05002840static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002841{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002842 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002843 struct nlmsghdr *nlh;
2844 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002845 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002846
Thomas Graf7deb2262007-08-22 13:57:39 -07002847 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002848 if (skb == NULL)
2849 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002850
Eric W. Biederman15e47302012-09-07 20:12:54 +00002851 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002852 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002853 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002854 goto out_free_skb;
2855 err = copy_to_user_policy_type(c->data.type, skb);
2856 if (err)
2857 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002858
Thomas Graf98250692007-08-22 12:47:26 -07002859 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002860
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002861 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002862
David S. Miller1d1e34d2012-06-27 21:57:03 -07002863out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002864 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002865 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002866}
2867
David S. Miller214e0052011-02-24 00:02:38 -05002868static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002869{
2870
2871 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002872 case XFRM_MSG_NEWPOLICY:
2873 case XFRM_MSG_UPDPOLICY:
2874 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002875 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002876 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002877 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002878 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002879 return xfrm_exp_policy_notify(xp, dir, c);
2880 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002881 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2882 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002883 }
2884
2885 return 0;
2886
2887}
2888
Thomas Graf7deb2262007-08-22 13:57:39 -07002889static inline size_t xfrm_report_msgsize(void)
2890{
2891 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2892}
2893
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002894static int build_report(struct sk_buff *skb, u8 proto,
2895 struct xfrm_selector *sel, xfrm_address_t *addr)
2896{
2897 struct xfrm_user_report *ur;
2898 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002899
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002900 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2901 if (nlh == NULL)
2902 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002903
Thomas Graf7b67c852007-08-22 13:53:52 -07002904 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002905 ur->proto = proto;
2906 memcpy(&ur->sel, sel, sizeof(ur->sel));
2907
David S. Miller1d1e34d2012-06-27 21:57:03 -07002908 if (addr) {
2909 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2910 if (err) {
2911 nlmsg_cancel(skb, nlh);
2912 return err;
2913 }
2914 }
Thomas Graf98250692007-08-22 12:47:26 -07002915 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002916}
2917
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002918static int xfrm_send_report(struct net *net, u8 proto,
2919 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002920{
2921 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002922
Thomas Graf7deb2262007-08-22 13:57:39 -07002923 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002924 if (skb == NULL)
2925 return -ENOMEM;
2926
2927 if (build_report(skb, proto, sel, addr) < 0)
2928 BUG();
2929
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002930 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002931}
2932
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002933static inline size_t xfrm_mapping_msgsize(void)
2934{
2935 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2936}
2937
2938static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2939 xfrm_address_t *new_saddr, __be16 new_sport)
2940{
2941 struct xfrm_user_mapping *um;
2942 struct nlmsghdr *nlh;
2943
2944 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2945 if (nlh == NULL)
2946 return -EMSGSIZE;
2947
2948 um = nlmsg_data(nlh);
2949
2950 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2951 um->id.spi = x->id.spi;
2952 um->id.family = x->props.family;
2953 um->id.proto = x->id.proto;
2954 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2955 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2956 um->new_sport = new_sport;
2957 um->old_sport = x->encap->encap_sport;
2958 um->reqid = x->props.reqid;
2959
2960 return nlmsg_end(skb, nlh);
2961}
2962
2963static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2964 __be16 sport)
2965{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002966 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002967 struct sk_buff *skb;
2968
2969 if (x->id.proto != IPPROTO_ESP)
2970 return -EINVAL;
2971
2972 if (!x->encap)
2973 return -EINVAL;
2974
2975 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2976 if (skb == NULL)
2977 return -ENOMEM;
2978
2979 if (build_mapping(skb, x, ipaddr, sport) < 0)
2980 BUG();
2981
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002982 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002983}
2984
Horia Geanta0f245582014-02-12 16:20:06 +02002985static bool xfrm_is_alive(const struct km_event *c)
2986{
2987 return (bool)xfrm_acquire_is_on(c->net);
2988}
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990static struct xfrm_mgr netlink_mgr = {
2991 .id = "netlink",
2992 .notify = xfrm_send_state_notify,
2993 .acquire = xfrm_send_acquire,
2994 .compile_policy = xfrm_compile_policy,
2995 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002996 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002997 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002998 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02002999 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000};
3001
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003002static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Patrick McHardybe336902006-03-20 22:40:54 -08003004 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003005 struct netlink_kernel_cfg cfg = {
3006 .groups = XFRMNLGRP_MAX,
3007 .input = xfrm_netlink_rcv,
3008 };
Patrick McHardybe336902006-03-20 22:40:54 -08003009
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003010 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003011 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003013 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003014 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return 0;
3016}
3017
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003018static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003019{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003020 struct net *net;
3021 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003022 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003023 synchronize_net();
3024 list_for_each_entry(net, net_exit_list, exit_list)
3025 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003026}
3027
3028static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003029 .init = xfrm_user_net_init,
3030 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003031};
3032
3033static int __init xfrm_user_init(void)
3034{
3035 int rv;
3036
3037 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3038
3039 rv = register_pernet_subsys(&xfrm_user_net_ops);
3040 if (rv < 0)
3041 return rv;
3042 rv = xfrm_register_km(&netlink_mgr);
3043 if (rv < 0)
3044 unregister_pernet_subsys(&xfrm_user_net_ops);
3045 return rv;
3046}
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048static void __exit xfrm_user_exit(void)
3049{
3050 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003051 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054module_init(xfrm_user_init);
3055module_exit(xfrm_user_exit);
3056MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003057MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003058