blob: 8f131c10a6f3d6793c6d0a049108ab66ccaa8664 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Graf5424f322007-08-22 14:01:33 -070035static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Graf5424f322007-08-22 14:01:33 -070037 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct xfrm_algo *algp;
39
40 if (!rt)
41 return 0;
42
Thomas Graf5424f322007-08-22 14:01:33 -070043 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080044 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070045 return -EINVAL;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 switch (type) {
48 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52
53 default:
54 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
58 return 0;
59}
60
Martin Willi4447bb32009-11-25 00:29:52 +000061static int verify_auth_trunc(struct nlattr **attrs)
62{
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
65
66 if (!rt)
67 return 0;
68
69 algp = nla_data(rt);
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
71 return -EINVAL;
72
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
Herbert Xu1a6509d2008-01-28 19:37:29 -080077static int verify_aead(struct nlattr **attrs)
78{
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
81
82 if (!rt)
83 return 0;
84
85 algp = nla_data(rt);
86 if (nla_len(rt) < aead_len(algp))
87 return -EINVAL;
88
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
90 return 0;
91}
92
Thomas Graf5424f322007-08-22 14:01:33 -070093static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094 xfrm_address_t **addrp)
95{
Thomas Graf5424f322007-08-22 14:01:33 -070096 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070097
Thomas Grafcf5cb792007-08-22 13:59:04 -070098 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070099 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100}
Trent Jaegerdf718372005-12-13 23:12:27 -0800101
Thomas Graf5424f322007-08-22 14:01:33 -0700102static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800103{
Thomas Graf5424f322007-08-22 14:01:33 -0700104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800105 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
107 if (!rt)
108 return 0;
109
Thomas Graf5424f322007-08-22 14:01:33 -0700110 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800112 return -EINVAL;
113
114 return 0;
115}
116
Steffen Klassertd8647b72011-03-08 00:10:27 +0000117static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
119{
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000121 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 if (p->flags & XFRM_STATE_ESN) {
124 if (!rt)
125 return -EINVAL;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
135 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000136
Steffen Klassertd8647b72011-03-08 00:10:27 +0000137 if (!rt)
138 return 0;
139
Fan Du01714102014-01-18 09:54:28 +0800140 /* As only ESP and AH support ESN feature. */
141 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000142 return -EINVAL;
143
Steffen Klassertd8647b72011-03-08 00:10:27 +0000144 if (p->replay_window != 0)
145 return -EINVAL;
146
147 return 0;
148}
Trent Jaegerdf718372005-12-13 23:12:27 -0800149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700151 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int err;
154
155 err = -EINVAL;
156 switch (p->family) {
157 case AF_INET:
158 break;
159
160 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000161#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163#else
164 err = -EAFNOSUPPORT;
165 goto out;
166#endif
167
168 default:
169 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 err = -EINVAL;
173 switch (p->id.proto) {
174 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000175 if ((!attrs[XFRMA_ALG_AUTH] &&
176 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700178 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000179 attrs[XFRMA_ALG_COMP] ||
Fan Duea9884b2013-12-16 18:47:48 +0800180 attrs[XFRMA_TFCPAD] ||
181 (ntohl(p->id.spi) >= 0x10000))
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto out;
184 break;
185
186 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800187 if (attrs[XFRMA_ALG_COMP])
188 goto out;
189 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000190 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800191 !attrs[XFRMA_ALG_CRYPT] &&
192 !attrs[XFRMA_ALG_AEAD])
193 goto out;
194 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000195 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800196 attrs[XFRMA_ALG_CRYPT]) &&
197 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000199 if (attrs[XFRMA_TFCPAD] &&
200 p->mode != XFRM_MODE_TUNNEL)
201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
203
204 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700205 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800206 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000208 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000209 attrs[XFRMA_ALG_CRYPT] ||
210 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto out;
212 break;
213
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000214#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700215 case IPPROTO_DSTOPTS:
216 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700217 if (attrs[XFRMA_ALG_COMP] ||
218 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000219 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800220 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700221 attrs[XFRMA_ALG_CRYPT] ||
222 attrs[XFRMA_ENCAP] ||
223 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000224 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700226 goto out;
227 break;
228#endif
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 default:
231 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Herbert Xu1a6509d2008-01-28 19:37:29 -0800234 if ((err = verify_aead(attrs)))
235 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000236 if ((err = verify_auth_trunc(attrs)))
237 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800245 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000246 if ((err = verify_replay(p, attrs)))
247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 err = -EINVAL;
250 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700251 case XFRM_MODE_TRANSPORT:
252 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700253 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700254 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256
257 default:
258 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 err = 0;
262
263out:
264 return err;
265}
266
267static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800268 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700269 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct xfrm_algo *p, *ualg;
272 struct xfrm_algo_desc *algo;
273
274 if (!rta)
275 return 0;
276
Thomas Graf5424f322007-08-22 14:01:33 -0700277 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 algo = get_byname(ualg->alg_name, 1);
280 if (!algo)
281 return -ENOSYS;
282 *props = algo->desc.sadb_alg_id;
283
Eric Dumazet0f99be02008-01-08 23:39:06 -0800284 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!p)
286 return -ENOMEM;
287
Herbert Xu04ff1262006-08-13 08:50:00 +1000288 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *algpp = p;
290 return 0;
291}
292
Martin Willi4447bb32009-11-25 00:29:52 +0000293static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
294 struct nlattr *rta)
295{
296 struct xfrm_algo *ualg;
297 struct xfrm_algo_auth *p;
298 struct xfrm_algo_desc *algo;
299
300 if (!rta)
301 return 0;
302
303 ualg = nla_data(rta);
304
305 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
306 if (!algo)
307 return -ENOSYS;
308 *props = algo->desc.sadb_alg_id;
309
310 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
311 if (!p)
312 return -ENOMEM;
313
314 strcpy(p->alg_name, algo->name);
315 p->alg_key_len = ualg->alg_key_len;
316 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
317 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
318
319 *algpp = p;
320 return 0;
321}
322
323static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
324 struct nlattr *rta)
325{
326 struct xfrm_algo_auth *p, *ualg;
327 struct xfrm_algo_desc *algo;
328
329 if (!rta)
330 return 0;
331
332 ualg = nla_data(rta);
333
334 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
335 if (!algo)
336 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000337 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
338 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000339 return -EINVAL;
340 *props = algo->desc.sadb_alg_id;
341
342 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
343 if (!p)
344 return -ENOMEM;
345
346 strcpy(p->alg_name, algo->name);
347 if (!p->alg_trunc_len)
348 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
349
350 *algpp = p;
351 return 0;
352}
353
Herbert Xu1a6509d2008-01-28 19:37:29 -0800354static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
355 struct nlattr *rta)
356{
357 struct xfrm_algo_aead *p, *ualg;
358 struct xfrm_algo_desc *algo;
359
360 if (!rta)
361 return 0;
362
363 ualg = nla_data(rta);
364
365 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
366 if (!algo)
367 return -ENOSYS;
368 *props = algo->desc.sadb_alg_id;
369
370 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
371 if (!p)
372 return -ENOMEM;
373
374 strcpy(p->alg_name, algo->name);
375 *algpp = p;
376 return 0;
377}
378
Steffen Klasserte2b19122011-03-28 19:47:30 +0000379static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
380 struct nlattr *rp)
381{
382 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000383 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000384
385 if (!replay_esn || !rp)
386 return 0;
387
388 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000389 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000390
Mathias Krauseecd79182012-09-20 10:01:49 +0000391 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000392 return -EINVAL;
393
394 return 0;
395}
396
Steffen Klassertd8647b72011-03-08 00:10:27 +0000397static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
398 struct xfrm_replay_state_esn **preplay_esn,
399 struct nlattr *rta)
400{
401 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000402 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000403
404 if (!rta)
405 return 0;
406
407 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000408 klen = xfrm_replay_state_esn_len(up);
409 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000410
Mathias Krauseecd79182012-09-20 10:01:49 +0000411 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412 if (!p)
413 return -ENOMEM;
414
Mathias Krauseecd79182012-09-20 10:01:49 +0000415 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000416 if (!pp) {
417 kfree(p);
418 return -ENOMEM;
419 }
420
Mathias Krauseecd79182012-09-20 10:01:49 +0000421 memcpy(p, up, ulen);
422 memcpy(pp, up, ulen);
423
Steffen Klassertd8647b72011-03-08 00:10:27 +0000424 *replay_esn = p;
425 *preplay_esn = pp;
426
427 return 0;
428}
429
Joy Latten661697f2007-04-13 16:14:35 -0700430static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800431{
Trent Jaegerdf718372005-12-13 23:12:27 -0800432 int len = 0;
433
434 if (xfrm_ctx) {
435 len += sizeof(struct xfrm_user_sec_ctx);
436 len += xfrm_ctx->ctx_len;
437 }
438 return len;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
442{
443 memcpy(&x->id, &p->id, sizeof(x->id));
444 memcpy(&x->sel, &p->sel, sizeof(x->sel));
445 memcpy(&x->lft, &p->lft, sizeof(x->lft));
446 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800447 x->props.replay_window = min_t(unsigned int, p->replay_window,
448 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 x->props.reqid = p->reqid;
450 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700451 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700453
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700454 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700455 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800458/*
459 * someday when pfkey also has support, we could have the code
460 * somehow made shareable and move it to xfrm_state.c - JHS
461 *
462*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000463static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
464 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800465{
Thomas Graf5424f322007-08-22 14:01:33 -0700466 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000467 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700468 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
469 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
470 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800471
Steffen Klassertd8647b72011-03-08 00:10:27 +0000472 if (re) {
473 struct xfrm_replay_state_esn *replay_esn;
474 replay_esn = nla_data(re);
475 memcpy(x->replay_esn, replay_esn,
476 xfrm_replay_state_esn_len(replay_esn));
477 memcpy(x->preplay_esn, replay_esn,
478 xfrm_replay_state_esn_len(replay_esn));
479 }
480
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800481 if (rp) {
482 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700483 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800484 memcpy(&x->replay, replay, sizeof(*replay));
485 memcpy(&x->preplay, replay, sizeof(*replay));
486 }
487
488 if (lt) {
489 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700490 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800491 x->curlft.bytes = ltime->bytes;
492 x->curlft.packets = ltime->packets;
493 x->curlft.add_time = ltime->add_time;
494 x->curlft.use_time = ltime->use_time;
495 }
496
Thomas Grafcf5cb792007-08-22 13:59:04 -0700497 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700498 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800499
Thomas Grafcf5cb792007-08-22 13:59:04 -0700500 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700501 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800502}
503
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800504static struct xfrm_state *xfrm_state_construct(struct net *net,
505 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700506 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int *errp)
508{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800509 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int err = -ENOMEM;
511
512 if (!x)
513 goto error_no_put;
514
515 copy_from_user_state(x, p);
516
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100517 if (attrs[XFRMA_SA_EXTRA_FLAGS])
518 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
519
Herbert Xu1a6509d2008-01-28 19:37:29 -0800520 if ((err = attach_aead(&x->aead, &x->props.ealgo,
521 attrs[XFRMA_ALG_AEAD])))
522 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000523 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
524 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000526 if (!x->props.aalgo) {
527 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
528 attrs[XFRMA_ALG_AUTH])))
529 goto error;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
532 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700533 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto error;
535 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
536 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700537 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700539
540 if (attrs[XFRMA_ENCAP]) {
541 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
542 sizeof(*x->encap), GFP_KERNEL);
543 if (x->encap == NULL)
544 goto error;
545 }
546
Martin Willi35d28562010-12-08 04:37:49 +0000547 if (attrs[XFRMA_TFCPAD])
548 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
549
Thomas Graffd211502007-09-06 03:28:08 -0700550 if (attrs[XFRMA_COADDR]) {
551 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
552 sizeof(*x->coaddr), GFP_KERNEL);
553 if (x->coaddr == NULL)
554 goto error;
555 }
556
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000557 xfrm_mark_get(attrs, &x->mark);
558
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700559 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (err)
561 goto error;
562
Thomas Graffd211502007-09-06 03:28:08 -0700563 if (attrs[XFRMA_SEC_CTX] &&
564 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800565 goto error;
566
Steffen Klassertd8647b72011-03-08 00:10:27 +0000567 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
568 attrs[XFRMA_REPLAY_ESN_VAL])))
569 goto error;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800572 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800573 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800574 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800575
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000576 if ((err = xfrm_init_replay(x)))
577 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800578
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000579 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000580 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return x;
583
584error:
585 x->km.state = XFRM_STATE_DEAD;
586 xfrm_state_put(x);
587error_no_put:
588 *errp = err;
589 return NULL;
590}
591
Christoph Hellwig22e70052007-01-02 15:22:30 -0800592static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700593 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800595 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700596 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct xfrm_state *x;
598 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700599 struct km_event c;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700600 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500601 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800602 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Thomas Graf35a7aa02007-08-22 14:00:40 -0700604 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (err)
606 return err;
607
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800608 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!x)
610 return err;
611
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700612 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
614 err = xfrm_state_add(x);
615 else
616 err = xfrm_state_update(x);
617
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800618 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400619 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (err < 0) {
622 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800623 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700624 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700627 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000628 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700629 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700630
631 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700632out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700633 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return err;
635}
636
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800637static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
638 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700639 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700640 int *errp)
641{
642 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000643 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700644 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000645 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700646
647 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
648 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000649 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700650 } else {
651 xfrm_address_t *saddr = NULL;
652
Thomas Graf35a7aa02007-08-22 14:00:40 -0700653 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700654 if (!saddr) {
655 err = -EINVAL;
656 goto out;
657 }
658
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800659 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000660 x = xfrm_state_lookup_byaddr(net, mark,
661 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800662 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700663 }
664
665 out:
666 if (!x && errp)
667 *errp = err;
668 return x;
669}
670
Christoph Hellwig22e70052007-01-02 15:22:30 -0800671static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700672 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800674 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700676 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700677 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700678 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700679 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -0500680 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800681 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800683 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
David S. Miller6f68dc32006-06-08 23:58:52 -0700687 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700688 goto out;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700691 err = -EPERM;
692 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700695 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600696
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700697 if (err < 0)
698 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700699
700 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000701 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700702 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700703 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700705out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800706 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400707 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700708 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
713{
Mathias Krausef778a632012-09-19 11:33:39 +0000714 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 memcpy(&p->id, &x->id, sizeof(p->id));
716 memcpy(&p->sel, &x->sel, sizeof(p->sel));
717 memcpy(&p->lft, &x->lft, sizeof(p->lft));
718 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
719 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700720 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 p->mode = x->props.mode;
722 p->replay_window = x->props.replay_window;
723 p->reqid = x->props.reqid;
724 p->family = x->props.family;
725 p->flags = x->props.flags;
726 p->seq = x->km.seq;
727}
728
729struct xfrm_dump_info {
730 struct sk_buff *in_skb;
731 struct sk_buff *out_skb;
732 u32 nlmsg_seq;
733 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734};
735
Thomas Grafc0144be2007-08-22 13:55:43 -0700736static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
737{
Thomas Grafc0144be2007-08-22 13:55:43 -0700738 struct xfrm_user_sec_ctx *uctx;
739 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700740 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700741
742 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
743 if (attr == NULL)
744 return -EMSGSIZE;
745
746 uctx = nla_data(attr);
747 uctx->exttype = XFRMA_SEC_CTX;
748 uctx->len = ctx_size;
749 uctx->ctx_doi = s->ctx_doi;
750 uctx->ctx_alg = s->ctx_alg;
751 uctx->ctx_len = s->ctx_len;
752 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
753
754 return 0;
755}
756
Martin Willi4447bb32009-11-25 00:29:52 +0000757static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
758{
759 struct xfrm_algo *algo;
760 struct nlattr *nla;
761
762 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
763 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
764 if (!nla)
765 return -EMSGSIZE;
766
767 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000768 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000769 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
770 algo->alg_key_len = auth->alg_key_len;
771
772 return 0;
773}
774
Herbert Xu68325d32007-10-09 13:30:57 -0700775/* Don't change this without updating xfrm_sa_len! */
776static int copy_to_user_state_extra(struct xfrm_state *x,
777 struct xfrm_usersa_info *p,
778 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700780 int ret = 0;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 copy_to_user_state(x, p);
783
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100784 if (x->props.extra_flags) {
785 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
786 x->props.extra_flags);
787 if (ret)
788 goto out;
789 }
790
David S. Miller1d1e34d2012-06-27 21:57:03 -0700791 if (x->coaddr) {
792 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
793 if (ret)
794 goto out;
795 }
796 if (x->lastused) {
797 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
798 if (ret)
799 goto out;
800 }
801 if (x->aead) {
802 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
803 if (ret)
804 goto out;
805 }
806 if (x->aalg) {
807 ret = copy_to_user_auth(x->aalg, skb);
808 if (!ret)
809 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
810 xfrm_alg_auth_len(x->aalg), x->aalg);
811 if (ret)
812 goto out;
813 }
814 if (x->ealg) {
815 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
816 if (ret)
817 goto out;
818 }
819 if (x->calg) {
820 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
821 if (ret)
822 goto out;
823 }
824 if (x->encap) {
825 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
826 if (ret)
827 goto out;
828 }
829 if (x->tfcpad) {
830 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
831 if (ret)
832 goto out;
833 }
834 ret = xfrm_mark_put(skb, &x->mark);
835 if (ret)
836 goto out;
837 if (x->replay_esn) {
838 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
839 xfrm_replay_state_esn_len(x->replay_esn),
840 x->replay_esn);
841 if (ret)
842 goto out;
843 }
844 if (x->security)
845 ret = copy_sec_ctx(x->security, skb);
846out:
847 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700848}
849
850static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
851{
852 struct xfrm_dump_info *sp = ptr;
853 struct sk_buff *in_skb = sp->in_skb;
854 struct sk_buff *skb = sp->out_skb;
855 struct xfrm_usersa_info *p;
856 struct nlmsghdr *nlh;
857 int err;
858
Eric W. Biederman15e47302012-09-07 20:12:54 +0000859 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700860 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
861 if (nlh == NULL)
862 return -EMSGSIZE;
863
864 p = nlmsg_data(nlh);
865
866 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700867 if (err) {
868 nlmsg_cancel(skb, nlh);
869 return err;
870 }
Thomas Graf98250692007-08-22 12:47:26 -0700871 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Timo Teras4c563f72008-02-28 21:31:08 -0800875static int xfrm_dump_sa_done(struct netlink_callback *cb)
876{
877 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800878 struct sock *sk = cb->skb->sk;
879 struct net *net = sock_net(sk);
880
881 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800882 return 0;
883}
884
Nicolas Dichteld3623092014-02-14 15:30:36 +0100885static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
887{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800888 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800889 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct xfrm_dump_info info;
891
Timo Teras4c563f72008-02-28 21:31:08 -0800892 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
893 sizeof(cb->args) - sizeof(cb->args[0]));
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 info.in_skb = cb->skb;
896 info.out_skb = skb;
897 info.nlmsg_seq = cb->nlh->nlmsg_seq;
898 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800899
900 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100901 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100902 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100903 u8 proto = 0;
904 int err;
905
Timo Teras4c563f72008-02-28 21:31:08 -0800906 cb->args[0] = 1;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100907
908 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
909 xfrma_policy);
910 if (err < 0)
911 return err;
912
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100913 if (attrs[XFRMA_ADDRESS_FILTER]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100914 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
915 if (filter == NULL)
916 return -ENOMEM;
917
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100918 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
Nicolas Dichteld3623092014-02-14 15:30:36 +0100919 sizeof(*filter));
920 }
921
922 if (attrs[XFRMA_PROTO])
923 proto = nla_get_u8(attrs[XFRMA_PROTO]);
924
925 xfrm_state_walk_init(walk, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -0800926 }
927
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800928 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 return skb->len;
931}
932
933static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
934 struct xfrm_state *x, u32 seq)
935{
936 struct xfrm_dump_info info;
937 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000938 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Thomas Graf7deb2262007-08-22 13:57:39 -0700940 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!skb)
942 return ERR_PTR(-ENOMEM);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 info.in_skb = in_skb;
945 info.out_skb = skb;
946 info.nlmsg_seq = seq;
947 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Mathias Krause864745d2012-09-13 11:41:26 +0000949 err = dump_one_state(x, 0, &info);
950 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000952 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954
955 return skb;
956}
957
Thomas Graf7deb2262007-08-22 13:57:39 -0700958static inline size_t xfrm_spdinfo_msgsize(void)
959{
960 return NLMSG_ALIGN(4)
961 + nla_total_size(sizeof(struct xfrmu_spdinfo))
962 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
963}
964
Alexey Dobriyane0710412010-01-23 13:37:10 +0000965static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000966 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700967{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700968 struct xfrmk_spdinfo si;
969 struct xfrmu_spdinfo spc;
970 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700971 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700972 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700973 u32 *f;
974
Eric W. Biederman15e47302012-09-07 20:12:54 +0000975 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300976 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700977 return -EMSGSIZE;
978
979 f = nlmsg_data(nlh);
980 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000981 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700982 spc.incnt = si.incnt;
983 spc.outcnt = si.outcnt;
984 spc.fwdcnt = si.fwdcnt;
985 spc.inscnt = si.inscnt;
986 spc.outscnt = si.outscnt;
987 spc.fwdscnt = si.fwdscnt;
988 sph.spdhcnt = si.spdhcnt;
989 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700990
David S. Miller1d1e34d2012-06-27 21:57:03 -0700991 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
992 if (!err)
993 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
994 if (err) {
995 nlmsg_cancel(skb, nlh);
996 return err;
997 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700998
999 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001000}
1001
1002static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001003 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001004{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001005 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001006 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001007 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001008 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001009 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001010
Thomas Graf7deb2262007-08-22 13:57:39 -07001011 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001012 if (r_skb == NULL)
1013 return -ENOMEM;
1014
Eric W. Biederman15e47302012-09-07 20:12:54 +00001015 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001016 BUG();
1017
Eric W. Biederman15e47302012-09-07 20:12:54 +00001018 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001019}
1020
Thomas Graf7deb2262007-08-22 13:57:39 -07001021static inline size_t xfrm_sadinfo_msgsize(void)
1022{
1023 return NLMSG_ALIGN(4)
1024 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1025 + nla_total_size(4); /* XFRMA_SAD_CNT */
1026}
1027
Alexey Dobriyane0710412010-01-23 13:37:10 +00001028static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001029 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001030{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001031 struct xfrmk_sadinfo si;
1032 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001033 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001034 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001035 u32 *f;
1036
Eric W. Biederman15e47302012-09-07 20:12:54 +00001037 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001038 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001039 return -EMSGSIZE;
1040
1041 f = nlmsg_data(nlh);
1042 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001043 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001044
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001045 sh.sadhmcnt = si.sadhmcnt;
1046 sh.sadhcnt = si.sadhcnt;
1047
David S. Miller1d1e34d2012-06-27 21:57:03 -07001048 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1049 if (!err)
1050 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1051 if (err) {
1052 nlmsg_cancel(skb, nlh);
1053 return err;
1054 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001055
1056 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001057}
1058
1059static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001060 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001061{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001062 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001063 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001064 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001065 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001066 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001067
Thomas Graf7deb2262007-08-22 13:57:39 -07001068 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001069 if (r_skb == NULL)
1070 return -ENOMEM;
1071
Eric W. Biederman15e47302012-09-07 20:12:54 +00001072 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001073 BUG();
1074
Eric W. Biederman15e47302012-09-07 20:12:54 +00001075 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001076}
1077
Christoph Hellwig22e70052007-01-02 15:22:30 -08001078static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001079 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001081 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001082 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct xfrm_state *x;
1084 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001085 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001087 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (x == NULL)
1089 goto out_noput;
1090
1091 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1092 if (IS_ERR(resp_skb)) {
1093 err = PTR_ERR(resp_skb);
1094 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001095 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 xfrm_state_put(x);
1098out_noput:
1099 return err;
1100}
1101
Christoph Hellwig22e70052007-01-02 15:22:30 -08001102static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001103 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001105 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct xfrm_state *x;
1107 struct xfrm_userspi_info *p;
1108 struct sk_buff *resp_skb;
1109 xfrm_address_t *daddr;
1110 int family;
1111 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001112 u32 mark;
1113 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Thomas Graf7b67c852007-08-22 13:53:52 -07001115 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001116 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (err)
1118 goto out_noput;
1119
1120 family = p->info.family;
1121 daddr = &p->info.id.daddr;
1122
1123 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001124
1125 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001127 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001128 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 xfrm_state_put(x);
1130 x = NULL;
1131 }
1132 }
1133
1134 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001135 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 p->info.id.proto, daddr,
1137 &p->info.saddr, 1,
1138 family);
1139 err = -ENOENT;
1140 if (x == NULL)
1141 goto out_noput;
1142
Herbert Xu658b2192007-10-09 13:29:52 -07001143 err = xfrm_alloc_spi(x, p->min, p->max);
1144 if (err)
1145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Herbert Xu658b2192007-10-09 13:29:52 -07001147 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (IS_ERR(resp_skb)) {
1149 err = PTR_ERR(resp_skb);
1150 goto out;
1151 }
1152
Eric W. Biederman15e47302012-09-07 20:12:54 +00001153 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155out:
1156 xfrm_state_put(x);
1157out_noput:
1158 return err;
1159}
1160
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001161static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 switch (dir) {
1164 case XFRM_POLICY_IN:
1165 case XFRM_POLICY_OUT:
1166 case XFRM_POLICY_FWD:
1167 break;
1168
1169 default:
1170 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 return 0;
1174}
1175
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001176static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001177{
1178 switch (type) {
1179 case XFRM_POLICY_TYPE_MAIN:
1180#ifdef CONFIG_XFRM_SUB_POLICY
1181 case XFRM_POLICY_TYPE_SUB:
1182#endif
1183 break;
1184
1185 default:
1186 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001187 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001188
1189 return 0;
1190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1193{
Fan Due682adf2013-11-07 17:47:48 +08001194 int ret;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 switch (p->share) {
1197 case XFRM_SHARE_ANY:
1198 case XFRM_SHARE_SESSION:
1199 case XFRM_SHARE_USER:
1200 case XFRM_SHARE_UNIQUE:
1201 break;
1202
1203 default:
1204 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 switch (p->action) {
1208 case XFRM_POLICY_ALLOW:
1209 case XFRM_POLICY_BLOCK:
1210 break;
1211
1212 default:
1213 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 switch (p->sel.family) {
1217 case AF_INET:
1218 break;
1219
1220 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001221#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223#else
1224 return -EAFNOSUPPORT;
1225#endif
1226
1227 default:
1228 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Fan Due682adf2013-11-07 17:47:48 +08001231 ret = verify_policy_dir(p->dir);
1232 if (ret)
1233 return ret;
1234 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1235 return -EINVAL;
1236
1237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Thomas Graf5424f322007-08-22 14:01:33 -07001240static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001241{
Thomas Graf5424f322007-08-22 14:01:33 -07001242 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001243 struct xfrm_user_sec_ctx *uctx;
1244
1245 if (!rt)
1246 return 0;
1247
Thomas Graf5424f322007-08-22 14:01:33 -07001248 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001249 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1253 int nr)
1254{
1255 int i;
1256
1257 xp->xfrm_nr = nr;
1258 for (i = 0; i < nr; i++, ut++) {
1259 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1260
1261 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1262 memcpy(&t->saddr, &ut->saddr,
1263 sizeof(xfrm_address_t));
1264 t->reqid = ut->reqid;
1265 t->mode = ut->mode;
1266 t->share = ut->share;
1267 t->optional = ut->optional;
1268 t->aalgos = ut->aalgos;
1269 t->ealgos = ut->ealgos;
1270 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001271 /* If all masks are ~0, then we allow all algorithms. */
1272 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001273 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275}
1276
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001277static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1278{
1279 int i;
1280
1281 if (nr > XFRM_MAX_DEPTH)
1282 return -EINVAL;
1283
1284 for (i = 0; i < nr; i++) {
1285 /* We never validated the ut->family value, so many
1286 * applications simply leave it at zero. The check was
1287 * never made and ut->family was ignored because all
1288 * templates could be assumed to have the same family as
1289 * the policy itself. Now that we will have ipv4-in-ipv6
1290 * and ipv6-in-ipv4 tunnels, this is no longer true.
1291 */
1292 if (!ut[i].family)
1293 ut[i].family = family;
1294
1295 switch (ut[i].family) {
1296 case AF_INET:
1297 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001298#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001299 case AF_INET6:
1300 break;
1301#endif
1302 default:
1303 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001304 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001305 }
1306
1307 return 0;
1308}
1309
Thomas Graf5424f322007-08-22 14:01:33 -07001310static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Thomas Graf5424f322007-08-22 14:01:33 -07001312 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 if (!rt) {
1315 pol->xfrm_nr = 0;
1316 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001317 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1318 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001321 err = validate_tmpl(nr, utmpl, pol->family);
1322 if (err)
1323 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Thomas Graf5424f322007-08-22 14:01:33 -07001325 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 return 0;
1328}
1329
Thomas Graf5424f322007-08-22 14:01:33 -07001330static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001331{
Thomas Graf5424f322007-08-22 14:01:33 -07001332 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001333 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001334 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001335 int err;
1336
1337 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001338 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001339 type = upt->type;
1340 }
1341
1342 err = verify_policy_type(type);
1343 if (err)
1344 return err;
1345
1346 *tp = type;
1347 return 0;
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1351{
1352 xp->priority = p->priority;
1353 xp->index = p->index;
1354 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1355 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1356 xp->action = p->action;
1357 xp->flags = p->flags;
1358 xp->family = p->sel.family;
1359 /* XXX xp->share = p->share; */
1360}
1361
1362static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1363{
Mathias Krause7b789832012-09-19 11:33:40 +00001364 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1366 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1367 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1368 p->priority = xp->priority;
1369 p->index = xp->index;
1370 p->sel.family = xp->family;
1371 p->dir = dir;
1372 p->action = xp->action;
1373 p->flags = xp->flags;
1374 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1375}
1376
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001377static 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 -07001378{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001379 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 int err;
1381
1382 if (!xp) {
1383 *errp = -ENOMEM;
1384 return NULL;
1385 }
1386
1387 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001388
Thomas Graf35a7aa02007-08-22 14:00:40 -07001389 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001390 if (err)
1391 goto error;
1392
Thomas Graf35a7aa02007-08-22 14:00:40 -07001393 if (!(err = copy_from_user_tmpl(xp, attrs)))
1394 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001395 if (err)
1396 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001398 xfrm_mark_get(attrs, &xp->mark);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001401 error:
1402 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001403 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001404 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001405 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Christoph Hellwig22e70052007-01-02 15:22:30 -08001408static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001409 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001411 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001412 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001414 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 int err;
1416 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001417 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001418 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001419 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 err = verify_newpolicy_info(p);
1422 if (err)
1423 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001424 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001425 if (err)
1426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001428 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (!xp)
1430 return err;
1431
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001432 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001433 * Aha! this is anti-netlink really i.e more pfkey derived
1434 * in netlink excl is a flag and you wouldnt need
1435 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1437 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001438 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001439 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001442 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 kfree(xp);
1444 return err;
1445 }
1446
Herbert Xuf60f6b82005-06-18 22:44:37 -07001447 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001448 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001449 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001450 km_policy_notify(xp, p->dir, &c);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 xfrm_pol_put(xp);
1453
1454 return 0;
1455}
1456
1457static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1458{
1459 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1460 int i;
1461
1462 if (xp->xfrm_nr == 0)
1463 return 0;
1464
1465 for (i = 0; i < xp->xfrm_nr; i++) {
1466 struct xfrm_user_tmpl *up = &vec[i];
1467 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1468
Mathias Krause1f868402012-09-19 11:33:41 +00001469 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001471 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1473 up->reqid = kp->reqid;
1474 up->mode = kp->mode;
1475 up->share = kp->share;
1476 up->optional = kp->optional;
1477 up->aalgos = kp->aalgos;
1478 up->ealgos = kp->ealgos;
1479 up->calgos = kp->calgos;
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Thomas Grafc0144be2007-08-22 13:55:43 -07001482 return nla_put(skb, XFRMA_TMPL,
1483 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001484}
1485
Serge Hallyn0d681622006-07-24 23:30:44 -07001486static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1487{
1488 if (x->security) {
1489 return copy_sec_ctx(x->security, skb);
1490 }
1491 return 0;
1492}
1493
1494static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1495{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001496 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001497 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001498 return 0;
1499}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001500static inline size_t userpolicy_type_attrsize(void)
1501{
1502#ifdef CONFIG_XFRM_SUB_POLICY
1503 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1504#else
1505 return 0;
1506#endif
1507}
Serge Hallyn0d681622006-07-24 23:30:44 -07001508
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001509#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001510static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001511{
Thomas Grafc0144be2007-08-22 13:55:43 -07001512 struct xfrm_userpolicy_type upt = {
1513 .type = type,
1514 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001515
Thomas Grafc0144be2007-08-22 13:55:43 -07001516 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001517}
1518
1519#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001520static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001521{
1522 return 0;
1523}
1524#endif
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1527{
1528 struct xfrm_dump_info *sp = ptr;
1529 struct xfrm_userpolicy_info *p;
1530 struct sk_buff *in_skb = sp->in_skb;
1531 struct sk_buff *skb = sp->out_skb;
1532 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001533 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Eric W. Biederman15e47302012-09-07 20:12:54 +00001535 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001536 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1537 if (nlh == NULL)
1538 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Thomas Graf7b67c852007-08-22 13:53:52 -07001540 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001542 err = copy_to_user_tmpl(xp, skb);
1543 if (!err)
1544 err = copy_to_user_sec_ctx(xp, skb);
1545 if (!err)
1546 err = copy_to_user_policy_type(xp->type, skb);
1547 if (!err)
1548 err = xfrm_mark_put(skb, &xp->mark);
1549 if (err) {
1550 nlmsg_cancel(skb, nlh);
1551 return err;
1552 }
Thomas Graf98250692007-08-22 12:47:26 -07001553 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Timo Teras4c563f72008-02-28 21:31:08 -08001557static int xfrm_dump_policy_done(struct netlink_callback *cb)
1558{
1559 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001560 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001561
Fan Du283bc9f2013-11-07 17:47:50 +08001562 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001563 return 0;
1564}
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1567{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001568 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001569 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 struct xfrm_dump_info info;
1571
Timo Teras4c563f72008-02-28 21:31:08 -08001572 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1573 sizeof(cb->args) - sizeof(cb->args[0]));
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 info.in_skb = cb->skb;
1576 info.out_skb = skb;
1577 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1578 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001579
1580 if (!cb->args[0]) {
1581 cb->args[0] = 1;
1582 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1583 }
1584
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001585 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 return skb->len;
1588}
1589
1590static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1591 struct xfrm_policy *xp,
1592 int dir, u32 seq)
1593{
1594 struct xfrm_dump_info info;
1595 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001596 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Thomas Graf7deb2262007-08-22 13:57:39 -07001598 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (!skb)
1600 return ERR_PTR(-ENOMEM);
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 info.in_skb = in_skb;
1603 info.out_skb = skb;
1604 info.nlmsg_seq = seq;
1605 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Mathias Krausec2546372012-09-14 09:58:32 +00001607 err = dump_one_policy(xp, dir, 0, &info);
1608 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001610 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612
1613 return skb;
1614}
1615
Christoph Hellwig22e70052007-01-02 15:22:30 -08001616static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001617 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001619 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct xfrm_policy *xp;
1621 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001622 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001624 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001626 struct xfrm_mark m;
1627 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Thomas Graf7b67c852007-08-22 13:53:52 -07001629 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1631
Thomas Graf35a7aa02007-08-22 14:00:40 -07001632 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001633 if (err)
1634 return err;
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 err = verify_policy_dir(p->dir);
1637 if (err)
1638 return err;
1639
1640 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001641 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001642 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001643 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001644 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001645
Thomas Graf35a7aa02007-08-22 14:00:40 -07001646 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001647 if (err)
1648 return err;
1649
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001650 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001651 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001652 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001653
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001654 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001655 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001656 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001657 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001658 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001659 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001660 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (xp == NULL)
1663 return -ENOENT;
1664
1665 if (!delete) {
1666 struct sk_buff *resp_skb;
1667
1668 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1669 if (IS_ERR(resp_skb)) {
1670 err = PTR_ERR(resp_skb);
1671 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001672 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001673 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001675 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001676 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001677 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001678 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001679
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001680 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001681 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1682 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001683
1684 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001685 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001686
Herbert Xue7443892005-06-18 22:44:18 -07001687 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001688 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001689 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001690 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001691 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001694out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001695 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001696 if (delete && err == 0)
1697 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return err;
1699}
1700
Christoph Hellwig22e70052007-01-02 15:22:30 -08001701static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001702 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001704 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001705 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001706 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001707 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001708 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001710 audit_info.loginuid = audit_get_loginuid(current);
1711 audit_info.sessionid = audit_get_sessionid(current);
1712 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001713 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001714 if (err) {
1715 if (err == -ESRCH) /* empty table */
1716 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001717 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001718 }
Herbert Xubf088672005-06-18 22:44:00 -07001719 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001720 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001721 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001722 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001723 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724 km_state_notify(NULL, &c);
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 0;
1727}
1728
Steffen Klassertd8647b72011-03-08 00:10:27 +00001729static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001730{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001731 size_t replay_size = x->replay_esn ?
1732 xfrm_replay_state_esn_len(x->replay_esn) :
1733 sizeof(struct xfrm_replay_state);
1734
Thomas Graf7deb2262007-08-22 13:57:39 -07001735 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001736 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001737 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001738 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001739 + nla_total_size(4) /* XFRM_AE_RTHR */
1740 + nla_total_size(4); /* XFRM_AE_ETHR */
1741}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001742
David S. Miller214e0052011-02-24 00:02:38 -05001743static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001744{
1745 struct xfrm_aevent_id *id;
1746 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001747 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001748
Eric W. Biederman15e47302012-09-07 20:12:54 +00001749 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001750 if (nlh == NULL)
1751 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001752
Thomas Graf7b67c852007-08-22 13:53:52 -07001753 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001754 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001755 id->sa_id.spi = x->id.spi;
1756 id->sa_id.family = x->props.family;
1757 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001758 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001759 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001760 id->flags = c->data.aevent;
1761
David S. Millerd0fde792012-03-29 04:02:26 -04001762 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001763 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1764 xfrm_replay_state_esn_len(x->replay_esn),
1765 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001766 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001767 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1768 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001769 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001770 if (err)
1771 goto out_cancel;
1772 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1773 if (err)
1774 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001775
David S. Miller1d1e34d2012-06-27 21:57:03 -07001776 if (id->flags & XFRM_AE_RTHR) {
1777 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1778 if (err)
1779 goto out_cancel;
1780 }
1781 if (id->flags & XFRM_AE_ETHR) {
1782 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1783 x->replay_maxage * 10 / HZ);
1784 if (err)
1785 goto out_cancel;
1786 }
1787 err = xfrm_mark_put(skb, &x->mark);
1788 if (err)
1789 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001790
Thomas Graf98250692007-08-22 12:47:26 -07001791 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001792
David S. Miller1d1e34d2012-06-27 21:57:03 -07001793out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001794 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001795 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001796}
1797
Christoph Hellwig22e70052007-01-02 15:22:30 -08001798static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001799 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001800{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001801 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001802 struct xfrm_state *x;
1803 struct sk_buff *r_skb;
1804 int err;
1805 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001806 u32 mark;
1807 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001808 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001809 struct xfrm_usersa_id *id = &p->sa_id;
1810
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001811 mark = xfrm_mark_get(attrs, &m);
1812
1813 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001814 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001815 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001816
1817 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1818 if (r_skb == NULL) {
1819 xfrm_state_put(x);
1820 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001821 }
1822
1823 /*
1824 * XXX: is this lock really needed - none of the other
1825 * gets lock (the concern is things getting updated
1826 * while we are still reading) - jhs
1827 */
1828 spin_lock_bh(&x->lock);
1829 c.data.aevent = p->flags;
1830 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001831 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001832
1833 if (build_aevent(r_skb, x, &c) < 0)
1834 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001835 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001836 spin_unlock_bh(&x->lock);
1837 xfrm_state_put(x);
1838 return err;
1839}
1840
Christoph Hellwig22e70052007-01-02 15:22:30 -08001841static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001842 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001843{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001844 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001845 struct xfrm_state *x;
1846 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001847 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001848 u32 mark = 0;
1849 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001850 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001851 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001852 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001853 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001854
Steffen Klassertd8647b72011-03-08 00:10:27 +00001855 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001856 return err;
1857
1858 /* pedantic mode - thou shalt sayeth replaceth */
1859 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1860 return err;
1861
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001862 mark = xfrm_mark_get(attrs, &m);
1863
1864 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 -08001865 if (x == NULL)
1866 return -ESRCH;
1867
1868 if (x->km.state != XFRM_STATE_VALID)
1869 goto out;
1870
Steffen Klassert4479ff72013-09-09 09:39:01 +02001871 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001872 if (err)
1873 goto out;
1874
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001875 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001876 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001877 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001878
1879 c.event = nlh->nlmsg_type;
1880 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001881 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001882 c.data.aevent = XFRM_AE_CU;
1883 km_state_notify(x, &c);
1884 err = 0;
1885out:
1886 xfrm_state_put(x);
1887 return err;
1888}
1889
Christoph Hellwig22e70052007-01-02 15:22:30 -08001890static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001891 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001893 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001894 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001895 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001896 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001897 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001898
Thomas Graf35a7aa02007-08-22 14:00:40 -07001899 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001900 if (err)
1901 return err;
1902
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001903 audit_info.loginuid = audit_get_loginuid(current);
1904 audit_info.sessionid = audit_get_sessionid(current);
1905 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001906 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001907 if (err) {
1908 if (err == -ESRCH) /* empty table */
1909 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001910 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001911 }
1912
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001913 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001914 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001915 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001916 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001917 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001918 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
1920}
1921
Christoph Hellwig22e70052007-01-02 15:22:30 -08001922static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001923 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001924{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001925 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001926 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001927 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001928 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001929 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001931 struct xfrm_mark m;
1932 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001933
Thomas Graf35a7aa02007-08-22 14:00:40 -07001934 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001935 if (err)
1936 return err;
1937
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001938 err = verify_policy_dir(p->dir);
1939 if (err)
1940 return err;
1941
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001942 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001943 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001945 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001946 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001947
Thomas Graf35a7aa02007-08-22 14:00:40 -07001948 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001949 if (err)
1950 return err;
1951
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001952 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001953 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001954 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001955
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001956 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001957 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001959 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001960 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001961 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001962 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001963 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001964 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001965 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001966
Timo Teräsea2dea92010-03-31 00:17:05 +00001967 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001968 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001969
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001970 err = 0;
1971 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001972 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001973 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001974 u32 sid;
1975
1976 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001977 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001978 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001979
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001980 } else {
1981 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001982 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001983 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001984 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001985
1986out:
1987 xfrm_pol_put(xp);
1988 return err;
1989}
1990
Christoph Hellwig22e70052007-01-02 15:22:30 -08001991static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001992 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001993{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001994 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001995 struct xfrm_state *x;
1996 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001997 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001998 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001999 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002000 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002001
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002002 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 -08002003
David S. Miller3a765aa2007-02-26 14:52:21 -08002004 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002005 if (x == NULL)
2006 return err;
2007
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002008 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002009 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002010 if (x->km.state != XFRM_STATE_VALID)
2011 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002012 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002013
Joy Latten161a09e2006-11-27 13:11:54 -06002014 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002015 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05002016 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08002017 u32 sid;
2018
2019 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002020 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002021 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002022 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002023 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002024out:
2025 spin_unlock_bh(&x->lock);
2026 xfrm_state_put(x);
2027 return err;
2028}
2029
Christoph Hellwig22e70052007-01-02 15:22:30 -08002030static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002031 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002032{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002033 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002034 struct xfrm_policy *xp;
2035 struct xfrm_user_tmpl *ut;
2036 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002037 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002038 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002039
Thomas Graf7b67c852007-08-22 13:53:52 -07002040 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002041 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002042 int err = -ENOMEM;
2043
2044 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002045 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002046
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002047 xfrm_mark_get(attrs, &mark);
2048
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002049 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002050 if (err)
2051 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002052
2053 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002054 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002055 if (!xp)
2056 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002057
2058 memcpy(&x->id, &ua->id, sizeof(ua->id));
2059 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2060 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002061 xp->mark.m = x->mark.m = mark.m;
2062 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002063 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002064 /* extract the templates and for each call km_key */
2065 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2066 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2067 memcpy(&x->id, &t->id, sizeof(x->id));
2068 x->props.mode = t->mode;
2069 x->props.reqid = t->reqid;
2070 x->props.family = ut->family;
2071 t->aalgos = ua->aalgos;
2072 t->ealgos = ua->ealgos;
2073 t->calgos = ua->calgos;
2074 err = km_query(x, t, xp);
2075
2076 }
2077
2078 kfree(x);
2079 kfree(xp);
2080
2081 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002082
2083bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002084 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002085free_state:
2086 kfree(x);
2087nomem:
2088 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002089}
2090
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002091#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002092static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002093 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002094 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002095{
Thomas Graf5424f322007-08-22 14:01:33 -07002096 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002097 struct xfrm_user_migrate *um;
2098 int i, num_migrate;
2099
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002100 if (k != NULL) {
2101 struct xfrm_user_kmaddress *uk;
2102
2103 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2104 memcpy(&k->local, &uk->local, sizeof(k->local));
2105 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2106 k->family = uk->family;
2107 k->reserved = uk->reserved;
2108 }
2109
Thomas Graf5424f322007-08-22 14:01:33 -07002110 um = nla_data(rt);
2111 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002112
2113 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2114 return -EINVAL;
2115
2116 for (i = 0; i < num_migrate; i++, um++, ma++) {
2117 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2118 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2119 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2120 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2121
2122 ma->proto = um->proto;
2123 ma->mode = um->mode;
2124 ma->reqid = um->reqid;
2125
2126 ma->old_family = um->old_family;
2127 ma->new_family = um->new_family;
2128 }
2129
2130 *num = i;
2131 return 0;
2132}
2133
2134static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002135 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002136{
Thomas Graf7b67c852007-08-22 13:53:52 -07002137 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002138 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002139 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002140 u8 type;
2141 int err;
2142 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002143 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002144
Thomas Graf35a7aa02007-08-22 14:00:40 -07002145 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002146 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002147
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002148 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2149
Thomas Graf5424f322007-08-22 14:01:33 -07002150 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002151 if (err)
2152 return err;
2153
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002154 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002155 if (err)
2156 return err;
2157
2158 if (!n)
2159 return 0;
2160
Fan Du8d549c42013-11-07 17:47:49 +08002161 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002162
2163 return 0;
2164}
2165#else
2166static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002167 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002168{
2169 return -ENOPROTOOPT;
2170}
2171#endif
2172
2173#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002174static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002175{
2176 struct xfrm_user_migrate um;
2177
2178 memset(&um, 0, sizeof(um));
2179 um.proto = m->proto;
2180 um.mode = m->mode;
2181 um.reqid = m->reqid;
2182 um.old_family = m->old_family;
2183 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2184 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2185 um.new_family = m->new_family;
2186 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2187 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2188
Thomas Grafc0144be2007-08-22 13:55:43 -07002189 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002190}
2191
David S. Miller183cad12011-02-24 00:28:01 -05002192static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002193{
2194 struct xfrm_user_kmaddress uk;
2195
2196 memset(&uk, 0, sizeof(uk));
2197 uk.family = k->family;
2198 uk.reserved = k->reserved;
2199 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002200 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002201
2202 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2203}
2204
2205static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002206{
2207 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002208 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2209 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2210 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002211}
2212
David S. Miller183cad12011-02-24 00:28:01 -05002213static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2214 int num_migrate, const struct xfrm_kmaddress *k,
2215 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002216{
David S. Miller183cad12011-02-24 00:28:01 -05002217 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002218 struct xfrm_userpolicy_id *pol_id;
2219 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002220 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002222 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2223 if (nlh == NULL)
2224 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002225
Thomas Graf7b67c852007-08-22 13:53:52 -07002226 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002227 /* copy data from selector, dir, and type to the pol_id */
2228 memset(pol_id, 0, sizeof(*pol_id));
2229 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2230 pol_id->dir = dir;
2231
David S. Miller1d1e34d2012-06-27 21:57:03 -07002232 if (k != NULL) {
2233 err = copy_to_user_kmaddress(k, skb);
2234 if (err)
2235 goto out_cancel;
2236 }
2237 err = copy_to_user_policy_type(type, skb);
2238 if (err)
2239 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002240 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002241 err = copy_to_user_migrate(mp, skb);
2242 if (err)
2243 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002244 }
2245
Thomas Graf98250692007-08-22 12:47:26 -07002246 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002247
2248out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002249 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002250 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002251}
2252
David S. Miller183cad12011-02-24 00:28:01 -05002253static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2254 const struct xfrm_migrate *m, int num_migrate,
2255 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002256{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002257 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002258 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002259
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002260 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002261 if (skb == NULL)
2262 return -ENOMEM;
2263
2264 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002265 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002266 BUG();
2267
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002268 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002269}
2270#else
David S. Miller183cad12011-02-24 00:28:01 -05002271static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2272 const struct xfrm_migrate *m, int num_migrate,
2273 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002274{
2275 return -ENOPROTOOPT;
2276}
2277#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002278
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002279#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002280
2281static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002282 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002283 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2284 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2285 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2286 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2287 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2288 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002289 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002290 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002291 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002292 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002293 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002294 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002295 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002296 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2297 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002298 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002299 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002300 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2301 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302};
2303
Thomas Graf492b5582005-05-03 14:26:40 -07002304#undef XMSGSIZE
2305
Thomas Grafcf5cb792007-08-22 13:59:04 -07002306static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002307 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2308 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2309 [XFRMA_LASTUSED] = { .type = NLA_U64},
2310 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002311 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002312 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2313 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2314 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2315 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2316 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2317 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2318 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2319 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2320 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2321 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2322 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2323 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2324 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2325 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002326 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002327 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002328 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002329 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002330 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002331 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002332 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002333};
2334
Mathias Krause05600a72013-02-24 14:10:27 +01002335static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002336 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002338 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002339} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2340 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2341 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2342 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002343 .dump = xfrm_dump_sa,
2344 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002345 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2346 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2347 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002348 .dump = xfrm_dump_policy,
2349 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002350 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002351 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002352 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002353 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2354 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002355 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002356 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2357 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002358 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2359 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002360 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002361 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002362 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363};
2364
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002365static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002367 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002368 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002369 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002370 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 type -= XFRM_MSG_BASE;
2377 link = &xfrm_dispatch[type];
2378
2379 /* All operations require privileges, even GET */
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002380 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002381 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Thomas Graf492b5582005-05-03 14:26:40 -07002383 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2384 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002385 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002389 {
2390 struct netlink_dump_control c = {
2391 .dump = link->dump,
2392 .done = link->done,
2393 };
2394 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
Thomas Graf35a7aa02007-08-22 14:00:40 -07002398 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002399 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002400 if (err < 0)
2401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Thomas Graf5424f322007-08-22 14:01:33 -07002406 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002409static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Fan Du283bc9f2013-11-07 17:47:50 +08002411 struct net *net = sock_net(skb->sk);
2412
2413 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002414 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002415 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Thomas Graf7deb2262007-08-22 13:57:39 -07002418static inline size_t xfrm_expire_msgsize(void)
2419{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002420 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2421 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002422}
2423
David S. Miller214e0052011-02-24 00:02:38 -05002424static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 struct xfrm_user_expire *ue;
2427 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002428 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Eric W. Biederman15e47302012-09-07 20:12:54 +00002430 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002431 if (nlh == NULL)
2432 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Thomas Graf7b67c852007-08-22 13:53:52 -07002434 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002436 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
David S. Miller1d1e34d2012-06-27 21:57:03 -07002438 err = xfrm_mark_put(skb, &x->mark);
2439 if (err)
2440 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002441
Thomas Graf98250692007-08-22 12:47:26 -07002442 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
David S. Miller214e0052011-02-24 00:02:38 -05002445static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002447 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct sk_buff *skb;
2449
Thomas Graf7deb2262007-08-22 13:57:39 -07002450 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (skb == NULL)
2452 return -ENOMEM;
2453
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002454 if (build_expire(skb, x, c) < 0) {
2455 kfree_skb(skb);
2456 return -EMSGSIZE;
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002459 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
David S. Miller214e0052011-02-24 00:02:38 -05002462static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002463{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002464 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002465 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002466
Steffen Klassertd8647b72011-03-08 00:10:27 +00002467 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002468 if (skb == NULL)
2469 return -ENOMEM;
2470
2471 if (build_aevent(skb, x, c) < 0)
2472 BUG();
2473
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002474 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002475}
2476
David S. Miller214e0052011-02-24 00:02:38 -05002477static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002478{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002479 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480 struct xfrm_usersa_flush *p;
2481 struct nlmsghdr *nlh;
2482 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002483 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002484
Thomas Graf7deb2262007-08-22 13:57:39 -07002485 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002486 if (skb == NULL)
2487 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002488
Eric W. Biederman15e47302012-09-07 20:12:54 +00002489 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002490 if (nlh == NULL) {
2491 kfree_skb(skb);
2492 return -EMSGSIZE;
2493 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002494
Thomas Graf7b67c852007-08-22 13:53:52 -07002495 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002496 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002497
Thomas Graf98250692007-08-22 12:47:26 -07002498 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002499
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002500 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002501}
2502
Thomas Graf7deb2262007-08-22 13:57:39 -07002503static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002504{
Thomas Graf7deb2262007-08-22 13:57:39 -07002505 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002506 if (x->aead)
2507 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002508 if (x->aalg) {
2509 l += nla_total_size(sizeof(struct xfrm_algo) +
2510 (x->aalg->alg_key_len + 7) / 8);
2511 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2512 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002513 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002514 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002515 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002516 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002517 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002518 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002519 if (x->tfcpad)
2520 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002521 if (x->replay_esn)
2522 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002523 if (x->security)
2524 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2525 x->security->ctx_len);
2526 if (x->coaddr)
2527 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002528 if (x->props.extra_flags)
2529 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002530
Herbert Xud26f3982007-11-13 21:47:08 -08002531 /* Must count x->lastused as it may become non-zero behind our back. */
2532 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002533
2534 return l;
2535}
2536
David S. Miller214e0052011-02-24 00:02:38 -05002537static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002538{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002539 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002540 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002541 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002542 struct nlmsghdr *nlh;
2543 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002544 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002545 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002546
2547 headlen = sizeof(*p);
2548 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002549 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002550 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002551 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002552 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002553 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002554
Thomas Graf7deb2262007-08-22 13:57:39 -07002555 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002556 if (skb == NULL)
2557 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002558
Eric W. Biederman15e47302012-09-07 20:12:54 +00002559 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002560 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002561 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002562 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002563
Thomas Graf7b67c852007-08-22 13:53:52 -07002564 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002565 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002566 struct nlattr *attr;
2567
Thomas Graf7b67c852007-08-22 13:53:52 -07002568 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002569 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2570 id->spi = x->id.spi;
2571 id->family = x->props.family;
2572 id->proto = x->id.proto;
2573
Thomas Grafc0144be2007-08-22 13:55:43 -07002574 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002575 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002576 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002577 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002578
2579 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002580 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002581 err = copy_to_user_state_extra(x, p, skb);
2582 if (err)
2583 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002584
Thomas Graf98250692007-08-22 12:47:26 -07002585 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002586
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002587 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002588
David S. Miller1d1e34d2012-06-27 21:57:03 -07002589out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002590 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002591 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002592}
2593
David S. Miller214e0052011-02-24 00:02:38 -05002594static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595{
2596
2597 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002598 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002599 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002600 case XFRM_MSG_NEWAE:
2601 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002602 case XFRM_MSG_DELSA:
2603 case XFRM_MSG_UPDSA:
2604 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002605 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002606 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002607 return xfrm_notify_sa_flush(c);
2608 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002609 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2610 c->event);
2611 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612 }
2613
2614 return 0;
2615
2616}
2617
Thomas Graf7deb2262007-08-22 13:57:39 -07002618static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2619 struct xfrm_policy *xp)
2620{
2621 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2622 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002623 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002624 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2625 + userpolicy_type_attrsize();
2626}
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002629 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002631 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 struct xfrm_user_acquire *ua;
2633 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002634 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002636 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2637 if (nlh == NULL)
2638 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Thomas Graf7b67c852007-08-22 13:53:52 -07002640 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 memcpy(&ua->id, &x->id, sizeof(ua->id));
2642 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2643 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002644 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 ua->aalgos = xt->aalgos;
2646 ua->ealgos = xt->ealgos;
2647 ua->calgos = xt->calgos;
2648 ua->seq = x->km.seq = seq;
2649
David S. Miller1d1e34d2012-06-27 21:57:03 -07002650 err = copy_to_user_tmpl(xp, skb);
2651 if (!err)
2652 err = copy_to_user_state_sec_ctx(x, skb);
2653 if (!err)
2654 err = copy_to_user_policy_type(xp->type, skb);
2655 if (!err)
2656 err = xfrm_mark_put(skb, &xp->mark);
2657 if (err) {
2658 nlmsg_cancel(skb, nlh);
2659 return err;
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Thomas Graf98250692007-08-22 12:47:26 -07002662 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002666 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002668 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Thomas Graf7deb2262007-08-22 13:57:39 -07002671 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (skb == NULL)
2673 return -ENOMEM;
2674
Fan Du65e07362012-08-15 10:13:47 +08002675 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 BUG();
2677
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002678 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
2681/* User gives us xfrm_user_policy_info followed by an array of 0
2682 * or more templates.
2683 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002684static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 u8 *data, int len, int *dir)
2686{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002687 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2689 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2690 struct xfrm_policy *xp;
2691 int nr;
2692
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002693 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 case AF_INET:
2695 if (opt != IP_XFRM_POLICY) {
2696 *dir = -EOPNOTSUPP;
2697 return NULL;
2698 }
2699 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002700#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 case AF_INET6:
2702 if (opt != IPV6_XFRM_POLICY) {
2703 *dir = -EOPNOTSUPP;
2704 return NULL;
2705 }
2706 break;
2707#endif
2708 default:
2709 *dir = -EINVAL;
2710 return NULL;
2711 }
2712
2713 *dir = -EINVAL;
2714
2715 if (len < sizeof(*p) ||
2716 verify_newpolicy_info(p))
2717 return NULL;
2718
2719 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002720 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return NULL;
2722
Herbert Xua4f1bac2005-07-26 15:43:17 -07002723 if (p->dir > XFRM_POLICY_OUT)
2724 return NULL;
2725
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002726 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 if (xp == NULL) {
2728 *dir = -ENOBUFS;
2729 return NULL;
2730 }
2731
2732 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002733 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 copy_templates(xp, ut, nr);
2735
2736 *dir = p->dir;
2737
2738 return xp;
2739}
2740
Thomas Graf7deb2262007-08-22 13:57:39 -07002741static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2742{
2743 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2744 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2745 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002746 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002747 + userpolicy_type_attrsize();
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002751 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
2753 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002754 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002755 struct nlmsghdr *nlh;
2756 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Eric W. Biederman15e47302012-09-07 20:12:54 +00002758 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002759 if (nlh == NULL)
2760 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Thomas Graf7b67c852007-08-22 13:53:52 -07002762 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002764 err = copy_to_user_tmpl(xp, skb);
2765 if (!err)
2766 err = copy_to_user_sec_ctx(xp, skb);
2767 if (!err)
2768 err = copy_to_user_policy_type(xp->type, skb);
2769 if (!err)
2770 err = xfrm_mark_put(skb, &xp->mark);
2771 if (err) {
2772 nlmsg_cancel(skb, nlh);
2773 return err;
2774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 upe->hard = !!hard;
2776
Thomas Graf98250692007-08-22 12:47:26 -07002777 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
David S. Miller214e0052011-02-24 00:02:38 -05002780static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002782 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Thomas Graf7deb2262007-08-22 13:57:39 -07002785 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (skb == NULL)
2787 return -ENOMEM;
2788
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002789 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 BUG();
2791
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002792 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
David S. Miller214e0052011-02-24 00:02:38 -05002795static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002796{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002797 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002798 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002799 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002800 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002801 struct nlmsghdr *nlh;
2802 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002803 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002804
2805 headlen = sizeof(*p);
2806 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002807 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002808 headlen = sizeof(*id);
2809 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002810 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002811 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002812 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002813
Thomas Graf7deb2262007-08-22 13:57:39 -07002814 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815 if (skb == NULL)
2816 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002817
Eric W. Biederman15e47302012-09-07 20:12:54 +00002818 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002819 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002820 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002821 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002822
Thomas Graf7b67c852007-08-22 13:53:52 -07002823 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002824 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002825 struct nlattr *attr;
2826
Thomas Graf7b67c852007-08-22 13:53:52 -07002827 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002828 memset(id, 0, sizeof(*id));
2829 id->dir = dir;
2830 if (c->data.byid)
2831 id->index = xp->index;
2832 else
2833 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2834
Thomas Grafc0144be2007-08-22 13:55:43 -07002835 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002836 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002837 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002838 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002839
2840 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002841 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002842
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002843 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002844 err = copy_to_user_tmpl(xp, skb);
2845 if (!err)
2846 err = copy_to_user_policy_type(xp->type, skb);
2847 if (!err)
2848 err = xfrm_mark_put(skb, &xp->mark);
2849 if (err)
2850 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002851
Thomas Graf98250692007-08-22 12:47:26 -07002852 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002853
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002854 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002855
David S. Miller1d1e34d2012-06-27 21:57:03 -07002856out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002857 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002858 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002859}
2860
David S. Miller214e0052011-02-24 00:02:38 -05002861static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002862{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002863 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002864 struct nlmsghdr *nlh;
2865 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002866 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002867
Thomas Graf7deb2262007-08-22 13:57:39 -07002868 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002869 if (skb == NULL)
2870 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002871
Eric W. Biederman15e47302012-09-07 20:12:54 +00002872 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002873 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002874 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002875 goto out_free_skb;
2876 err = copy_to_user_policy_type(c->data.type, skb);
2877 if (err)
2878 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002879
Thomas Graf98250692007-08-22 12:47:26 -07002880 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002881
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002882 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002883
David S. Miller1d1e34d2012-06-27 21:57:03 -07002884out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002885 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002886 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002887}
2888
David S. Miller214e0052011-02-24 00:02:38 -05002889static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002890{
2891
2892 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002893 case XFRM_MSG_NEWPOLICY:
2894 case XFRM_MSG_UPDPOLICY:
2895 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002896 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002897 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002898 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002899 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002900 return xfrm_exp_policy_notify(xp, dir, c);
2901 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002902 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2903 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002904 }
2905
2906 return 0;
2907
2908}
2909
Thomas Graf7deb2262007-08-22 13:57:39 -07002910static inline size_t xfrm_report_msgsize(void)
2911{
2912 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2913}
2914
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002915static int build_report(struct sk_buff *skb, u8 proto,
2916 struct xfrm_selector *sel, xfrm_address_t *addr)
2917{
2918 struct xfrm_user_report *ur;
2919 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002920
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002921 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2922 if (nlh == NULL)
2923 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002924
Thomas Graf7b67c852007-08-22 13:53:52 -07002925 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002926 ur->proto = proto;
2927 memcpy(&ur->sel, sel, sizeof(ur->sel));
2928
David S. Miller1d1e34d2012-06-27 21:57:03 -07002929 if (addr) {
2930 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2931 if (err) {
2932 nlmsg_cancel(skb, nlh);
2933 return err;
2934 }
2935 }
Thomas Graf98250692007-08-22 12:47:26 -07002936 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002937}
2938
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002939static int xfrm_send_report(struct net *net, u8 proto,
2940 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002941{
2942 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002943
Thomas Graf7deb2262007-08-22 13:57:39 -07002944 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002945 if (skb == NULL)
2946 return -ENOMEM;
2947
2948 if (build_report(skb, proto, sel, addr) < 0)
2949 BUG();
2950
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002951 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002952}
2953
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002954static inline size_t xfrm_mapping_msgsize(void)
2955{
2956 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2957}
2958
2959static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2960 xfrm_address_t *new_saddr, __be16 new_sport)
2961{
2962 struct xfrm_user_mapping *um;
2963 struct nlmsghdr *nlh;
2964
2965 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2966 if (nlh == NULL)
2967 return -EMSGSIZE;
2968
2969 um = nlmsg_data(nlh);
2970
2971 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2972 um->id.spi = x->id.spi;
2973 um->id.family = x->props.family;
2974 um->id.proto = x->id.proto;
2975 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2976 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2977 um->new_sport = new_sport;
2978 um->old_sport = x->encap->encap_sport;
2979 um->reqid = x->props.reqid;
2980
2981 return nlmsg_end(skb, nlh);
2982}
2983
2984static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2985 __be16 sport)
2986{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002987 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002988 struct sk_buff *skb;
2989
2990 if (x->id.proto != IPPROTO_ESP)
2991 return -EINVAL;
2992
2993 if (!x->encap)
2994 return -EINVAL;
2995
2996 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2997 if (skb == NULL)
2998 return -ENOMEM;
2999
3000 if (build_mapping(skb, x, ipaddr, sport) < 0)
3001 BUG();
3002
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003003 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003004}
3005
Horia Geanta0f245582014-02-12 16:20:06 +02003006static bool xfrm_is_alive(const struct km_event *c)
3007{
3008 return (bool)xfrm_acquire_is_on(c->net);
3009}
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011static struct xfrm_mgr netlink_mgr = {
3012 .id = "netlink",
3013 .notify = xfrm_send_state_notify,
3014 .acquire = xfrm_send_acquire,
3015 .compile_policy = xfrm_compile_policy,
3016 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003017 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003018 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003019 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003020 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021};
3022
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003023static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Patrick McHardybe336902006-03-20 22:40:54 -08003025 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003026 struct netlink_kernel_cfg cfg = {
3027 .groups = XFRMNLGRP_MAX,
3028 .input = xfrm_netlink_rcv,
3029 };
Patrick McHardybe336902006-03-20 22:40:54 -08003030
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003031 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003032 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003034 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003035 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return 0;
3037}
3038
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003039static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003040{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003041 struct net *net;
3042 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003043 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003044 synchronize_net();
3045 list_for_each_entry(net, net_exit_list, exit_list)
3046 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003047}
3048
3049static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003050 .init = xfrm_user_net_init,
3051 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003052};
3053
3054static int __init xfrm_user_init(void)
3055{
3056 int rv;
3057
3058 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3059
3060 rv = register_pernet_subsys(&xfrm_user_net_ops);
3061 if (rv < 0)
3062 return rv;
3063 rv = xfrm_register_km(&netlink_mgr);
3064 if (rv < 0)
3065 unregister_pernet_subsys(&xfrm_user_net_ops);
3066 return rv;
3067}
3068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069static void __exit xfrm_user_exit(void)
3070{
3071 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003072 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
3075module_init(xfrm_user_init);
3076module_exit(xfrm_user_exit);
3077MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003078MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003079