blob: 09336b2680011f218da08adcd239255d1e2ea73a [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 Beltrami0a69452c2006-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
Michal Kubecek21ee5432014-06-03 10:26:06 +0200958/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
959 * Must be called with RCU read lock.
960 */
961static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
962 u32 pid, unsigned int group)
963{
964 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
965
966 if (nlsk)
967 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
968 else
969 return -1;
970}
971
Thomas Graf7deb2262007-08-22 13:57:39 -0700972static inline size_t xfrm_spdinfo_msgsize(void)
973{
974 return NLMSG_ALIGN(4)
975 + nla_total_size(sizeof(struct xfrmu_spdinfo))
976 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
977}
978
Alexey Dobriyane0710412010-01-23 13:37:10 +0000979static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000980 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700981{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700982 struct xfrmk_spdinfo si;
983 struct xfrmu_spdinfo spc;
984 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700985 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700986 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700987 u32 *f;
988
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300990 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700991 return -EMSGSIZE;
992
993 f = nlmsg_data(nlh);
994 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000995 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700996 spc.incnt = si.incnt;
997 spc.outcnt = si.outcnt;
998 spc.fwdcnt = si.fwdcnt;
999 spc.inscnt = si.inscnt;
1000 spc.outscnt = si.outscnt;
1001 spc.fwdscnt = si.fwdscnt;
1002 sph.spdhcnt = si.spdhcnt;
1003 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001004
David S. Miller1d1e34d2012-06-27 21:57:03 -07001005 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
1006 if (!err)
1007 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1008 if (err) {
1009 nlmsg_cancel(skb, nlh);
1010 return err;
1011 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001012
1013 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001014}
1015
1016static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001017 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001018{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001019 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001020 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001021 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001022 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001023 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001024
Thomas Graf7deb2262007-08-22 13:57:39 -07001025 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001026 if (r_skb == NULL)
1027 return -ENOMEM;
1028
Eric W. Biederman15e47302012-09-07 20:12:54 +00001029 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001030 BUG();
1031
Eric W. Biederman15e47302012-09-07 20:12:54 +00001032 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001033}
1034
Thomas Graf7deb2262007-08-22 13:57:39 -07001035static inline size_t xfrm_sadinfo_msgsize(void)
1036{
1037 return NLMSG_ALIGN(4)
1038 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1039 + nla_total_size(4); /* XFRMA_SAD_CNT */
1040}
1041
Alexey Dobriyane0710412010-01-23 13:37:10 +00001042static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001043 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001044{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001045 struct xfrmk_sadinfo si;
1046 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001047 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001048 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001049 u32 *f;
1050
Eric W. Biederman15e47302012-09-07 20:12:54 +00001051 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001052 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001053 return -EMSGSIZE;
1054
1055 f = nlmsg_data(nlh);
1056 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001057 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001058
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001059 sh.sadhmcnt = si.sadhmcnt;
1060 sh.sadhcnt = si.sadhcnt;
1061
David S. Miller1d1e34d2012-06-27 21:57:03 -07001062 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1063 if (!err)
1064 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1065 if (err) {
1066 nlmsg_cancel(skb, nlh);
1067 return err;
1068 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001069
1070 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001071}
1072
1073static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001074 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001075{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001076 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001077 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001078 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001079 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001080 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001081
Thomas Graf7deb2262007-08-22 13:57:39 -07001082 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001083 if (r_skb == NULL)
1084 return -ENOMEM;
1085
Eric W. Biederman15e47302012-09-07 20:12:54 +00001086 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001087 BUG();
1088
Eric W. Biederman15e47302012-09-07 20:12:54 +00001089 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001090}
1091
Christoph Hellwig22e70052007-01-02 15:22:30 -08001092static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001093 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001095 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001096 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct xfrm_state *x;
1098 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001099 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001101 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (x == NULL)
1103 goto out_noput;
1104
1105 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1106 if (IS_ERR(resp_skb)) {
1107 err = PTR_ERR(resp_skb);
1108 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001109 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111 xfrm_state_put(x);
1112out_noput:
1113 return err;
1114}
1115
Christoph Hellwig22e70052007-01-02 15:22:30 -08001116static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001117 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001119 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct xfrm_state *x;
1121 struct xfrm_userspi_info *p;
1122 struct sk_buff *resp_skb;
1123 xfrm_address_t *daddr;
1124 int family;
1125 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001126 u32 mark;
1127 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Thomas Graf7b67c852007-08-22 13:53:52 -07001129 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001130 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (err)
1132 goto out_noput;
1133
1134 family = p->info.family;
1135 daddr = &p->info.id.daddr;
1136
1137 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001138
1139 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001141 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001142 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 xfrm_state_put(x);
1144 x = NULL;
1145 }
1146 }
1147
1148 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001149 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 p->info.id.proto, daddr,
1151 &p->info.saddr, 1,
1152 family);
1153 err = -ENOENT;
1154 if (x == NULL)
1155 goto out_noput;
1156
Herbert Xu658b2192007-10-09 13:29:52 -07001157 err = xfrm_alloc_spi(x, p->min, p->max);
1158 if (err)
1159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Herbert Xu658b2192007-10-09 13:29:52 -07001161 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (IS_ERR(resp_skb)) {
1163 err = PTR_ERR(resp_skb);
1164 goto out;
1165 }
1166
Eric W. Biederman15e47302012-09-07 20:12:54 +00001167 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169out:
1170 xfrm_state_put(x);
1171out_noput:
1172 return err;
1173}
1174
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001175static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 switch (dir) {
1178 case XFRM_POLICY_IN:
1179 case XFRM_POLICY_OUT:
1180 case XFRM_POLICY_FWD:
1181 break;
1182
1183 default:
1184 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 return 0;
1188}
1189
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001190static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001191{
1192 switch (type) {
1193 case XFRM_POLICY_TYPE_MAIN:
1194#ifdef CONFIG_XFRM_SUB_POLICY
1195 case XFRM_POLICY_TYPE_SUB:
1196#endif
1197 break;
1198
1199 default:
1200 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001201 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001202
1203 return 0;
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1207{
Fan Due682adf02013-11-07 17:47:48 +08001208 int ret;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 switch (p->share) {
1211 case XFRM_SHARE_ANY:
1212 case XFRM_SHARE_SESSION:
1213 case XFRM_SHARE_USER:
1214 case XFRM_SHARE_UNIQUE:
1215 break;
1216
1217 default:
1218 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 switch (p->action) {
1222 case XFRM_POLICY_ALLOW:
1223 case XFRM_POLICY_BLOCK:
1224 break;
1225
1226 default:
1227 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 switch (p->sel.family) {
1231 case AF_INET:
1232 break;
1233
1234 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001235#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237#else
1238 return -EAFNOSUPPORT;
1239#endif
1240
1241 default:
1242 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Fan Due682adf02013-11-07 17:47:48 +08001245 ret = verify_policy_dir(p->dir);
1246 if (ret)
1247 return ret;
1248 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1249 return -EINVAL;
1250
1251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
Thomas Graf5424f322007-08-22 14:01:33 -07001254static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001255{
Thomas Graf5424f322007-08-22 14:01:33 -07001256 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001257 struct xfrm_user_sec_ctx *uctx;
1258
1259 if (!rt)
1260 return 0;
1261
Thomas Graf5424f322007-08-22 14:01:33 -07001262 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001263 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001264}
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1267 int nr)
1268{
1269 int i;
1270
1271 xp->xfrm_nr = nr;
1272 for (i = 0; i < nr; i++, ut++) {
1273 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1274
1275 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1276 memcpy(&t->saddr, &ut->saddr,
1277 sizeof(xfrm_address_t));
1278 t->reqid = ut->reqid;
1279 t->mode = ut->mode;
1280 t->share = ut->share;
1281 t->optional = ut->optional;
1282 t->aalgos = ut->aalgos;
1283 t->ealgos = ut->ealgos;
1284 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001285 /* If all masks are ~0, then we allow all algorithms. */
1286 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001287 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289}
1290
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001291static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1292{
1293 int i;
1294
1295 if (nr > XFRM_MAX_DEPTH)
1296 return -EINVAL;
1297
1298 for (i = 0; i < nr; i++) {
1299 /* We never validated the ut->family value, so many
1300 * applications simply leave it at zero. The check was
1301 * never made and ut->family was ignored because all
1302 * templates could be assumed to have the same family as
1303 * the policy itself. Now that we will have ipv4-in-ipv6
1304 * and ipv6-in-ipv4 tunnels, this is no longer true.
1305 */
1306 if (!ut[i].family)
1307 ut[i].family = family;
1308
1309 switch (ut[i].family) {
1310 case AF_INET:
1311 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001312#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001313 case AF_INET6:
1314 break;
1315#endif
1316 default:
1317 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001318 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001319 }
1320
1321 return 0;
1322}
1323
Thomas Graf5424f322007-08-22 14:01:33 -07001324static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Thomas Graf5424f322007-08-22 14:01:33 -07001326 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (!rt) {
1329 pol->xfrm_nr = 0;
1330 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001331 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1332 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001333 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001335 err = validate_tmpl(nr, utmpl, pol->family);
1336 if (err)
1337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Thomas Graf5424f322007-08-22 14:01:33 -07001339 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 return 0;
1342}
1343
Thomas Graf5424f322007-08-22 14:01:33 -07001344static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001345{
Thomas Graf5424f322007-08-22 14:01:33 -07001346 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001347 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001348 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001349 int err;
1350
1351 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001352 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001353 type = upt->type;
1354 }
1355
1356 err = verify_policy_type(type);
1357 if (err)
1358 return err;
1359
1360 *tp = type;
1361 return 0;
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1365{
1366 xp->priority = p->priority;
1367 xp->index = p->index;
1368 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1369 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1370 xp->action = p->action;
1371 xp->flags = p->flags;
1372 xp->family = p->sel.family;
1373 /* XXX xp->share = p->share; */
1374}
1375
1376static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1377{
Mathias Krause7b789832012-09-19 11:33:40 +00001378 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1380 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1381 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1382 p->priority = xp->priority;
1383 p->index = xp->index;
1384 p->sel.family = xp->family;
1385 p->dir = dir;
1386 p->action = xp->action;
1387 p->flags = xp->flags;
1388 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1389}
1390
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001391static 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 -07001392{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001393 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int err;
1395
1396 if (!xp) {
1397 *errp = -ENOMEM;
1398 return NULL;
1399 }
1400
1401 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001402
Thomas Graf35a7aa02007-08-22 14:00:40 -07001403 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001404 if (err)
1405 goto error;
1406
Thomas Graf35a7aa02007-08-22 14:00:40 -07001407 if (!(err = copy_from_user_tmpl(xp, attrs)))
1408 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001409 if (err)
1410 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001412 xfrm_mark_get(attrs, &xp->mark);
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001415 error:
1416 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001417 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001418 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001419 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Christoph Hellwig22e70052007-01-02 15:22:30 -08001422static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001423 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001425 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001426 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001428 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int err;
1430 int excl;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001431 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001432 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001433 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 err = verify_newpolicy_info(p);
1436 if (err)
1437 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001438 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001439 if (err)
1440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001442 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (!xp)
1444 return err;
1445
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001446 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001447 * Aha! this is anti-netlink really i.e more pfkey derived
1448 * in netlink excl is a flag and you wouldnt need
1449 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1451 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001452 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001453 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001456 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 kfree(xp);
1458 return err;
1459 }
1460
Herbert Xuf60f6b82005-06-18 22:44:37 -07001461 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001462 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001463 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001464 km_policy_notify(xp, p->dir, &c);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 xfrm_pol_put(xp);
1467
1468 return 0;
1469}
1470
1471static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1472{
1473 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1474 int i;
1475
1476 if (xp->xfrm_nr == 0)
1477 return 0;
1478
1479 for (i = 0; i < xp->xfrm_nr; i++) {
1480 struct xfrm_user_tmpl *up = &vec[i];
1481 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1482
Mathias Krause1f868402012-09-19 11:33:41 +00001483 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001485 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1487 up->reqid = kp->reqid;
1488 up->mode = kp->mode;
1489 up->share = kp->share;
1490 up->optional = kp->optional;
1491 up->aalgos = kp->aalgos;
1492 up->ealgos = kp->ealgos;
1493 up->calgos = kp->calgos;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Thomas Grafc0144be2007-08-22 13:55:43 -07001496 return nla_put(skb, XFRMA_TMPL,
1497 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001498}
1499
Serge Hallyn0d681622006-07-24 23:30:44 -07001500static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1501{
1502 if (x->security) {
1503 return copy_sec_ctx(x->security, skb);
1504 }
1505 return 0;
1506}
1507
1508static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1509{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001510 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001511 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001512 return 0;
1513}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001514static inline size_t userpolicy_type_attrsize(void)
1515{
1516#ifdef CONFIG_XFRM_SUB_POLICY
1517 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1518#else
1519 return 0;
1520#endif
1521}
Serge Hallyn0d681622006-07-24 23:30:44 -07001522
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001523#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001524static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001525{
Thomas Grafc0144be2007-08-22 13:55:43 -07001526 struct xfrm_userpolicy_type upt = {
1527 .type = type,
1528 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001529
Thomas Grafc0144be2007-08-22 13:55:43 -07001530 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001531}
1532
1533#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001534static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001535{
1536 return 0;
1537}
1538#endif
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1541{
1542 struct xfrm_dump_info *sp = ptr;
1543 struct xfrm_userpolicy_info *p;
1544 struct sk_buff *in_skb = sp->in_skb;
1545 struct sk_buff *skb = sp->out_skb;
1546 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Eric W. Biederman15e47302012-09-07 20:12:54 +00001549 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001550 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1551 if (nlh == NULL)
1552 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Thomas Graf7b67c852007-08-22 13:53:52 -07001554 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001556 err = copy_to_user_tmpl(xp, skb);
1557 if (!err)
1558 err = copy_to_user_sec_ctx(xp, skb);
1559 if (!err)
1560 err = copy_to_user_policy_type(xp->type, skb);
1561 if (!err)
1562 err = xfrm_mark_put(skb, &xp->mark);
1563 if (err) {
1564 nlmsg_cancel(skb, nlh);
1565 return err;
1566 }
Thomas Graf98250692007-08-22 12:47:26 -07001567 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Timo Teras4c563f72008-02-28 21:31:08 -08001571static int xfrm_dump_policy_done(struct netlink_callback *cb)
1572{
1573 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001574 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001575
Fan Du283bc9f2013-11-07 17:47:50 +08001576 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001577 return 0;
1578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1581{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001582 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001583 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct xfrm_dump_info info;
1585
Timo Teras4c563f72008-02-28 21:31:08 -08001586 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1587 sizeof(cb->args) - sizeof(cb->args[0]));
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 info.in_skb = cb->skb;
1590 info.out_skb = skb;
1591 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1592 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001593
1594 if (!cb->args[0]) {
1595 cb->args[0] = 1;
1596 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1597 }
1598
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001599 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 return skb->len;
1602}
1603
1604static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1605 struct xfrm_policy *xp,
1606 int dir, u32 seq)
1607{
1608 struct xfrm_dump_info info;
1609 struct sk_buff *skb;
Mathias Krausec25463722012-09-14 09:58:32 +00001610 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Thomas Graf7deb2262007-08-22 13:57:39 -07001612 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (!skb)
1614 return ERR_PTR(-ENOMEM);
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 info.in_skb = in_skb;
1617 info.out_skb = skb;
1618 info.nlmsg_seq = seq;
1619 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Mathias Krausec25463722012-09-14 09:58:32 +00001621 err = dump_one_policy(xp, dir, 0, &info);
1622 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 kfree_skb(skb);
Mathias Krausec25463722012-09-14 09:58:32 +00001624 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
1627 return skb;
1628}
1629
Christoph Hellwig22e70052007-01-02 15:22:30 -08001630static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001631 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001633 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct xfrm_policy *xp;
1635 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001636 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001638 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001640 struct xfrm_mark m;
1641 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Thomas Graf7b67c852007-08-22 13:53:52 -07001643 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1645
Thomas Graf35a7aa02007-08-22 14:00:40 -07001646 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001647 if (err)
1648 return err;
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 err = verify_policy_dir(p->dir);
1651 if (err)
1652 return err;
1653
1654 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001655 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001656 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001657 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001658 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001659
Thomas Graf35a7aa02007-08-22 14:00:40 -07001660 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001661 if (err)
1662 return err;
1663
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001664 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001665 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001666 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001667
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001668 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001669 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001670 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001671 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001672 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001673 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001674 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (xp == NULL)
1677 return -ENOENT;
1678
1679 if (!delete) {
1680 struct sk_buff *resp_skb;
1681
1682 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1683 if (IS_ERR(resp_skb)) {
1684 err = PTR_ERR(resp_skb);
1685 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001686 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001687 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001689 } else {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001690 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001691 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001692 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001693
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001694 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001695 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1696 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001697
1698 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001699 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001700
Herbert Xue7443892005-06-18 22:44:18 -07001701 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001702 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001703 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001704 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001705 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001708out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001709 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001710 if (delete && err == 0)
1711 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return err;
1713}
1714
Christoph Hellwig22e70052007-01-02 15:22:30 -08001715static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001716 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001718 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001719 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001720 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001721 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001722 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001724 audit_info.loginuid = audit_get_loginuid(current);
1725 audit_info.sessionid = audit_get_sessionid(current);
1726 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001727 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001728 if (err) {
1729 if (err == -ESRCH) /* empty table */
1730 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001731 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001732 }
Herbert Xubf08867f92005-06-18 22:44:00 -07001733 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001734 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001735 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001736 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001737 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738 km_state_notify(NULL, &c);
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return 0;
1741}
1742
Steffen Klassertd8647b72011-03-08 00:10:27 +00001743static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001744{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001745 size_t replay_size = x->replay_esn ?
1746 xfrm_replay_state_esn_len(x->replay_esn) :
1747 sizeof(struct xfrm_replay_state);
1748
Thomas Graf7deb2262007-08-22 13:57:39 -07001749 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001750 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001751 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001752 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001753 + nla_total_size(4) /* XFRM_AE_RTHR */
1754 + nla_total_size(4); /* XFRM_AE_ETHR */
1755}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001756
David S. Miller214e0052011-02-24 00:02:38 -05001757static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001758{
1759 struct xfrm_aevent_id *id;
1760 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001761 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001762
Eric W. Biederman15e47302012-09-07 20:12:54 +00001763 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001764 if (nlh == NULL)
1765 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001766
Thomas Graf7b67c852007-08-22 13:53:52 -07001767 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001768 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001769 id->sa_id.spi = x->id.spi;
1770 id->sa_id.family = x->props.family;
1771 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001772 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001773 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001774 id->flags = c->data.aevent;
1775
David S. Millerd0fde792012-03-29 04:02:26 -04001776 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001777 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1778 xfrm_replay_state_esn_len(x->replay_esn),
1779 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001780 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001781 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1782 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001783 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001784 if (err)
1785 goto out_cancel;
1786 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1787 if (err)
1788 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001789
David S. Miller1d1e34d2012-06-27 21:57:03 -07001790 if (id->flags & XFRM_AE_RTHR) {
1791 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1792 if (err)
1793 goto out_cancel;
1794 }
1795 if (id->flags & XFRM_AE_ETHR) {
1796 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1797 x->replay_maxage * 10 / HZ);
1798 if (err)
1799 goto out_cancel;
1800 }
1801 err = xfrm_mark_put(skb, &x->mark);
1802 if (err)
1803 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001804
Thomas Graf98250692007-08-22 12:47:26 -07001805 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001806
David S. Miller1d1e34d2012-06-27 21:57:03 -07001807out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001808 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001809 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001810}
1811
Christoph Hellwig22e70052007-01-02 15:22:30 -08001812static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001813 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001814{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001815 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001816 struct xfrm_state *x;
1817 struct sk_buff *r_skb;
1818 int err;
1819 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001820 u32 mark;
1821 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001822 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001823 struct xfrm_usersa_id *id = &p->sa_id;
1824
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001825 mark = xfrm_mark_get(attrs, &m);
1826
1827 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001828 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001829 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001830
1831 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1832 if (r_skb == NULL) {
1833 xfrm_state_put(x);
1834 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001835 }
1836
1837 /*
1838 * XXX: is this lock really needed - none of the other
1839 * gets lock (the concern is things getting updated
1840 * while we are still reading) - jhs
1841 */
1842 spin_lock_bh(&x->lock);
1843 c.data.aevent = p->flags;
1844 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001845 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001846
1847 if (build_aevent(r_skb, x, &c) < 0)
1848 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001849 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001850 spin_unlock_bh(&x->lock);
1851 xfrm_state_put(x);
1852 return err;
1853}
1854
Christoph Hellwig22e70052007-01-02 15:22:30 -08001855static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001856 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001857{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001858 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001859 struct xfrm_state *x;
1860 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001861 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001862 u32 mark = 0;
1863 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001864 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001865 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001866 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001867 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001868
Steffen Klassertd8647b72011-03-08 00:10:27 +00001869 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001870 return err;
1871
1872 /* pedantic mode - thou shalt sayeth replaceth */
1873 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1874 return err;
1875
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001876 mark = xfrm_mark_get(attrs, &m);
1877
1878 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 -08001879 if (x == NULL)
1880 return -ESRCH;
1881
1882 if (x->km.state != XFRM_STATE_VALID)
1883 goto out;
1884
Steffen Klassert4479ff72013-09-09 09:39:01 +02001885 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001886 if (err)
1887 goto out;
1888
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001889 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001890 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001891 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001892
1893 c.event = nlh->nlmsg_type;
1894 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001895 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001896 c.data.aevent = XFRM_AE_CU;
1897 km_state_notify(x, &c);
1898 err = 0;
1899out:
1900 xfrm_state_put(x);
1901 return err;
1902}
1903
Christoph Hellwig22e70052007-01-02 15:22:30 -08001904static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001905 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001907 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001908 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001909 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001910 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001911 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001912
Thomas Graf35a7aa02007-08-22 14:00:40 -07001913 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001914 if (err)
1915 return err;
1916
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001917 audit_info.loginuid = audit_get_loginuid(current);
1918 audit_info.sessionid = audit_get_sessionid(current);
1919 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001920 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001921 if (err) {
1922 if (err == -ESRCH) /* empty table */
1923 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001924 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001925 }
1926
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001927 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001928 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001929 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001930 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001931 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001932 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return 0;
1934}
1935
Christoph Hellwig22e70052007-01-02 15:22:30 -08001936static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001937 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001939 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001940 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001941 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001942 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001943 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001945 struct xfrm_mark m;
1946 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001947
Thomas Graf35a7aa02007-08-22 14:00:40 -07001948 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001949 if (err)
1950 return err;
1951
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001952 err = verify_policy_dir(p->dir);
1953 if (err)
1954 return err;
1955
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001956 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001957 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001959 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001960 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001961
Thomas Graf35a7aa02007-08-22 14:00:40 -07001962 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001963 if (err)
1964 return err;
1965
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001966 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001967 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001968 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001969
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001970 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001971 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001972 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001973 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001974 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001975 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001976 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001977 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001978 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001979 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001980
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001981 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001982 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001983
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001984 err = 0;
1985 if (up->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001986 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05001987 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001988 u32 sid;
1989
1990 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001991 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001992 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001993
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001994 } else {
1995 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001996 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001997 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001998 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001999
2000out:
2001 xfrm_pol_put(xp);
2002 return err;
2003}
2004
Christoph Hellwig22e70052007-01-02 15:22:30 -08002005static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002006 struct nlattr **attrs)
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002007{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002008 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002009 struct xfrm_state *x;
2010 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07002011 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002012 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002013 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00002014 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002015
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002016 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002017
David S. Miller3a765aa2007-02-26 14:52:21 -08002018 err = -ENOENT;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002019 if (x == NULL)
2020 return err;
2021
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002022 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08002023 err = -EINVAL;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002024 if (x->km.state != XFRM_STATE_VALID)
2025 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00002026 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002027
Joy Latten161a09e2006-11-27 13:11:54 -06002028 if (ue->hard) {
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002029 kuid_t loginuid = audit_get_loginuid(current);
Eric Paris4440e852013-11-27 17:35:17 -05002030 unsigned int sessionid = audit_get_sessionid(current);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08002031 u32 sid;
2032
2033 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002034 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04002035 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06002036 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002037 err = 0;
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002038out:
2039 spin_unlock_bh(&x->lock);
2040 xfrm_state_put(x);
2041 return err;
2042}
2043
Christoph Hellwig22e70052007-01-02 15:22:30 -08002044static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002045 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002046{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002047 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002048 struct xfrm_policy *xp;
2049 struct xfrm_user_tmpl *ut;
2050 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002051 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002052 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002053
Thomas Graf7b67c852007-08-22 13:53:52 -07002054 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002055 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002056 int err = -ENOMEM;
2057
2058 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002059 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002060
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002061 xfrm_mark_get(attrs, &mark);
2062
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002063 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002064 if (err)
2065 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002066
2067 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002068 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002069 if (!xp)
2070 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002071
2072 memcpy(&x->id, &ua->id, sizeof(ua->id));
2073 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2074 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002075 xp->mark.m = x->mark.m = mark.m;
2076 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002077 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002078 /* extract the templates and for each call km_key */
2079 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2080 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2081 memcpy(&x->id, &t->id, sizeof(x->id));
2082 x->props.mode = t->mode;
2083 x->props.reqid = t->reqid;
2084 x->props.family = ut->family;
2085 t->aalgos = ua->aalgos;
2086 t->ealgos = ua->ealgos;
2087 t->calgos = ua->calgos;
2088 err = km_query(x, t, xp);
2089
2090 }
2091
2092 kfree(x);
2093 kfree(xp);
2094
2095 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002096
2097bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002098 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002099free_state:
2100 kfree(x);
2101nomem:
2102 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002103}
2104
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002105#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002106static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002107 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002108 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002109{
Thomas Graf5424f322007-08-22 14:01:33 -07002110 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002111 struct xfrm_user_migrate *um;
2112 int i, num_migrate;
2113
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002114 if (k != NULL) {
2115 struct xfrm_user_kmaddress *uk;
2116
2117 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2118 memcpy(&k->local, &uk->local, sizeof(k->local));
2119 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2120 k->family = uk->family;
2121 k->reserved = uk->reserved;
2122 }
2123
Thomas Graf5424f322007-08-22 14:01:33 -07002124 um = nla_data(rt);
2125 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002126
2127 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2128 return -EINVAL;
2129
2130 for (i = 0; i < num_migrate; i++, um++, ma++) {
2131 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2132 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2133 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2134 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2135
2136 ma->proto = um->proto;
2137 ma->mode = um->mode;
2138 ma->reqid = um->reqid;
2139
2140 ma->old_family = um->old_family;
2141 ma->new_family = um->new_family;
2142 }
2143
2144 *num = i;
2145 return 0;
2146}
2147
2148static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002149 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002150{
Thomas Graf7b67c852007-08-22 13:53:52 -07002151 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002153 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002154 u8 type;
2155 int err;
2156 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002157 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002158
Thomas Graf35a7aa02007-08-22 14:00:40 -07002159 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002160 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002161
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002162 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2163
Thomas Graf5424f322007-08-22 14:01:33 -07002164 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002165 if (err)
2166 return err;
2167
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002168 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002169 if (err)
2170 return err;
2171
2172 if (!n)
2173 return 0;
2174
Fan Du8d549c42013-11-07 17:47:49 +08002175 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002176
2177 return 0;
2178}
2179#else
2180static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002181 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182{
2183 return -ENOPROTOOPT;
2184}
2185#endif
2186
2187#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002188static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002189{
2190 struct xfrm_user_migrate um;
2191
2192 memset(&um, 0, sizeof(um));
2193 um.proto = m->proto;
2194 um.mode = m->mode;
2195 um.reqid = m->reqid;
2196 um.old_family = m->old_family;
2197 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2198 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2199 um.new_family = m->new_family;
2200 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2201 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2202
Thomas Grafc0144be2007-08-22 13:55:43 -07002203 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002204}
2205
David S. Miller183cad12011-02-24 00:28:01 -05002206static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002207{
2208 struct xfrm_user_kmaddress uk;
2209
2210 memset(&uk, 0, sizeof(uk));
2211 uk.family = k->family;
2212 uk.reserved = k->reserved;
2213 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002214 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002215
2216 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2217}
2218
2219static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002220{
2221 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002222 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2223 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2224 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002225}
2226
David S. Miller183cad12011-02-24 00:28:01 -05002227static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2228 int num_migrate, const struct xfrm_kmaddress *k,
2229 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002230{
David S. Miller183cad12011-02-24 00:28:01 -05002231 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232 struct xfrm_userpolicy_id *pol_id;
2233 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002234 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002236 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2237 if (nlh == NULL)
2238 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002239
Thomas Graf7b67c852007-08-22 13:53:52 -07002240 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002241 /* copy data from selector, dir, and type to the pol_id */
2242 memset(pol_id, 0, sizeof(*pol_id));
2243 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2244 pol_id->dir = dir;
2245
David S. Miller1d1e34d2012-06-27 21:57:03 -07002246 if (k != NULL) {
2247 err = copy_to_user_kmaddress(k, skb);
2248 if (err)
2249 goto out_cancel;
2250 }
2251 err = copy_to_user_policy_type(type, skb);
2252 if (err)
2253 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002254 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002255 err = copy_to_user_migrate(mp, skb);
2256 if (err)
2257 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002258 }
2259
Thomas Graf98250692007-08-22 12:47:26 -07002260 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002261
2262out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002263 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002264 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002265}
2266
David S. Miller183cad12011-02-24 00:28:01 -05002267static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2268 const struct xfrm_migrate *m, int num_migrate,
2269 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002270{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002271 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002272 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002273
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002274 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002275 if (skb == NULL)
2276 return -ENOMEM;
2277
2278 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002279 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002280 BUG();
2281
Michal Kubecek21ee5432014-06-03 10:26:06 +02002282 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002283}
2284#else
David S. Miller183cad12011-02-24 00:28:01 -05002285static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2286 const struct xfrm_migrate *m, int num_migrate,
2287 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002288{
2289 return -ENOPROTOOPT;
2290}
2291#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002292
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002293#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002294
2295static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002296 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002297 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2298 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2299 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2300 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2301 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2302 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002303 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002304 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002305 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002306 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002307 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002308 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002309 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002310 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2311 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002312 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002313 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002314 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2315 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316};
2317
Thomas Graf492b5582005-05-03 14:26:40 -07002318#undef XMSGSIZE
2319
Thomas Grafcf5cb792007-08-22 13:59:04 -07002320static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002321 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2322 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2323 [XFRMA_LASTUSED] = { .type = NLA_U64},
2324 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002325 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002326 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2327 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2328 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2329 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2330 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2331 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2332 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2333 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2334 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2335 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2336 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2337 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2338 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2339 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002340 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002341 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002342 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002343 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002344 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002345 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002346 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002347};
2348
Mathias Krause05600a72013-02-24 14:10:27 +01002349static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002350 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002352 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002353} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2354 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2355 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2356 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002357 .dump = xfrm_dump_sa,
2358 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002359 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2360 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2361 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002362 .dump = xfrm_dump_policy,
2363 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002364 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002365 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b4d2006-03-20 19:17:03 -08002366 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002367 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2368 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002369 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002370 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2371 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002372 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2373 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002374 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002375 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002376 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377};
2378
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002379static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002381 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002382 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002383 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002384 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 type -= XFRM_MSG_BASE;
2391 link = &xfrm_dispatch[type];
2392
2393 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002394 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002395 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Thomas Graf492b5582005-05-03 14:26:40 -07002397 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2398 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002399 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002403 {
2404 struct netlink_dump_control c = {
2405 .dump = link->dump,
2406 .done = link->done,
2407 };
2408 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411
Thomas Graf35a7aa02007-08-22 14:00:40 -07002412 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002413 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002414 if (err < 0)
2415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002418 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Thomas Graf5424f322007-08-22 14:01:33 -07002420 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002423static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Fan Du283bc9f2013-11-07 17:47:50 +08002425 struct net *net = sock_net(skb->sk);
2426
2427 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002428 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002429 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Thomas Graf7deb2262007-08-22 13:57:39 -07002432static inline size_t xfrm_expire_msgsize(void)
2433{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002434 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2435 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002436}
2437
David S. Miller214e0052011-02-24 00:02:38 -05002438static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 struct xfrm_user_expire *ue;
2441 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002442 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Eric W. Biederman15e47302012-09-07 20:12:54 +00002444 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002445 if (nlh == NULL)
2446 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Thomas Graf7b67c852007-08-22 13:53:52 -07002448 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002450 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
David S. Miller1d1e34d2012-06-27 21:57:03 -07002452 err = xfrm_mark_put(skb, &x->mark);
2453 if (err)
2454 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002455
Thomas Graf98250692007-08-22 12:47:26 -07002456 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
David S. Miller214e0052011-02-24 00:02:38 -05002459static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002461 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct sk_buff *skb;
2463
Thomas Graf7deb2262007-08-22 13:57:39 -07002464 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (skb == NULL)
2466 return -ENOMEM;
2467
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002468 if (build_expire(skb, x, c) < 0) {
2469 kfree_skb(skb);
2470 return -EMSGSIZE;
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Michal Kubecek21ee5432014-06-03 10:26:06 +02002473 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
David S. Miller214e0052011-02-24 00:02:38 -05002476static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002477{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002478 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002479 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002480
Steffen Klassertd8647b72011-03-08 00:10:27 +00002481 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002482 if (skb == NULL)
2483 return -ENOMEM;
2484
2485 if (build_aevent(skb, x, c) < 0)
2486 BUG();
2487
Michal Kubecek21ee5432014-06-03 10:26:06 +02002488 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002489}
2490
David S. Miller214e0052011-02-24 00:02:38 -05002491static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002492{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002493 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002494 struct xfrm_usersa_flush *p;
2495 struct nlmsghdr *nlh;
2496 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002497 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002498
Thomas Graf7deb2262007-08-22 13:57:39 -07002499 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002500 if (skb == NULL)
2501 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002502
Eric W. Biederman15e47302012-09-07 20:12:54 +00002503 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002504 if (nlh == NULL) {
2505 kfree_skb(skb);
2506 return -EMSGSIZE;
2507 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002508
Thomas Graf7b67c852007-08-22 13:53:52 -07002509 p = nlmsg_data(nlh);
Herbert Xubf08867f92005-06-18 22:44:00 -07002510 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002511
Thomas Graf98250692007-08-22 12:47:26 -07002512 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002513
Michal Kubecek21ee5432014-06-03 10:26:06 +02002514 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002515}
2516
Thomas Graf7deb2262007-08-22 13:57:39 -07002517static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002518{
Thomas Graf7deb2262007-08-22 13:57:39 -07002519 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002520 if (x->aead)
2521 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002522 if (x->aalg) {
2523 l += nla_total_size(sizeof(struct xfrm_algo) +
2524 (x->aalg->alg_key_len + 7) / 8);
2525 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2526 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002527 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002528 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002529 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002530 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002531 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002532 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002533 if (x->tfcpad)
2534 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002535 if (x->replay_esn)
2536 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002537 if (x->security)
2538 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2539 x->security->ctx_len);
2540 if (x->coaddr)
2541 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002542 if (x->props.extra_flags)
2543 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002544
Herbert Xud26f3982007-11-13 21:47:08 -08002545 /* Must count x->lastused as it may become non-zero behind our back. */
2546 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002547
2548 return l;
2549}
2550
David S. Miller214e0052011-02-24 00:02:38 -05002551static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002552{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002553 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002554 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002555 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002556 struct nlmsghdr *nlh;
2557 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002558 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002559 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002560
2561 headlen = sizeof(*p);
2562 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002563 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002564 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002565 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002566 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002567 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002568
Thomas Graf7deb2262007-08-22 13:57:39 -07002569 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002570 if (skb == NULL)
2571 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002572
Eric W. Biederman15e47302012-09-07 20:12:54 +00002573 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002574 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002575 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002576 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577
Thomas Graf7b67c852007-08-22 13:53:52 -07002578 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002579 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002580 struct nlattr *attr;
2581
Thomas Graf7b67c852007-08-22 13:53:52 -07002582 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002583 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2584 id->spi = x->id.spi;
2585 id->family = x->props.family;
2586 id->proto = x->id.proto;
2587
Thomas Grafc0144be2007-08-22 13:55:43 -07002588 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002589 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002590 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002591 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002592
2593 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002594 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002595 err = copy_to_user_state_extra(x, p, skb);
2596 if (err)
2597 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002598
Thomas Graf98250692007-08-22 12:47:26 -07002599 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002600
Michal Kubecek21ee5432014-06-03 10:26:06 +02002601 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002602
David S. Miller1d1e34d2012-06-27 21:57:03 -07002603out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002604 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002605 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002606}
2607
David S. Miller214e0052011-02-24 00:02:38 -05002608static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002609{
2610
2611 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002612 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002613 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002614 case XFRM_MSG_NEWAE:
2615 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002616 case XFRM_MSG_DELSA:
2617 case XFRM_MSG_UPDSA:
2618 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002619 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002620 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002621 return xfrm_notify_sa_flush(c);
2622 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002623 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2624 c->event);
2625 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002626 }
2627
2628 return 0;
2629
2630}
2631
Thomas Graf7deb2262007-08-22 13:57:39 -07002632static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2633 struct xfrm_policy *xp)
2634{
2635 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2636 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002637 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002638 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2639 + userpolicy_type_attrsize();
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002643 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002645 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct xfrm_user_acquire *ua;
2647 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002648 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002650 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2651 if (nlh == NULL)
2652 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Thomas Graf7b67c852007-08-22 13:53:52 -07002654 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 memcpy(&ua->id, &x->id, sizeof(ua->id));
2656 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2657 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002658 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 ua->aalgos = xt->aalgos;
2660 ua->ealgos = xt->ealgos;
2661 ua->calgos = xt->calgos;
2662 ua->seq = x->km.seq = seq;
2663
David S. Miller1d1e34d2012-06-27 21:57:03 -07002664 err = copy_to_user_tmpl(xp, skb);
2665 if (!err)
2666 err = copy_to_user_state_sec_ctx(x, skb);
2667 if (!err)
2668 err = copy_to_user_policy_type(xp->type, skb);
2669 if (!err)
2670 err = xfrm_mark_put(skb, &xp->mark);
2671 if (err) {
2672 nlmsg_cancel(skb, nlh);
2673 return err;
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Thomas Graf98250692007-08-22 12:47:26 -07002676 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
2679static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002680 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002682 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Thomas Graf7deb2262007-08-22 13:57:39 -07002685 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (skb == NULL)
2687 return -ENOMEM;
2688
Fan Du65e07362012-08-15 10:13:47 +08002689 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 BUG();
2691
Michal Kubecek21ee5432014-06-03 10:26:06 +02002692 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
2695/* User gives us xfrm_user_policy_info followed by an array of 0
2696 * or more templates.
2697 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002698static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 u8 *data, int len, int *dir)
2700{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002701 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2703 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2704 struct xfrm_policy *xp;
2705 int nr;
2706
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002707 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 case AF_INET:
2709 if (opt != IP_XFRM_POLICY) {
2710 *dir = -EOPNOTSUPP;
2711 return NULL;
2712 }
2713 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002714#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 case AF_INET6:
2716 if (opt != IPV6_XFRM_POLICY) {
2717 *dir = -EOPNOTSUPP;
2718 return NULL;
2719 }
2720 break;
2721#endif
2722 default:
2723 *dir = -EINVAL;
2724 return NULL;
2725 }
2726
2727 *dir = -EINVAL;
2728
2729 if (len < sizeof(*p) ||
2730 verify_newpolicy_info(p))
2731 return NULL;
2732
2733 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002734 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return NULL;
2736
Herbert Xua4f1bac2005-07-26 15:43:17 -07002737 if (p->dir > XFRM_POLICY_OUT)
2738 return NULL;
2739
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002740 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (xp == NULL) {
2742 *dir = -ENOBUFS;
2743 return NULL;
2744 }
2745
2746 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002747 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 copy_templates(xp, ut, nr);
2749
2750 *dir = p->dir;
2751
2752 return xp;
2753}
2754
Thomas Graf7deb2262007-08-22 13:57:39 -07002755static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2756{
2757 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2758 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2759 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002760 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002761 + userpolicy_type_attrsize();
2762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002765 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002768 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002769 struct nlmsghdr *nlh;
2770 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Eric W. Biederman15e47302012-09-07 20:12:54 +00002772 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002773 if (nlh == NULL)
2774 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Thomas Graf7b67c852007-08-22 13:53:52 -07002776 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002778 err = copy_to_user_tmpl(xp, skb);
2779 if (!err)
2780 err = copy_to_user_sec_ctx(xp, skb);
2781 if (!err)
2782 err = copy_to_user_policy_type(xp->type, skb);
2783 if (!err)
2784 err = xfrm_mark_put(skb, &xp->mark);
2785 if (err) {
2786 nlmsg_cancel(skb, nlh);
2787 return err;
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 upe->hard = !!hard;
2790
Thomas Graf98250692007-08-22 12:47:26 -07002791 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
David S. Miller214e0052011-02-24 00:02:38 -05002794static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002796 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Thomas Graf7deb2262007-08-22 13:57:39 -07002799 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (skb == NULL)
2801 return -ENOMEM;
2802
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002803 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 BUG();
2805
Michal Kubecek21ee5432014-06-03 10:26:06 +02002806 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
David S. Miller214e0052011-02-24 00:02:38 -05002809static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002810{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002811 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002812 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002813 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002814 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002815 struct nlmsghdr *nlh;
2816 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002817 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002818
2819 headlen = sizeof(*p);
2820 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002821 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002822 headlen = sizeof(*id);
2823 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002824 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002825 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002826 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002827
Thomas Graf7deb2262007-08-22 13:57:39 -07002828 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002829 if (skb == NULL)
2830 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002831
Eric W. Biederman15e47302012-09-07 20:12:54 +00002832 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002833 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002834 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002835 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002836
Thomas Graf7b67c852007-08-22 13:53:52 -07002837 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002838 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002839 struct nlattr *attr;
2840
Thomas Graf7b67c852007-08-22 13:53:52 -07002841 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002842 memset(id, 0, sizeof(*id));
2843 id->dir = dir;
2844 if (c->data.byid)
2845 id->index = xp->index;
2846 else
2847 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2848
Thomas Grafc0144be2007-08-22 13:55:43 -07002849 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002850 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002851 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002852 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002853
2854 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002855 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002856
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002857 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002858 err = copy_to_user_tmpl(xp, skb);
2859 if (!err)
2860 err = copy_to_user_policy_type(xp->type, skb);
2861 if (!err)
2862 err = xfrm_mark_put(skb, &xp->mark);
2863 if (err)
2864 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002865
Thomas Graf98250692007-08-22 12:47:26 -07002866 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002867
Michal Kubecek21ee5432014-06-03 10:26:06 +02002868 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002869
David S. Miller1d1e34d2012-06-27 21:57:03 -07002870out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002871 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002872 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002873}
2874
David S. Miller214e0052011-02-24 00:02:38 -05002875static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002876{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002877 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002878 struct nlmsghdr *nlh;
2879 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002880 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002881
Thomas Graf7deb2262007-08-22 13:57:39 -07002882 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002883 if (skb == NULL)
2884 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002885
Eric W. Biederman15e47302012-09-07 20:12:54 +00002886 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002887 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002888 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002889 goto out_free_skb;
2890 err = copy_to_user_policy_type(c->data.type, skb);
2891 if (err)
2892 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002893
Thomas Graf98250692007-08-22 12:47:26 -07002894 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002895
Michal Kubecek21ee5432014-06-03 10:26:06 +02002896 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002897
David S. Miller1d1e34d2012-06-27 21:57:03 -07002898out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002899 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002900 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002901}
2902
David S. Miller214e0052011-02-24 00:02:38 -05002903static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002904{
2905
2906 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002907 case XFRM_MSG_NEWPOLICY:
2908 case XFRM_MSG_UPDPOLICY:
2909 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002910 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002911 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002912 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002913 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002914 return xfrm_exp_policy_notify(xp, dir, c);
2915 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002916 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2917 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002918 }
2919
2920 return 0;
2921
2922}
2923
Thomas Graf7deb2262007-08-22 13:57:39 -07002924static inline size_t xfrm_report_msgsize(void)
2925{
2926 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2927}
2928
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002929static int build_report(struct sk_buff *skb, u8 proto,
2930 struct xfrm_selector *sel, xfrm_address_t *addr)
2931{
2932 struct xfrm_user_report *ur;
2933 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002934
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002935 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2936 if (nlh == NULL)
2937 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002938
Thomas Graf7b67c852007-08-22 13:53:52 -07002939 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002940 ur->proto = proto;
2941 memcpy(&ur->sel, sel, sizeof(ur->sel));
2942
David S. Miller1d1e34d2012-06-27 21:57:03 -07002943 if (addr) {
2944 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2945 if (err) {
2946 nlmsg_cancel(skb, nlh);
2947 return err;
2948 }
2949 }
Thomas Graf98250692007-08-22 12:47:26 -07002950 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002951}
2952
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002953static int xfrm_send_report(struct net *net, u8 proto,
2954 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002955{
2956 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002957
Thomas Graf7deb2262007-08-22 13:57:39 -07002958 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002959 if (skb == NULL)
2960 return -ENOMEM;
2961
2962 if (build_report(skb, proto, sel, addr) < 0)
2963 BUG();
2964
Michal Kubecek21ee5432014-06-03 10:26:06 +02002965 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002966}
2967
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002968static inline size_t xfrm_mapping_msgsize(void)
2969{
2970 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2971}
2972
2973static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2974 xfrm_address_t *new_saddr, __be16 new_sport)
2975{
2976 struct xfrm_user_mapping *um;
2977 struct nlmsghdr *nlh;
2978
2979 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2980 if (nlh == NULL)
2981 return -EMSGSIZE;
2982
2983 um = nlmsg_data(nlh);
2984
2985 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2986 um->id.spi = x->id.spi;
2987 um->id.family = x->props.family;
2988 um->id.proto = x->id.proto;
2989 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2990 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2991 um->new_sport = new_sport;
2992 um->old_sport = x->encap->encap_sport;
2993 um->reqid = x->props.reqid;
2994
2995 return nlmsg_end(skb, nlh);
2996}
2997
2998static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2999 __be16 sport)
3000{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003001 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003002 struct sk_buff *skb;
3003
3004 if (x->id.proto != IPPROTO_ESP)
3005 return -EINVAL;
3006
3007 if (!x->encap)
3008 return -EINVAL;
3009
3010 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
3011 if (skb == NULL)
3012 return -ENOMEM;
3013
3014 if (build_mapping(skb, x, ipaddr, sport) < 0)
3015 BUG();
3016
Michal Kubecek21ee5432014-06-03 10:26:06 +02003017 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003018}
3019
Horia Geanta0f245582014-02-12 16:20:06 +02003020static bool xfrm_is_alive(const struct km_event *c)
3021{
3022 return (bool)xfrm_acquire_is_on(c->net);
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static struct xfrm_mgr netlink_mgr = {
3026 .id = "netlink",
3027 .notify = xfrm_send_state_notify,
3028 .acquire = xfrm_send_acquire,
3029 .compile_policy = xfrm_compile_policy,
3030 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07003031 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08003032 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07003033 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02003034 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035};
3036
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003037static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Patrick McHardybe336902006-03-20 22:40:54 -08003039 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003040 struct netlink_kernel_cfg cfg = {
3041 .groups = XFRMNLGRP_MAX,
3042 .input = xfrm_netlink_rcv,
3043 };
Patrick McHardybe336902006-03-20 22:40:54 -08003044
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003045 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003046 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003048 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003049 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return 0;
3051}
3052
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003053static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003054{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003055 struct net *net;
3056 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003057 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003058 synchronize_net();
3059 list_for_each_entry(net, net_exit_list, exit_list)
3060 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003061}
3062
3063static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003064 .init = xfrm_user_net_init,
3065 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003066};
3067
3068static int __init xfrm_user_init(void)
3069{
3070 int rv;
3071
3072 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3073
3074 rv = register_pernet_subsys(&xfrm_user_net_ops);
3075 if (rv < 0)
3076 return rv;
3077 rv = xfrm_register_km(&netlink_mgr);
3078 if (rv < 0)
3079 unregister_pernet_subsys(&xfrm_user_net_ops);
3080 return rv;
3081}
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083static void __exit xfrm_user_exit(void)
3084{
3085 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003086 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
3089module_init(xfrm_user_init);
3090module_exit(xfrm_user_exit);
3091MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003092MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003093