blob: 2f7ddc3a59b42db13c1df648b188a8dc312c5f8b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Graf5424f322007-08-22 14:01:33 -070035static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Graf5424f322007-08-22 14:01:33 -070037 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct xfrm_algo *algp;
39
40 if (!rt)
41 return 0;
42
Thomas Graf5424f322007-08-22 14:01:33 -070043 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080044 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070045 return -EINVAL;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 switch (type) {
48 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52
53 default:
54 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
58 return 0;
59}
60
Martin Willi4447bb32009-11-25 00:29:52 +000061static int verify_auth_trunc(struct nlattr **attrs)
62{
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
65
66 if (!rt)
67 return 0;
68
69 algp = nla_data(rt);
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
71 return -EINVAL;
72
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
Herbert Xu1a6509d2008-01-28 19:37:29 -080077static int verify_aead(struct nlattr **attrs)
78{
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
81
82 if (!rt)
83 return 0;
84
85 algp = nla_data(rt);
86 if (nla_len(rt) < aead_len(algp))
87 return -EINVAL;
88
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
90 return 0;
91}
92
Thomas Graf5424f322007-08-22 14:01:33 -070093static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094 xfrm_address_t **addrp)
95{
Thomas Graf5424f322007-08-22 14:01:33 -070096 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070097
Thomas Grafcf5cb792007-08-22 13:59:04 -070098 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070099 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100}
Trent Jaegerdf718372005-12-13 23:12:27 -0800101
Thomas Graf5424f322007-08-22 14:01:33 -0700102static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800103{
Thomas Graf5424f322007-08-22 14:01:33 -0700104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800105 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
107 if (!rt)
108 return 0;
109
Thomas Graf5424f322007-08-22 14:01:33 -0700110 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800112 return -EINVAL;
113
114 return 0;
115}
116
Steffen Klassertd8647b72011-03-08 00:10:27 +0000117static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
119{
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000121 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 if (p->flags & XFRM_STATE_ESN) {
124 if (!rt)
125 return -EINVAL;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
135 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000136
Steffen Klassertd8647b72011-03-08 00:10:27 +0000137 if (!rt)
138 return 0;
139
Steffen Klassert02aadf72011-03-28 19:48:09 +0000140 if (p->id.proto != IPPROTO_ESP)
141 return -EINVAL;
142
Steffen Klassertd8647b72011-03-08 00:10:27 +0000143 if (p->replay_window != 0)
144 return -EINVAL;
145
146 return 0;
147}
Trent Jaegerdf718372005-12-13 23:12:27 -0800148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700150 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int err;
153
154 err = -EINVAL;
155 switch (p->family) {
156 case AF_INET:
157 break;
158
159 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000160#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162#else
163 err = -EAFNOSUPPORT;
164 goto out;
165#endif
166
167 default:
168 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 err = -EINVAL;
172 switch (p->id.proto) {
173 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000174 if ((!attrs[XFRMA_ALG_AUTH] &&
175 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800176 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700177 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000178 attrs[XFRMA_ALG_COMP] ||
Fan Duea9884b2013-12-16 18:47:48 +0800179 attrs[XFRMA_TFCPAD] ||
180 (ntohl(p->id.spi) >= 0x10000))
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
183 break;
184
185 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800186 if (attrs[XFRMA_ALG_COMP])
187 goto out;
188 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000189 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800190 !attrs[XFRMA_ALG_CRYPT] &&
191 !attrs[XFRMA_ALG_AEAD])
192 goto out;
193 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000194 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800195 attrs[XFRMA_ALG_CRYPT]) &&
196 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000198 if (attrs[XFRMA_TFCPAD] &&
199 p->mode != XFRM_MODE_TUNNEL)
200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202
203 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800205 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700206 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000207 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000208 attrs[XFRMA_ALG_CRYPT] ||
209 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto out;
211 break;
212
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000213#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700214 case IPPROTO_DSTOPTS:
215 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700216 if (attrs[XFRMA_ALG_COMP] ||
217 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000218 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800219 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700220 attrs[XFRMA_ALG_CRYPT] ||
221 attrs[XFRMA_ENCAP] ||
222 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000223 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700224 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700225 goto out;
226 break;
227#endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 default:
230 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Herbert Xu1a6509d2008-01-28 19:37:29 -0800233 if ((err = verify_aead(attrs)))
234 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000235 if ((err = verify_auth_trunc(attrs)))
236 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700237 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700239 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700241 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700243 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800244 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000245 if ((err = verify_replay(p, attrs)))
246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 err = -EINVAL;
249 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700250 case XFRM_MODE_TRANSPORT:
251 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700252 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700253 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255
256 default:
257 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 err = 0;
261
262out:
263 return err;
264}
265
266static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800267 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700268 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct xfrm_algo *p, *ualg;
271 struct xfrm_algo_desc *algo;
272
273 if (!rta)
274 return 0;
275
Thomas Graf5424f322007-08-22 14:01:33 -0700276 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 algo = get_byname(ualg->alg_name, 1);
279 if (!algo)
280 return -ENOSYS;
281 *props = algo->desc.sadb_alg_id;
282
Eric Dumazet0f99be02008-01-08 23:39:06 -0800283 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!p)
285 return -ENOMEM;
286
Herbert Xu04ff1262006-08-13 08:50:00 +1000287 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *algpp = p;
289 return 0;
290}
291
Martin Willi4447bb32009-11-25 00:29:52 +0000292static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
293 struct nlattr *rta)
294{
295 struct xfrm_algo *ualg;
296 struct xfrm_algo_auth *p;
297 struct xfrm_algo_desc *algo;
298
299 if (!rta)
300 return 0;
301
302 ualg = nla_data(rta);
303
304 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
305 if (!algo)
306 return -ENOSYS;
307 *props = algo->desc.sadb_alg_id;
308
309 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
310 if (!p)
311 return -ENOMEM;
312
313 strcpy(p->alg_name, algo->name);
314 p->alg_key_len = ualg->alg_key_len;
315 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
316 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
317
318 *algpp = p;
319 return 0;
320}
321
322static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
323 struct nlattr *rta)
324{
325 struct xfrm_algo_auth *p, *ualg;
326 struct xfrm_algo_desc *algo;
327
328 if (!rta)
329 return 0;
330
331 ualg = nla_data(rta);
332
333 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
334 if (!algo)
335 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000336 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
337 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000338 return -EINVAL;
339 *props = algo->desc.sadb_alg_id;
340
341 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
342 if (!p)
343 return -ENOMEM;
344
345 strcpy(p->alg_name, algo->name);
346 if (!p->alg_trunc_len)
347 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
348
349 *algpp = p;
350 return 0;
351}
352
Herbert Xu1a6509d2008-01-28 19:37:29 -0800353static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
354 struct nlattr *rta)
355{
356 struct xfrm_algo_aead *p, *ualg;
357 struct xfrm_algo_desc *algo;
358
359 if (!rta)
360 return 0;
361
362 ualg = nla_data(rta);
363
364 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
365 if (!algo)
366 return -ENOSYS;
367 *props = algo->desc.sadb_alg_id;
368
369 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
370 if (!p)
371 return -ENOMEM;
372
373 strcpy(p->alg_name, algo->name);
374 *algpp = p;
375 return 0;
376}
377
Steffen Klasserte2b19122011-03-28 19:47:30 +0000378static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
379 struct nlattr *rp)
380{
381 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000382 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000383
384 if (!replay_esn || !rp)
385 return 0;
386
387 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000388 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000389
Mathias Krauseecd79182012-09-20 10:01:49 +0000390 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000391 return -EINVAL;
392
393 return 0;
394}
395
Steffen Klassertd8647b72011-03-08 00:10:27 +0000396static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
397 struct xfrm_replay_state_esn **preplay_esn,
398 struct nlattr *rta)
399{
400 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000401 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000402
403 if (!rta)
404 return 0;
405
406 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000407 klen = xfrm_replay_state_esn_len(up);
408 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000409
Mathias Krauseecd79182012-09-20 10:01:49 +0000410 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000411 if (!p)
412 return -ENOMEM;
413
Mathias Krauseecd79182012-09-20 10:01:49 +0000414 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000415 if (!pp) {
416 kfree(p);
417 return -ENOMEM;
418 }
419
Mathias Krauseecd79182012-09-20 10:01:49 +0000420 memcpy(p, up, ulen);
421 memcpy(pp, up, ulen);
422
Steffen Klassertd8647b72011-03-08 00:10:27 +0000423 *replay_esn = p;
424 *preplay_esn = pp;
425
426 return 0;
427}
428
Joy Latten661697f2007-04-13 16:14:35 -0700429static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800430{
Trent Jaegerdf718372005-12-13 23:12:27 -0800431 int len = 0;
432
433 if (xfrm_ctx) {
434 len += sizeof(struct xfrm_user_sec_ctx);
435 len += xfrm_ctx->ctx_len;
436 }
437 return len;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
441{
442 memcpy(&x->id, &p->id, sizeof(x->id));
443 memcpy(&x->sel, &p->sel, sizeof(x->sel));
444 memcpy(&x->lft, &p->lft, sizeof(x->lft));
445 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800446 x->props.replay_window = min_t(unsigned int, p->replay_window,
447 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 x->props.reqid = p->reqid;
449 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700450 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700452
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700453 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700454 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800457/*
458 * someday when pfkey also has support, we could have the code
459 * somehow made shareable and move it to xfrm_state.c - JHS
460 *
461*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000462static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
463 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800464{
Thomas Graf5424f322007-08-22 14:01:33 -0700465 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000466 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700467 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
468 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
469 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800470
Steffen Klassertd8647b72011-03-08 00:10:27 +0000471 if (re) {
472 struct xfrm_replay_state_esn *replay_esn;
473 replay_esn = nla_data(re);
474 memcpy(x->replay_esn, replay_esn,
475 xfrm_replay_state_esn_len(replay_esn));
476 memcpy(x->preplay_esn, replay_esn,
477 xfrm_replay_state_esn_len(replay_esn));
478 }
479
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800480 if (rp) {
481 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700482 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800483 memcpy(&x->replay, replay, sizeof(*replay));
484 memcpy(&x->preplay, replay, sizeof(*replay));
485 }
486
487 if (lt) {
488 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700489 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800490 x->curlft.bytes = ltime->bytes;
491 x->curlft.packets = ltime->packets;
492 x->curlft.add_time = ltime->add_time;
493 x->curlft.use_time = ltime->use_time;
494 }
495
Thomas Grafcf5cb792007-08-22 13:59:04 -0700496 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700497 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800498
Thomas Grafcf5cb792007-08-22 13:59:04 -0700499 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700500 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800501}
502
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800503static struct xfrm_state *xfrm_state_construct(struct net *net,
504 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700505 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 int *errp)
507{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800508 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int err = -ENOMEM;
510
511 if (!x)
512 goto error_no_put;
513
514 copy_from_user_state(x, p);
515
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100516 if (attrs[XFRMA_SA_EXTRA_FLAGS])
517 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
518
Herbert Xu1a6509d2008-01-28 19:37:29 -0800519 if ((err = attach_aead(&x->aead, &x->props.ealgo,
520 attrs[XFRMA_ALG_AEAD])))
521 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000522 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
523 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000525 if (!x->props.aalgo) {
526 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
527 attrs[XFRMA_ALG_AUTH])))
528 goto error;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
531 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700532 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto error;
534 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
535 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700536 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700538
539 if (attrs[XFRMA_ENCAP]) {
540 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
541 sizeof(*x->encap), GFP_KERNEL);
542 if (x->encap == NULL)
543 goto error;
544 }
545
Martin Willi35d28562010-12-08 04:37:49 +0000546 if (attrs[XFRMA_TFCPAD])
547 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
548
Thomas Graffd211502007-09-06 03:28:08 -0700549 if (attrs[XFRMA_COADDR]) {
550 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
551 sizeof(*x->coaddr), GFP_KERNEL);
552 if (x->coaddr == NULL)
553 goto error;
554 }
555
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000556 xfrm_mark_get(attrs, &x->mark);
557
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700558 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (err)
560 goto error;
561
Thomas Graffd211502007-09-06 03:28:08 -0700562 if (attrs[XFRMA_SEC_CTX] &&
563 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800564 goto error;
565
Steffen Klassertd8647b72011-03-08 00:10:27 +0000566 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
567 attrs[XFRMA_REPLAY_ESN_VAL])))
568 goto error;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800571 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800572 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800573 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800574
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000575 if ((err = xfrm_init_replay(x)))
576 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800577
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000578 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000579 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return x;
582
583error:
584 x->km.state = XFRM_STATE_DEAD;
585 xfrm_state_put(x);
586error_no_put:
587 *errp = err;
588 return NULL;
589}
590
Christoph Hellwig22e70052007-01-02 15:22:30 -0800591static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700592 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800594 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700595 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct xfrm_state *x;
597 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700598 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700599 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500600 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800601 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Thomas Graf35a7aa02007-08-22 14:00:40 -0700603 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (err)
605 return err;
606
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800607 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!x)
609 return err;
610
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700611 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
613 err = xfrm_state_add(x);
614 else
615 err = xfrm_state_update(x);
616
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800617 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400618 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (err < 0) {
621 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800622 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700623 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700626 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000627 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700628 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700629
630 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700631out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700632 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return err;
634}
635
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800636static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
637 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700638 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700639 int *errp)
640{
641 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000642 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700643 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000644 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700645
646 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
647 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000648 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700649 } else {
650 xfrm_address_t *saddr = NULL;
651
Thomas Graf35a7aa02007-08-22 14:00:40 -0700652 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700653 if (!saddr) {
654 err = -EINVAL;
655 goto out;
656 }
657
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800658 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000659 x = xfrm_state_lookup_byaddr(net, mark,
660 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800661 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700662 }
663
664 out:
665 if (!x && errp)
666 *errp = err;
667 return x;
668}
669
Christoph Hellwig22e70052007-01-02 15:22:30 -0800670static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700671 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800673 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700675 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700676 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700677 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700678 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500679 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800680 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800682 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
David S. Miller6f68dc32006-06-08 23:58:52 -0700686 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700687 goto out;
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700690 err = -EPERM;
691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700694 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600695
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700696 if (err < 0)
697 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700698
699 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000700 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700701 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700702 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700704out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800705 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400706 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700707 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
712{
Mathias Krausef778a632012-09-19 11:33:39 +0000713 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 memcpy(&p->id, &x->id, sizeof(p->id));
715 memcpy(&p->sel, &x->sel, sizeof(p->sel));
716 memcpy(&p->lft, &x->lft, sizeof(p->lft));
717 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
718 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700719 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 p->mode = x->props.mode;
721 p->replay_window = x->props.replay_window;
722 p->reqid = x->props.reqid;
723 p->family = x->props.family;
724 p->flags = x->props.flags;
725 p->seq = x->km.seq;
726}
727
728struct xfrm_dump_info {
729 struct sk_buff *in_skb;
730 struct sk_buff *out_skb;
731 u32 nlmsg_seq;
732 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733};
734
Thomas Grafc0144be2007-08-22 13:55:43 -0700735static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
736{
Thomas Grafc0144be2007-08-22 13:55:43 -0700737 struct xfrm_user_sec_ctx *uctx;
738 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700739 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700740
741 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
742 if (attr == NULL)
743 return -EMSGSIZE;
744
745 uctx = nla_data(attr);
746 uctx->exttype = XFRMA_SEC_CTX;
747 uctx->len = ctx_size;
748 uctx->ctx_doi = s->ctx_doi;
749 uctx->ctx_alg = s->ctx_alg;
750 uctx->ctx_len = s->ctx_len;
751 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
752
753 return 0;
754}
755
Martin Willi4447bb32009-11-25 00:29:52 +0000756static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
757{
758 struct xfrm_algo *algo;
759 struct nlattr *nla;
760
761 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
762 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
763 if (!nla)
764 return -EMSGSIZE;
765
766 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000767 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000768 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
769 algo->alg_key_len = auth->alg_key_len;
770
771 return 0;
772}
773
Herbert Xu68325d32007-10-09 13:30:57 -0700774/* Don't change this without updating xfrm_sa_len! */
775static int copy_to_user_state_extra(struct xfrm_state *x,
776 struct xfrm_usersa_info *p,
777 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700779 int ret = 0;
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 copy_to_user_state(x, p);
782
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100783 if (x->props.extra_flags) {
784 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
785 x->props.extra_flags);
786 if (ret)
787 goto out;
788 }
789
David S. Miller1d1e34d2012-06-27 21:57:03 -0700790 if (x->coaddr) {
791 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
792 if (ret)
793 goto out;
794 }
795 if (x->lastused) {
796 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
797 if (ret)
798 goto out;
799 }
800 if (x->aead) {
801 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
802 if (ret)
803 goto out;
804 }
805 if (x->aalg) {
806 ret = copy_to_user_auth(x->aalg, skb);
807 if (!ret)
808 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
809 xfrm_alg_auth_len(x->aalg), x->aalg);
810 if (ret)
811 goto out;
812 }
813 if (x->ealg) {
814 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
815 if (ret)
816 goto out;
817 }
818 if (x->calg) {
819 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
820 if (ret)
821 goto out;
822 }
823 if (x->encap) {
824 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
825 if (ret)
826 goto out;
827 }
828 if (x->tfcpad) {
829 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
830 if (ret)
831 goto out;
832 }
833 ret = xfrm_mark_put(skb, &x->mark);
834 if (ret)
835 goto out;
836 if (x->replay_esn) {
837 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
838 xfrm_replay_state_esn_len(x->replay_esn),
839 x->replay_esn);
840 if (ret)
841 goto out;
842 }
843 if (x->security)
844 ret = copy_sec_ctx(x->security, skb);
845out:
846 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700847}
848
849static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
850{
851 struct xfrm_dump_info *sp = ptr;
852 struct sk_buff *in_skb = sp->in_skb;
853 struct sk_buff *skb = sp->out_skb;
854 struct xfrm_usersa_info *p;
855 struct nlmsghdr *nlh;
856 int err;
857
Eric W. Biederman15e47302012-09-07 20:12:54 +0000858 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700859 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
860 if (nlh == NULL)
861 return -EMSGSIZE;
862
863 p = nlmsg_data(nlh);
864
865 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700866 if (err) {
867 nlmsg_cancel(skb, nlh);
868 return err;
869 }
Thomas Graf98250692007-08-22 12:47:26 -0700870 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Timo Teras4c563f72008-02-28 21:31:08 -0800874static int xfrm_dump_sa_done(struct netlink_callback *cb)
875{
876 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800877 struct sock *sk = cb->skb->sk;
878 struct net *net = sock_net(sk);
879
880 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800881 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
Christoph Hellwig22e70052007-01-02 15:22:30 -08001077static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001078 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001080 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct xfrm_state *x;
1082 struct xfrm_userspi_info *p;
1083 struct sk_buff *resp_skb;
1084 xfrm_address_t *daddr;
1085 int family;
1086 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001087 u32 mark;
1088 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Thomas Graf7b67c852007-08-22 13:53:52 -07001090 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001091 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (err)
1093 goto out_noput;
1094
1095 family = p->info.family;
1096 daddr = &p->info.id.daddr;
1097
1098 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001099
1100 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001102 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001103 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 xfrm_state_put(x);
1105 x = NULL;
1106 }
1107 }
1108
1109 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001110 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 p->info.id.proto, daddr,
1112 &p->info.saddr, 1,
1113 family);
1114 err = -ENOENT;
1115 if (x == NULL)
1116 goto out_noput;
1117
Herbert Xu658b2192007-10-09 13:29:52 -07001118 err = xfrm_alloc_spi(x, p->min, p->max);
1119 if (err)
1120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Herbert Xu658b2192007-10-09 13:29:52 -07001122 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (IS_ERR(resp_skb)) {
1124 err = PTR_ERR(resp_skb);
1125 goto out;
1126 }
1127
Eric W. Biederman15e47302012-09-07 20:12:54 +00001128 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130out:
1131 xfrm_state_put(x);
1132out_noput:
1133 return err;
1134}
1135
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001136static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 switch (dir) {
1139 case XFRM_POLICY_IN:
1140 case XFRM_POLICY_OUT:
1141 case XFRM_POLICY_FWD:
1142 break;
1143
1144 default:
1145 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 return 0;
1149}
1150
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001151static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001152{
1153 switch (type) {
1154 case XFRM_POLICY_TYPE_MAIN:
1155#ifdef CONFIG_XFRM_SUB_POLICY
1156 case XFRM_POLICY_TYPE_SUB:
1157#endif
1158 break;
1159
1160 default:
1161 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001162 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001163
1164 return 0;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1168{
Fan Due682adf2013-11-07 17:47:48 +08001169 int ret;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 switch (p->share) {
1172 case XFRM_SHARE_ANY:
1173 case XFRM_SHARE_SESSION:
1174 case XFRM_SHARE_USER:
1175 case XFRM_SHARE_UNIQUE:
1176 break;
1177
1178 default:
1179 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 switch (p->action) {
1183 case XFRM_POLICY_ALLOW:
1184 case XFRM_POLICY_BLOCK:
1185 break;
1186
1187 default:
1188 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 switch (p->sel.family) {
1192 case AF_INET:
1193 break;
1194
1195 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001196#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198#else
1199 return -EAFNOSUPPORT;
1200#endif
1201
1202 default:
1203 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Fan Due682adf2013-11-07 17:47:48 +08001206 ret = verify_policy_dir(p->dir);
1207 if (ret)
1208 return ret;
1209 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1210 return -EINVAL;
1211
1212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Thomas Graf5424f322007-08-22 14:01:33 -07001215static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001216{
Thomas Graf5424f322007-08-22 14:01:33 -07001217 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001218 struct xfrm_user_sec_ctx *uctx;
1219
1220 if (!rt)
1221 return 0;
1222
Thomas Graf5424f322007-08-22 14:01:33 -07001223 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001224 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1228 int nr)
1229{
1230 int i;
1231
1232 xp->xfrm_nr = nr;
1233 for (i = 0; i < nr; i++, ut++) {
1234 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1235
1236 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1237 memcpy(&t->saddr, &ut->saddr,
1238 sizeof(xfrm_address_t));
1239 t->reqid = ut->reqid;
1240 t->mode = ut->mode;
1241 t->share = ut->share;
1242 t->optional = ut->optional;
1243 t->aalgos = ut->aalgos;
1244 t->ealgos = ut->ealgos;
1245 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001246 /* If all masks are ~0, then we allow all algorithms. */
1247 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001248 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250}
1251
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001252static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1253{
1254 int i;
1255
1256 if (nr > XFRM_MAX_DEPTH)
1257 return -EINVAL;
1258
1259 for (i = 0; i < nr; i++) {
1260 /* We never validated the ut->family value, so many
1261 * applications simply leave it at zero. The check was
1262 * never made and ut->family was ignored because all
1263 * templates could be assumed to have the same family as
1264 * the policy itself. Now that we will have ipv4-in-ipv6
1265 * and ipv6-in-ipv4 tunnels, this is no longer true.
1266 */
1267 if (!ut[i].family)
1268 ut[i].family = family;
1269
1270 switch (ut[i].family) {
1271 case AF_INET:
1272 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001273#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001274 case AF_INET6:
1275 break;
1276#endif
1277 default:
1278 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001279 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001280 }
1281
1282 return 0;
1283}
1284
Thomas Graf5424f322007-08-22 14:01:33 -07001285static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Thomas Graf5424f322007-08-22 14:01:33 -07001287 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (!rt) {
1290 pol->xfrm_nr = 0;
1291 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001292 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1293 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001294 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001296 err = validate_tmpl(nr, utmpl, pol->family);
1297 if (err)
1298 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Thomas Graf5424f322007-08-22 14:01:33 -07001300 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 return 0;
1303}
1304
Thomas Graf5424f322007-08-22 14:01:33 -07001305static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001306{
Thomas Graf5424f322007-08-22 14:01:33 -07001307 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001308 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001309 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001310 int err;
1311
1312 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001313 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001314 type = upt->type;
1315 }
1316
1317 err = verify_policy_type(type);
1318 if (err)
1319 return err;
1320
1321 *tp = type;
1322 return 0;
1323}
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1326{
1327 xp->priority = p->priority;
1328 xp->index = p->index;
1329 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1330 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1331 xp->action = p->action;
1332 xp->flags = p->flags;
1333 xp->family = p->sel.family;
1334 /* XXX xp->share = p->share; */
1335}
1336
1337static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1338{
Mathias Krause7b789832012-09-19 11:33:40 +00001339 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1341 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1342 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1343 p->priority = xp->priority;
1344 p->index = xp->index;
1345 p->sel.family = xp->family;
1346 p->dir = dir;
1347 p->action = xp->action;
1348 p->flags = xp->flags;
1349 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1350}
1351
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001352static 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 -07001353{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001354 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int err;
1356
1357 if (!xp) {
1358 *errp = -ENOMEM;
1359 return NULL;
1360 }
1361
1362 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001363
Thomas Graf35a7aa02007-08-22 14:00:40 -07001364 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001365 if (err)
1366 goto error;
1367
Thomas Graf35a7aa02007-08-22 14:00:40 -07001368 if (!(err = copy_from_user_tmpl(xp, attrs)))
1369 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001370 if (err)
1371 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001373 xfrm_mark_get(attrs, &xp->mark);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001376 error:
1377 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001378 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001379 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001380 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Christoph Hellwig22e70052007-01-02 15:22:30 -08001383static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001384 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001386 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001387 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001389 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int err;
1391 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001392 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001393 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001394 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 err = verify_newpolicy_info(p);
1397 if (err)
1398 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001399 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001400 if (err)
1401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001403 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (!xp)
1405 return err;
1406
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001407 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001408 * Aha! this is anti-netlink really i.e more pfkey derived
1409 * in netlink excl is a flag and you wouldnt need
1410 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1412 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001413 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001414 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001417 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 kfree(xp);
1419 return err;
1420 }
1421
Herbert Xuf60f6b82005-06-18 22:44:37 -07001422 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001423 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001424 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001425 km_policy_notify(xp, p->dir, &c);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 xfrm_pol_put(xp);
1428
1429 return 0;
1430}
1431
1432static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1433{
1434 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1435 int i;
1436
1437 if (xp->xfrm_nr == 0)
1438 return 0;
1439
1440 for (i = 0; i < xp->xfrm_nr; i++) {
1441 struct xfrm_user_tmpl *up = &vec[i];
1442 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1443
Mathias Krause1f868402012-09-19 11:33:41 +00001444 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001446 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1448 up->reqid = kp->reqid;
1449 up->mode = kp->mode;
1450 up->share = kp->share;
1451 up->optional = kp->optional;
1452 up->aalgos = kp->aalgos;
1453 up->ealgos = kp->ealgos;
1454 up->calgos = kp->calgos;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Thomas Grafc0144be2007-08-22 13:55:43 -07001457 return nla_put(skb, XFRMA_TMPL,
1458 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001459}
1460
Serge Hallyn0d681622006-07-24 23:30:44 -07001461static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1462{
1463 if (x->security) {
1464 return copy_sec_ctx(x->security, skb);
1465 }
1466 return 0;
1467}
1468
1469static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1470{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001471 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001472 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001473 return 0;
1474}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001475static inline size_t userpolicy_type_attrsize(void)
1476{
1477#ifdef CONFIG_XFRM_SUB_POLICY
1478 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1479#else
1480 return 0;
1481#endif
1482}
Serge Hallyn0d681622006-07-24 23:30:44 -07001483
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001484#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001485static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001486{
Thomas Grafc0144be2007-08-22 13:55:43 -07001487 struct xfrm_userpolicy_type upt = {
1488 .type = type,
1489 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001490
Thomas Grafc0144be2007-08-22 13:55:43 -07001491 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001492}
1493
1494#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001495static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001496{
1497 return 0;
1498}
1499#endif
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1502{
1503 struct xfrm_dump_info *sp = ptr;
1504 struct xfrm_userpolicy_info *p;
1505 struct sk_buff *in_skb = sp->in_skb;
1506 struct sk_buff *skb = sp->out_skb;
1507 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001508 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Eric W. Biederman15e47302012-09-07 20:12:54 +00001510 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001511 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1512 if (nlh == NULL)
1513 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Thomas Graf7b67c852007-08-22 13:53:52 -07001515 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001517 err = copy_to_user_tmpl(xp, skb);
1518 if (!err)
1519 err = copy_to_user_sec_ctx(xp, skb);
1520 if (!err)
1521 err = copy_to_user_policy_type(xp->type, skb);
1522 if (!err)
1523 err = xfrm_mark_put(skb, &xp->mark);
1524 if (err) {
1525 nlmsg_cancel(skb, nlh);
1526 return err;
1527 }
Thomas Graf98250692007-08-22 12:47:26 -07001528 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Timo Teras4c563f72008-02-28 21:31:08 -08001532static int xfrm_dump_policy_done(struct netlink_callback *cb)
1533{
1534 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001535 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001536
Fan Du283bc9f2013-11-07 17:47:50 +08001537 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001538 return 0;
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1542{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001543 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001544 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct xfrm_dump_info info;
1546
Timo Teras4c563f72008-02-28 21:31:08 -08001547 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1548 sizeof(cb->args) - sizeof(cb->args[0]));
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 info.in_skb = cb->skb;
1551 info.out_skb = skb;
1552 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1553 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001554
1555 if (!cb->args[0]) {
1556 cb->args[0] = 1;
1557 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1558 }
1559
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001560 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 return skb->len;
1563}
1564
1565static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1566 struct xfrm_policy *xp,
1567 int dir, u32 seq)
1568{
1569 struct xfrm_dump_info info;
1570 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001571 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Thomas Graf7deb2262007-08-22 13:57:39 -07001573 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (!skb)
1575 return ERR_PTR(-ENOMEM);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 info.in_skb = in_skb;
1578 info.out_skb = skb;
1579 info.nlmsg_seq = seq;
1580 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Mathias Krausec2546372012-09-14 09:58:32 +00001582 err = dump_one_policy(xp, dir, 0, &info);
1583 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001585 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 return skb;
1589}
1590
Christoph Hellwig22e70052007-01-02 15:22:30 -08001591static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001592 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001594 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct xfrm_policy *xp;
1596 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001597 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001599 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001601 struct xfrm_mark m;
1602 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Thomas Graf7b67c852007-08-22 13:53:52 -07001604 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1606
Thomas Graf35a7aa02007-08-22 14:00:40 -07001607 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001608 if (err)
1609 return err;
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 err = verify_policy_dir(p->dir);
1612 if (err)
1613 return err;
1614
1615 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001616 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001617 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001618 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001619 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001620
Thomas Graf35a7aa02007-08-22 14:00:40 -07001621 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001622 if (err)
1623 return err;
1624
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001625 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001626 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001627 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001628
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001629 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001630 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001631 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001632 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001633 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001634 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001635 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (xp == NULL)
1638 return -ENOENT;
1639
1640 if (!delete) {
1641 struct sk_buff *resp_skb;
1642
1643 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1644 if (IS_ERR(resp_skb)) {
1645 err = PTR_ERR(resp_skb);
1646 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001647 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001648 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001650 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001651 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001652 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001653 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001654
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001655 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001656 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1657 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001658
1659 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001660 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001661
Herbert Xue7443892005-06-18 22:44:18 -07001662 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001663 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001664 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001665 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001666 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001669out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001670 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001671 if (delete && err == 0)
1672 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return err;
1674}
1675
Christoph Hellwig22e70052007-01-02 15:22:30 -08001676static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001677 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001679 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001681 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001682 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001685 audit_info.loginuid = audit_get_loginuid(current);
1686 audit_info.sessionid = audit_get_sessionid(current);
1687 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001688 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001689 if (err) {
1690 if (err == -ESRCH) /* empty table */
1691 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001692 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001693 }
Herbert Xubf088672005-06-18 22:44:00 -07001694 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001695 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001697 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001698 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699 km_state_notify(NULL, &c);
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702}
1703
Steffen Klassertd8647b72011-03-08 00:10:27 +00001704static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001705{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001706 size_t replay_size = x->replay_esn ?
1707 xfrm_replay_state_esn_len(x->replay_esn) :
1708 sizeof(struct xfrm_replay_state);
1709
Thomas Graf7deb2262007-08-22 13:57:39 -07001710 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001711 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001712 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001713 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001714 + nla_total_size(4) /* XFRM_AE_RTHR */
1715 + nla_total_size(4); /* XFRM_AE_ETHR */
1716}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001717
David S. Miller214e0052011-02-24 00:02:38 -05001718static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001719{
1720 struct xfrm_aevent_id *id;
1721 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001722 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001723
Eric W. Biederman15e47302012-09-07 20:12:54 +00001724 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001725 if (nlh == NULL)
1726 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001727
Thomas Graf7b67c852007-08-22 13:53:52 -07001728 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001729 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001730 id->sa_id.spi = x->id.spi;
1731 id->sa_id.family = x->props.family;
1732 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001733 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001734 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001735 id->flags = c->data.aevent;
1736
David S. Millerd0fde792012-03-29 04:02:26 -04001737 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001738 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1739 xfrm_replay_state_esn_len(x->replay_esn),
1740 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001741 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001742 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1743 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001744 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001745 if (err)
1746 goto out_cancel;
1747 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1748 if (err)
1749 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001750
David S. Miller1d1e34d2012-06-27 21:57:03 -07001751 if (id->flags & XFRM_AE_RTHR) {
1752 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1753 if (err)
1754 goto out_cancel;
1755 }
1756 if (id->flags & XFRM_AE_ETHR) {
1757 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1758 x->replay_maxage * 10 / HZ);
1759 if (err)
1760 goto out_cancel;
1761 }
1762 err = xfrm_mark_put(skb, &x->mark);
1763 if (err)
1764 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001765
Thomas Graf98250692007-08-22 12:47:26 -07001766 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001767
David S. Miller1d1e34d2012-06-27 21:57:03 -07001768out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001769 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001770 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771}
1772
Christoph Hellwig22e70052007-01-02 15:22:30 -08001773static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001774 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001775{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001776 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001777 struct xfrm_state *x;
1778 struct sk_buff *r_skb;
1779 int err;
1780 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001781 u32 mark;
1782 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001783 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001784 struct xfrm_usersa_id *id = &p->sa_id;
1785
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001786 mark = xfrm_mark_get(attrs, &m);
1787
1788 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001789 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001790 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001791
1792 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1793 if (r_skb == NULL) {
1794 xfrm_state_put(x);
1795 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001796 }
1797
1798 /*
1799 * XXX: is this lock really needed - none of the other
1800 * gets lock (the concern is things getting updated
1801 * while we are still reading) - jhs
1802 */
1803 spin_lock_bh(&x->lock);
1804 c.data.aevent = p->flags;
1805 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001806 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001807
1808 if (build_aevent(r_skb, x, &c) < 0)
1809 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001810 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001811 spin_unlock_bh(&x->lock);
1812 xfrm_state_put(x);
1813 return err;
1814}
1815
Christoph Hellwig22e70052007-01-02 15:22:30 -08001816static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001817 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001818{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001819 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001820 struct xfrm_state *x;
1821 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001822 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001823 u32 mark = 0;
1824 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001825 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001826 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001827 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001828 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001829
Steffen Klassertd8647b72011-03-08 00:10:27 +00001830 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001831 return err;
1832
1833 /* pedantic mode - thou shalt sayeth replaceth */
1834 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1835 return err;
1836
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001837 mark = xfrm_mark_get(attrs, &m);
1838
1839 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 -08001840 if (x == NULL)
1841 return -ESRCH;
1842
1843 if (x->km.state != XFRM_STATE_VALID)
1844 goto out;
1845
Steffen Klassert4479ff72013-09-09 09:39:01 +02001846 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001847 if (err)
1848 goto out;
1849
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001850 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001851 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001852 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001853
1854 c.event = nlh->nlmsg_type;
1855 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001856 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001857 c.data.aevent = XFRM_AE_CU;
1858 km_state_notify(x, &c);
1859 err = 0;
1860out:
1861 xfrm_state_put(x);
1862 return err;
1863}
1864
Christoph Hellwig22e70052007-01-02 15:22:30 -08001865static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001866 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001868 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001869 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001870 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001871 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001872 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001873
Thomas Graf35a7aa02007-08-22 14:00:40 -07001874 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001875 if (err)
1876 return err;
1877
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001878 audit_info.loginuid = audit_get_loginuid(current);
1879 audit_info.sessionid = audit_get_sessionid(current);
1880 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001881 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001882 if (err) {
1883 if (err == -ESRCH) /* empty table */
1884 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001885 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001886 }
1887
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001888 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001889 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001890 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001891 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001892 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001893 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return 0;
1895}
1896
Christoph Hellwig22e70052007-01-02 15:22:30 -08001897static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001898 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001899{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001900 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001901 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001902 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001903 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001904 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001905 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001906 struct xfrm_mark m;
1907 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001908
Thomas Graf35a7aa02007-08-22 14:00:40 -07001909 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001910 if (err)
1911 return err;
1912
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001913 err = verify_policy_dir(p->dir);
1914 if (err)
1915 return err;
1916
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001917 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001918 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001919 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001920 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001921 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001922
Thomas Graf35a7aa02007-08-22 14:00:40 -07001923 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001924 if (err)
1925 return err;
1926
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001927 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001928 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001929 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001931 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001932 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001933 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001934 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001935 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001936 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001937 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001939 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001940 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001941
Timo Teräsea2dea92010-03-31 00:17:05 +00001942 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001943 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001945 err = 0;
1946 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001947 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001948 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001949 u32 sid;
1950
1951 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001952 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001953 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001954
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001955 } else {
1956 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001957 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001959 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001960
1961out:
1962 xfrm_pol_put(xp);
1963 return err;
1964}
1965
Christoph Hellwig22e70052007-01-02 15:22:30 -08001966static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001967 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001968{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001969 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001970 struct xfrm_state *x;
1971 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001972 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001973 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001974 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001975 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001976
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001977 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 -08001978
David S. Miller3a765aa2007-02-26 14:52:21 -08001979 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001980 if (x == NULL)
1981 return err;
1982
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001983 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001984 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001985 if (x->km.state != XFRM_STATE_VALID)
1986 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001987 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001988
Joy Latten161a09e2006-11-27 13:11:54 -06001989 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001990 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001991 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001992 u32 sid;
1993
1994 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001995 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001996 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001997 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001998 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001999out:
2000 spin_unlock_bh(&x->lock);
2001 xfrm_state_put(x);
2002 return err;
2003}
2004
Christoph Hellwig22e70052007-01-02 15:22:30 -08002005static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002006 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002007{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002008 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002009 struct xfrm_policy *xp;
2010 struct xfrm_user_tmpl *ut;
2011 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002012 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002013 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002014
Thomas Graf7b67c852007-08-22 13:53:52 -07002015 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002016 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002017 int err = -ENOMEM;
2018
2019 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002020 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002021
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002022 xfrm_mark_get(attrs, &mark);
2023
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002024 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002025 if (err)
2026 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002027
2028 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002029 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002030 if (!xp)
2031 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002032
2033 memcpy(&x->id, &ua->id, sizeof(ua->id));
2034 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2035 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002036 xp->mark.m = x->mark.m = mark.m;
2037 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002038 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002039 /* extract the templates and for each call km_key */
2040 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2041 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2042 memcpy(&x->id, &t->id, sizeof(x->id));
2043 x->props.mode = t->mode;
2044 x->props.reqid = t->reqid;
2045 x->props.family = ut->family;
2046 t->aalgos = ua->aalgos;
2047 t->ealgos = ua->ealgos;
2048 t->calgos = ua->calgos;
2049 err = km_query(x, t, xp);
2050
2051 }
2052
2053 kfree(x);
2054 kfree(xp);
2055
2056 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002057
2058bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002059 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002060free_state:
2061 kfree(x);
2062nomem:
2063 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002064}
2065
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002066#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002068 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002069 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002070{
Thomas Graf5424f322007-08-22 14:01:33 -07002071 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072 struct xfrm_user_migrate *um;
2073 int i, num_migrate;
2074
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002075 if (k != NULL) {
2076 struct xfrm_user_kmaddress *uk;
2077
2078 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2079 memcpy(&k->local, &uk->local, sizeof(k->local));
2080 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2081 k->family = uk->family;
2082 k->reserved = uk->reserved;
2083 }
2084
Thomas Graf5424f322007-08-22 14:01:33 -07002085 um = nla_data(rt);
2086 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002087
2088 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2089 return -EINVAL;
2090
2091 for (i = 0; i < num_migrate; i++, um++, ma++) {
2092 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2093 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2094 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2095 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2096
2097 ma->proto = um->proto;
2098 ma->mode = um->mode;
2099 ma->reqid = um->reqid;
2100
2101 ma->old_family = um->old_family;
2102 ma->new_family = um->new_family;
2103 }
2104
2105 *num = i;
2106 return 0;
2107}
2108
2109static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002110 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002111{
Thomas Graf7b67c852007-08-22 13:53:52 -07002112 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002113 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002114 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002115 u8 type;
2116 int err;
2117 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002118 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002119
Thomas Graf35a7aa02007-08-22 14:00:40 -07002120 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002121 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002122
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002123 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2124
Thomas Graf5424f322007-08-22 14:01:33 -07002125 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002126 if (err)
2127 return err;
2128
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002129 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002130 if (err)
2131 return err;
2132
2133 if (!n)
2134 return 0;
2135
Fan Du8d549c42013-11-07 17:47:49 +08002136 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002137
2138 return 0;
2139}
2140#else
2141static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002142 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002143{
2144 return -ENOPROTOOPT;
2145}
2146#endif
2147
2148#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002149static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002150{
2151 struct xfrm_user_migrate um;
2152
2153 memset(&um, 0, sizeof(um));
2154 um.proto = m->proto;
2155 um.mode = m->mode;
2156 um.reqid = m->reqid;
2157 um.old_family = m->old_family;
2158 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2159 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2160 um.new_family = m->new_family;
2161 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2162 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2163
Thomas Grafc0144be2007-08-22 13:55:43 -07002164 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002165}
2166
David S. Miller183cad12011-02-24 00:28:01 -05002167static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002168{
2169 struct xfrm_user_kmaddress uk;
2170
2171 memset(&uk, 0, sizeof(uk));
2172 uk.family = k->family;
2173 uk.reserved = k->reserved;
2174 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002175 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002176
2177 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2178}
2179
2180static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002181{
2182 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002183 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2184 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2185 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002186}
2187
David S. Miller183cad12011-02-24 00:28:01 -05002188static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2189 int num_migrate, const struct xfrm_kmaddress *k,
2190 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002191{
David S. Miller183cad12011-02-24 00:28:01 -05002192 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002193 struct xfrm_userpolicy_id *pol_id;
2194 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002195 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002196
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002197 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2198 if (nlh == NULL)
2199 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002200
Thomas Graf7b67c852007-08-22 13:53:52 -07002201 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002202 /* copy data from selector, dir, and type to the pol_id */
2203 memset(pol_id, 0, sizeof(*pol_id));
2204 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2205 pol_id->dir = dir;
2206
David S. Miller1d1e34d2012-06-27 21:57:03 -07002207 if (k != NULL) {
2208 err = copy_to_user_kmaddress(k, skb);
2209 if (err)
2210 goto out_cancel;
2211 }
2212 err = copy_to_user_policy_type(type, skb);
2213 if (err)
2214 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002215 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002216 err = copy_to_user_migrate(mp, skb);
2217 if (err)
2218 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002219 }
2220
Thomas Graf98250692007-08-22 12:47:26 -07002221 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002222
2223out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002224 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002225 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002226}
2227
David S. Miller183cad12011-02-24 00:28:01 -05002228static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2229 const struct xfrm_migrate *m, int num_migrate,
2230 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002231{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002232 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002233 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002234
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002235 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002236 if (skb == NULL)
2237 return -ENOMEM;
2238
2239 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002240 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002241 BUG();
2242
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002243 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002244}
2245#else
David S. Miller183cad12011-02-24 00:28:01 -05002246static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2247 const struct xfrm_migrate *m, int num_migrate,
2248 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002249{
2250 return -ENOPROTOOPT;
2251}
2252#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002253
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002254#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002255
2256static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002257 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002258 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2259 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2260 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2261 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2262 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2263 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002264 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002265 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002266 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002267 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002268 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002269 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002270 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002271 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2272 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002273 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002274 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002275 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2276 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277};
2278
Thomas Graf492b5582005-05-03 14:26:40 -07002279#undef XMSGSIZE
2280
Thomas Grafcf5cb792007-08-22 13:59:04 -07002281static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002282 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2283 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2284 [XFRMA_LASTUSED] = { .type = NLA_U64},
2285 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002286 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002287 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2288 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2291 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2292 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2293 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2294 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2295 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2296 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2297 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2298 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2299 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2300 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002301 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002302 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002303 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002304 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002305 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002306};
2307
Mathias Krause05600a72013-02-24 14:10:27 +01002308static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002309 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002311 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002312} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2313 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2314 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2315 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002316 .dump = xfrm_dump_sa,
2317 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002318 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2319 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2320 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002321 .dump = xfrm_dump_policy,
2322 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002323 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002324 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002325 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002326 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2327 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002328 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002329 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2330 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002331 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2332 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002333 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002334 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002335 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336};
2337
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002338static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002340 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002341 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002342 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002343 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 type -= XFRM_MSG_BASE;
2350 link = &xfrm_dispatch[type];
2351
2352 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002353 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002354 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Thomas Graf492b5582005-05-03 14:26:40 -07002356 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2357 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002358 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002360 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002362 {
2363 struct netlink_dump_control c = {
2364 .dump = link->dump,
2365 .done = link->done,
2366 };
2367 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370
Thomas Graf35a7aa02007-08-22 14:00:40 -07002371 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002372 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002373 if (err < 0)
2374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002377 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Thomas Graf5424f322007-08-22 14:01:33 -07002379 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002382static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Fan Du283bc9f2013-11-07 17:47:50 +08002384 struct net *net = sock_net(skb->sk);
2385
2386 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002387 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002388 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Thomas Graf7deb2262007-08-22 13:57:39 -07002391static inline size_t xfrm_expire_msgsize(void)
2392{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002393 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2394 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002395}
2396
David S. Miller214e0052011-02-24 00:02:38 -05002397static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 struct xfrm_user_expire *ue;
2400 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002401 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Eric W. Biederman15e47302012-09-07 20:12:54 +00002403 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002404 if (nlh == NULL)
2405 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Thomas Graf7b67c852007-08-22 13:53:52 -07002407 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002409 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
David S. Miller1d1e34d2012-06-27 21:57:03 -07002411 err = xfrm_mark_put(skb, &x->mark);
2412 if (err)
2413 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002414
Thomas Graf98250692007-08-22 12:47:26 -07002415 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
David S. Miller214e0052011-02-24 00:02:38 -05002418static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002420 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct sk_buff *skb;
2422
Thomas Graf7deb2262007-08-22 13:57:39 -07002423 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (skb == NULL)
2425 return -ENOMEM;
2426
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002427 if (build_expire(skb, x, c) < 0) {
2428 kfree_skb(skb);
2429 return -EMSGSIZE;
2430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002432 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
David S. Miller214e0052011-02-24 00:02:38 -05002435static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002436{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002437 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002438 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002439
Steffen Klassertd8647b72011-03-08 00:10:27 +00002440 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002441 if (skb == NULL)
2442 return -ENOMEM;
2443
2444 if (build_aevent(skb, x, c) < 0)
2445 BUG();
2446
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002447 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002448}
2449
David S. Miller214e0052011-02-24 00:02:38 -05002450static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002451{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002452 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002453 struct xfrm_usersa_flush *p;
2454 struct nlmsghdr *nlh;
2455 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002456 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457
Thomas Graf7deb2262007-08-22 13:57:39 -07002458 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002459 if (skb == NULL)
2460 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002461
Eric W. Biederman15e47302012-09-07 20:12:54 +00002462 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002463 if (nlh == NULL) {
2464 kfree_skb(skb);
2465 return -EMSGSIZE;
2466 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002467
Thomas Graf7b67c852007-08-22 13:53:52 -07002468 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002469 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470
Thomas Graf98250692007-08-22 12:47:26 -07002471 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002472
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002473 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002474}
2475
Thomas Graf7deb2262007-08-22 13:57:39 -07002476static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002477{
Thomas Graf7deb2262007-08-22 13:57:39 -07002478 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002479 if (x->aead)
2480 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002481 if (x->aalg) {
2482 l += nla_total_size(sizeof(struct xfrm_algo) +
2483 (x->aalg->alg_key_len + 7) / 8);
2484 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2485 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002486 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002487 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002488 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002489 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002490 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002491 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002492 if (x->tfcpad)
2493 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002494 if (x->replay_esn)
2495 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002496 if (x->security)
2497 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2498 x->security->ctx_len);
2499 if (x->coaddr)
2500 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002501 if (x->props.extra_flags)
2502 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002503
Herbert Xud26f3982007-11-13 21:47:08 -08002504 /* Must count x->lastused as it may become non-zero behind our back. */
2505 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002506
2507 return l;
2508}
2509
David S. Miller214e0052011-02-24 00:02:38 -05002510static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002511{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002512 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002513 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002514 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002515 struct nlmsghdr *nlh;
2516 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002517 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002518 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002519
2520 headlen = sizeof(*p);
2521 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002522 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002523 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002524 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002525 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002526 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002527
Thomas Graf7deb2262007-08-22 13:57:39 -07002528 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002529 if (skb == NULL)
2530 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002531
Eric W. Biederman15e47302012-09-07 20:12:54 +00002532 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002533 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002534 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002535 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002536
Thomas Graf7b67c852007-08-22 13:53:52 -07002537 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002538 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002539 struct nlattr *attr;
2540
Thomas Graf7b67c852007-08-22 13:53:52 -07002541 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002542 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2543 id->spi = x->id.spi;
2544 id->family = x->props.family;
2545 id->proto = x->id.proto;
2546
Thomas Grafc0144be2007-08-22 13:55:43 -07002547 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002548 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002549 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002550 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002551
2552 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002553 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002554 err = copy_to_user_state_extra(x, p, skb);
2555 if (err)
2556 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002557
Thomas Graf98250692007-08-22 12:47:26 -07002558 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002559
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002560 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002561
David S. Miller1d1e34d2012-06-27 21:57:03 -07002562out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002563 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002564 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002565}
2566
David S. Miller214e0052011-02-24 00:02:38 -05002567static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002568{
2569
2570 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002571 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002572 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002573 case XFRM_MSG_NEWAE:
2574 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002575 case XFRM_MSG_DELSA:
2576 case XFRM_MSG_UPDSA:
2577 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002578 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002579 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002580 return xfrm_notify_sa_flush(c);
2581 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002582 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2583 c->event);
2584 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002585 }
2586
2587 return 0;
2588
2589}
2590
Thomas Graf7deb2262007-08-22 13:57:39 -07002591static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2592 struct xfrm_policy *xp)
2593{
2594 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2595 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002596 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002597 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2598 + userpolicy_type_attrsize();
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002602 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002604 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct xfrm_user_acquire *ua;
2606 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002609 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2610 if (nlh == NULL)
2611 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Thomas Graf7b67c852007-08-22 13:53:52 -07002613 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 memcpy(&ua->id, &x->id, sizeof(ua->id));
2615 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2616 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002617 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 ua->aalgos = xt->aalgos;
2619 ua->ealgos = xt->ealgos;
2620 ua->calgos = xt->calgos;
2621 ua->seq = x->km.seq = seq;
2622
David S. Miller1d1e34d2012-06-27 21:57:03 -07002623 err = copy_to_user_tmpl(xp, skb);
2624 if (!err)
2625 err = copy_to_user_state_sec_ctx(x, skb);
2626 if (!err)
2627 err = copy_to_user_policy_type(xp->type, skb);
2628 if (!err)
2629 err = xfrm_mark_put(skb, &xp->mark);
2630 if (err) {
2631 nlmsg_cancel(skb, nlh);
2632 return err;
2633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Thomas Graf98250692007-08-22 12:47:26 -07002635 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
2638static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002639 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002641 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Thomas Graf7deb2262007-08-22 13:57:39 -07002644 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (skb == NULL)
2646 return -ENOMEM;
2647
Fan Du65e07362012-08-15 10:13:47 +08002648 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 BUG();
2650
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002651 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
2654/* User gives us xfrm_user_policy_info followed by an array of 0
2655 * or more templates.
2656 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002657static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 u8 *data, int len, int *dir)
2659{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002660 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2662 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2663 struct xfrm_policy *xp;
2664 int nr;
2665
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002666 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 case AF_INET:
2668 if (opt != IP_XFRM_POLICY) {
2669 *dir = -EOPNOTSUPP;
2670 return NULL;
2671 }
2672 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002673#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 case AF_INET6:
2675 if (opt != IPV6_XFRM_POLICY) {
2676 *dir = -EOPNOTSUPP;
2677 return NULL;
2678 }
2679 break;
2680#endif
2681 default:
2682 *dir = -EINVAL;
2683 return NULL;
2684 }
2685
2686 *dir = -EINVAL;
2687
2688 if (len < sizeof(*p) ||
2689 verify_newpolicy_info(p))
2690 return NULL;
2691
2692 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002693 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return NULL;
2695
Herbert Xua4f1bac2005-07-26 15:43:17 -07002696 if (p->dir > XFRM_POLICY_OUT)
2697 return NULL;
2698
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002699 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if (xp == NULL) {
2701 *dir = -ENOBUFS;
2702 return NULL;
2703 }
2704
2705 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002706 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 copy_templates(xp, ut, nr);
2708
2709 *dir = p->dir;
2710
2711 return xp;
2712}
2713
Thomas Graf7deb2262007-08-22 13:57:39 -07002714static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2715{
2716 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2717 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2718 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002719 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002720 + userpolicy_type_attrsize();
2721}
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002724 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002727 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002728 struct nlmsghdr *nlh;
2729 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Eric W. Biederman15e47302012-09-07 20:12:54 +00002731 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002732 if (nlh == NULL)
2733 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Thomas Graf7b67c852007-08-22 13:53:52 -07002735 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002737 err = copy_to_user_tmpl(xp, skb);
2738 if (!err)
2739 err = copy_to_user_sec_ctx(xp, skb);
2740 if (!err)
2741 err = copy_to_user_policy_type(xp->type, skb);
2742 if (!err)
2743 err = xfrm_mark_put(skb, &xp->mark);
2744 if (err) {
2745 nlmsg_cancel(skb, nlh);
2746 return err;
2747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 upe->hard = !!hard;
2749
Thomas Graf98250692007-08-22 12:47:26 -07002750 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
David S. Miller214e0052011-02-24 00:02:38 -05002753static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002755 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Thomas Graf7deb2262007-08-22 13:57:39 -07002758 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (skb == NULL)
2760 return -ENOMEM;
2761
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002762 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 BUG();
2764
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002765 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
David S. Miller214e0052011-02-24 00:02:38 -05002768static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002769{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002770 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002771 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002773 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774 struct nlmsghdr *nlh;
2775 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002776 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002777
2778 headlen = sizeof(*p);
2779 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002780 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002781 headlen = sizeof(*id);
2782 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002783 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002784 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002785 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002786
Thomas Graf7deb2262007-08-22 13:57:39 -07002787 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002788 if (skb == NULL)
2789 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790
Eric W. Biederman15e47302012-09-07 20:12:54 +00002791 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002792 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002793 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002794 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795
Thomas Graf7b67c852007-08-22 13:53:52 -07002796 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002797 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002798 struct nlattr *attr;
2799
Thomas Graf7b67c852007-08-22 13:53:52 -07002800 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002801 memset(id, 0, sizeof(*id));
2802 id->dir = dir;
2803 if (c->data.byid)
2804 id->index = xp->index;
2805 else
2806 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2807
Thomas Grafc0144be2007-08-22 13:55:43 -07002808 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002809 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002810 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002811 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002812
2813 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002814 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002816 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002817 err = copy_to_user_tmpl(xp, skb);
2818 if (!err)
2819 err = copy_to_user_policy_type(xp->type, skb);
2820 if (!err)
2821 err = xfrm_mark_put(skb, &xp->mark);
2822 if (err)
2823 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002824
Thomas Graf98250692007-08-22 12:47:26 -07002825 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002826
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002827 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002828
David S. Miller1d1e34d2012-06-27 21:57:03 -07002829out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002830 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002831 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002832}
2833
David S. Miller214e0052011-02-24 00:02:38 -05002834static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002835{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002836 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002837 struct nlmsghdr *nlh;
2838 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002839 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002840
Thomas Graf7deb2262007-08-22 13:57:39 -07002841 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002842 if (skb == NULL)
2843 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002844
Eric W. Biederman15e47302012-09-07 20:12:54 +00002845 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002846 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002847 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002848 goto out_free_skb;
2849 err = copy_to_user_policy_type(c->data.type, skb);
2850 if (err)
2851 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002852
Thomas Graf98250692007-08-22 12:47:26 -07002853 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002854
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002855 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002856
David S. Miller1d1e34d2012-06-27 21:57:03 -07002857out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002858 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002859 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002860}
2861
David S. Miller214e0052011-02-24 00:02:38 -05002862static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002863{
2864
2865 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002866 case XFRM_MSG_NEWPOLICY:
2867 case XFRM_MSG_UPDPOLICY:
2868 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002869 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002870 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002871 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002872 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002873 return xfrm_exp_policy_notify(xp, dir, c);
2874 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002875 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2876 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002877 }
2878
2879 return 0;
2880
2881}
2882
Thomas Graf7deb2262007-08-22 13:57:39 -07002883static inline size_t xfrm_report_msgsize(void)
2884{
2885 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2886}
2887
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002888static int build_report(struct sk_buff *skb, u8 proto,
2889 struct xfrm_selector *sel, xfrm_address_t *addr)
2890{
2891 struct xfrm_user_report *ur;
2892 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002893
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002894 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2895 if (nlh == NULL)
2896 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002897
Thomas Graf7b67c852007-08-22 13:53:52 -07002898 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002899 ur->proto = proto;
2900 memcpy(&ur->sel, sel, sizeof(ur->sel));
2901
David S. Miller1d1e34d2012-06-27 21:57:03 -07002902 if (addr) {
2903 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2904 if (err) {
2905 nlmsg_cancel(skb, nlh);
2906 return err;
2907 }
2908 }
Thomas Graf98250692007-08-22 12:47:26 -07002909 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002910}
2911
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002912static int xfrm_send_report(struct net *net, u8 proto,
2913 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002914{
2915 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002916
Thomas Graf7deb2262007-08-22 13:57:39 -07002917 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002918 if (skb == NULL)
2919 return -ENOMEM;
2920
2921 if (build_report(skb, proto, sel, addr) < 0)
2922 BUG();
2923
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002924 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002925}
2926
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002927static inline size_t xfrm_mapping_msgsize(void)
2928{
2929 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2930}
2931
2932static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2933 xfrm_address_t *new_saddr, __be16 new_sport)
2934{
2935 struct xfrm_user_mapping *um;
2936 struct nlmsghdr *nlh;
2937
2938 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2939 if (nlh == NULL)
2940 return -EMSGSIZE;
2941
2942 um = nlmsg_data(nlh);
2943
2944 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2945 um->id.spi = x->id.spi;
2946 um->id.family = x->props.family;
2947 um->id.proto = x->id.proto;
2948 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2949 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2950 um->new_sport = new_sport;
2951 um->old_sport = x->encap->encap_sport;
2952 um->reqid = x->props.reqid;
2953
2954 return nlmsg_end(skb, nlh);
2955}
2956
2957static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2958 __be16 sport)
2959{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002960 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002961 struct sk_buff *skb;
2962
2963 if (x->id.proto != IPPROTO_ESP)
2964 return -EINVAL;
2965
2966 if (!x->encap)
2967 return -EINVAL;
2968
2969 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2970 if (skb == NULL)
2971 return -ENOMEM;
2972
2973 if (build_mapping(skb, x, ipaddr, sport) < 0)
2974 BUG();
2975
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002976 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002977}
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979static struct xfrm_mgr netlink_mgr = {
2980 .id = "netlink",
2981 .notify = xfrm_send_state_notify,
2982 .acquire = xfrm_send_acquire,
2983 .compile_policy = xfrm_compile_policy,
2984 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002985 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002986 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002987 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988};
2989
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002990static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Patrick McHardybe336902006-03-20 22:40:54 -08002992 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002993 struct netlink_kernel_cfg cfg = {
2994 .groups = XFRMNLGRP_MAX,
2995 .input = xfrm_netlink_rcv,
2996 };
Patrick McHardybe336902006-03-20 22:40:54 -08002997
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002998 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08002999 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003001 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003002 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return 0;
3004}
3005
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003006static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003007{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003008 struct net *net;
3009 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003010 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003011 synchronize_net();
3012 list_for_each_entry(net, net_exit_list, exit_list)
3013 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003014}
3015
3016static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003017 .init = xfrm_user_net_init,
3018 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003019};
3020
3021static int __init xfrm_user_init(void)
3022{
3023 int rv;
3024
3025 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3026
3027 rv = register_pernet_subsys(&xfrm_user_net_ops);
3028 if (rv < 0)
3029 return rv;
3030 rv = xfrm_register_km(&netlink_mgr);
3031 if (rv < 0)
3032 unregister_pernet_subsys(&xfrm_user_net_ops);
3033 return rv;
3034}
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036static void __exit xfrm_user_exit(void)
3037{
3038 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003039 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
3042module_init(xfrm_user_init);
3043module_exit(xfrm_user_exit);
3044MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003045MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003046