blob: 412d9dc3a8731ec580d4b6596fec491fb10a2a48 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Graf5424f322007-08-22 14:01:33 -070035static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Graf5424f322007-08-22 14:01:33 -070037 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct xfrm_algo *algp;
39
40 if (!rt)
41 return 0;
42
Thomas Graf5424f322007-08-22 14:01:33 -070043 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080044 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070045 return -EINVAL;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 switch (type) {
48 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52
53 default:
54 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
58 return 0;
59}
60
Martin Willi4447bb32009-11-25 00:29:52 +000061static int verify_auth_trunc(struct nlattr **attrs)
62{
63 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
64 struct xfrm_algo_auth *algp;
65
66 if (!rt)
67 return 0;
68
69 algp = nla_data(rt);
70 if (nla_len(rt) < xfrm_alg_auth_len(algp))
71 return -EINVAL;
72
73 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
74 return 0;
75}
76
Herbert Xu1a6509d2008-01-28 19:37:29 -080077static int verify_aead(struct nlattr **attrs)
78{
79 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
80 struct xfrm_algo_aead *algp;
81
82 if (!rt)
83 return 0;
84
85 algp = nla_data(rt);
86 if (nla_len(rt) < aead_len(algp))
87 return -EINVAL;
88
89 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
90 return 0;
91}
92
Thomas Graf5424f322007-08-22 14:01:33 -070093static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070094 xfrm_address_t **addrp)
95{
Thomas Graf5424f322007-08-22 14:01:33 -070096 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070097
Thomas Grafcf5cb792007-08-22 13:59:04 -070098 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070099 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100}
Trent Jaegerdf718372005-12-13 23:12:27 -0800101
Thomas Graf5424f322007-08-22 14:01:33 -0700102static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800103{
Thomas Graf5424f322007-08-22 14:01:33 -0700104 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800105 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
107 if (!rt)
108 return 0;
109
Thomas Graf5424f322007-08-22 14:01:33 -0700110 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700111 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800112 return -EINVAL;
113
114 return 0;
115}
116
Steffen Klassertd8647b72011-03-08 00:10:27 +0000117static inline int verify_replay(struct xfrm_usersa_info *p,
118 struct nlattr **attrs)
119{
120 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000121 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122
Mathias Krauseecd79182012-09-20 10:01:49 +0000123 if (p->flags & XFRM_STATE_ESN) {
124 if (!rt)
125 return -EINVAL;
126
127 rs = nla_data(rt);
128
129 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
130 return -EINVAL;
131
132 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
133 nla_len(rt) != sizeof(*rs))
134 return -EINVAL;
135 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000136
Steffen Klassertd8647b72011-03-08 00:10:27 +0000137 if (!rt)
138 return 0;
139
Fan Du01714102014-01-18 09:54:28 +0800140 /* As only ESP and AH support ESN feature. */
141 if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
Steffen Klassert02aadf72011-03-28 19:48:09 +0000142 return -EINVAL;
143
Steffen Klassertd8647b72011-03-08 00:10:27 +0000144 if (p->replay_window != 0)
145 return -EINVAL;
146
147 return 0;
148}
Trent Jaegerdf718372005-12-13 23:12:27 -0800149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700151 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int err;
154
155 err = -EINVAL;
156 switch (p->family) {
157 case AF_INET:
158 break;
159
160 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000161#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163#else
164 err = -EAFNOSUPPORT;
165 goto out;
166#endif
167
168 default:
169 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 err = -EINVAL;
173 switch (p->id.proto) {
174 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000175 if ((!attrs[XFRMA_ALG_AUTH] &&
176 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700178 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000179 attrs[XFRMA_ALG_COMP] ||
Fan Duea9884b2013-12-16 18:47:48 +0800180 attrs[XFRMA_TFCPAD] ||
181 (ntohl(p->id.spi) >= 0x10000))
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto out;
184 break;
185
186 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800187 if (attrs[XFRMA_ALG_COMP])
188 goto out;
189 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000190 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800191 !attrs[XFRMA_ALG_CRYPT] &&
192 !attrs[XFRMA_ALG_AEAD])
193 goto out;
194 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000195 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800196 attrs[XFRMA_ALG_CRYPT]) &&
197 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000199 if (attrs[XFRMA_TFCPAD] &&
200 p->mode != XFRM_MODE_TUNNEL)
201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
203
204 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700205 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800206 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000208 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000209 attrs[XFRMA_ALG_CRYPT] ||
210 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto out;
212 break;
213
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000214#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700215 case IPPROTO_DSTOPTS:
216 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700217 if (attrs[XFRMA_ALG_COMP] ||
218 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000219 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800220 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700221 attrs[XFRMA_ALG_CRYPT] ||
222 attrs[XFRMA_ENCAP] ||
223 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000224 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700226 goto out;
227 break;
228#endif
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 default:
231 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Herbert Xu1a6509d2008-01-28 19:37:29 -0800234 if ((err = verify_aead(attrs)))
235 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000236 if ((err = verify_auth_trunc(attrs)))
237 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700238 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800245 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000246 if ((err = verify_replay(p, attrs)))
247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 err = -EINVAL;
250 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700251 case XFRM_MODE_TRANSPORT:
252 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700253 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700254 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 break;
256
257 default:
258 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 err = 0;
262
263out:
264 return err;
265}
266
267static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800268 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700269 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 struct xfrm_algo *p, *ualg;
272 struct xfrm_algo_desc *algo;
273
274 if (!rta)
275 return 0;
276
Thomas Graf5424f322007-08-22 14:01:33 -0700277 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 algo = get_byname(ualg->alg_name, 1);
280 if (!algo)
281 return -ENOSYS;
282 *props = algo->desc.sadb_alg_id;
283
Eric Dumazet0f99be02008-01-08 23:39:06 -0800284 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (!p)
286 return -ENOMEM;
287
Herbert Xu04ff1262006-08-13 08:50:00 +1000288 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *algpp = p;
290 return 0;
291}
292
Martin Willi4447bb32009-11-25 00:29:52 +0000293static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
294 struct nlattr *rta)
295{
296 struct xfrm_algo *ualg;
297 struct xfrm_algo_auth *p;
298 struct xfrm_algo_desc *algo;
299
300 if (!rta)
301 return 0;
302
303 ualg = nla_data(rta);
304
305 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
306 if (!algo)
307 return -ENOSYS;
308 *props = algo->desc.sadb_alg_id;
309
310 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
311 if (!p)
312 return -ENOMEM;
313
314 strcpy(p->alg_name, algo->name);
315 p->alg_key_len = ualg->alg_key_len;
316 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
317 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
318
319 *algpp = p;
320 return 0;
321}
322
323static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
324 struct nlattr *rta)
325{
326 struct xfrm_algo_auth *p, *ualg;
327 struct xfrm_algo_desc *algo;
328
329 if (!rta)
330 return 0;
331
332 ualg = nla_data(rta);
333
334 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
335 if (!algo)
336 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000337 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
338 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000339 return -EINVAL;
340 *props = algo->desc.sadb_alg_id;
341
342 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
343 if (!p)
344 return -ENOMEM;
345
346 strcpy(p->alg_name, algo->name);
347 if (!p->alg_trunc_len)
348 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
349
350 *algpp = p;
351 return 0;
352}
353
Herbert Xu1a6509d2008-01-28 19:37:29 -0800354static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
355 struct nlattr *rta)
356{
357 struct xfrm_algo_aead *p, *ualg;
358 struct xfrm_algo_desc *algo;
359
360 if (!rta)
361 return 0;
362
363 ualg = nla_data(rta);
364
365 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
366 if (!algo)
367 return -ENOSYS;
368 *props = algo->desc.sadb_alg_id;
369
370 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
371 if (!p)
372 return -ENOMEM;
373
374 strcpy(p->alg_name, algo->name);
375 *algpp = p;
376 return 0;
377}
378
Steffen Klasserte2b19122011-03-28 19:47:30 +0000379static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
380 struct nlattr *rp)
381{
382 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000383 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000384
385 if (!replay_esn || !rp)
386 return 0;
387
388 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000389 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000390
Mathias Krauseecd79182012-09-20 10:01:49 +0000391 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000392 return -EINVAL;
393
394 return 0;
395}
396
Steffen Klassertd8647b72011-03-08 00:10:27 +0000397static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
398 struct xfrm_replay_state_esn **preplay_esn,
399 struct nlattr *rta)
400{
401 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000402 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000403
404 if (!rta)
405 return 0;
406
407 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000408 klen = xfrm_replay_state_esn_len(up);
409 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000410
Mathias Krauseecd79182012-09-20 10:01:49 +0000411 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412 if (!p)
413 return -ENOMEM;
414
Mathias Krauseecd79182012-09-20 10:01:49 +0000415 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000416 if (!pp) {
417 kfree(p);
418 return -ENOMEM;
419 }
420
Mathias Krauseecd79182012-09-20 10:01:49 +0000421 memcpy(p, up, ulen);
422 memcpy(pp, up, ulen);
423
Steffen Klassertd8647b72011-03-08 00:10:27 +0000424 *replay_esn = p;
425 *preplay_esn = pp;
426
427 return 0;
428}
429
Joy Latten661697f2007-04-13 16:14:35 -0700430static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800431{
Trent Jaegerdf718372005-12-13 23:12:27 -0800432 int len = 0;
433
434 if (xfrm_ctx) {
435 len += sizeof(struct xfrm_user_sec_ctx);
436 len += xfrm_ctx->ctx_len;
437 }
438 return len;
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
442{
443 memcpy(&x->id, &p->id, sizeof(x->id));
444 memcpy(&x->sel, &p->sel, sizeof(x->sel));
445 memcpy(&x->lft, &p->lft, sizeof(x->lft));
446 x->props.mode = p->mode;
Fan Du33fce602013-09-17 15:14:13 +0800447 x->props.replay_window = min_t(unsigned int, p->replay_window,
448 sizeof(x->replay.bitmap) * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 x->props.reqid = p->reqid;
450 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700451 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700453
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700454 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700455 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800458/*
459 * someday when pfkey also has support, we could have the code
460 * somehow made shareable and move it to xfrm_state.c - JHS
461 *
462*/
Mathias Krausee3ac1042012-09-19 11:33:43 +0000463static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
464 int update_esn)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800465{
Thomas Graf5424f322007-08-22 14:01:33 -0700466 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Mathias Krausee3ac1042012-09-19 11:33:43 +0000467 struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
Thomas Graf5424f322007-08-22 14:01:33 -0700468 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
469 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
470 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800471
Steffen Klassertd8647b72011-03-08 00:10:27 +0000472 if (re) {
473 struct xfrm_replay_state_esn *replay_esn;
474 replay_esn = nla_data(re);
475 memcpy(x->replay_esn, replay_esn,
476 xfrm_replay_state_esn_len(replay_esn));
477 memcpy(x->preplay_esn, replay_esn,
478 xfrm_replay_state_esn_len(replay_esn));
479 }
480
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800481 if (rp) {
482 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700483 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800484 memcpy(&x->replay, replay, sizeof(*replay));
485 memcpy(&x->preplay, replay, sizeof(*replay));
486 }
487
488 if (lt) {
489 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700490 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800491 x->curlft.bytes = ltime->bytes;
492 x->curlft.packets = ltime->packets;
493 x->curlft.add_time = ltime->add_time;
494 x->curlft.use_time = ltime->use_time;
495 }
496
Thomas Grafcf5cb792007-08-22 13:59:04 -0700497 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700498 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800499
Thomas Grafcf5cb792007-08-22 13:59:04 -0700500 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700501 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800502}
503
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800504static struct xfrm_state *xfrm_state_construct(struct net *net,
505 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700506 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int *errp)
508{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800509 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int err = -ENOMEM;
511
512 if (!x)
513 goto error_no_put;
514
515 copy_from_user_state(x, p);
516
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100517 if (attrs[XFRMA_SA_EXTRA_FLAGS])
518 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
519
Herbert Xu1a6509d2008-01-28 19:37:29 -0800520 if ((err = attach_aead(&x->aead, &x->props.ealgo,
521 attrs[XFRMA_ALG_AEAD])))
522 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000523 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
524 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000526 if (!x->props.aalgo) {
527 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
528 attrs[XFRMA_ALG_AUTH])))
529 goto error;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
532 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700533 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto error;
535 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
536 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700537 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700539
540 if (attrs[XFRMA_ENCAP]) {
541 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
542 sizeof(*x->encap), GFP_KERNEL);
543 if (x->encap == NULL)
544 goto error;
545 }
546
Martin Willi35d28562010-12-08 04:37:49 +0000547 if (attrs[XFRMA_TFCPAD])
548 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
549
Thomas Graffd211502007-09-06 03:28:08 -0700550 if (attrs[XFRMA_COADDR]) {
551 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
552 sizeof(*x->coaddr), GFP_KERNEL);
553 if (x->coaddr == NULL)
554 goto error;
555 }
556
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000557 xfrm_mark_get(attrs, &x->mark);
558
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700559 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (err)
561 goto error;
562
Thomas Graffd211502007-09-06 03:28:08 -0700563 if (attrs[XFRMA_SEC_CTX] &&
564 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800565 goto error;
566
Steffen Klassertd8647b72011-03-08 00:10:27 +0000567 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
568 attrs[XFRMA_REPLAY_ESN_VAL])))
569 goto error;
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800572 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800573 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800574 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800575
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000576 if ((err = xfrm_init_replay(x)))
577 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800578
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000579 /* override default values from above */
Mathias Krausee3ac1042012-09-19 11:33:43 +0000580 xfrm_update_ae_params(x, attrs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return x;
583
584error:
585 x->km.state = XFRM_STATE_DEAD;
586 xfrm_state_put(x);
587error_no_put:
588 *errp = err;
589 return NULL;
590}
591
Christoph Hellwig22e70052007-01-02 15:22:30 -0800592static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700593 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800595 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700596 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct xfrm_state *x;
598 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700599 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Thomas Graf35a7aa02007-08-22 14:00:40 -0700601 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (err)
603 return err;
604
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800605 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!x)
607 return err;
608
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700609 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
611 err = xfrm_state_add(x);
612 else
613 err = xfrm_state_update(x);
614
Tetsuo Handa2e710292014-04-22 21:48:30 +0900615 xfrm_audit_state_add(x, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -0600616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (err < 0) {
618 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800619 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700623 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000624 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700625 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700626
627 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700628out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700629 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return err;
631}
632
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800633static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
634 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700635 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700636 int *errp)
637{
638 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000639 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700640 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000641 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700642
643 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
644 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000645 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700646 } else {
647 xfrm_address_t *saddr = NULL;
648
Thomas Graf35a7aa02007-08-22 14:00:40 -0700649 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700650 if (!saddr) {
651 err = -EINVAL;
652 goto out;
653 }
654
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800655 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000656 x = xfrm_state_lookup_byaddr(net, mark,
657 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800658 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700659 }
660
661 out:
662 if (!x && errp)
663 *errp = err;
664 return x;
665}
666
Christoph Hellwig22e70052007-01-02 15:22:30 -0800667static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700668 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800670 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700672 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700673 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700674 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800676 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700678 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
David S. Miller6f68dc32006-06-08 23:58:52 -0700680 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700681 goto out;
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700684 err = -EPERM;
685 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700688 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600689
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700690 if (err < 0)
691 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700692
693 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000694 c.portid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700695 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700696 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700698out:
Tetsuo Handa2e710292014-04-22 21:48:30 +0900699 xfrm_audit_state_delete(x, err ? 0 : 1, true);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700700 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700701 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
705{
Mathias Krausef778a632012-09-19 11:33:39 +0000706 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 memcpy(&p->id, &x->id, sizeof(p->id));
708 memcpy(&p->sel, &x->sel, sizeof(p->sel));
709 memcpy(&p->lft, &x->lft, sizeof(p->lft));
710 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
711 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700712 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 p->mode = x->props.mode;
714 p->replay_window = x->props.replay_window;
715 p->reqid = x->props.reqid;
716 p->family = x->props.family;
717 p->flags = x->props.flags;
718 p->seq = x->km.seq;
719}
720
721struct xfrm_dump_info {
722 struct sk_buff *in_skb;
723 struct sk_buff *out_skb;
724 u32 nlmsg_seq;
725 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726};
727
Thomas Grafc0144be2007-08-22 13:55:43 -0700728static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
729{
Thomas Grafc0144be2007-08-22 13:55:43 -0700730 struct xfrm_user_sec_ctx *uctx;
731 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700732 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700733
734 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
735 if (attr == NULL)
736 return -EMSGSIZE;
737
738 uctx = nla_data(attr);
739 uctx->exttype = XFRMA_SEC_CTX;
740 uctx->len = ctx_size;
741 uctx->ctx_doi = s->ctx_doi;
742 uctx->ctx_alg = s->ctx_alg;
743 uctx->ctx_len = s->ctx_len;
744 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
745
746 return 0;
747}
748
Martin Willi4447bb32009-11-25 00:29:52 +0000749static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
750{
751 struct xfrm_algo *algo;
752 struct nlattr *nla;
753
754 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
755 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
756 if (!nla)
757 return -EMSGSIZE;
758
759 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000760 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000761 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
762 algo->alg_key_len = auth->alg_key_len;
763
764 return 0;
765}
766
Herbert Xu68325d32007-10-09 13:30:57 -0700767/* Don't change this without updating xfrm_sa_len! */
768static int copy_to_user_state_extra(struct xfrm_state *x,
769 struct xfrm_usersa_info *p,
770 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700772 int ret = 0;
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 copy_to_user_state(x, p);
775
Nicolas Dichtela947b0a2013-02-22 10:54:54 +0100776 if (x->props.extra_flags) {
777 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
778 x->props.extra_flags);
779 if (ret)
780 goto out;
781 }
782
David S. Miller1d1e34d2012-06-27 21:57:03 -0700783 if (x->coaddr) {
784 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
785 if (ret)
786 goto out;
787 }
788 if (x->lastused) {
789 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
790 if (ret)
791 goto out;
792 }
793 if (x->aead) {
794 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
795 if (ret)
796 goto out;
797 }
798 if (x->aalg) {
799 ret = copy_to_user_auth(x->aalg, skb);
800 if (!ret)
801 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
802 xfrm_alg_auth_len(x->aalg), x->aalg);
803 if (ret)
804 goto out;
805 }
806 if (x->ealg) {
807 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
808 if (ret)
809 goto out;
810 }
811 if (x->calg) {
812 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
813 if (ret)
814 goto out;
815 }
816 if (x->encap) {
817 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
818 if (ret)
819 goto out;
820 }
821 if (x->tfcpad) {
822 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
823 if (ret)
824 goto out;
825 }
826 ret = xfrm_mark_put(skb, &x->mark);
827 if (ret)
828 goto out;
829 if (x->replay_esn) {
830 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
831 xfrm_replay_state_esn_len(x->replay_esn),
832 x->replay_esn);
833 if (ret)
834 goto out;
835 }
836 if (x->security)
837 ret = copy_sec_ctx(x->security, skb);
838out:
839 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700840}
841
842static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
843{
844 struct xfrm_dump_info *sp = ptr;
845 struct sk_buff *in_skb = sp->in_skb;
846 struct sk_buff *skb = sp->out_skb;
847 struct xfrm_usersa_info *p;
848 struct nlmsghdr *nlh;
849 int err;
850
Eric W. Biederman15e47302012-09-07 20:12:54 +0000851 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Herbert Xu68325d32007-10-09 13:30:57 -0700852 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
853 if (nlh == NULL)
854 return -EMSGSIZE;
855
856 p = nlmsg_data(nlh);
857
858 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700859 if (err) {
860 nlmsg_cancel(skb, nlh);
861 return err;
862 }
Thomas Graf98250692007-08-22 12:47:26 -0700863 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Timo Teras4c563f72008-02-28 21:31:08 -0800867static int xfrm_dump_sa_done(struct netlink_callback *cb)
868{
869 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +0800870 struct sock *sk = cb->skb->sk;
871 struct net *net = sock_net(sk);
872
873 xfrm_state_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -0800874 return 0;
875}
876
Nicolas Dichteld3623092014-02-14 15:30:36 +0100877static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
879{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800880 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800881 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct xfrm_dump_info info;
883
Timo Teras4c563f72008-02-28 21:31:08 -0800884 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
885 sizeof(cb->args) - sizeof(cb->args[0]));
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 info.in_skb = cb->skb;
888 info.out_skb = skb;
889 info.nlmsg_seq = cb->nlh->nlmsg_seq;
890 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800891
892 if (!cb->args[0]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100893 struct nlattr *attrs[XFRMA_MAX+1];
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100894 struct xfrm_address_filter *filter = NULL;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100895 u8 proto = 0;
896 int err;
897
Timo Teras4c563f72008-02-28 21:31:08 -0800898 cb->args[0] = 1;
Nicolas Dichteld3623092014-02-14 15:30:36 +0100899
900 err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX,
901 xfrma_policy);
902 if (err < 0)
903 return err;
904
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100905 if (attrs[XFRMA_ADDRESS_FILTER]) {
Nicolas Dichteld3623092014-02-14 15:30:36 +0100906 filter = kmalloc(sizeof(*filter), GFP_KERNEL);
907 if (filter == NULL)
908 return -ENOMEM;
909
Nicolas Dichtel870a2df2014-03-06 18:24:29 +0100910 memcpy(filter, nla_data(attrs[XFRMA_ADDRESS_FILTER]),
Nicolas Dichteld3623092014-02-14 15:30:36 +0100911 sizeof(*filter));
912 }
913
914 if (attrs[XFRMA_PROTO])
915 proto = nla_get_u8(attrs[XFRMA_PROTO]);
916
917 xfrm_state_walk_init(walk, proto, filter);
Timo Teras4c563f72008-02-28 21:31:08 -0800918 }
919
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800920 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 return skb->len;
923}
924
925static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
926 struct xfrm_state *x, u32 seq)
927{
928 struct xfrm_dump_info info;
929 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000930 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Thomas Graf7deb2262007-08-22 13:57:39 -0700932 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (!skb)
934 return ERR_PTR(-ENOMEM);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 info.in_skb = in_skb;
937 info.out_skb = skb;
938 info.nlmsg_seq = seq;
939 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Mathias Krause864745d2012-09-13 11:41:26 +0000941 err = dump_one_state(x, 0, &info);
942 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000944 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 return skb;
948}
949
Michal Kubecek21ee5432014-06-03 10:26:06 +0200950/* A wrapper for nlmsg_multicast() checking that nlsk is still available.
951 * Must be called with RCU read lock.
952 */
953static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
954 u32 pid, unsigned int group)
955{
956 struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
957
958 if (nlsk)
959 return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
960 else
961 return -1;
962}
963
Thomas Graf7deb2262007-08-22 13:57:39 -0700964static inline size_t xfrm_spdinfo_msgsize(void)
965{
966 return NLMSG_ALIGN(4)
967 + nla_total_size(sizeof(struct xfrmu_spdinfo))
968 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
969}
970
Alexey Dobriyane0710412010-01-23 13:37:10 +0000971static int build_spdinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000972 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700973{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700974 struct xfrmk_spdinfo si;
975 struct xfrmu_spdinfo spc;
976 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700977 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700978 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700979 u32 *f;
980
Eric W. Biederman15e47302012-09-07 20:12:54 +0000981 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300982 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700983 return -EMSGSIZE;
984
985 f = nlmsg_data(nlh);
986 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000987 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700988 spc.incnt = si.incnt;
989 spc.outcnt = si.outcnt;
990 spc.fwdcnt = si.fwdcnt;
991 spc.inscnt = si.inscnt;
992 spc.outscnt = si.outscnt;
993 spc.fwdscnt = si.fwdscnt;
994 sph.spdhcnt = si.spdhcnt;
995 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700996
David S. Miller1d1e34d2012-06-27 21:57:03 -0700997 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
998 if (!err)
999 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
1000 if (err) {
1001 nlmsg_cancel(skb, nlh);
1002 return err;
1003 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001004
1005 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001006}
1007
1008static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001009 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001010{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001011 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001012 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001013 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001014 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001015 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001016
Thomas Graf7deb2262007-08-22 13:57:39 -07001017 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001018 if (r_skb == NULL)
1019 return -ENOMEM;
1020
Eric W. Biederman15e47302012-09-07 20:12:54 +00001021 if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001022 BUG();
1023
Eric W. Biederman15e47302012-09-07 20:12:54 +00001024 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001025}
1026
Thomas Graf7deb2262007-08-22 13:57:39 -07001027static inline size_t xfrm_sadinfo_msgsize(void)
1028{
1029 return NLMSG_ALIGN(4)
1030 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
1031 + nla_total_size(4); /* XFRMA_SAD_CNT */
1032}
1033
Alexey Dobriyane0710412010-01-23 13:37:10 +00001034static int build_sadinfo(struct sk_buff *skb, struct net *net,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001035 u32 portid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001036{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001037 struct xfrmk_sadinfo si;
1038 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001039 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001040 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001041 u32 *f;
1042
Eric W. Biederman15e47302012-09-07 20:12:54 +00001043 nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001044 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001045 return -EMSGSIZE;
1046
1047 f = nlmsg_data(nlh);
1048 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001049 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001050
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001051 sh.sadhmcnt = si.sadhmcnt;
1052 sh.sadhcnt = si.sadhcnt;
1053
David S. Miller1d1e34d2012-06-27 21:57:03 -07001054 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1055 if (!err)
1056 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1057 if (err) {
1058 nlmsg_cancel(skb, nlh);
1059 return err;
1060 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001061
1062 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001063}
1064
1065static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001066 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001067{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001068 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001069 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001070 u32 *flags = nlmsg_data(nlh);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001071 u32 sportid = NETLINK_CB(skb).portid;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001072 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001073
Thomas Graf7deb2262007-08-22 13:57:39 -07001074 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001075 if (r_skb == NULL)
1076 return -ENOMEM;
1077
Eric W. Biederman15e47302012-09-07 20:12:54 +00001078 if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001079 BUG();
1080
Eric W. Biederman15e47302012-09-07 20:12:54 +00001081 return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001082}
1083
Christoph Hellwig22e70052007-01-02 15:22:30 -08001084static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001085 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001087 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001088 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct xfrm_state *x;
1090 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001091 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001093 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (x == NULL)
1095 goto out_noput;
1096
1097 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1098 if (IS_ERR(resp_skb)) {
1099 err = PTR_ERR(resp_skb);
1100 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001101 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 xfrm_state_put(x);
1104out_noput:
1105 return err;
1106}
1107
Christoph Hellwig22e70052007-01-02 15:22:30 -08001108static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001109 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001111 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct xfrm_state *x;
1113 struct xfrm_userspi_info *p;
1114 struct sk_buff *resp_skb;
1115 xfrm_address_t *daddr;
1116 int family;
1117 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001118 u32 mark;
1119 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Thomas Graf7b67c852007-08-22 13:53:52 -07001121 p = nlmsg_data(nlh);
Fan Du776e9dd2013-12-16 18:47:49 +08001122 err = verify_spi_info(p->info.id.proto, p->min, p->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (err)
1124 goto out_noput;
1125
1126 family = p->info.family;
1127 daddr = &p->info.id.daddr;
1128
1129 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001130
1131 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001133 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00001134 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 xfrm_state_put(x);
1136 x = NULL;
1137 }
1138 }
1139
1140 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001141 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 p->info.id.proto, daddr,
1143 &p->info.saddr, 1,
1144 family);
1145 err = -ENOENT;
1146 if (x == NULL)
1147 goto out_noput;
1148
Herbert Xu658b2192007-10-09 13:29:52 -07001149 err = xfrm_alloc_spi(x, p->min, p->max);
1150 if (err)
1151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Herbert Xu658b2192007-10-09 13:29:52 -07001153 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (IS_ERR(resp_skb)) {
1155 err = PTR_ERR(resp_skb);
1156 goto out;
1157 }
1158
Eric W. Biederman15e47302012-09-07 20:12:54 +00001159 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161out:
1162 xfrm_state_put(x);
1163out_noput:
1164 return err;
1165}
1166
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001167static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 switch (dir) {
1170 case XFRM_POLICY_IN:
1171 case XFRM_POLICY_OUT:
1172 case XFRM_POLICY_FWD:
1173 break;
1174
1175 default:
1176 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 return 0;
1180}
1181
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001182static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001183{
1184 switch (type) {
1185 case XFRM_POLICY_TYPE_MAIN:
1186#ifdef CONFIG_XFRM_SUB_POLICY
1187 case XFRM_POLICY_TYPE_SUB:
1188#endif
1189 break;
1190
1191 default:
1192 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001193 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001194
1195 return 0;
1196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1199{
Fan Due682adf2013-11-07 17:47:48 +08001200 int ret;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 switch (p->share) {
1203 case XFRM_SHARE_ANY:
1204 case XFRM_SHARE_SESSION:
1205 case XFRM_SHARE_USER:
1206 case XFRM_SHARE_UNIQUE:
1207 break;
1208
1209 default:
1210 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 switch (p->action) {
1214 case XFRM_POLICY_ALLOW:
1215 case XFRM_POLICY_BLOCK:
1216 break;
1217
1218 default:
1219 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 switch (p->sel.family) {
1223 case AF_INET:
1224 break;
1225
1226 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001227#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 break;
1229#else
1230 return -EAFNOSUPPORT;
1231#endif
1232
1233 default:
1234 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Fan Due682adf2013-11-07 17:47:48 +08001237 ret = verify_policy_dir(p->dir);
1238 if (ret)
1239 return ret;
1240 if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
1241 return -EINVAL;
1242
1243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Thomas Graf5424f322007-08-22 14:01:33 -07001246static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001247{
Thomas Graf5424f322007-08-22 14:01:33 -07001248 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001249 struct xfrm_user_sec_ctx *uctx;
1250
1251 if (!rt)
1252 return 0;
1253
Thomas Graf5424f322007-08-22 14:01:33 -07001254 uctx = nla_data(rt);
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001255 return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
Trent Jaegerdf718372005-12-13 23:12:27 -08001256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1259 int nr)
1260{
1261 int i;
1262
1263 xp->xfrm_nr = nr;
1264 for (i = 0; i < nr; i++, ut++) {
1265 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1266
1267 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1268 memcpy(&t->saddr, &ut->saddr,
1269 sizeof(xfrm_address_t));
1270 t->reqid = ut->reqid;
1271 t->mode = ut->mode;
1272 t->share = ut->share;
1273 t->optional = ut->optional;
1274 t->aalgos = ut->aalgos;
1275 t->ealgos = ut->ealgos;
1276 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001277 /* If all masks are ~0, then we allow all algorithms. */
1278 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001279 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281}
1282
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001283static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1284{
1285 int i;
1286
1287 if (nr > XFRM_MAX_DEPTH)
1288 return -EINVAL;
1289
1290 for (i = 0; i < nr; i++) {
1291 /* We never validated the ut->family value, so many
1292 * applications simply leave it at zero. The check was
1293 * never made and ut->family was ignored because all
1294 * templates could be assumed to have the same family as
1295 * the policy itself. Now that we will have ipv4-in-ipv6
1296 * and ipv6-in-ipv4 tunnels, this is no longer true.
1297 */
1298 if (!ut[i].family)
1299 ut[i].family = family;
1300
1301 switch (ut[i].family) {
1302 case AF_INET:
1303 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001304#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001305 case AF_INET6:
1306 break;
1307#endif
1308 default:
1309 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001310 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001311 }
1312
1313 return 0;
1314}
1315
Thomas Graf5424f322007-08-22 14:01:33 -07001316static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Thomas Graf5424f322007-08-22 14:01:33 -07001318 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 if (!rt) {
1321 pol->xfrm_nr = 0;
1322 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001323 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1324 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001325 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001327 err = validate_tmpl(nr, utmpl, pol->family);
1328 if (err)
1329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Thomas Graf5424f322007-08-22 14:01:33 -07001331 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 return 0;
1334}
1335
Thomas Graf5424f322007-08-22 14:01:33 -07001336static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001337{
Thomas Graf5424f322007-08-22 14:01:33 -07001338 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001339 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001340 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001341 int err;
1342
1343 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001344 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001345 type = upt->type;
1346 }
1347
1348 err = verify_policy_type(type);
1349 if (err)
1350 return err;
1351
1352 *tp = type;
1353 return 0;
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1357{
1358 xp->priority = p->priority;
1359 xp->index = p->index;
1360 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1361 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1362 xp->action = p->action;
1363 xp->flags = p->flags;
1364 xp->family = p->sel.family;
1365 /* XXX xp->share = p->share; */
1366}
1367
1368static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1369{
Mathias Krause7b789832012-09-19 11:33:40 +00001370 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1372 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1373 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1374 p->priority = xp->priority;
1375 p->index = xp->index;
1376 p->sel.family = xp->family;
1377 p->dir = dir;
1378 p->action = xp->action;
1379 p->flags = xp->flags;
1380 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1381}
1382
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001383static 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 -07001384{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001385 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int err;
1387
1388 if (!xp) {
1389 *errp = -ENOMEM;
1390 return NULL;
1391 }
1392
1393 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001394
Thomas Graf35a7aa02007-08-22 14:00:40 -07001395 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001396 if (err)
1397 goto error;
1398
Thomas Graf35a7aa02007-08-22 14:00:40 -07001399 if (!(err = copy_from_user_tmpl(xp, attrs)))
1400 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001401 if (err)
1402 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001404 xfrm_mark_get(attrs, &xp->mark);
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001407 error:
1408 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001409 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001410 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001411 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Christoph Hellwig22e70052007-01-02 15:22:30 -08001414static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001415 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001417 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001418 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001420 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int err;
1422 int excl;
1423
1424 err = verify_newpolicy_info(p);
1425 if (err)
1426 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001427 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001428 if (err)
1429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001431 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!xp)
1433 return err;
1434
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001435 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001436 * Aha! this is anti-netlink really i.e more pfkey derived
1437 * in netlink excl is a flag and you wouldnt need
1438 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1440 err = xfrm_policy_insert(p->dir, xp, excl);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001441 xfrm_audit_policy_add(xp, err ? 0 : 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001444 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 kfree(xp);
1446 return err;
1447 }
1448
Herbert Xuf60f6b82005-06-18 22:44:37 -07001449 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001450 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001451 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001452 km_policy_notify(xp, p->dir, &c);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 xfrm_pol_put(xp);
1455
1456 return 0;
1457}
1458
1459static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1460{
1461 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1462 int i;
1463
1464 if (xp->xfrm_nr == 0)
1465 return 0;
1466
1467 for (i = 0; i < xp->xfrm_nr; i++) {
1468 struct xfrm_user_tmpl *up = &vec[i];
1469 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1470
Mathias Krause1f868402012-09-19 11:33:41 +00001471 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001473 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1475 up->reqid = kp->reqid;
1476 up->mode = kp->mode;
1477 up->share = kp->share;
1478 up->optional = kp->optional;
1479 up->aalgos = kp->aalgos;
1480 up->ealgos = kp->ealgos;
1481 up->calgos = kp->calgos;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Thomas Grafc0144be2007-08-22 13:55:43 -07001484 return nla_put(skb, XFRMA_TMPL,
1485 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001486}
1487
Serge Hallyn0d681622006-07-24 23:30:44 -07001488static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1489{
1490 if (x->security) {
1491 return copy_sec_ctx(x->security, skb);
1492 }
1493 return 0;
1494}
1495
1496static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1497{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001498 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001499 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001500 return 0;
1501}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001502static inline size_t userpolicy_type_attrsize(void)
1503{
1504#ifdef CONFIG_XFRM_SUB_POLICY
1505 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1506#else
1507 return 0;
1508#endif
1509}
Serge Hallyn0d681622006-07-24 23:30:44 -07001510
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001511#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001512static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001513{
Thomas Grafc0144be2007-08-22 13:55:43 -07001514 struct xfrm_userpolicy_type upt = {
1515 .type = type,
1516 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001517
Thomas Grafc0144be2007-08-22 13:55:43 -07001518 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001519}
1520
1521#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001522static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001523{
1524 return 0;
1525}
1526#endif
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1529{
1530 struct xfrm_dump_info *sp = ptr;
1531 struct xfrm_userpolicy_info *p;
1532 struct sk_buff *in_skb = sp->in_skb;
1533 struct sk_buff *skb = sp->out_skb;
1534 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Eric W. Biederman15e47302012-09-07 20:12:54 +00001537 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001538 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1539 if (nlh == NULL)
1540 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Thomas Graf7b67c852007-08-22 13:53:52 -07001542 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001544 err = copy_to_user_tmpl(xp, skb);
1545 if (!err)
1546 err = copy_to_user_sec_ctx(xp, skb);
1547 if (!err)
1548 err = copy_to_user_policy_type(xp->type, skb);
1549 if (!err)
1550 err = xfrm_mark_put(skb, &xp->mark);
1551 if (err) {
1552 nlmsg_cancel(skb, nlh);
1553 return err;
1554 }
Thomas Graf98250692007-08-22 12:47:26 -07001555 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Timo Teras4c563f72008-02-28 21:31:08 -08001559static int xfrm_dump_policy_done(struct netlink_callback *cb)
1560{
1561 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Fan Du283bc9f2013-11-07 17:47:50 +08001562 struct net *net = sock_net(cb->skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001563
Fan Du283bc9f2013-11-07 17:47:50 +08001564 xfrm_policy_walk_done(walk, net);
Timo Teras4c563f72008-02-28 21:31:08 -08001565 return 0;
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1569{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001570 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001571 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct xfrm_dump_info info;
1573
Timo Teras4c563f72008-02-28 21:31:08 -08001574 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1575 sizeof(cb->args) - sizeof(cb->args[0]));
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 info.in_skb = cb->skb;
1578 info.out_skb = skb;
1579 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1580 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001581
1582 if (!cb->args[0]) {
1583 cb->args[0] = 1;
1584 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1585 }
1586
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001587 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 return skb->len;
1590}
1591
1592static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1593 struct xfrm_policy *xp,
1594 int dir, u32 seq)
1595{
1596 struct xfrm_dump_info info;
1597 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001598 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Thomas Graf7deb2262007-08-22 13:57:39 -07001600 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!skb)
1602 return ERR_PTR(-ENOMEM);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 info.in_skb = in_skb;
1605 info.out_skb = skb;
1606 info.nlmsg_seq = seq;
1607 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Mathias Krausec2546372012-09-14 09:58:32 +00001609 err = dump_one_policy(xp, dir, 0, &info);
1610 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001612 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614
1615 return skb;
1616}
1617
Christoph Hellwig22e70052007-01-02 15:22:30 -08001618static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001619 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001621 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct xfrm_policy *xp;
1623 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001624 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001626 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001628 struct xfrm_mark m;
1629 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Thomas Graf7b67c852007-08-22 13:53:52 -07001631 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1633
Thomas Graf35a7aa02007-08-22 14:00:40 -07001634 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001635 if (err)
1636 return err;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 err = verify_policy_dir(p->dir);
1639 if (err)
1640 return err;
1641
1642 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001643 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001644 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001645 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001646 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001647
Thomas Graf35a7aa02007-08-22 14:00:40 -07001648 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001649 if (err)
1650 return err;
1651
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001652 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001653 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001654 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001655
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001656 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001657 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001658 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001659 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001660 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001661 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001662 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (xp == NULL)
1665 return -ENOENT;
1666
1667 if (!delete) {
1668 struct sk_buff *resp_skb;
1669
1670 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1671 if (IS_ERR(resp_skb)) {
1672 err = PTR_ERR(resp_skb);
1673 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001674 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001675 NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001677 } else {
Tetsuo Handa2e710292014-04-22 21:48:30 +09001678 xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001679
1680 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001681 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001682
Herbert Xue7443892005-06-18 22:44:18 -07001683 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001684 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001685 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001686 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001687 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001690out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001691 xfrm_pol_put(xp);
Paul Mooree4c17212013-05-29 07:36:25 +00001692 if (delete && err == 0)
1693 xfrm_garbage_collect(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return err;
1695}
1696
Christoph Hellwig22e70052007-01-02 15:22:30 -08001697static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001698 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001700 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001701 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001702 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten4aa2e622007-06-04 19:05:57 -04001703 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Tetsuo Handa2e710292014-04-22 21:48:30 +09001705 err = xfrm_state_flush(net, p->proto, true);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001706 if (err) {
1707 if (err == -ESRCH) /* empty table */
1708 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001709 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001710 }
Herbert Xubf088672005-06-18 22:44:00 -07001711 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001712 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001713 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001714 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001715 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001716 km_state_notify(NULL, &c);
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719}
1720
Steffen Klassertd8647b72011-03-08 00:10:27 +00001721static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001722{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001723 size_t replay_size = x->replay_esn ?
1724 xfrm_replay_state_esn_len(x->replay_esn) :
1725 sizeof(struct xfrm_replay_state);
1726
Thomas Graf7deb2262007-08-22 13:57:39 -07001727 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001728 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001729 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001730 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001731 + nla_total_size(4) /* XFRM_AE_RTHR */
1732 + nla_total_size(4); /* XFRM_AE_ETHR */
1733}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001734
David S. Miller214e0052011-02-24 00:02:38 -05001735static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001736{
1737 struct xfrm_aevent_id *id;
1738 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001739 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001740
Eric W. Biederman15e47302012-09-07 20:12:54 +00001741 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001742 if (nlh == NULL)
1743 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001744
Thomas Graf7b67c852007-08-22 13:53:52 -07001745 id = nlmsg_data(nlh);
Weilong Chen9b7a7872013-12-24 09:43:46 +08001746 memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001747 id->sa_id.spi = x->id.spi;
1748 id->sa_id.family = x->props.family;
1749 id->sa_id.proto = x->id.proto;
Weilong Chen9b7a7872013-12-24 09:43:46 +08001750 memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001751 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001752 id->flags = c->data.aevent;
1753
David S. Millerd0fde792012-03-29 04:02:26 -04001754 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001755 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1756 xfrm_replay_state_esn_len(x->replay_esn),
1757 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001758 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001759 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1760 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001761 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001762 if (err)
1763 goto out_cancel;
1764 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1765 if (err)
1766 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001767
David S. Miller1d1e34d2012-06-27 21:57:03 -07001768 if (id->flags & XFRM_AE_RTHR) {
1769 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1770 if (err)
1771 goto out_cancel;
1772 }
1773 if (id->flags & XFRM_AE_ETHR) {
1774 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1775 x->replay_maxage * 10 / HZ);
1776 if (err)
1777 goto out_cancel;
1778 }
1779 err = xfrm_mark_put(skb, &x->mark);
1780 if (err)
1781 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001782
Thomas Graf98250692007-08-22 12:47:26 -07001783 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001784
David S. Miller1d1e34d2012-06-27 21:57:03 -07001785out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001786 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001787 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001788}
1789
Christoph Hellwig22e70052007-01-02 15:22:30 -08001790static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001791 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001792{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001793 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001794 struct xfrm_state *x;
1795 struct sk_buff *r_skb;
1796 int err;
1797 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001798 u32 mark;
1799 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001800 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001801 struct xfrm_usersa_id *id = &p->sa_id;
1802
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001803 mark = xfrm_mark_get(attrs, &m);
1804
1805 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001806 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001807 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001808
1809 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1810 if (r_skb == NULL) {
1811 xfrm_state_put(x);
1812 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001813 }
1814
1815 /*
1816 * XXX: is this lock really needed - none of the other
1817 * gets lock (the concern is things getting updated
1818 * while we are still reading) - jhs
1819 */
1820 spin_lock_bh(&x->lock);
1821 c.data.aevent = p->flags;
1822 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001823 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001824
1825 if (build_aevent(r_skb, x, &c) < 0)
1826 BUG();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001827 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001828 spin_unlock_bh(&x->lock);
1829 xfrm_state_put(x);
1830 return err;
1831}
1832
Christoph Hellwig22e70052007-01-02 15:22:30 -08001833static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001834 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001835{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001836 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001837 struct xfrm_state *x;
1838 struct km_event c;
Weilong Chen02d08922013-12-24 09:43:48 +08001839 int err = -EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001840 u32 mark = 0;
1841 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001842 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001843 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001844 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001845 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001846
Steffen Klassertd8647b72011-03-08 00:10:27 +00001847 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001848 return err;
1849
1850 /* pedantic mode - thou shalt sayeth replaceth */
1851 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1852 return err;
1853
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001854 mark = xfrm_mark_get(attrs, &m);
1855
1856 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 -08001857 if (x == NULL)
1858 return -ESRCH;
1859
1860 if (x->km.state != XFRM_STATE_VALID)
1861 goto out;
1862
Steffen Klassert4479ff72013-09-09 09:39:01 +02001863 err = xfrm_replay_verify_len(x->replay_esn, re);
Steffen Klasserte2b19122011-03-28 19:47:30 +00001864 if (err)
1865 goto out;
1866
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001867 spin_lock_bh(&x->lock);
Mathias Krausee3ac1042012-09-19 11:33:43 +00001868 xfrm_update_ae_params(x, attrs, 1);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001869 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001870
1871 c.event = nlh->nlmsg_type;
1872 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001873 c.portid = nlh->nlmsg_pid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001874 c.data.aevent = XFRM_AE_CU;
1875 km_state_notify(x, &c);
1876 err = 0;
1877out:
1878 xfrm_state_put(x);
1879 return err;
1880}
1881
Christoph Hellwig22e70052007-01-02 15:22:30 -08001882static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001883 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001885 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001886 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001887 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001888 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001889
Thomas Graf35a7aa02007-08-22 14:00:40 -07001890 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001891 if (err)
1892 return err;
1893
Tetsuo Handa2e710292014-04-22 21:48:30 +09001894 err = xfrm_policy_flush(net, type, true);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001895 if (err) {
1896 if (err == -ESRCH) /* empty table */
1897 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001898 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001899 }
1900
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001901 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001902 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001903 c.seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001904 c.portid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001905 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001906 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return 0;
1908}
1909
Christoph Hellwig22e70052007-01-02 15:22:30 -08001910static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001911 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001912{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001913 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001914 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001915 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001916 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001917 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001918 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001919 struct xfrm_mark m;
1920 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001921
Thomas Graf35a7aa02007-08-22 14:00:40 -07001922 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001923 if (err)
1924 return err;
1925
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001926 err = verify_policy_dir(p->dir);
1927 if (err)
1928 return err;
1929
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001931 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001932 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001933 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001934 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001935
Thomas Graf35a7aa02007-08-22 14:00:40 -07001936 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001937 if (err)
1938 return err;
1939
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001940 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001941 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001942 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001943
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001944 err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
Paul Moore03e1ad72008-04-12 19:07:52 -07001945 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001946 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001947 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001948 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001949 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001950 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001951 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001952 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001953 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001954
Timo Teräsea2dea92010-03-31 00:17:05 +00001955 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001956 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001957
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 err = 0;
1959 if (up->hard) {
1960 xfrm_policy_delete(xp, p->dir);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001961 xfrm_audit_policy_delete(xp, 1, true);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001962 } else {
1963 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001964 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001965 }
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001966 km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001967
1968out:
1969 xfrm_pol_put(xp);
1970 return err;
1971}
1972
Christoph Hellwig22e70052007-01-02 15:22:30 -08001973static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001974 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001975{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001976 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001977 struct xfrm_state *x;
1978 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001979 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001980 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001981 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001982 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001983
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001984 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001985
David S. Miller3a765aa2007-02-26 14:52:21 -08001986 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001987 if (x == NULL)
1988 return err;
1989
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001990 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001991 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001992 if (x->km.state != XFRM_STATE_VALID)
1993 goto out;
Eric W. Biedermanc6bb8132012-09-07 21:17:17 +00001994 km_state_expired(x, ue->hard, nlh->nlmsg_pid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001995
Joy Latten161a09e2006-11-27 13:11:54 -06001996 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001997 __xfrm_state_delete(x);
Tetsuo Handa2e710292014-04-22 21:48:30 +09001998 xfrm_audit_state_delete(x, 1, true);
Joy Latten161a09e2006-11-27 13:11:54 -06001999 }
David S. Miller3a765aa2007-02-26 14:52:21 -08002000 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002001out:
2002 spin_unlock_bh(&x->lock);
2003 xfrm_state_put(x);
2004 return err;
2005}
2006
Christoph Hellwig22e70052007-01-02 15:22:30 -08002007static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002008 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002009{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002010 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002011 struct xfrm_policy *xp;
2012 struct xfrm_user_tmpl *ut;
2013 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002014 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002015 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002016
Thomas Graf7b67c852007-08-22 13:53:52 -07002017 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002018 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002019 int err = -ENOMEM;
2020
2021 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002022 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002023
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002024 xfrm_mark_get(attrs, &mark);
2025
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002026 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002027 if (err)
2028 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002029
2030 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002031 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002032 if (!xp)
2033 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002034
2035 memcpy(&x->id, &ua->id, sizeof(ua->id));
2036 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2037 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002038 xp->mark.m = x->mark.m = mark.m;
2039 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002040 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002041 /* extract the templates and for each call km_key */
2042 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2043 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2044 memcpy(&x->id, &t->id, sizeof(x->id));
2045 x->props.mode = t->mode;
2046 x->props.reqid = t->reqid;
2047 x->props.family = ut->family;
2048 t->aalgos = ua->aalgos;
2049 t->ealgos = ua->ealgos;
2050 t->calgos = ua->calgos;
2051 err = km_query(x, t, xp);
2052
2053 }
2054
2055 kfree(x);
2056 kfree(xp);
2057
2058 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002059
2060bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002061 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002062free_state:
2063 kfree(x);
2064nomem:
2065 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002066}
2067
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002068#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002070 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002071 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072{
Thomas Graf5424f322007-08-22 14:01:33 -07002073 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002074 struct xfrm_user_migrate *um;
2075 int i, num_migrate;
2076
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002077 if (k != NULL) {
2078 struct xfrm_user_kmaddress *uk;
2079
2080 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2081 memcpy(&k->local, &uk->local, sizeof(k->local));
2082 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2083 k->family = uk->family;
2084 k->reserved = uk->reserved;
2085 }
2086
Thomas Graf5424f322007-08-22 14:01:33 -07002087 um = nla_data(rt);
2088 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002089
2090 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2091 return -EINVAL;
2092
2093 for (i = 0; i < num_migrate; i++, um++, ma++) {
2094 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2095 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2096 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2097 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2098
2099 ma->proto = um->proto;
2100 ma->mode = um->mode;
2101 ma->reqid = um->reqid;
2102
2103 ma->old_family = um->old_family;
2104 ma->new_family = um->new_family;
2105 }
2106
2107 *num = i;
2108 return 0;
2109}
2110
2111static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002112 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002113{
Thomas Graf7b67c852007-08-22 13:53:52 -07002114 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002115 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002116 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002117 u8 type;
2118 int err;
2119 int n = 0;
Fan Du8d549c42013-11-07 17:47:49 +08002120 struct net *net = sock_net(skb->sk);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002121
Thomas Graf35a7aa02007-08-22 14:00:40 -07002122 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002123 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002124
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002125 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2126
Thomas Graf5424f322007-08-22 14:01:33 -07002127 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002128 if (err)
2129 return err;
2130
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002131 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002132 if (err)
2133 return err;
2134
2135 if (!n)
2136 return 0;
2137
Fan Du8d549c42013-11-07 17:47:49 +08002138 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002139
2140 return 0;
2141}
2142#else
2143static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002144 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002145{
2146 return -ENOPROTOOPT;
2147}
2148#endif
2149
2150#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002151static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152{
2153 struct xfrm_user_migrate um;
2154
2155 memset(&um, 0, sizeof(um));
2156 um.proto = m->proto;
2157 um.mode = m->mode;
2158 um.reqid = m->reqid;
2159 um.old_family = m->old_family;
2160 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2161 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2162 um.new_family = m->new_family;
2163 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2164 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2165
Thomas Grafc0144be2007-08-22 13:55:43 -07002166 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002167}
2168
David S. Miller183cad12011-02-24 00:28:01 -05002169static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002170{
2171 struct xfrm_user_kmaddress uk;
2172
2173 memset(&uk, 0, sizeof(uk));
2174 uk.family = k->family;
2175 uk.reserved = k->reserved;
2176 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002177 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002178
2179 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2180}
2181
2182static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002183{
2184 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002185 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2186 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2187 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002188}
2189
David S. Miller183cad12011-02-24 00:28:01 -05002190static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2191 int num_migrate, const struct xfrm_kmaddress *k,
2192 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002193{
David S. Miller183cad12011-02-24 00:28:01 -05002194 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002195 struct xfrm_userpolicy_id *pol_id;
2196 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002197 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002198
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002199 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2200 if (nlh == NULL)
2201 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002202
Thomas Graf7b67c852007-08-22 13:53:52 -07002203 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002204 /* copy data from selector, dir, and type to the pol_id */
2205 memset(pol_id, 0, sizeof(*pol_id));
2206 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2207 pol_id->dir = dir;
2208
David S. Miller1d1e34d2012-06-27 21:57:03 -07002209 if (k != NULL) {
2210 err = copy_to_user_kmaddress(k, skb);
2211 if (err)
2212 goto out_cancel;
2213 }
2214 err = copy_to_user_policy_type(type, skb);
2215 if (err)
2216 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002217 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002218 err = copy_to_user_migrate(mp, skb);
2219 if (err)
2220 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002221 }
2222
Thomas Graf98250692007-08-22 12:47:26 -07002223 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002224
2225out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002226 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002227 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002228}
2229
David S. Miller183cad12011-02-24 00:28:01 -05002230static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2231 const struct xfrm_migrate *m, int num_migrate,
2232 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002233{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002234 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002236
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002237 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002238 if (skb == NULL)
2239 return -ENOMEM;
2240
2241 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002242 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002243 BUG();
2244
Michal Kubecek21ee5432014-06-03 10:26:06 +02002245 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002246}
2247#else
David S. Miller183cad12011-02-24 00:28:01 -05002248static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2249 const struct xfrm_migrate *m, int num_migrate,
2250 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002251{
2252 return -ENOPROTOOPT;
2253}
2254#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002255
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002256#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002257
2258static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002259 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002260 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2261 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2262 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2263 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2264 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2265 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002266 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002267 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002268 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002269 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002270 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002271 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002272 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002273 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2274 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002275 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002276 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002277 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2278 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279};
2280
Thomas Graf492b5582005-05-03 14:26:40 -07002281#undef XMSGSIZE
2282
Thomas Grafcf5cb792007-08-22 13:59:04 -07002283static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002284 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2285 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2286 [XFRMA_LASTUSED] = { .type = NLA_U64},
2287 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002288 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002289 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2290 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2291 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2292 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2293 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2294 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2295 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2296 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2297 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2298 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2299 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2300 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2301 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2302 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002303 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002304 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002305 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002306 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002307 [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
Nicolas Dichteld3623092014-02-14 15:30:36 +01002308 [XFRMA_PROTO] = { .type = NLA_U8 },
Nicolas Dichtel870a2df2014-03-06 18:24:29 +01002309 [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002310};
2311
Mathias Krause05600a72013-02-24 14:10:27 +01002312static const struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002313 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002315 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002316} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2317 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2318 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2319 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002320 .dump = xfrm_dump_sa,
2321 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002322 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2323 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2324 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002325 .dump = xfrm_dump_policy,
2326 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002327 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002328 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002329 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002330 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2331 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002332 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002333 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2334 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002335 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2336 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002337 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002338 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002339 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340};
2341
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002342static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002344 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002345 struct nlattr *attrs[XFRMA_MAX+1];
Mathias Krause05600a72013-02-24 14:10:27 +01002346 const struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002347 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 type -= XFRM_MSG_BASE;
2354 link = &xfrm_dispatch[type];
2355
2356 /* All operations require privileges, even GET */
Eric W. Biederman90f62cf2014-04-23 14:29:27 -07002357 if (!netlink_net_capable(skb, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002358 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Thomas Graf492b5582005-05-03 14:26:40 -07002360 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2361 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002362 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002366 {
2367 struct netlink_dump_control c = {
2368 .dump = link->dump,
2369 .done = link->done,
2370 };
2371 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
2374
Thomas Graf35a7aa02007-08-22 14:00:40 -07002375 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002376 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002377 if (err < 0)
2378 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Thomas Graf5424f322007-08-22 14:01:33 -07002383 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002386static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Fan Du283bc9f2013-11-07 17:47:50 +08002388 struct net *net = sock_net(skb->sk);
2389
2390 mutex_lock(&net->xfrm.xfrm_cfg_mutex);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002391 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
Fan Du283bc9f2013-11-07 17:47:50 +08002392 mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Thomas Graf7deb2262007-08-22 13:57:39 -07002395static inline size_t xfrm_expire_msgsize(void)
2396{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002397 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2398 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002399}
2400
David S. Miller214e0052011-02-24 00:02:38 -05002401static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
2403 struct xfrm_user_expire *ue;
2404 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002405 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Eric W. Biederman15e47302012-09-07 20:12:54 +00002407 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002408 if (nlh == NULL)
2409 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Thomas Graf7b67c852007-08-22 13:53:52 -07002411 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002413 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
David S. Miller1d1e34d2012-06-27 21:57:03 -07002415 err = xfrm_mark_put(skb, &x->mark);
2416 if (err)
2417 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002418
Thomas Graf98250692007-08-22 12:47:26 -07002419 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
David S. Miller214e0052011-02-24 00:02:38 -05002422static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002424 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 struct sk_buff *skb;
2426
Thomas Graf7deb2262007-08-22 13:57:39 -07002427 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (skb == NULL)
2429 return -ENOMEM;
2430
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002431 if (build_expire(skb, x, c) < 0) {
2432 kfree_skb(skb);
2433 return -EMSGSIZE;
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Michal Kubecek21ee5432014-06-03 10:26:06 +02002436 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
David S. Miller214e0052011-02-24 00:02:38 -05002439static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002440{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002441 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002442 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002443
Steffen Klassertd8647b72011-03-08 00:10:27 +00002444 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002445 if (skb == NULL)
2446 return -ENOMEM;
2447
2448 if (build_aevent(skb, x, c) < 0)
2449 BUG();
2450
Michal Kubecek21ee5432014-06-03 10:26:06 +02002451 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002452}
2453
David S. Miller214e0052011-02-24 00:02:38 -05002454static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002455{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002456 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457 struct xfrm_usersa_flush *p;
2458 struct nlmsghdr *nlh;
2459 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002460 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002461
Thomas Graf7deb2262007-08-22 13:57:39 -07002462 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463 if (skb == NULL)
2464 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002465
Eric W. Biederman15e47302012-09-07 20:12:54 +00002466 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002467 if (nlh == NULL) {
2468 kfree_skb(skb);
2469 return -EMSGSIZE;
2470 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002471
Thomas Graf7b67c852007-08-22 13:53:52 -07002472 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002473 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002474
Thomas Graf98250692007-08-22 12:47:26 -07002475 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002476
Michal Kubecek21ee5432014-06-03 10:26:06 +02002477 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002478}
2479
Thomas Graf7deb2262007-08-22 13:57:39 -07002480static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002481{
Thomas Graf7deb2262007-08-22 13:57:39 -07002482 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002483 if (x->aead)
2484 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002485 if (x->aalg) {
2486 l += nla_total_size(sizeof(struct xfrm_algo) +
2487 (x->aalg->alg_key_len + 7) / 8);
2488 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2489 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002490 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002491 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002492 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002493 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002494 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002495 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002496 if (x->tfcpad)
2497 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002498 if (x->replay_esn)
2499 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002500 if (x->security)
2501 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2502 x->security->ctx_len);
2503 if (x->coaddr)
2504 l += nla_total_size(sizeof(*x->coaddr));
Nicolas Dichtela947b0a2013-02-22 10:54:54 +01002505 if (x->props.extra_flags)
2506 l += nla_total_size(sizeof(x->props.extra_flags));
Herbert Xu68325d32007-10-09 13:30:57 -07002507
Herbert Xud26f3982007-11-13 21:47:08 -08002508 /* Must count x->lastused as it may become non-zero behind our back. */
2509 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002510
2511 return l;
2512}
2513
David S. Miller214e0052011-02-24 00:02:38 -05002514static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002515{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002516 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002517 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002518 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002519 struct nlmsghdr *nlh;
2520 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002522 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002523
2524 headlen = sizeof(*p);
2525 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002526 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002527 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002528 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002529 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002530 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002531
Thomas Graf7deb2262007-08-22 13:57:39 -07002532 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002533 if (skb == NULL)
2534 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002535
Eric W. Biederman15e47302012-09-07 20:12:54 +00002536 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002537 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002538 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002539 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002540
Thomas Graf7b67c852007-08-22 13:53:52 -07002541 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002542 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002543 struct nlattr *attr;
2544
Thomas Graf7b67c852007-08-22 13:53:52 -07002545 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002546 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2547 id->spi = x->id.spi;
2548 id->family = x->props.family;
2549 id->proto = x->id.proto;
2550
Thomas Grafc0144be2007-08-22 13:55:43 -07002551 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002552 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002553 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002554 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002555
2556 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002557 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002558 err = copy_to_user_state_extra(x, p, skb);
2559 if (err)
2560 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002561
Thomas Graf98250692007-08-22 12:47:26 -07002562 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002563
Michal Kubecek21ee5432014-06-03 10:26:06 +02002564 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002565
David S. Miller1d1e34d2012-06-27 21:57:03 -07002566out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002568 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569}
2570
David S. Miller214e0052011-02-24 00:02:38 -05002571static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002572{
2573
2574 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002575 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002576 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002577 case XFRM_MSG_NEWAE:
2578 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002579 case XFRM_MSG_DELSA:
2580 case XFRM_MSG_UPDSA:
2581 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002582 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002583 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002584 return xfrm_notify_sa_flush(c);
2585 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002586 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2587 c->event);
2588 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002589 }
2590
2591 return 0;
2592
2593}
2594
Thomas Graf7deb2262007-08-22 13:57:39 -07002595static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2596 struct xfrm_policy *xp)
2597{
2598 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2599 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002600 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002601 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2602 + userpolicy_type_attrsize();
2603}
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
Fan Du65e07362012-08-15 10:13:47 +08002606 struct xfrm_tmpl *xt, struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002608 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 struct xfrm_user_acquire *ua;
2610 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002611 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002613 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2614 if (nlh == NULL)
2615 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Thomas Graf7b67c852007-08-22 13:53:52 -07002617 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 memcpy(&ua->id, &x->id, sizeof(ua->id));
2619 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2620 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
Fan Du65e07362012-08-15 10:13:47 +08002621 copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 ua->aalgos = xt->aalgos;
2623 ua->ealgos = xt->ealgos;
2624 ua->calgos = xt->calgos;
2625 ua->seq = x->km.seq = seq;
2626
David S. Miller1d1e34d2012-06-27 21:57:03 -07002627 err = copy_to_user_tmpl(xp, skb);
2628 if (!err)
2629 err = copy_to_user_state_sec_ctx(x, skb);
2630 if (!err)
2631 err = copy_to_user_policy_type(xp->type, skb);
2632 if (!err)
2633 err = xfrm_mark_put(skb, &xp->mark);
2634 if (err) {
2635 nlmsg_cancel(skb, nlh);
2636 return err;
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Thomas Graf98250692007-08-22 12:47:26 -07002639 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
2642static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
Fan Du65e07362012-08-15 10:13:47 +08002643 struct xfrm_policy *xp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002645 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Thomas Graf7deb2262007-08-22 13:57:39 -07002648 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (skb == NULL)
2650 return -ENOMEM;
2651
Fan Du65e07362012-08-15 10:13:47 +08002652 if (build_acquire(skb, x, xt, xp) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 BUG();
2654
Michal Kubecek21ee5432014-06-03 10:26:06 +02002655 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/* User gives us xfrm_user_policy_info followed by an array of 0
2659 * or more templates.
2660 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002661static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 u8 *data, int len, int *dir)
2663{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002664 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2666 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2667 struct xfrm_policy *xp;
2668 int nr;
2669
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002670 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 case AF_INET:
2672 if (opt != IP_XFRM_POLICY) {
2673 *dir = -EOPNOTSUPP;
2674 return NULL;
2675 }
2676 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002677#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 case AF_INET6:
2679 if (opt != IPV6_XFRM_POLICY) {
2680 *dir = -EOPNOTSUPP;
2681 return NULL;
2682 }
2683 break;
2684#endif
2685 default:
2686 *dir = -EINVAL;
2687 return NULL;
2688 }
2689
2690 *dir = -EINVAL;
2691
2692 if (len < sizeof(*p) ||
2693 verify_newpolicy_info(p))
2694 return NULL;
2695
2696 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002697 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return NULL;
2699
Herbert Xua4f1bac2005-07-26 15:43:17 -07002700 if (p->dir > XFRM_POLICY_OUT)
2701 return NULL;
2702
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002703 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (xp == NULL) {
2705 *dir = -ENOBUFS;
2706 return NULL;
2707 }
2708
2709 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002710 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 copy_templates(xp, ut, nr);
2712
2713 *dir = p->dir;
2714
2715 return xp;
2716}
2717
Thomas Graf7deb2262007-08-22 13:57:39 -07002718static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2719{
2720 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2721 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2722 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002723 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002724 + userpolicy_type_attrsize();
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002728 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002731 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002732 struct nlmsghdr *nlh;
2733 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Eric W. Biederman15e47302012-09-07 20:12:54 +00002735 nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002736 if (nlh == NULL)
2737 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Thomas Graf7b67c852007-08-22 13:53:52 -07002739 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002741 err = copy_to_user_tmpl(xp, skb);
2742 if (!err)
2743 err = copy_to_user_sec_ctx(xp, skb);
2744 if (!err)
2745 err = copy_to_user_policy_type(xp->type, skb);
2746 if (!err)
2747 err = xfrm_mark_put(skb, &xp->mark);
2748 if (err) {
2749 nlmsg_cancel(skb, nlh);
2750 return err;
2751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 upe->hard = !!hard;
2753
Thomas Graf98250692007-08-22 12:47:26 -07002754 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
David S. Miller214e0052011-02-24 00:02:38 -05002757static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002759 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Thomas Graf7deb2262007-08-22 13:57:39 -07002762 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (skb == NULL)
2764 return -ENOMEM;
2765
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002766 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 BUG();
2768
Michal Kubecek21ee5432014-06-03 10:26:06 +02002769 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
David S. Miller214e0052011-02-24 00:02:38 -05002772static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002773{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002774 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002775 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002776 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002777 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002778 struct nlmsghdr *nlh;
2779 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002780 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002781
2782 headlen = sizeof(*p);
2783 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002784 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002785 headlen = sizeof(*id);
2786 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002787 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002788 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002789 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790
Thomas Graf7deb2262007-08-22 13:57:39 -07002791 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002792 if (skb == NULL)
2793 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002794
Eric W. Biederman15e47302012-09-07 20:12:54 +00002795 nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002796 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002797 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002798 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002799
Thomas Graf7b67c852007-08-22 13:53:52 -07002800 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002801 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002802 struct nlattr *attr;
2803
Thomas Graf7b67c852007-08-22 13:53:52 -07002804 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002805 memset(id, 0, sizeof(*id));
2806 id->dir = dir;
2807 if (c->data.byid)
2808 id->index = xp->index;
2809 else
2810 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2811
Thomas Grafc0144be2007-08-22 13:55:43 -07002812 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002813 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002814 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002815 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002816
2817 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002818 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002819
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002820 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002821 err = copy_to_user_tmpl(xp, skb);
2822 if (!err)
2823 err = copy_to_user_policy_type(xp->type, skb);
2824 if (!err)
2825 err = xfrm_mark_put(skb, &xp->mark);
2826 if (err)
2827 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002828
Thomas Graf98250692007-08-22 12:47:26 -07002829 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002830
Michal Kubecek21ee5432014-06-03 10:26:06 +02002831 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002832
David S. Miller1d1e34d2012-06-27 21:57:03 -07002833out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002834 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002835 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002836}
2837
David S. Miller214e0052011-02-24 00:02:38 -05002838static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002839{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002840 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002841 struct nlmsghdr *nlh;
2842 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002843 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002844
Thomas Graf7deb2262007-08-22 13:57:39 -07002845 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002846 if (skb == NULL)
2847 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002848
Eric W. Biederman15e47302012-09-07 20:12:54 +00002849 nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002850 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002851 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002852 goto out_free_skb;
2853 err = copy_to_user_policy_type(c->data.type, skb);
2854 if (err)
2855 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002856
Thomas Graf98250692007-08-22 12:47:26 -07002857 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002858
Michal Kubecek21ee5432014-06-03 10:26:06 +02002859 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002860
David S. Miller1d1e34d2012-06-27 21:57:03 -07002861out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002862 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002863 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002864}
2865
David S. Miller214e0052011-02-24 00:02:38 -05002866static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002867{
2868
2869 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002870 case XFRM_MSG_NEWPOLICY:
2871 case XFRM_MSG_UPDPOLICY:
2872 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002873 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002874 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002875 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002876 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002877 return xfrm_exp_policy_notify(xp, dir, c);
2878 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002879 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2880 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002881 }
2882
2883 return 0;
2884
2885}
2886
Thomas Graf7deb2262007-08-22 13:57:39 -07002887static inline size_t xfrm_report_msgsize(void)
2888{
2889 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2890}
2891
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002892static int build_report(struct sk_buff *skb, u8 proto,
2893 struct xfrm_selector *sel, xfrm_address_t *addr)
2894{
2895 struct xfrm_user_report *ur;
2896 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002897
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002898 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2899 if (nlh == NULL)
2900 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002901
Thomas Graf7b67c852007-08-22 13:53:52 -07002902 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002903 ur->proto = proto;
2904 memcpy(&ur->sel, sel, sizeof(ur->sel));
2905
David S. Miller1d1e34d2012-06-27 21:57:03 -07002906 if (addr) {
2907 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2908 if (err) {
2909 nlmsg_cancel(skb, nlh);
2910 return err;
2911 }
2912 }
Thomas Graf98250692007-08-22 12:47:26 -07002913 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002914}
2915
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002916static int xfrm_send_report(struct net *net, u8 proto,
2917 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002918{
2919 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002920
Thomas Graf7deb2262007-08-22 13:57:39 -07002921 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002922 if (skb == NULL)
2923 return -ENOMEM;
2924
2925 if (build_report(skb, proto, sel, addr) < 0)
2926 BUG();
2927
Michal Kubecek21ee5432014-06-03 10:26:06 +02002928 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002929}
2930
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002931static inline size_t xfrm_mapping_msgsize(void)
2932{
2933 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2934}
2935
2936static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2937 xfrm_address_t *new_saddr, __be16 new_sport)
2938{
2939 struct xfrm_user_mapping *um;
2940 struct nlmsghdr *nlh;
2941
2942 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2943 if (nlh == NULL)
2944 return -EMSGSIZE;
2945
2946 um = nlmsg_data(nlh);
2947
2948 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2949 um->id.spi = x->id.spi;
2950 um->id.family = x->props.family;
2951 um->id.proto = x->id.proto;
2952 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2953 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2954 um->new_sport = new_sport;
2955 um->old_sport = x->encap->encap_sport;
2956 um->reqid = x->props.reqid;
2957
2958 return nlmsg_end(skb, nlh);
2959}
2960
2961static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2962 __be16 sport)
2963{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002964 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002965 struct sk_buff *skb;
2966
2967 if (x->id.proto != IPPROTO_ESP)
2968 return -EINVAL;
2969
2970 if (!x->encap)
2971 return -EINVAL;
2972
2973 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2974 if (skb == NULL)
2975 return -ENOMEM;
2976
2977 if (build_mapping(skb, x, ipaddr, sport) < 0)
2978 BUG();
2979
Michal Kubecek21ee5432014-06-03 10:26:06 +02002980 return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002981}
2982
Horia Geanta0f245582014-02-12 16:20:06 +02002983static bool xfrm_is_alive(const struct km_event *c)
2984{
2985 return (bool)xfrm_acquire_is_on(c->net);
2986}
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988static struct xfrm_mgr netlink_mgr = {
2989 .id = "netlink",
2990 .notify = xfrm_send_state_notify,
2991 .acquire = xfrm_send_acquire,
2992 .compile_policy = xfrm_compile_policy,
2993 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002994 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002995 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002996 .new_mapping = xfrm_send_mapping,
Horia Geanta0f245582014-02-12 16:20:06 +02002997 .is_alive = xfrm_is_alive,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998};
2999
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003000static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Patrick McHardybe336902006-03-20 22:40:54 -08003002 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00003003 struct netlink_kernel_cfg cfg = {
3004 .groups = XFRMNLGRP_MAX,
3005 .input = xfrm_netlink_rcv,
3006 };
Patrick McHardybe336902006-03-20 22:40:54 -08003007
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00003008 nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08003009 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003011 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00003012 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return 0;
3014}
3015
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003016static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003017{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003018 struct net *net;
3019 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003020 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003021 synchronize_net();
3022 list_for_each_entry(net, net_exit_list, exit_list)
3023 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003024}
3025
3026static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003027 .init = xfrm_user_net_init,
3028 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003029};
3030
3031static int __init xfrm_user_init(void)
3032{
3033 int rv;
3034
3035 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3036
3037 rv = register_pernet_subsys(&xfrm_user_net_ops);
3038 if (rv < 0)
3039 return rv;
3040 rv = xfrm_register_km(&netlink_mgr);
3041 if (rv < 0)
3042 unregister_pernet_subsys(&xfrm_user_net_ops);
3043 return rv;
3044}
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046static void __exit xfrm_user_exit(void)
3047{
3048 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003049 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
3052module_init(xfrm_user_init);
3053module_exit(xfrm_user_exit);
3054MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003055MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003056