blob: 4e0546e9bb0a04c8ec90b14ef70812692662646e [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
Steffen Klassert02aadf72011-03-28 19:48:09 +0000145 if (p->id.proto != IPPROTO_ESP)
146 return -EINVAL;
147
Steffen Klassertd8647b72011-03-08 00:10:27 +0000148 if (p->replay_window != 0)
149 return -EINVAL;
150
151 return 0;
152}
Trent Jaegerdf718372005-12-13 23:12:27 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700155 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int err;
158
159 err = -EINVAL;
160 switch (p->family) {
161 case AF_INET:
162 break;
163
164 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000165#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167#else
168 err = -EAFNOSUPPORT;
169 goto out;
170#endif
171
172 default:
173 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 err = -EINVAL;
177 switch (p->id.proto) {
178 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700182 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000183 attrs[XFRMA_ALG_COMP] ||
184 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 break;
187
188 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800189 if (attrs[XFRMA_ALG_COMP])
190 goto out;
191 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
195 goto out;
196 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205
206 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800208 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
214 break;
215
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000216#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800222 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000226 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700228 goto out;
229 break;
230#endif
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 default:
233 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Herbert Xu1a6509d2008-01-28 19:37:29 -0800236 if ((err = verify_aead(attrs)))
237 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000238 if ((err = verify_auth_trunc(attrs)))
239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800247 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000248 if ((err = verify_replay(p, attrs)))
249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 err = -EINVAL;
252 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700255 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700256 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258
259 default:
260 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 err = 0;
264
265out:
266 return err;
267}
268
269static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700271 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
275
276 if (!rta)
277 return 0;
278
Thomas Graf5424f322007-08-22 14:01:33 -0700279 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 algo = get_byname(ualg->alg_name, 1);
282 if (!algo)
283 return -ENOSYS;
284 *props = algo->desc.sadb_alg_id;
285
Eric Dumazet0f99be02008-01-08 23:39:06 -0800286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!p)
288 return -ENOMEM;
289
Herbert Xu04ff1262006-08-13 08:50:00 +1000290 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *algpp = p;
292 return 0;
293}
294
Martin Willi4447bb32009-11-25 00:29:52 +0000295static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
296 struct nlattr *rta)
297{
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
301
302 if (!rta)
303 return 0;
304
305 ualg = nla_data(rta);
306
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
308 if (!algo)
309 return -ENOSYS;
310 *props = algo->desc.sadb_alg_id;
311
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313 if (!p)
314 return -ENOMEM;
315
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
320
321 *algpp = p;
322 return 0;
323}
324
325static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
326 struct nlattr *rta)
327{
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
330
331 if (!rta)
332 return 0;
333
334 ualg = nla_data(rta);
335
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
337 if (!algo)
338 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000341 return -EINVAL;
342 *props = algo->desc.sadb_alg_id;
343
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345 if (!p)
346 return -ENOMEM;
347
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
351
352 *algpp = p;
353 return 0;
354}
355
Herbert Xu1a6509d2008-01-28 19:37:29 -0800356static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
357 struct nlattr *rta)
358{
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
361
362 if (!rta)
363 return 0;
364
365 ualg = nla_data(rta);
366
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
368 if (!algo)
369 return -ENOSYS;
370 *props = algo->desc.sadb_alg_id;
371
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373 if (!p)
374 return -ENOMEM;
375
376 strcpy(p->alg_name, algo->name);
377 *algpp = p;
378 return 0;
379}
380
Steffen Klasserte2b19122011-03-28 19:47:30 +0000381static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382 struct nlattr *rp)
383{
384 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000385 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000386
387 if (!replay_esn || !rp)
388 return 0;
389
390 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000391 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000392
Mathias Krauseecd79182012-09-20 10:01:49 +0000393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000394 return -EINVAL;
395
396 return 0;
397}
398
Steffen Klassertd8647b72011-03-08 00:10:27 +0000399static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400 struct xfrm_replay_state_esn **preplay_esn,
401 struct nlattr *rta)
402{
403 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000404 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000405
406 if (!rta)
407 return 0;
408
409 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000410 klen = xfrm_replay_state_esn_len(up);
411 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414 if (!p)
415 return -ENOMEM;
416
Mathias Krauseecd79182012-09-20 10:01:49 +0000417 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000418 if (!pp) {
419 kfree(p);
420 return -ENOMEM;
421 }
422
Mathias Krauseecd79182012-09-20 10:01:49 +0000423 memcpy(p, up, ulen);
424 memcpy(pp, up, ulen);
425
Steffen Klassertd8647b72011-03-08 00:10:27 +0000426 *replay_esn = p;
427 *preplay_esn = pp;
428
429 return 0;
430}
431
Joy Latten661697f2007-04-13 16:14:35 -0700432static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800433{
Trent Jaegerdf718372005-12-13 23:12:27 -0800434 int len = 0;
435
436 if (xfrm_ctx) {
437 len += sizeof(struct xfrm_user_sec_ctx);
438 len += xfrm_ctx->ctx_len;
439 }
440 return len;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
444{
445 memcpy(&x->id, &p->id, sizeof(x->id));
446 memcpy(&x->sel, &p->sel, sizeof(x->sel));
447 memcpy(&x->lft, &p->lft, sizeof(x->lft));
448 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800449 x->props.replay_window = min_t(unsigned int, p->replay_window,
450 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 x->props.reqid = p->reqid;
452 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700453 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700455
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700456 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700457 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800460/*
461 * someday when pfkey also has support, we could have the code
462 * somehow made shareable and move it to xfrm_state.c - JHS
463 *
464*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000465static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
466 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800467{
Thomas Graf5424f322007-08-22 14:01:33 -0700468 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000469 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700470 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
471 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
472 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800473
Steffen Klassertd8647b72011-03-08 00:10:27 +0000474 if (re) {
475 struct xfrm_replay_state_esn *replay_esn;
476 replay_esn = nla_data(re);
477 memcpy(x->replay_esn, replay_esn,
478 xfrm_replay_state_esn_len(replay_esn));
479 memcpy(x->preplay_esn, replay_esn,
480 xfrm_replay_state_esn_len(replay_esn));
481 }
482
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800483 if (rp) {
484 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700485 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800486 memcpy(&x->replay, replay, sizeof(*replay));
487 memcpy(&x->preplay, replay, sizeof(*replay));
488 }
489
490 if (lt) {
491 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700492 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800493 x->curlft.bytes = ltime->bytes;
494 x->curlft.packets = ltime->packets;
495 x->curlft.add_time = ltime->add_time;
496 x->curlft.use_time = ltime->use_time;
497 }
498
Thomas Grafcf5cb792007-08-22 13:59:04 -0700499 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700500 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800501
Thomas Grafcf5cb792007-08-22 13:59:04 -0700502 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700503 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800504}
505
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800506static struct xfrm_state *xfrm_state_construct(struct net *net,
507 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700508 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int *errp)
510{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800511 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int err = -ENOMEM;
513
514 if (!x)
515 goto error_no_put;
516
517 copy_from_user_state(x, p);
518
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100519 if (attrs[XFRMA_SA_EXTRA_FLAGS])
520 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
521
Herbert Xu1a6509d2008-01-28 19:37:29 -0800522 if ((err = attach_aead(&x->aead, &x->props.ealgo,
523 attrs[XFRMA_ALG_AEAD])))
524 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000525 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
526 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000528 if (!x->props.aalgo) {
529 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
530 attrs[XFRMA_ALG_AUTH])))
531 goto error;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
534 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700535 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 goto error;
537 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
538 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700539 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700541
542 if (attrs[XFRMA_ENCAP]) {
543 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
544 sizeof(*x->encap), GFP_KERNEL);
545 if (x->encap == NULL)
546 goto error;
547 }
548
Martin Willi35d28562010-12-08 04:37:49 +0000549 if (attrs[XFRMA_TFCPAD])
550 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
551
Thomas Graffd211502007-09-06 03:28:08 -0700552 if (attrs[XFRMA_COADDR]) {
553 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
554 sizeof(*x->coaddr), GFP_KERNEL);
555 if (x->coaddr == NULL)
556 goto error;
557 }
558
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000559 xfrm_mark_get(attrs, &x->mark);
560
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700561 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (err)
563 goto error;
564
Thomas Graffd211502007-09-06 03:28:08 -0700565 if (attrs[XFRMA_SEC_CTX] &&
566 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800567 goto error;
568
Steffen Klassertd8647b72011-03-08 00:10:27 +0000569 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
570 attrs[XFRMA_REPLAY_ESN_VAL])))
571 goto error;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800574 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800575 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800576 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800577
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000578 if ((err = xfrm_init_replay(x)))
579 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800580
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000581 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000582 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 return x;
585
586error:
587 x->km.state = XFRM_STATE_DEAD;
588 xfrm_state_put(x);
589error_no_put:
590 *errp = err;
591 return NULL;
592}
593
Christoph Hellwig22e70052007-01-02 15:22:30 -0800594static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700595 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800597 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700598 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct xfrm_state *x;
600 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700601 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700602 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800603 u32 sessionid = audit_get_sessionid(current);
604 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Thomas Graf35a7aa02007-08-22 14:00:40 -0700606 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (err)
608 return err;
609
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800610 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!x)
612 return err;
613
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700614 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
616 err = xfrm_state_add(x);
617 else
618 err = xfrm_state_update(x);
619
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800620 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400621 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (err < 0) {
624 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800625 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700629 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000630 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700631 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700632
633 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700634out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700635 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return err;
637}
638
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800639static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
640 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700641 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700642 int *errp)
643{
644 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000645 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700646 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000647 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700648
649 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
650 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000651 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700652 } else {
653 xfrm_address_t *saddr = NULL;
654
Thomas Graf35a7aa02007-08-22 14:00:40 -0700655 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700656 if (!saddr) {
657 err = -EINVAL;
658 goto out;
659 }
660
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800661 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000662 x = xfrm_state_lookup_byaddr(net, mark,
663 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800664 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700665 }
666
667 out:
668 if (!x && errp)
669 *errp = err;
670 return x;
671}
672
Christoph Hellwig22e70052007-01-02 15:22:30 -0800673static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700674 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800676 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700678 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700679 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700680 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700681 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800682 u32 sessionid = audit_get_sessionid(current);
683 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800685 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700687 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David S. Miller6f68dc32006-06-08 23:58:52 -0700689 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700690 goto out;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700693 err = -EPERM;
694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700697 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600698
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700699 if (err < 0)
700 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700701
702 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000703 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700704 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700705 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700707out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800708 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400709 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700710 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
715{
Mathias Krausef778a632012-09-19 11:33:39 +0000716 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 memcpy(&p->id, &x->id, sizeof(p->id));
718 memcpy(&p->sel, &x->sel, sizeof(p->sel));
719 memcpy(&p->lft, &x->lft, sizeof(p->lft));
720 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
721 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700722 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 p->mode = x->props.mode;
724 p->replay_window = x->props.replay_window;
725 p->reqid = x->props.reqid;
726 p->family = x->props.family;
727 p->flags = x->props.flags;
728 p->seq = x->km.seq;
729}
730
731struct xfrm_dump_info {
732 struct sk_buff *in_skb;
733 struct sk_buff *out_skb;
734 u32 nlmsg_seq;
735 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736};
737
Thomas Grafc0144be2007-08-22 13:55:43 -0700738static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
739{
Thomas Grafc0144be2007-08-22 13:55:43 -0700740 struct xfrm_user_sec_ctx *uctx;
741 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700742 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700743
744 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
745 if (attr == NULL)
746 return -EMSGSIZE;
747
748 uctx = nla_data(attr);
749 uctx->exttype = XFRMA_SEC_CTX;
750 uctx->len = ctx_size;
751 uctx->ctx_doi = s->ctx_doi;
752 uctx->ctx_alg = s->ctx_alg;
753 uctx->ctx_len = s->ctx_len;
754 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
755
756 return 0;
757}
758
Martin Willi4447bb32009-11-25 00:29:52 +0000759static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
760{
761 struct xfrm_algo *algo;
762 struct nlattr *nla;
763
764 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
765 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
766 if (!nla)
767 return -EMSGSIZE;
768
769 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000770 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000771 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
772 algo->alg_key_len = auth->alg_key_len;
773
774 return 0;
775}
776
Herbert Xu68325d32007-10-09 13:30:57 -0700777/* Don't change this without updating xfrm_sa_len! */
778static int copy_to_user_state_extra(struct xfrm_state *x,
779 struct xfrm_usersa_info *p,
780 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700782 int ret = 0;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 copy_to_user_state(x, p);
785
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100786 if (x->props.extra_flags) {
787 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
788 x->props.extra_flags);
789 if (ret)
790 goto out;
791 }
792
David S. Miller1d1e34d2012-06-27 21:57:03 -0700793 if (x->coaddr) {
794 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
795 if (ret)
796 goto out;
797 }
798 if (x->lastused) {
799 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
800 if (ret)
801 goto out;
802 }
803 if (x->aead) {
804 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
805 if (ret)
806 goto out;
807 }
808 if (x->aalg) {
809 ret = copy_to_user_auth(x->aalg, skb);
810 if (!ret)
811 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
812 xfrm_alg_auth_len(x->aalg), x->aalg);
813 if (ret)
814 goto out;
815 }
816 if (x->ealg) {
817 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
818 if (ret)
819 goto out;
820 }
821 if (x->calg) {
822 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
823 if (ret)
824 goto out;
825 }
826 if (x->encap) {
827 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
828 if (ret)
829 goto out;
830 }
831 if (x->tfcpad) {
832 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
833 if (ret)
834 goto out;
835 }
836 ret = xfrm_mark_put(skb, &x->mark);
837 if (ret)
838 goto out;
839 if (x->replay_esn) {
840 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
841 xfrm_replay_state_esn_len(x->replay_esn),
842 x->replay_esn);
843 if (ret)
844 goto out;
845 }
846 if (x->security)
847 ret = copy_sec_ctx(x->security, skb);
848out:
849 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700850}
851
852static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
853{
854 struct xfrm_dump_info *sp = ptr;
855 struct sk_buff *in_skb = sp->in_skb;
856 struct sk_buff *skb = sp->out_skb;
857 struct xfrm_usersa_info *p;
858 struct nlmsghdr *nlh;
859 int err;
860
Eric W. Biederman15e47302012-09-07 20:12:54 +0000861 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700862 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
863 if (nlh == NULL)
864 return -EMSGSIZE;
865
866 p = nlmsg_data(nlh);
867
868 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700869 if (err) {
870 nlmsg_cancel(skb, nlh);
871 return err;
872 }
Thomas Graf98250692007-08-22 12:47:26 -0700873 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Timo Teras4c563f72008-02-28 21:31:08 -0800877static int xfrm_dump_sa_done(struct netlink_callback *cb)
878{
879 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
880 xfrm_state_walk_done(walk);
881 return 0;
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
885{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800886 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800887 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct xfrm_dump_info info;
889
Timo Teras4c563f72008-02-28 21:31:08 -0800890 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
891 sizeof(cb->args) - sizeof(cb->args[0]));
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 info.in_skb = cb->skb;
894 info.out_skb = skb;
895 info.nlmsg_seq = cb->nlh->nlmsg_seq;
896 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800897
898 if (!cb->args[0]) {
899 cb->args[0] = 1;
900 xfrm_state_walk_init(walk, 0);
901 }
902
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800903 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 return skb->len;
906}
907
908static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
909 struct xfrm_state *x, u32 seq)
910{
911 struct xfrm_dump_info info;
912 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000913 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Thomas Graf7deb2262007-08-22 13:57:39 -0700915 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!skb)
917 return ERR_PTR(-ENOMEM);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 info.in_skb = in_skb;
920 info.out_skb = skb;
921 info.nlmsg_seq = seq;
922 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Mathias Krause864745d2012-09-13 11:41:26 +0000924 err = dump_one_state(x, 0, &info);
925 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000927 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
930 return skb;
931}
932
Thomas Graf7deb2262007-08-22 13:57:39 -0700933static inline size_t xfrm_spdinfo_msgsize(void)
934{
935 return NLMSG_ALIGN(4)
936 + nla_total_size(sizeof(struct xfrmu_spdinfo))
937 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
938}
939
Alexey Dobriyane0710412010-01-23 13:37:10 +0000940static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000941 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700942{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700943 struct xfrmk_spdinfo si;
944 struct xfrmu_spdinfo spc;
945 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700946 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700947 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700948 u32 *f;
949
Eric W. Biederman15e47302012-09-07 20:12:54 +0000950 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300951 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700952 return -EMSGSIZE;
953
954 f = nlmsg_data(nlh);
955 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000956 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700957 spc.incnt = si.incnt;
958 spc.outcnt = si.outcnt;
959 spc.fwdcnt = si.fwdcnt;
960 spc.inscnt = si.inscnt;
961 spc.outscnt = si.outscnt;
962 spc.fwdscnt = si.fwdscnt;
963 sph.spdhcnt = si.spdhcnt;
964 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700965
David S. Miller1d1e34d2012-06-27 21:57:03 -0700966 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
967 if (!err)
968 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
969 if (err) {
970 nlmsg_cancel(skb, nlh);
971 return err;
972 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700973
974 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700975}
976
977static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700978 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700979{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800980 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700981 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700982 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000983 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700984 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700985
Thomas Graf7deb2262007-08-22 13:57:39 -0700986 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700987 if (r_skb == NULL)
988 return -ENOMEM;
989
Eric W. Biederman15e47302012-09-07 20:12:54 +0000990 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700991 BUG();
992
Eric W. Biederman15e47302012-09-07 20:12:54 +0000993 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700994}
995
Thomas Graf7deb2262007-08-22 13:57:39 -0700996static inline size_t xfrm_sadinfo_msgsize(void)
997{
998 return NLMSG_ALIGN(4)
999 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1000 + nla_total_size(4); /* XFRMA_SAD_CNT */
1001}
1002
Alexey Dobriyane0710412010-01-23 13:37:10 +00001003static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001004 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001005{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001006 struct xfrmk_sadinfo si;
1007 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001008 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001009 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001010 u32 *f;
1011
Eric W. Biederman15e47302012-09-07 20:12:54 +00001012 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001013 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001014 return -EMSGSIZE;
1015
1016 f = nlmsg_data(nlh);
1017 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001018 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001019
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001020 sh.sadhmcnt = si.sadhmcnt;
1021 sh.sadhcnt = si.sadhcnt;
1022
David S. Miller1d1e34d2012-06-27 21:57:03 -07001023 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1024 if (!err)
1025 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1026 if (err) {
1027 nlmsg_cancel(skb, nlh);
1028 return err;
1029 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001030
1031 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001032}
1033
1034static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001035 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001036{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001037 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001038 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001039 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001040 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001041 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001042
Thomas Graf7deb2262007-08-22 13:57:39 -07001043 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001044 if (r_skb == NULL)
1045 return -ENOMEM;
1046
Eric W. Biederman15e47302012-09-07 20:12:54 +00001047 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001048 BUG();
1049
Eric W. Biederman15e47302012-09-07 20:12:54 +00001050 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001051}
1052
Christoph Hellwig22e70052007-01-02 15:22:30 -08001053static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001054 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001056 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001057 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct xfrm_state *x;
1059 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001060 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001062 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (x == NULL)
1064 goto out_noput;
1065
1066 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1067 if (IS_ERR(resp_skb)) {
1068 err = PTR_ERR(resp_skb);
1069 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001070 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 xfrm_state_put(x);
1073out_noput:
1074 return err;
1075}
1076
1077static int verify_userspi_info(struct xfrm_userspi_info *p)
1078{
1079 switch (p->info.id.proto) {
1080 case IPPROTO_AH:
1081 case IPPROTO_ESP:
1082 break;
1083
1084 case IPPROTO_COMP:
1085 /* IPCOMP spi is 16-bits. */
1086 if (p->max >= 0x10000)
1087 return -EINVAL;
1088 break;
1089
1090 default:
1091 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 if (p->min > p->max)
1095 return -EINVAL;
1096
1097 return 0;
1098}
1099
Christoph Hellwig22e70052007-01-02 15:22:30 -08001100static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001101 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001103 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct xfrm_state *x;
1105 struct xfrm_userspi_info *p;
1106 struct sk_buff *resp_skb;
1107 xfrm_address_t *daddr;
1108 int family;
1109 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001110 u32 mark;
1111 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Thomas Graf7b67c852007-08-22 13:53:52 -07001113 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 err = verify_userspi_info(p);
1115 if (err)
1116 goto out_noput;
1117
1118 family = p->info.family;
1119 daddr = &p->info.id.daddr;
1120
1121 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001122
1123 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001125 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001126 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 xfrm_state_put(x);
1128 x = NULL;
1129 }
1130 }
1131
1132 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001133 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 p->info.id.proto, daddr,
1135 &p->info.saddr, 1,
1136 family);
1137 err = -ENOENT;
1138 if (x == NULL)
1139 goto out_noput;
1140
Herbert Xu658b2192007-10-09 13:29:52 -07001141 err = xfrm_alloc_spi(x, p->min, p->max);
1142 if (err)
1143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Herbert Xu658b2192007-10-09 13:29:52 -07001145 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (IS_ERR(resp_skb)) {
1147 err = PTR_ERR(resp_skb);
1148 goto out;
1149 }
1150
Eric W. Biederman15e47302012-09-07 20:12:54 +00001151 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153out:
1154 xfrm_state_put(x);
1155out_noput:
1156 return err;
1157}
1158
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001159static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 switch (dir) {
1162 case XFRM_POLICY_IN:
1163 case XFRM_POLICY_OUT:
1164 case XFRM_POLICY_FWD:
1165 break;
1166
1167 default:
1168 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 return 0;
1172}
1173
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001174static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001175{
1176 switch (type) {
1177 case XFRM_POLICY_TYPE_MAIN:
1178#ifdef CONFIG_XFRM_SUB_POLICY
1179 case XFRM_POLICY_TYPE_SUB:
1180#endif
1181 break;
1182
1183 default:
1184 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001185 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001186
1187 return 0;
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1191{
Fan Due682adf2013-11-07 17:47:48 +08001192 int ret;
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 switch (p->share) {
1195 case XFRM_SHARE_ANY:
1196 case XFRM_SHARE_SESSION:
1197 case XFRM_SHARE_USER:
1198 case XFRM_SHARE_UNIQUE:
1199 break;
1200
1201 default:
1202 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 switch (p->action) {
1206 case XFRM_POLICY_ALLOW:
1207 case XFRM_POLICY_BLOCK:
1208 break;
1209
1210 default:
1211 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 switch (p->sel.family) {
1215 case AF_INET:
1216 break;
1217
1218 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001219#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 break;
1221#else
1222 return -EAFNOSUPPORT;
1223#endif
1224
1225 default:
1226 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Fan Due682adf2013-11-07 17:47:48 +08001229 ret = verify_policy_dir(p->dir);
1230 if (ret)
1231 return ret;
1232 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1233 return -EINVAL;
1234
1235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Thomas Graf5424f322007-08-22 14:01:33 -07001238static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001239{
Thomas Graf5424f322007-08-22 14:01:33 -07001240 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001241 struct xfrm_user_sec_ctx *uctx;
1242
1243 if (!rt)
1244 return 0;
1245
Thomas Graf5424f322007-08-22 14:01:33 -07001246 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001247 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1251 int nr)
1252{
1253 int i;
1254
1255 xp->xfrm_nr = nr;
1256 for (i = 0; i < nr; i++, ut++) {
1257 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1258
1259 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1260 memcpy(&t->saddr, &ut->saddr,
1261 sizeof(xfrm_address_t));
1262 t->reqid = ut->reqid;
1263 t->mode = ut->mode;
1264 t->share = ut->share;
1265 t->optional = ut->optional;
1266 t->aalgos = ut->aalgos;
1267 t->ealgos = ut->ealgos;
1268 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001269 /* If all masks are ~0, then we allow all algorithms. */
1270 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001271 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273}
1274
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001275static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1276{
1277 int i;
1278
1279 if (nr > XFRM_MAX_DEPTH)
1280 return -EINVAL;
1281
1282 for (i = 0; i < nr; i++) {
1283 /* We never validated the ut->family value, so many
1284 * applications simply leave it at zero. The check was
1285 * never made and ut->family was ignored because all
1286 * templates could be assumed to have the same family as
1287 * the policy itself. Now that we will have ipv4-in-ipv6
1288 * and ipv6-in-ipv4 tunnels, this is no longer true.
1289 */
1290 if (!ut[i].family)
1291 ut[i].family = family;
1292
1293 switch (ut[i].family) {
1294 case AF_INET:
1295 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001296#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001297 case AF_INET6:
1298 break;
1299#endif
1300 default:
1301 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001302 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001303 }
1304
1305 return 0;
1306}
1307
Thomas Graf5424f322007-08-22 14:01:33 -07001308static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Thomas Graf5424f322007-08-22 14:01:33 -07001310 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (!rt) {
1313 pol->xfrm_nr = 0;
1314 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001315 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1316 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001317 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001319 err = validate_tmpl(nr, utmpl, pol->family);
1320 if (err)
1321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Thomas Graf5424f322007-08-22 14:01:33 -07001323 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 return 0;
1326}
1327
Thomas Graf5424f322007-08-22 14:01:33 -07001328static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001329{
Thomas Graf5424f322007-08-22 14:01:33 -07001330 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001331 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001332 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001333 int err;
1334
1335 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001336 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001337 type = upt->type;
1338 }
1339
1340 err = verify_policy_type(type);
1341 if (err)
1342 return err;
1343
1344 *tp = type;
1345 return 0;
1346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1349{
1350 xp->priority = p->priority;
1351 xp->index = p->index;
1352 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1353 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1354 xp->action = p->action;
1355 xp->flags = p->flags;
1356 xp->family = p->sel.family;
1357 /* XXX xp->share = p->share; */
1358}
1359
1360static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1361{
Mathias Krause7b789832012-09-19 11:33:40 +00001362 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1364 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1365 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1366 p->priority = xp->priority;
1367 p->index = xp->index;
1368 p->sel.family = xp->family;
1369 p->dir = dir;
1370 p->action = xp->action;
1371 p->flags = xp->flags;
1372 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1373}
1374
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001375static 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 -07001376{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001377 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int err;
1379
1380 if (!xp) {
1381 *errp = -ENOMEM;
1382 return NULL;
1383 }
1384
1385 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001386
Thomas Graf35a7aa02007-08-22 14:00:40 -07001387 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001388 if (err)
1389 goto error;
1390
Thomas Graf35a7aa02007-08-22 14:00:40 -07001391 if (!(err = copy_from_user_tmpl(xp, attrs)))
1392 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001393 if (err)
1394 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001396 xfrm_mark_get(attrs, &xp->mark);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001399 error:
1400 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001401 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001402 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Christoph Hellwig22e70052007-01-02 15:22:30 -08001406static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001407 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001409 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001410 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001412 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int err;
1414 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001415 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001416 u32 sessionid = audit_get_sessionid(current);
1417 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 err = verify_newpolicy_info(p);
1420 if (err)
1421 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001422 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001423 if (err)
1424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001426 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (!xp)
1428 return err;
1429
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001430 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001431 * Aha! this is anti-netlink really i.e more pfkey derived
1432 * in netlink excl is a flag and you wouldnt need
1433 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1435 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001436 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001437 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001440 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 kfree(xp);
1442 return err;
1443 }
1444
Herbert Xuf60f6b82005-06-18 22:44:37 -07001445 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001446 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001447 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 km_policy_notify(xp, p->dir, &c);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 xfrm_pol_put(xp);
1451
1452 return 0;
1453}
1454
1455static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1456{
1457 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1458 int i;
1459
1460 if (xp->xfrm_nr == 0)
1461 return 0;
1462
1463 for (i = 0; i < xp->xfrm_nr; i++) {
1464 struct xfrm_user_tmpl *up = &vec[i];
1465 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1466
Mathias Krause1f868402012-09-19 11:33:41 +00001467 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001469 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1471 up->reqid = kp->reqid;
1472 up->mode = kp->mode;
1473 up->share = kp->share;
1474 up->optional = kp->optional;
1475 up->aalgos = kp->aalgos;
1476 up->ealgos = kp->ealgos;
1477 up->calgos = kp->calgos;
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Thomas Grafc0144be2007-08-22 13:55:43 -07001480 return nla_put(skb, XFRMA_TMPL,
1481 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001482}
1483
Serge Hallyn0d681622006-07-24 23:30:44 -07001484static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1485{
1486 if (x->security) {
1487 return copy_sec_ctx(x->security, skb);
1488 }
1489 return 0;
1490}
1491
1492static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1493{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001494 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001495 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001496 return 0;
1497}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001498static inline size_t userpolicy_type_attrsize(void)
1499{
1500#ifdef CONFIG_XFRM_SUB_POLICY
1501 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1502#else
1503 return 0;
1504#endif
1505}
Serge Hallyn0d681622006-07-24 23:30:44 -07001506
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001507#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001508static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001509{
Thomas Grafc0144be2007-08-22 13:55:43 -07001510 struct xfrm_userpolicy_type upt = {
1511 .type = type,
1512 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001513
Thomas Grafc0144be2007-08-22 13:55:43 -07001514 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001515}
1516
1517#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001518static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001519{
1520 return 0;
1521}
1522#endif
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1525{
1526 struct xfrm_dump_info *sp = ptr;
1527 struct xfrm_userpolicy_info *p;
1528 struct sk_buff *in_skb = sp->in_skb;
1529 struct sk_buff *skb = sp->out_skb;
1530 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Eric W. Biederman15e47302012-09-07 20:12:54 +00001533 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001534 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1535 if (nlh == NULL)
1536 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Thomas Graf7b67c852007-08-22 13:53:52 -07001538 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001540 err = copy_to_user_tmpl(xp, skb);
1541 if (!err)
1542 err = copy_to_user_sec_ctx(xp, skb);
1543 if (!err)
1544 err = copy_to_user_policy_type(xp->type, skb);
1545 if (!err)
1546 err = xfrm_mark_put(skb, &xp->mark);
1547 if (err) {
1548 nlmsg_cancel(skb, nlh);
1549 return err;
1550 }
Thomas Graf98250692007-08-22 12:47:26 -07001551 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Timo Teras4c563f72008-02-28 21:31:08 -08001555static int xfrm_dump_policy_done(struct netlink_callback *cb)
1556{
1557 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1558
1559 xfrm_policy_walk_done(walk);
1560 return 0;
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1564{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001565 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001566 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 struct xfrm_dump_info info;
1568
Timo Teras4c563f72008-02-28 21:31:08 -08001569 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1570 sizeof(cb->args) - sizeof(cb->args[0]));
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 info.in_skb = cb->skb;
1573 info.out_skb = skb;
1574 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1575 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001576
1577 if (!cb->args[0]) {
1578 cb->args[0] = 1;
1579 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1580 }
1581
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001582 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 return skb->len;
1585}
1586
1587static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1588 struct xfrm_policy *xp,
1589 int dir, u32 seq)
1590{
1591 struct xfrm_dump_info info;
1592 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001593 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Thomas Graf7deb2262007-08-22 13:57:39 -07001595 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!skb)
1597 return ERR_PTR(-ENOMEM);
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 info.in_skb = in_skb;
1600 info.out_skb = skb;
1601 info.nlmsg_seq = seq;
1602 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Mathias Krausec2546372012-09-14 09:58:32 +00001604 err = dump_one_policy(xp, dir, 0, &info);
1605 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001607 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609
1610 return skb;
1611}
1612
Christoph Hellwig22e70052007-01-02 15:22:30 -08001613static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001614 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001616 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct xfrm_policy *xp;
1618 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001619 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001621 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001623 struct xfrm_mark m;
1624 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Thomas Graf7b67c852007-08-22 13:53:52 -07001626 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1628
Thomas Graf35a7aa02007-08-22 14:00:40 -07001629 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001630 if (err)
1631 return err;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 err = verify_policy_dir(p->dir);
1634 if (err)
1635 return err;
1636
1637 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001638 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001639 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001640 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001641 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001642
Thomas Graf35a7aa02007-08-22 14:00:40 -07001643 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001644 if (err)
1645 return err;
1646
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001647 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001648 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001649 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001650
Paul Moore03e1ad72008-04-12 19:07:52 -07001651 err = security_xfrm_policy_alloc(&ctx, uctx);
1652 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001653 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001654 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001655 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001656 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001657 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (xp == NULL)
1660 return -ENOENT;
1661
1662 if (!delete) {
1663 struct sk_buff *resp_skb;
1664
1665 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1666 if (IS_ERR(resp_skb)) {
1667 err = PTR_ERR(resp_skb);
1668 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001669 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001670 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001672 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001673 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001674 u32 sessionid = audit_get_sessionid(current);
1675 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001676
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001677 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001678 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1679 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001680
1681 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001682 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001683
Herbert Xue7443892005-06-18 22:44:18 -07001684 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001685 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001686 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001687 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001688 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001691out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001692 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001693 if (delete && err == 0)
1694 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return err;
1696}
1697
Christoph Hellwig22e70052007-01-02 15:22:30 -08001698static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001699 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001701 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001702 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001703 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001704 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001705 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001707 audit_info.loginuid = audit_get_loginuid(current);
1708 audit_info.sessionid = audit_get_sessionid(current);
1709 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001710 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001711 if (err) {
1712 if (err == -ESRCH) /* empty table */
1713 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001714 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001715 }
Herbert Xubf088672005-06-18 22:44:00 -07001716 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001717 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001719 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001720 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721 km_state_notify(NULL, &c);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
Steffen Klassertd8647b72011-03-08 00:10:27 +00001726static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001727{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001728 size_t replay_size = x->replay_esn ?
1729 xfrm_replay_state_esn_len(x->replay_esn) :
1730 sizeof(struct xfrm_replay_state);
1731
Thomas Graf7deb2262007-08-22 13:57:39 -07001732 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001733 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001734 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001735 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001736 + nla_total_size(4) /* XFRM_AE_RTHR */
1737 + nla_total_size(4); /* XFRM_AE_ETHR */
1738}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001739
David S. Miller214e0052011-02-24 00:02:38 -05001740static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001741{
1742 struct xfrm_aevent_id *id;
1743 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001744 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001745
Eric W. Biederman15e47302012-09-07 20:12:54 +00001746 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001747 if (nlh == NULL)
1748 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001749
Thomas Graf7b67c852007-08-22 13:53:52 -07001750 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001751 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001752 id->sa_id.spi = x->id.spi;
1753 id->sa_id.family = x->props.family;
1754 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001755 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1756 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001757 id->flags = c->data.aevent;
1758
David S. Millerd0fde792012-03-29 04:02:26 -04001759 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001760 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1761 xfrm_replay_state_esn_len(x->replay_esn),
1762 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001763 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001764 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1765 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001766 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001767 if (err)
1768 goto out_cancel;
1769 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1770 if (err)
1771 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001772
David S. Miller1d1e34d2012-06-27 21:57:03 -07001773 if (id->flags & XFRM_AE_RTHR) {
1774 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1775 if (err)
1776 goto out_cancel;
1777 }
1778 if (id->flags & XFRM_AE_ETHR) {
1779 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1780 x->replay_maxage * 10 / HZ);
1781 if (err)
1782 goto out_cancel;
1783 }
1784 err = xfrm_mark_put(skb, &x->mark);
1785 if (err)
1786 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001787
Thomas Graf98250692007-08-22 12:47:26 -07001788 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001789
David S. Miller1d1e34d2012-06-27 21:57:03 -07001790out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001791 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001792 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001793}
1794
Christoph Hellwig22e70052007-01-02 15:22:30 -08001795static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001796 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001797{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001798 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001799 struct xfrm_state *x;
1800 struct sk_buff *r_skb;
1801 int err;
1802 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001803 u32 mark;
1804 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001805 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806 struct xfrm_usersa_id *id = &p->sa_id;
1807
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001808 mark = xfrm_mark_get(attrs, &m);
1809
1810 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001811 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001812 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001813
1814 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1815 if (r_skb == NULL) {
1816 xfrm_state_put(x);
1817 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001818 }
1819
1820 /*
1821 * XXX: is this lock really needed - none of the other
1822 * gets lock (the concern is things getting updated
1823 * while we are still reading) - jhs
1824 */
1825 spin_lock_bh(&x->lock);
1826 c.data.aevent = p->flags;
1827 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001828 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001829
1830 if (build_aevent(r_skb, x, &c) < 0)
1831 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001832 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001833 spin_unlock_bh(&x->lock);
1834 xfrm_state_put(x);
1835 return err;
1836}
1837
Christoph Hellwig22e70052007-01-02 15:22:30 -08001838static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001839 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001840{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001841 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001842 struct xfrm_state *x;
1843 struct km_event c;
1844 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001845 u32 mark = 0;
1846 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001847 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001848 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001849 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001850 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001851
Steffen Klassertd8647b72011-03-08 00:10:27 +00001852 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001853 return err;
1854
1855 /* pedantic mode - thou shalt sayeth replaceth */
1856 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1857 return err;
1858
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001859 mark = xfrm_mark_get(attrs, &m);
1860
1861 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 -08001862 if (x == NULL)
1863 return -ESRCH;
1864
1865 if (x->km.state != XFRM_STATE_VALID)
1866 goto out;
1867
Steffen Klassert4479ff72013-09-09 09:39:01 +02001868 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001869 if (err)
1870 goto out;
1871
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001872 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001873 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001874 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001875
1876 c.event = nlh->nlmsg_type;
1877 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001878 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001879 c.data.aevent = XFRM_AE_CU;
1880 km_state_notify(x, &c);
1881 err = 0;
1882out:
1883 xfrm_state_put(x);
1884 return err;
1885}
1886
Christoph Hellwig22e70052007-01-02 15:22:30 -08001887static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001888 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001890 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001891 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001892 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001893 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001894 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001895
Thomas Graf35a7aa02007-08-22 14:00:40 -07001896 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001897 if (err)
1898 return err;
1899
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001900 audit_info.loginuid = audit_get_loginuid(current);
1901 audit_info.sessionid = audit_get_sessionid(current);
1902 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001903 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001904 if (err) {
1905 if (err == -ESRCH) /* empty table */
1906 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001907 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001908 }
1909
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001910 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001911 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001912 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001913 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001914 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001915 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return 0;
1917}
1918
Christoph Hellwig22e70052007-01-02 15:22:30 -08001919static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001920 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001921{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001922 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001923 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001924 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001925 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001926 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001927 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001928 struct xfrm_mark m;
1929 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930
Thomas Graf35a7aa02007-08-22 14:00:40 -07001931 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001932 if (err)
1933 return err;
1934
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001935 err = verify_policy_dir(p->dir);
1936 if (err)
1937 return err;
1938
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001939 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001940 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001941 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001942 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001943 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944
Thomas Graf35a7aa02007-08-22 14:00:40 -07001945 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001946 if (err)
1947 return err;
1948
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001949 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001950 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001951 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001952
Paul Moore03e1ad72008-04-12 19:07:52 -07001953 err = security_xfrm_policy_alloc(&ctx, uctx);
1954 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001955 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001956 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001957 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001958 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001959 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001960 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001961 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001962 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001963
Timo Teräsea2dea92010-03-31 00:17:05 +00001964 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001965 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001966
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001967 err = 0;
1968 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001969 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001970 u32 sessionid = audit_get_sessionid(current);
1971 u32 sid;
1972
1973 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001974 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001975 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001976
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001977 } else {
1978 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001979 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001980 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001981 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001982
1983out:
1984 xfrm_pol_put(xp);
1985 return err;
1986}
1987
Christoph Hellwig22e70052007-01-02 15:22:30 -08001988static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001989 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001990{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001991 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001992 struct xfrm_state *x;
1993 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001994 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001995 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001996 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001997 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001998
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001999 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 -08002000
David S. Miller3a765aa2007-02-26 14:52:21 -08002001 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002002 if (x == NULL)
2003 return err;
2004
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002005 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002006 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002007 if (x->km.state != XFRM_STATE_VALID)
2008 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002009 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002010
Joy Latten161a09e2006-11-27 13:11:54 -06002011 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002012 kuid_t loginuid = audit_get_loginuid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08002013 u32 sessionid = audit_get_sessionid(current);
2014 u32 sid;
2015
2016 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002017 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002018 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002019 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002020 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002021out:
2022 spin_unlock_bh(&x->lock);
2023 xfrm_state_put(x);
2024 return err;
2025}
2026
Christoph Hellwig22e70052007-01-02 15:22:30 -08002027static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002028 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002029{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002030 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002031 struct xfrm_policy *xp;
2032 struct xfrm_user_tmpl *ut;
2033 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002034 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002035 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002036
Thomas Graf7b67c852007-08-22 13:53:52 -07002037 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002038 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002039 int err = -ENOMEM;
2040
2041 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002042 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002043
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002044 xfrm_mark_get(attrs, &mark);
2045
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002046 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002047 if (err)
2048 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002049
2050 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002051 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002052 if (!xp)
2053 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002054
2055 memcpy(&x->id, &ua->id, sizeof(ua->id));
2056 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2057 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002058 xp->mark.m = x->mark.m = mark.m;
2059 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002060 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002061 /* extract the templates and for each call km_key */
2062 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2063 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2064 memcpy(&x->id, &t->id, sizeof(x->id));
2065 x->props.mode = t->mode;
2066 x->props.reqid = t->reqid;
2067 x->props.family = ut->family;
2068 t->aalgos = ua->aalgos;
2069 t->ealgos = ua->ealgos;
2070 t->calgos = ua->calgos;
2071 err = km_query(x, t, xp);
2072
2073 }
2074
2075 kfree(x);
2076 kfree(xp);
2077
2078 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002079
2080bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002081 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002082free_state:
2083 kfree(x);
2084nomem:
2085 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002086}
2087
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002088#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002089static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002090 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002091 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002092{
Thomas Graf5424f322007-08-22 14:01:33 -07002093 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002094 struct xfrm_user_migrate *um;
2095 int i, num_migrate;
2096
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002097 if (k != NULL) {
2098 struct xfrm_user_kmaddress *uk;
2099
2100 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2101 memcpy(&k->local, &uk->local, sizeof(k->local));
2102 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2103 k->family = uk->family;
2104 k->reserved = uk->reserved;
2105 }
2106
Thomas Graf5424f322007-08-22 14:01:33 -07002107 um = nla_data(rt);
2108 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002109
2110 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2111 return -EINVAL;
2112
2113 for (i = 0; i < num_migrate; i++, um++, ma++) {
2114 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2115 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2116 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2117 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2118
2119 ma->proto = um->proto;
2120 ma->mode = um->mode;
2121 ma->reqid = um->reqid;
2122
2123 ma->old_family = um->old_family;
2124 ma->new_family = um->new_family;
2125 }
2126
2127 *num = i;
2128 return 0;
2129}
2130
2131static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002132 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002133{
Thomas Graf7b67c852007-08-22 13:53:52 -07002134 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002135 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002136 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002137 u8 type;
2138 int err;
2139 int n = 0;
2140
Thomas Graf35a7aa02007-08-22 14:00:40 -07002141 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002142 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002143
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002144 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2145
Thomas Graf5424f322007-08-22 14:01:33 -07002146 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002147 if (err)
2148 return err;
2149
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002150 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002151 if (err)
2152 return err;
2153
2154 if (!n)
2155 return 0;
2156
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002157 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002158
2159 return 0;
2160}
2161#else
2162static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002163 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002164{
2165 return -ENOPROTOOPT;
2166}
2167#endif
2168
2169#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002170static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002171{
2172 struct xfrm_user_migrate um;
2173
2174 memset(&um, 0, sizeof(um));
2175 um.proto = m->proto;
2176 um.mode = m->mode;
2177 um.reqid = m->reqid;
2178 um.old_family = m->old_family;
2179 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2180 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2181 um.new_family = m->new_family;
2182 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2183 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2184
Thomas Grafc0144be2007-08-22 13:55:43 -07002185 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002186}
2187
David S. Miller183cad12011-02-24 00:28:01 -05002188static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002189{
2190 struct xfrm_user_kmaddress uk;
2191
2192 memset(&uk, 0, sizeof(uk));
2193 uk.family = k->family;
2194 uk.reserved = k->reserved;
2195 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002196 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002197
2198 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2199}
2200
2201static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002202{
2203 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002204 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2205 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2206 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002207}
2208
David S. Miller183cad12011-02-24 00:28:01 -05002209static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2210 int num_migrate, const struct xfrm_kmaddress *k,
2211 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002212{
David S. Miller183cad12011-02-24 00:28:01 -05002213 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214 struct xfrm_userpolicy_id *pol_id;
2215 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002216 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002217
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002218 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2219 if (nlh == NULL)
2220 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221
Thomas Graf7b67c852007-08-22 13:53:52 -07002222 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002223 /* copy data from selector, dir, and type to the pol_id */
2224 memset(pol_id, 0, sizeof(*pol_id));
2225 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2226 pol_id->dir = dir;
2227
David S. Miller1d1e34d2012-06-27 21:57:03 -07002228 if (k != NULL) {
2229 err = copy_to_user_kmaddress(k, skb);
2230 if (err)
2231 goto out_cancel;
2232 }
2233 err = copy_to_user_policy_type(type, skb);
2234 if (err)
2235 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002236 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002237 err = copy_to_user_migrate(mp, skb);
2238 if (err)
2239 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002240 }
2241
Thomas Graf98250692007-08-22 12:47:26 -07002242 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002243
2244out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002245 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002246 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002247}
2248
David S. Miller183cad12011-02-24 00:28:01 -05002249static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2250 const struct xfrm_migrate *m, int num_migrate,
2251 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002252{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002253 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002254 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002255
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002256 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002257 if (skb == NULL)
2258 return -ENOMEM;
2259
2260 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002261 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002262 BUG();
2263
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002264 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002265}
2266#else
David S. Miller183cad12011-02-24 00:28:01 -05002267static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2268 const struct xfrm_migrate *m, int num_migrate,
2269 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002270{
2271 return -ENOPROTOOPT;
2272}
2273#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002274
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002275#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002276
2277static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002278 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002279 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2280 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2281 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2282 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2283 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2284 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002285 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002286 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002287 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002288 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002289 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002290 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002291 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002292 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2293 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002294 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002295 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002296 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2297 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298};
2299
Thomas Graf492b5582005-05-03 14:26:40 -07002300#undef XMSGSIZE
2301
Thomas Grafcf5cb792007-08-22 13:59:04 -07002302static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002303 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2304 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2305 [XFRMA_LASTUSED] = { .type = NLA_U64},
2306 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002307 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002308 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2309 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2310 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2311 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2312 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2313 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2314 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2315 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2316 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2317 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2318 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2319 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2320 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2321 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002322 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002323 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002324 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002325 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002326 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002327};
2328
Mathias Krause05600a72013-02-24 14:10:27 +01002329static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002330 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002332 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002333} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2334 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2335 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2336 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002337 .dump = xfrm_dump_sa,
2338 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002339 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2340 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2341 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002342 .dump = xfrm_dump_policy,
2343 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002344 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002345 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002346 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002347 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2348 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002349 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002350 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2351 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002352 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2353 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002354 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002355 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002356 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357};
2358
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002359static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002361 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002362 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002363 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002364 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 type -= XFRM_MSG_BASE;
2371 link = &xfrm_dispatch[type];
2372
2373 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002374 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002375 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Thomas Graf492b5582005-05-03 14:26:40 -07002377 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2378 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002379 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002383 {
2384 struct netlink_dump_control c = {
2385 .dump = link->dump,
2386 .done = link->done,
2387 };
2388 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
2391
Thomas Graf35a7aa02007-08-22 14:00:40 -07002392 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002393 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002394 if (err < 0)
2395 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002398 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Thomas Graf5424f322007-08-22 14:01:33 -07002400 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002403static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002405 mutex_lock(&xfrm_cfg_mutex);
2406 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2407 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Thomas Graf7deb2262007-08-22 13:57:39 -07002410static inline size_t xfrm_expire_msgsize(void)
2411{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002412 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2413 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002414}
2415
David S. Miller214e0052011-02-24 00:02:38 -05002416static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 struct xfrm_user_expire *ue;
2419 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002420 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Eric W. Biederman15e47302012-09-07 20:12:54 +00002422 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002423 if (nlh == NULL)
2424 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Thomas Graf7b67c852007-08-22 13:53:52 -07002426 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002428 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
David S. Miller1d1e34d2012-06-27 21:57:03 -07002430 err = xfrm_mark_put(skb, &x->mark);
2431 if (err)
2432 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002433
Thomas Graf98250692007-08-22 12:47:26 -07002434 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
David S. Miller214e0052011-02-24 00:02:38 -05002437static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002439 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 struct sk_buff *skb;
2441
Thomas Graf7deb2262007-08-22 13:57:39 -07002442 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (skb == NULL)
2444 return -ENOMEM;
2445
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002446 if (build_expire(skb, x, c) < 0) {
2447 kfree_skb(skb);
2448 return -EMSGSIZE;
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002451 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
David S. Miller214e0052011-02-24 00:02:38 -05002454static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002455{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002456 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002457 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002458
Steffen Klassertd8647b72011-03-08 00:10:27 +00002459 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002460 if (skb == NULL)
2461 return -ENOMEM;
2462
2463 if (build_aevent(skb, x, c) < 0)
2464 BUG();
2465
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002466 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002467}
2468
David S. Miller214e0052011-02-24 00:02:38 -05002469static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002471 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002472 struct xfrm_usersa_flush *p;
2473 struct nlmsghdr *nlh;
2474 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002475 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002476
Thomas Graf7deb2262007-08-22 13:57:39 -07002477 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002478 if (skb == NULL)
2479 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480
Eric W. Biederman15e47302012-09-07 20:12:54 +00002481 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002482 if (nlh == NULL) {
2483 kfree_skb(skb);
2484 return -EMSGSIZE;
2485 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002486
Thomas Graf7b67c852007-08-22 13:53:52 -07002487 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002488 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002489
Thomas Graf98250692007-08-22 12:47:26 -07002490 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002491
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002492 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493}
2494
Thomas Graf7deb2262007-08-22 13:57:39 -07002495static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002496{
Thomas Graf7deb2262007-08-22 13:57:39 -07002497 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002498 if (x->aead)
2499 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002500 if (x->aalg) {
2501 l += nla_total_size(sizeof(struct xfrm_algo) +
2502 (x->aalg->alg_key_len + 7) / 8);
2503 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2504 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002506 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002507 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002508 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002509 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002510 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002511 if (x->tfcpad)
2512 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002513 if (x->replay_esn)
2514 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002515 if (x->security)
2516 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2517 x->security->ctx_len);
2518 if (x->coaddr)
2519 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002520 if (x->props.extra_flags)
2521 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002522
Herbert Xud26f3982007-11-13 21:47:08 -08002523 /* Must count x->lastused as it may become non-zero behind our back. */
2524 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002525
2526 return l;
2527}
2528
David S. Miller214e0052011-02-24 00:02:38 -05002529static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002530{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002531 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002532 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002533 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002534 struct nlmsghdr *nlh;
2535 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002536 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002537 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002538
2539 headlen = sizeof(*p);
2540 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002541 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002542 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002543 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002544 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002545 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002546
Thomas Graf7deb2262007-08-22 13:57:39 -07002547 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002548 if (skb == NULL)
2549 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002550
Eric W. Biederman15e47302012-09-07 20:12:54 +00002551 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002552 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002553 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002554 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002555
Thomas Graf7b67c852007-08-22 13:53:52 -07002556 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002557 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002558 struct nlattr *attr;
2559
Thomas Graf7b67c852007-08-22 13:53:52 -07002560 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002561 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2562 id->spi = x->id.spi;
2563 id->family = x->props.family;
2564 id->proto = x->id.proto;
2565
Thomas Grafc0144be2007-08-22 13:55:43 -07002566 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002567 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002568 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002569 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002570
2571 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002572 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002573 err = copy_to_user_state_extra(x, p, skb);
2574 if (err)
2575 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002576
Thomas Graf98250692007-08-22 12:47:26 -07002577 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002578
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002579 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002580
David S. Miller1d1e34d2012-06-27 21:57:03 -07002581out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002582 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002583 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002584}
2585
David S. Miller214e0052011-02-24 00:02:38 -05002586static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002587{
2588
2589 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002590 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002591 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002592 case XFRM_MSG_NEWAE:
2593 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002594 case XFRM_MSG_DELSA:
2595 case XFRM_MSG_UPDSA:
2596 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002598 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002599 return xfrm_notify_sa_flush(c);
2600 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002601 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2602 c->event);
2603 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002604 }
2605
2606 return 0;
2607
2608}
2609
Thomas Graf7deb2262007-08-22 13:57:39 -07002610static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2611 struct xfrm_policy *xp)
2612{
2613 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2614 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002615 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002616 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2617 + userpolicy_type_attrsize();
2618}
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002621 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002623 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct xfrm_user_acquire *ua;
2625 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002626 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002628 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2629 if (nlh == NULL)
2630 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Thomas Graf7b67c852007-08-22 13:53:52 -07002632 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 memcpy(&ua->id, &x->id, sizeof(ua->id));
2634 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2635 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002636 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 ua->aalgos = xt->aalgos;
2638 ua->ealgos = xt->ealgos;
2639 ua->calgos = xt->calgos;
2640 ua->seq = x->km.seq = seq;
2641
David S. Miller1d1e34d2012-06-27 21:57:03 -07002642 err = copy_to_user_tmpl(xp, skb);
2643 if (!err)
2644 err = copy_to_user_state_sec_ctx(x, skb);
2645 if (!err)
2646 err = copy_to_user_policy_type(xp->type, skb);
2647 if (!err)
2648 err = xfrm_mark_put(skb, &xp->mark);
2649 if (err) {
2650 nlmsg_cancel(skb, nlh);
2651 return err;
2652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Thomas Graf98250692007-08-22 12:47:26 -07002654 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
2657static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002658 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002660 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Thomas Graf7deb2262007-08-22 13:57:39 -07002663 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (skb == NULL)
2665 return -ENOMEM;
2666
Fan Du65e07362012-08-15 10:13:47 +08002667 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 BUG();
2669
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002670 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
2673/* User gives us xfrm_user_policy_info followed by an array of 0
2674 * or more templates.
2675 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002676static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 u8 *data, int len, int *dir)
2678{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002679 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2681 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2682 struct xfrm_policy *xp;
2683 int nr;
2684
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002685 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 case AF_INET:
2687 if (opt != IP_XFRM_POLICY) {
2688 *dir = -EOPNOTSUPP;
2689 return NULL;
2690 }
2691 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002692#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 case AF_INET6:
2694 if (opt != IPV6_XFRM_POLICY) {
2695 *dir = -EOPNOTSUPP;
2696 return NULL;
2697 }
2698 break;
2699#endif
2700 default:
2701 *dir = -EINVAL;
2702 return NULL;
2703 }
2704
2705 *dir = -EINVAL;
2706
2707 if (len < sizeof(*p) ||
2708 verify_newpolicy_info(p))
2709 return NULL;
2710
2711 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002712 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return NULL;
2714
Herbert Xua4f1bac2005-07-26 15:43:17 -07002715 if (p->dir > XFRM_POLICY_OUT)
2716 return NULL;
2717
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002718 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (xp == NULL) {
2720 *dir = -ENOBUFS;
2721 return NULL;
2722 }
2723
2724 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002725 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 copy_templates(xp, ut, nr);
2727
2728 *dir = p->dir;
2729
2730 return xp;
2731}
2732
Thomas Graf7deb2262007-08-22 13:57:39 -07002733static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2734{
2735 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2736 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2737 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002738 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002739 + userpolicy_type_attrsize();
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002743 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
2745 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002746 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002747 struct nlmsghdr *nlh;
2748 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Eric W. Biederman15e47302012-09-07 20:12:54 +00002750 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002751 if (nlh == NULL)
2752 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Thomas Graf7b67c852007-08-22 13:53:52 -07002754 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002756 err = copy_to_user_tmpl(xp, skb);
2757 if (!err)
2758 err = copy_to_user_sec_ctx(xp, skb);
2759 if (!err)
2760 err = copy_to_user_policy_type(xp->type, skb);
2761 if (!err)
2762 err = xfrm_mark_put(skb, &xp->mark);
2763 if (err) {
2764 nlmsg_cancel(skb, nlh);
2765 return err;
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 upe->hard = !!hard;
2768
Thomas Graf98250692007-08-22 12:47:26 -07002769 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
David S. Miller214e0052011-02-24 00:02:38 -05002772static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002774 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Thomas Graf7deb2262007-08-22 13:57:39 -07002777 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (skb == NULL)
2779 return -ENOMEM;
2780
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002781 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 BUG();
2783
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002784 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
David S. Miller214e0052011-02-24 00:02:38 -05002787static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002789 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002790 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002791 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002792 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002793 struct nlmsghdr *nlh;
2794 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002795 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002796
2797 headlen = sizeof(*p);
2798 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002799 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002800 headlen = sizeof(*id);
2801 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002802 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002803 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002804 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002805
Thomas Graf7deb2262007-08-22 13:57:39 -07002806 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002807 if (skb == NULL)
2808 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002809
Eric W. Biederman15e47302012-09-07 20:12:54 +00002810 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002811 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002812 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002813 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002814
Thomas Graf7b67c852007-08-22 13:53:52 -07002815 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002816 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002817 struct nlattr *attr;
2818
Thomas Graf7b67c852007-08-22 13:53:52 -07002819 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002820 memset(id, 0, sizeof(*id));
2821 id->dir = dir;
2822 if (c->data.byid)
2823 id->index = xp->index;
2824 else
2825 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2826
Thomas Grafc0144be2007-08-22 13:55:43 -07002827 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002828 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002829 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002830 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002831
2832 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002833 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002834
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002835 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002836 err = copy_to_user_tmpl(xp, skb);
2837 if (!err)
2838 err = copy_to_user_policy_type(xp->type, skb);
2839 if (!err)
2840 err = xfrm_mark_put(skb, &xp->mark);
2841 if (err)
2842 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002843
Thomas Graf98250692007-08-22 12:47:26 -07002844 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002845
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002846 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002847
David S. Miller1d1e34d2012-06-27 21:57:03 -07002848out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002849 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002850 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002851}
2852
David S. Miller214e0052011-02-24 00:02:38 -05002853static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002854{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002855 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002856 struct nlmsghdr *nlh;
2857 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002858 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002859
Thomas Graf7deb2262007-08-22 13:57:39 -07002860 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002861 if (skb == NULL)
2862 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002863
Eric W. Biederman15e47302012-09-07 20:12:54 +00002864 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002865 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002866 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002867 goto out_free_skb;
2868 err = copy_to_user_policy_type(c->data.type, skb);
2869 if (err)
2870 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002871
Thomas Graf98250692007-08-22 12:47:26 -07002872 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002873
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002874 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002875
David S. Miller1d1e34d2012-06-27 21:57:03 -07002876out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002877 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002878 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002879}
2880
David S. Miller214e0052011-02-24 00:02:38 -05002881static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002882{
2883
2884 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002885 case XFRM_MSG_NEWPOLICY:
2886 case XFRM_MSG_UPDPOLICY:
2887 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002888 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002889 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002890 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002891 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002892 return xfrm_exp_policy_notify(xp, dir, c);
2893 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002894 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2895 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002896 }
2897
2898 return 0;
2899
2900}
2901
Thomas Graf7deb2262007-08-22 13:57:39 -07002902static inline size_t xfrm_report_msgsize(void)
2903{
2904 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2905}
2906
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002907static int build_report(struct sk_buff *skb, u8 proto,
2908 struct xfrm_selector *sel, xfrm_address_t *addr)
2909{
2910 struct xfrm_user_report *ur;
2911 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002912
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002913 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2914 if (nlh == NULL)
2915 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002916
Thomas Graf7b67c852007-08-22 13:53:52 -07002917 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002918 ur->proto = proto;
2919 memcpy(&ur->sel, sel, sizeof(ur->sel));
2920
David S. Miller1d1e34d2012-06-27 21:57:03 -07002921 if (addr) {
2922 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2923 if (err) {
2924 nlmsg_cancel(skb, nlh);
2925 return err;
2926 }
2927 }
Thomas Graf98250692007-08-22 12:47:26 -07002928 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002929}
2930
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002931static int xfrm_send_report(struct net *net, u8 proto,
2932 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002933{
2934 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002935
Thomas Graf7deb2262007-08-22 13:57:39 -07002936 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002937 if (skb == NULL)
2938 return -ENOMEM;
2939
2940 if (build_report(skb, proto, sel, addr) < 0)
2941 BUG();
2942
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002943 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002944}
2945
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002946static inline size_t xfrm_mapping_msgsize(void)
2947{
2948 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2949}
2950
2951static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2952 xfrm_address_t *new_saddr, __be16 new_sport)
2953{
2954 struct xfrm_user_mapping *um;
2955 struct nlmsghdr *nlh;
2956
2957 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2958 if (nlh == NULL)
2959 return -EMSGSIZE;
2960
2961 um = nlmsg_data(nlh);
2962
2963 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2964 um->id.spi = x->id.spi;
2965 um->id.family = x->props.family;
2966 um->id.proto = x->id.proto;
2967 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2968 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2969 um->new_sport = new_sport;
2970 um->old_sport = x->encap->encap_sport;
2971 um->reqid = x->props.reqid;
2972
2973 return nlmsg_end(skb, nlh);
2974}
2975
2976static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2977 __be16 sport)
2978{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002979 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002980 struct sk_buff *skb;
2981
2982 if (x->id.proto != IPPROTO_ESP)
2983 return -EINVAL;
2984
2985 if (!x->encap)
2986 return -EINVAL;
2987
2988 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2989 if (skb == NULL)
2990 return -ENOMEM;
2991
2992 if (build_mapping(skb, x, ipaddr, sport) < 0)
2993 BUG();
2994
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002995 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002996}
2997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998static struct xfrm_mgr netlink_mgr = {
2999 .id = "netlink",
3000 .notify = xfrm_send_state_notify,
3001 .acquire = xfrm_send_acquire,
3002 .compile_policy = xfrm_compile_policy,
3003 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003004 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003005 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003006 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007};
3008
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003009static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
Patrick McHardybe336902006-03-20 22:40:54 -08003011 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003012 struct netlink_kernel_cfg cfg = {
3013 .groups = XFRMNLGRP_MAX,
3014 .input = xfrm_netlink_rcv,
3015 };
Patrick McHardybe336902006-03-20 22:40:54 -08003016
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003017 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003018 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003020 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003021 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return 0;
3023}
3024
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003025static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003026{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003027 struct net *net;
3028 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003029 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003030 synchronize_net();
3031 list_for_each_entry(net, net_exit_list, exit_list)
3032 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003033}
3034
3035static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003036 .init = xfrm_user_net_init,
3037 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003038};
3039
3040static int __init xfrm_user_init(void)
3041{
3042 int rv;
3043
3044 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3045
3046 rv = register_pernet_subsys(&xfrm_user_net_ops);
3047 if (rv < 0)
3048 return rv;
3049 rv = xfrm_register_km(&netlink_mgr);
3050 if (rv < 0)
3051 unregister_pernet_subsys(&xfrm_user_net_ops);
3052 return rv;
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055static void __exit xfrm_user_exit(void)
3056{
3057 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003058 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
3061module_init(xfrm_user_init);
3062module_exit(xfrm_user_exit);
3063MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003064MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003065