blob: 5927065e97cf9747965475175f268311e616ec82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +000029#include <net/ah.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000031#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070032#include <linux/in6.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Herbert Xu1a6509d2008-01-28 19:37:29 -080035static inline int aead_len(struct xfrm_algo_aead *alg)
36{
37 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
38}
39
Thomas Graf5424f322007-08-22 14:01:33 -070040static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Thomas Graf5424f322007-08-22 14:01:33 -070042 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct xfrm_algo *algp;
44
45 if (!rt)
46 return 0;
47
Thomas Graf5424f322007-08-22 14:01:33 -070048 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080049 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070050 return -EINVAL;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 switch (type) {
53 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57
58 default:
59 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
63 return 0;
64}
65
Martin Willi4447bb32009-11-25 00:29:52 +000066static int verify_auth_trunc(struct nlattr **attrs)
67{
68 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
69 struct xfrm_algo_auth *algp;
70
71 if (!rt)
72 return 0;
73
74 algp = nla_data(rt);
75 if (nla_len(rt) < xfrm_alg_auth_len(algp))
76 return -EINVAL;
77
78 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
79 return 0;
80}
81
Herbert Xu1a6509d2008-01-28 19:37:29 -080082static int verify_aead(struct nlattr **attrs)
83{
84 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
85 struct xfrm_algo_aead *algp;
86
87 if (!rt)
88 return 0;
89
90 algp = nla_data(rt);
91 if (nla_len(rt) < aead_len(algp))
92 return -EINVAL;
93
94 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
95 return 0;
96}
97
Thomas Graf5424f322007-08-22 14:01:33 -070098static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099 xfrm_address_t **addrp)
100{
Thomas Graf5424f322007-08-22 14:01:33 -0700101 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700102
Thomas Grafcf5cb792007-08-22 13:59:04 -0700103 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700104 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700105}
Trent Jaegerdf718372005-12-13 23:12:27 -0800106
Thomas Graf5424f322007-08-22 14:01:33 -0700107static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800108{
Thomas Graf5424f322007-08-22 14:01:33 -0700109 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800110 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800111
112 if (!rt)
113 return 0;
114
Thomas Graf5424f322007-08-22 14:01:33 -0700115 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700116 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800117 return -EINVAL;
118
119 return 0;
120}
121
Steffen Klassertd8647b72011-03-08 00:10:27 +0000122static inline int verify_replay(struct xfrm_usersa_info *p,
123 struct nlattr **attrs)
124{
125 struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
Mathias Krauseecd79182012-09-20 10:01:49 +0000126 struct xfrm_replay_state_esn *rs;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000127
Mathias Krauseecd79182012-09-20 10:01:49 +0000128 if (p->flags & XFRM_STATE_ESN) {
129 if (!rt)
130 return -EINVAL;
131
132 rs = nla_data(rt);
133
134 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
135 return -EINVAL;
136
137 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
138 nla_len(rt) != sizeof(*rs))
139 return -EINVAL;
140 }
Steffen Klassert7833aa02011-04-25 19:41:21 +0000141
Steffen Klassertd8647b72011-03-08 00:10:27 +0000142 if (!rt)
143 return 0;
144
Steffen Klassert02aadf72011-03-28 19:48:09 +0000145 if (p->id.proto != IPPROTO_ESP)
146 return -EINVAL;
147
Steffen Klassertd8647b72011-03-08 00:10:27 +0000148 if (p->replay_window != 0)
149 return -EINVAL;
150
151 return 0;
152}
Trent Jaegerdf718372005-12-13 23:12:27 -0800153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700155 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 int err;
158
159 err = -EINVAL;
160 switch (p->family) {
161 case AF_INET:
162 break;
163
164 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000165#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 break;
167#else
168 err = -EAFNOSUPPORT;
169 goto out;
170#endif
171
172 default:
173 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 err = -EINVAL;
177 switch (p->id.proto) {
178 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000179 if ((!attrs[XFRMA_ALG_AUTH] &&
180 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800181 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700182 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000183 attrs[XFRMA_ALG_COMP] ||
184 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 break;
187
188 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800189 if (attrs[XFRMA_ALG_COMP])
190 goto out;
191 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000192 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800193 !attrs[XFRMA_ALG_CRYPT] &&
194 !attrs[XFRMA_ALG_AEAD])
195 goto out;
196 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000197 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 attrs[XFRMA_ALG_CRYPT]) &&
199 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000201 if (attrs[XFRMA_TFCPAD] &&
202 p->mode != XFRM_MODE_TUNNEL)
203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205
206 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800208 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000210 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000211 attrs[XFRMA_ALG_CRYPT] ||
212 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
214 break;
215
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000216#if IS_ENABLED(CONFIG_IPV6)
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700217 case IPPROTO_DSTOPTS:
218 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700219 if (attrs[XFRMA_ALG_COMP] ||
220 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000221 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800222 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700223 attrs[XFRMA_ALG_CRYPT] ||
224 attrs[XFRMA_ENCAP] ||
225 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000226 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700228 goto out;
229 break;
230#endif
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 default:
233 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Herbert Xu1a6509d2008-01-28 19:37:29 -0800236 if ((err = verify_aead(attrs)))
237 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000238 if ((err = verify_auth_trunc(attrs)))
239 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700240 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700242 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700244 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700246 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800247 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000248 if ((err = verify_replay(p, attrs)))
249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 err = -EINVAL;
252 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700253 case XFRM_MODE_TRANSPORT:
254 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700255 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700256 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258
259 default:
260 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 err = 0;
264
265out:
266 return err;
267}
268
269static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800270 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700271 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct xfrm_algo *p, *ualg;
274 struct xfrm_algo_desc *algo;
275
276 if (!rta)
277 return 0;
278
Thomas Graf5424f322007-08-22 14:01:33 -0700279 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 algo = get_byname(ualg->alg_name, 1);
282 if (!algo)
283 return -ENOSYS;
284 *props = algo->desc.sadb_alg_id;
285
Eric Dumazet0f99be02008-01-08 23:39:06 -0800286 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!p)
288 return -ENOMEM;
289
Herbert Xu04ff1262006-08-13 08:50:00 +1000290 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *algpp = p;
292 return 0;
293}
294
Martin Willi4447bb32009-11-25 00:29:52 +0000295static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
296 struct nlattr *rta)
297{
298 struct xfrm_algo *ualg;
299 struct xfrm_algo_auth *p;
300 struct xfrm_algo_desc *algo;
301
302 if (!rta)
303 return 0;
304
305 ualg = nla_data(rta);
306
307 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
308 if (!algo)
309 return -ENOSYS;
310 *props = algo->desc.sadb_alg_id;
311
312 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
313 if (!p)
314 return -ENOMEM;
315
316 strcpy(p->alg_name, algo->name);
317 p->alg_key_len = ualg->alg_key_len;
318 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
319 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
320
321 *algpp = p;
322 return 0;
323}
324
325static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
326 struct nlattr *rta)
327{
328 struct xfrm_algo_auth *p, *ualg;
329 struct xfrm_algo_desc *algo;
330
331 if (!rta)
332 return 0;
333
334 ualg = nla_data(rta);
335
336 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
337 if (!algo)
338 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000339 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
340 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000341 return -EINVAL;
342 *props = algo->desc.sadb_alg_id;
343
344 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
345 if (!p)
346 return -ENOMEM;
347
348 strcpy(p->alg_name, algo->name);
349 if (!p->alg_trunc_len)
350 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
351
352 *algpp = p;
353 return 0;
354}
355
Herbert Xu1a6509d2008-01-28 19:37:29 -0800356static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
357 struct nlattr *rta)
358{
359 struct xfrm_algo_aead *p, *ualg;
360 struct xfrm_algo_desc *algo;
361
362 if (!rta)
363 return 0;
364
365 ualg = nla_data(rta);
366
367 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
368 if (!algo)
369 return -ENOSYS;
370 *props = algo->desc.sadb_alg_id;
371
372 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
373 if (!p)
374 return -ENOMEM;
375
376 strcpy(p->alg_name, algo->name);
377 *algpp = p;
378 return 0;
379}
380
Steffen Klasserte2b19122011-03-28 19:47:30 +0000381static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
382 struct nlattr *rp)
383{
384 struct xfrm_replay_state_esn *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000385 int ulen;
Steffen Klasserte2b19122011-03-28 19:47:30 +0000386
387 if (!replay_esn || !rp)
388 return 0;
389
390 up = nla_data(rp);
Mathias Krauseecd79182012-09-20 10:01:49 +0000391 ulen = xfrm_replay_state_esn_len(up);
Steffen Klasserte2b19122011-03-28 19:47:30 +0000392
Mathias Krauseecd79182012-09-20 10:01:49 +0000393 if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
Steffen Klasserte2b19122011-03-28 19:47:30 +0000394 return -EINVAL;
395
396 return 0;
397}
398
Steffen Klassertd8647b72011-03-08 00:10:27 +0000399static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
400 struct xfrm_replay_state_esn **preplay_esn,
401 struct nlattr *rta)
402{
403 struct xfrm_replay_state_esn *p, *pp, *up;
Mathias Krauseecd79182012-09-20 10:01:49 +0000404 int klen, ulen;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000405
406 if (!rta)
407 return 0;
408
409 up = nla_data(rta);
Mathias Krauseecd79182012-09-20 10:01:49 +0000410 klen = xfrm_replay_state_esn_len(up);
411 ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000412
Mathias Krauseecd79182012-09-20 10:01:49 +0000413 p = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000414 if (!p)
415 return -ENOMEM;
416
Mathias Krauseecd79182012-09-20 10:01:49 +0000417 pp = kzalloc(klen, GFP_KERNEL);
Steffen Klassertd8647b72011-03-08 00:10:27 +0000418 if (!pp) {
419 kfree(p);
420 return -ENOMEM;
421 }
422
Mathias Krauseecd79182012-09-20 10:01:49 +0000423 memcpy(p, up, ulen);
424 memcpy(pp, up, ulen);
425
Steffen Klassertd8647b72011-03-08 00:10:27 +0000426 *replay_esn = p;
427 *preplay_esn = pp;
428
429 return 0;
430}
431
Joy Latten661697f2007-04-13 16:14:35 -0700432static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800433{
Trent Jaegerdf718372005-12-13 23:12:27 -0800434 int len = 0;
435
436 if (xfrm_ctx) {
437 len += sizeof(struct xfrm_user_sec_ctx);
438 len += xfrm_ctx->ctx_len;
439 }
440 return len;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
444{
445 memcpy(&x->id, &p->id, sizeof(x->id));
446 memcpy(&x->sel, &p->sel, sizeof(x->sel));
447 memcpy(&x->lft, &p->lft, sizeof(x->lft));
448 x->props.mode = p->mode;
449 x->props.replay_window = p->replay_window;
450 x->props.reqid = p->reqid;
451 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700452 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700454
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700455 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700456 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800459/*
460 * someday when pfkey also has support, we could have the code
461 * somehow made shareable and move it to xfrm_state.c - JHS
462 *
463*/
Thomas Graf5424f322007-08-22 14:01:33 -0700464static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800465{
Thomas Graf5424f322007-08-22 14:01:33 -0700466 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +0000467 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
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
Herbert Xu1a6509d2008-01-28 19:37:29 -0800517 if ((err = attach_aead(&x->aead, &x->props.ealgo,
518 attrs[XFRMA_ALG_AEAD])))
519 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000520 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
521 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000523 if (!x->props.aalgo) {
524 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
525 attrs[XFRMA_ALG_AUTH])))
526 goto error;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
529 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700530 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto error;
532 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
533 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700534 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700536
537 if (attrs[XFRMA_ENCAP]) {
538 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
539 sizeof(*x->encap), GFP_KERNEL);
540 if (x->encap == NULL)
541 goto error;
542 }
543
Martin Willi35d28562010-12-08 04:37:49 +0000544 if (attrs[XFRMA_TFCPAD])
545 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
546
Thomas Graffd211502007-09-06 03:28:08 -0700547 if (attrs[XFRMA_COADDR]) {
548 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
549 sizeof(*x->coaddr), GFP_KERNEL);
550 if (x->coaddr == NULL)
551 goto error;
552 }
553
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000554 xfrm_mark_get(attrs, &x->mark);
555
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700556 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (err)
558 goto error;
559
Thomas Graffd211502007-09-06 03:28:08 -0700560 if (attrs[XFRMA_SEC_CTX] &&
561 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800562 goto error;
563
Steffen Klassertd8647b72011-03-08 00:10:27 +0000564 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
565 attrs[XFRMA_REPLAY_ESN_VAL])))
566 goto error;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800569 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800570 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800571 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800572
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000573 if ((err = xfrm_init_replay(x)))
574 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800575
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000576 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700577 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return x;
580
581error:
582 x->km.state = XFRM_STATE_DEAD;
583 xfrm_state_put(x);
584error_no_put:
585 *errp = err;
586 return NULL;
587}
588
Christoph Hellwig22e70052007-01-02 15:22:30 -0800589static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700590 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800592 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700593 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct xfrm_state *x;
595 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700596 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800597 uid_t loginuid = audit_get_loginuid(current);
598 u32 sessionid = audit_get_sessionid(current);
599 u32 sid;
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
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800615 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400616 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (err < 0) {
619 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800620 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700624 c.seq = nlh->nlmsg_seq;
625 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700626 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700627
628 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700629out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700630 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return err;
632}
633
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800634static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
635 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700636 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700637 int *errp)
638{
639 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000640 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700641 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000642 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700643
644 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
645 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000646 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700647 } else {
648 xfrm_address_t *saddr = NULL;
649
Thomas Graf35a7aa02007-08-22 14:00:40 -0700650 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700651 if (!saddr) {
652 err = -EINVAL;
653 goto out;
654 }
655
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800656 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000657 x = xfrm_state_lookup_byaddr(net, mark,
658 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800659 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700660 }
661
662 out:
663 if (!x && errp)
664 *errp = err;
665 return x;
666}
667
Christoph Hellwig22e70052007-01-02 15:22:30 -0800668static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700669 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800671 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700673 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700674 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700675 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800676 uid_t loginuid = audit_get_loginuid(current);
677 u32 sessionid = audit_get_sessionid(current);
678 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800680 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700682 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David S. Miller6f68dc32006-06-08 23:58:52 -0700684 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700685 goto out;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700688 err = -EPERM;
689 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700692 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600693
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700694 if (err < 0)
695 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700696
697 c.seq = nlh->nlmsg_seq;
698 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700699 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700700 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700702out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800703 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400704 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700705 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700706 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
710{
Mathias Krausef778a632012-09-19 11:33:39 +0000711 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 memcpy(&p->id, &x->id, sizeof(p->id));
713 memcpy(&p->sel, &x->sel, sizeof(p->sel));
714 memcpy(&p->lft, &x->lft, sizeof(p->lft));
715 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
716 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700717 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 p->mode = x->props.mode;
719 p->replay_window = x->props.replay_window;
720 p->reqid = x->props.reqid;
721 p->family = x->props.family;
722 p->flags = x->props.flags;
723 p->seq = x->km.seq;
724}
725
726struct xfrm_dump_info {
727 struct sk_buff *in_skb;
728 struct sk_buff *out_skb;
729 u32 nlmsg_seq;
730 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731};
732
Thomas Grafc0144be2007-08-22 13:55:43 -0700733static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
734{
Thomas Grafc0144be2007-08-22 13:55:43 -0700735 struct xfrm_user_sec_ctx *uctx;
736 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700737 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700738
739 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
740 if (attr == NULL)
741 return -EMSGSIZE;
742
743 uctx = nla_data(attr);
744 uctx->exttype = XFRMA_SEC_CTX;
745 uctx->len = ctx_size;
746 uctx->ctx_doi = s->ctx_doi;
747 uctx->ctx_alg = s->ctx_alg;
748 uctx->ctx_len = s->ctx_len;
749 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
750
751 return 0;
752}
753
Martin Willi4447bb32009-11-25 00:29:52 +0000754static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
755{
756 struct xfrm_algo *algo;
757 struct nlattr *nla;
758
759 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
760 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
761 if (!nla)
762 return -EMSGSIZE;
763
764 algo = nla_data(nla);
Mathias Krause4c873082012-09-19 11:33:38 +0000765 strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
Martin Willi4447bb32009-11-25 00:29:52 +0000766 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
767 algo->alg_key_len = auth->alg_key_len;
768
769 return 0;
770}
771
Herbert Xu68325d32007-10-09 13:30:57 -0700772/* Don't change this without updating xfrm_sa_len! */
773static int copy_to_user_state_extra(struct xfrm_state *x,
774 struct xfrm_usersa_info *p,
775 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
David S. Miller1d1e34d2012-06-27 21:57:03 -0700777 int ret = 0;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 copy_to_user_state(x, p);
780
David S. Miller1d1e34d2012-06-27 21:57:03 -0700781 if (x->coaddr) {
782 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
783 if (ret)
784 goto out;
785 }
786 if (x->lastused) {
787 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
788 if (ret)
789 goto out;
790 }
791 if (x->aead) {
792 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
793 if (ret)
794 goto out;
795 }
796 if (x->aalg) {
797 ret = copy_to_user_auth(x->aalg, skb);
798 if (!ret)
799 ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
800 xfrm_alg_auth_len(x->aalg), x->aalg);
801 if (ret)
802 goto out;
803 }
804 if (x->ealg) {
805 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
806 if (ret)
807 goto out;
808 }
809 if (x->calg) {
810 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
811 if (ret)
812 goto out;
813 }
814 if (x->encap) {
815 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
816 if (ret)
817 goto out;
818 }
819 if (x->tfcpad) {
820 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
821 if (ret)
822 goto out;
823 }
824 ret = xfrm_mark_put(skb, &x->mark);
825 if (ret)
826 goto out;
827 if (x->replay_esn) {
828 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
829 xfrm_replay_state_esn_len(x->replay_esn),
830 x->replay_esn);
831 if (ret)
832 goto out;
833 }
834 if (x->security)
835 ret = copy_sec_ctx(x->security, skb);
836out:
837 return ret;
Herbert Xu68325d32007-10-09 13:30:57 -0700838}
839
840static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
841{
842 struct xfrm_dump_info *sp = ptr;
843 struct sk_buff *in_skb = sp->in_skb;
844 struct sk_buff *skb = sp->out_skb;
845 struct xfrm_usersa_info *p;
846 struct nlmsghdr *nlh;
847 int err;
848
Herbert Xu68325d32007-10-09 13:30:57 -0700849 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
850 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
851 if (nlh == NULL)
852 return -EMSGSIZE;
853
854 p = nlmsg_data(nlh);
855
856 err = copy_to_user_state_extra(x, p, skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -0700857 if (err) {
858 nlmsg_cancel(skb, nlh);
859 return err;
860 }
Thomas Graf98250692007-08-22 12:47:26 -0700861 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Timo Teras4c563f72008-02-28 21:31:08 -0800865static int xfrm_dump_sa_done(struct netlink_callback *cb)
866{
867 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
868 xfrm_state_walk_done(walk);
869 return 0;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
873{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800874 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800875 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct xfrm_dump_info info;
877
Timo Teras4c563f72008-02-28 21:31:08 -0800878 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
879 sizeof(cb->args) - sizeof(cb->args[0]));
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 info.in_skb = cb->skb;
882 info.out_skb = skb;
883 info.nlmsg_seq = cb->nlh->nlmsg_seq;
884 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800885
886 if (!cb->args[0]) {
887 cb->args[0] = 1;
888 xfrm_state_walk_init(walk, 0);
889 }
890
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800891 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 return skb->len;
894}
895
896static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
897 struct xfrm_state *x, u32 seq)
898{
899 struct xfrm_dump_info info;
900 struct sk_buff *skb;
Mathias Krause864745d2012-09-13 11:41:26 +0000901 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Thomas Graf7deb2262007-08-22 13:57:39 -0700903 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (!skb)
905 return ERR_PTR(-ENOMEM);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 info.in_skb = in_skb;
908 info.out_skb = skb;
909 info.nlmsg_seq = seq;
910 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Mathias Krause864745d2012-09-13 11:41:26 +0000912 err = dump_one_state(x, 0, &info);
913 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 kfree_skb(skb);
Mathias Krause864745d2012-09-13 11:41:26 +0000915 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
918 return skb;
919}
920
Thomas Graf7deb2262007-08-22 13:57:39 -0700921static inline size_t xfrm_spdinfo_msgsize(void)
922{
923 return NLMSG_ALIGN(4)
924 + nla_total_size(sizeof(struct xfrmu_spdinfo))
925 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
926}
927
Alexey Dobriyane0710412010-01-23 13:37:10 +0000928static int build_spdinfo(struct sk_buff *skb, struct net *net,
929 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700930{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700931 struct xfrmk_spdinfo si;
932 struct xfrmu_spdinfo spc;
933 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700934 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700935 int err;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700936 u32 *f;
937
938 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300939 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700940 return -EMSGSIZE;
941
942 f = nlmsg_data(nlh);
943 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000944 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700945 spc.incnt = si.incnt;
946 spc.outcnt = si.outcnt;
947 spc.fwdcnt = si.fwdcnt;
948 spc.inscnt = si.inscnt;
949 spc.outscnt = si.outscnt;
950 spc.fwdscnt = si.fwdscnt;
951 sph.spdhcnt = si.spdhcnt;
952 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700953
David S. Miller1d1e34d2012-06-27 21:57:03 -0700954 err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
955 if (!err)
956 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
957 if (err) {
958 nlmsg_cancel(skb, nlh);
959 return err;
960 }
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700961
962 return nlmsg_end(skb, nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700963}
964
965static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700966 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700967{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800968 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700969 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700970 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700971 u32 spid = NETLINK_CB(skb).pid;
972 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700973
Thomas Graf7deb2262007-08-22 13:57:39 -0700974 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700975 if (r_skb == NULL)
976 return -ENOMEM;
977
Alexey Dobriyane0710412010-01-23 13:37:10 +0000978 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700979 BUG();
980
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800981 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700982}
983
Thomas Graf7deb2262007-08-22 13:57:39 -0700984static inline size_t xfrm_sadinfo_msgsize(void)
985{
986 return NLMSG_ALIGN(4)
987 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
988 + nla_total_size(4); /* XFRMA_SAD_CNT */
989}
990
Alexey Dobriyane0710412010-01-23 13:37:10 +0000991static int build_sadinfo(struct sk_buff *skb, struct net *net,
992 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700993{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700994 struct xfrmk_sadinfo si;
995 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700996 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -0700997 int err;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700998 u32 *f;
999
1000 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001001 if (nlh == NULL) /* shouldn't really happen ... */
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001002 return -EMSGSIZE;
1003
1004 f = nlmsg_data(nlh);
1005 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +00001006 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001007
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -07001008 sh.sadhmcnt = si.sadhmcnt;
1009 sh.sadhcnt = si.sadhcnt;
1010
David S. Miller1d1e34d2012-06-27 21:57:03 -07001011 err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
1012 if (!err)
1013 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
1014 if (err) {
1015 nlmsg_cancel(skb, nlh);
1016 return err;
1017 }
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001018
1019 return nlmsg_end(skb, nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001020}
1021
1022static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001023 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001024{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001025 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001026 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -07001027 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001028 u32 spid = NETLINK_CB(skb).pid;
1029 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001030
Thomas Graf7deb2262007-08-22 13:57:39 -07001031 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001032 if (r_skb == NULL)
1033 return -ENOMEM;
1034
Alexey Dobriyane0710412010-01-23 13:37:10 +00001035 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001036 BUG();
1037
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001038 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -07001039}
1040
Christoph Hellwig22e70052007-01-02 15:22:30 -08001041static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001042 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001044 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001045 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct xfrm_state *x;
1047 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001048 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001050 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (x == NULL)
1052 goto out_noput;
1053
1054 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1055 if (IS_ERR(resp_skb)) {
1056 err = PTR_ERR(resp_skb);
1057 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001058 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 xfrm_state_put(x);
1061out_noput:
1062 return err;
1063}
1064
1065static int verify_userspi_info(struct xfrm_userspi_info *p)
1066{
1067 switch (p->info.id.proto) {
1068 case IPPROTO_AH:
1069 case IPPROTO_ESP:
1070 break;
1071
1072 case IPPROTO_COMP:
1073 /* IPCOMP spi is 16-bits. */
1074 if (p->max >= 0x10000)
1075 return -EINVAL;
1076 break;
1077
1078 default:
1079 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (p->min > p->max)
1083 return -EINVAL;
1084
1085 return 0;
1086}
1087
Christoph Hellwig22e70052007-01-02 15:22:30 -08001088static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001089 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001091 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct xfrm_state *x;
1093 struct xfrm_userspi_info *p;
1094 struct sk_buff *resp_skb;
1095 xfrm_address_t *daddr;
1096 int family;
1097 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001098 u32 mark;
1099 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Thomas Graf7b67c852007-08-22 13:53:52 -07001101 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 err = verify_userspi_info(p);
1103 if (err)
1104 goto out_noput;
1105
1106 family = p->info.family;
1107 daddr = &p->info.id.daddr;
1108
1109 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001110
1111 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001113 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1115 xfrm_state_put(x);
1116 x = NULL;
1117 }
1118 }
1119
1120 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001121 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 p->info.id.proto, daddr,
1123 &p->info.saddr, 1,
1124 family);
1125 err = -ENOENT;
1126 if (x == NULL)
1127 goto out_noput;
1128
Herbert Xu658b2192007-10-09 13:29:52 -07001129 err = xfrm_alloc_spi(x, p->min, p->max);
1130 if (err)
1131 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Herbert Xu658b2192007-10-09 13:29:52 -07001133 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (IS_ERR(resp_skb)) {
1135 err = PTR_ERR(resp_skb);
1136 goto out;
1137 }
1138
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001139 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141out:
1142 xfrm_state_put(x);
1143out_noput:
1144 return err;
1145}
1146
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001147static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 switch (dir) {
1150 case XFRM_POLICY_IN:
1151 case XFRM_POLICY_OUT:
1152 case XFRM_POLICY_FWD:
1153 break;
1154
1155 default:
1156 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 return 0;
1160}
1161
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001162static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001163{
1164 switch (type) {
1165 case XFRM_POLICY_TYPE_MAIN:
1166#ifdef CONFIG_XFRM_SUB_POLICY
1167 case XFRM_POLICY_TYPE_SUB:
1168#endif
1169 break;
1170
1171 default:
1172 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001173 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001174
1175 return 0;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1179{
1180 switch (p->share) {
1181 case XFRM_SHARE_ANY:
1182 case XFRM_SHARE_SESSION:
1183 case XFRM_SHARE_USER:
1184 case XFRM_SHARE_UNIQUE:
1185 break;
1186
1187 default:
1188 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 switch (p->action) {
1192 case XFRM_POLICY_ALLOW:
1193 case XFRM_POLICY_BLOCK:
1194 break;
1195
1196 default:
1197 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 switch (p->sel.family) {
1201 case AF_INET:
1202 break;
1203
1204 case AF_INET6:
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001205#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207#else
1208 return -EAFNOSUPPORT;
1209#endif
1210
1211 default:
1212 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 return verify_policy_dir(p->dir);
1216}
1217
Thomas Graf5424f322007-08-22 14:01:33 -07001218static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001219{
Thomas Graf5424f322007-08-22 14:01:33 -07001220 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001221 struct xfrm_user_sec_ctx *uctx;
1222
1223 if (!rt)
1224 return 0;
1225
Thomas Graf5424f322007-08-22 14:01:33 -07001226 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001227 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001228}
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1231 int nr)
1232{
1233 int i;
1234
1235 xp->xfrm_nr = nr;
1236 for (i = 0; i < nr; i++, ut++) {
1237 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1238
1239 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1240 memcpy(&t->saddr, &ut->saddr,
1241 sizeof(xfrm_address_t));
1242 t->reqid = ut->reqid;
1243 t->mode = ut->mode;
1244 t->share = ut->share;
1245 t->optional = ut->optional;
1246 t->aalgos = ut->aalgos;
1247 t->ealgos = ut->ealgos;
1248 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001249 /* If all masks are ~0, then we allow all algorithms. */
1250 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001251 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253}
1254
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001255static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1256{
1257 int i;
1258
1259 if (nr > XFRM_MAX_DEPTH)
1260 return -EINVAL;
1261
1262 for (i = 0; i < nr; i++) {
1263 /* We never validated the ut->family value, so many
1264 * applications simply leave it at zero. The check was
1265 * never made and ut->family was ignored because all
1266 * templates could be assumed to have the same family as
1267 * the policy itself. Now that we will have ipv4-in-ipv6
1268 * and ipv6-in-ipv4 tunnels, this is no longer true.
1269 */
1270 if (!ut[i].family)
1271 ut[i].family = family;
1272
1273 switch (ut[i].family) {
1274 case AF_INET:
1275 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001276#if IS_ENABLED(CONFIG_IPV6)
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001277 case AF_INET6:
1278 break;
1279#endif
1280 default:
1281 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001282 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001283 }
1284
1285 return 0;
1286}
1287
Thomas Graf5424f322007-08-22 14:01:33 -07001288static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Thomas Graf5424f322007-08-22 14:01:33 -07001290 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (!rt) {
1293 pol->xfrm_nr = 0;
1294 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001295 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1296 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001297 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001299 err = validate_tmpl(nr, utmpl, pol->family);
1300 if (err)
1301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Thomas Graf5424f322007-08-22 14:01:33 -07001303 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305 return 0;
1306}
1307
Thomas Graf5424f322007-08-22 14:01:33 -07001308static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001309{
Thomas Graf5424f322007-08-22 14:01:33 -07001310 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001311 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001312 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001313 int err;
1314
1315 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001316 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001317 type = upt->type;
1318 }
1319
1320 err = verify_policy_type(type);
1321 if (err)
1322 return err;
1323
1324 *tp = type;
1325 return 0;
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1329{
1330 xp->priority = p->priority;
1331 xp->index = p->index;
1332 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1333 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1334 xp->action = p->action;
1335 xp->flags = p->flags;
1336 xp->family = p->sel.family;
1337 /* XXX xp->share = p->share; */
1338}
1339
1340static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1341{
Mathias Krause7b789832012-09-19 11:33:40 +00001342 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1344 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1345 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1346 p->priority = xp->priority;
1347 p->index = xp->index;
1348 p->sel.family = xp->family;
1349 p->dir = dir;
1350 p->action = xp->action;
1351 p->flags = xp->flags;
1352 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1353}
1354
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001355static 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 -07001356{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001357 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 int err;
1359
1360 if (!xp) {
1361 *errp = -ENOMEM;
1362 return NULL;
1363 }
1364
1365 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001366
Thomas Graf35a7aa02007-08-22 14:00:40 -07001367 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001368 if (err)
1369 goto error;
1370
Thomas Graf35a7aa02007-08-22 14:00:40 -07001371 if (!(err = copy_from_user_tmpl(xp, attrs)))
1372 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001373 if (err)
1374 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001376 xfrm_mark_get(attrs, &xp->mark);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001379 error:
1380 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001381 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001382 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001383 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Christoph Hellwig22e70052007-01-02 15:22:30 -08001386static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001387 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001389 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001390 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001392 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int err;
1394 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001395 uid_t loginuid = audit_get_loginuid(current);
1396 u32 sessionid = audit_get_sessionid(current);
1397 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 err = verify_newpolicy_info(p);
1400 if (err)
1401 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001402 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001403 if (err)
1404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001406 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (!xp)
1408 return err;
1409
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001410 /* shouldn't excl be based on nlh flags??
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001411 * Aha! this is anti-netlink really i.e more pfkey derived
1412 * in netlink excl is a flag and you wouldnt need
1413 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1415 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001416 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001417 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001420 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 kfree(xp);
1422 return err;
1423 }
1424
Herbert Xuf60f6b82005-06-18 22:44:37 -07001425 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001426 c.seq = nlh->nlmsg_seq;
1427 c.pid = nlh->nlmsg_pid;
1428 km_policy_notify(xp, p->dir, &c);
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 xfrm_pol_put(xp);
1431
1432 return 0;
1433}
1434
1435static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1436{
1437 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1438 int i;
1439
1440 if (xp->xfrm_nr == 0)
1441 return 0;
1442
1443 for (i = 0; i < xp->xfrm_nr; i++) {
1444 struct xfrm_user_tmpl *up = &vec[i];
1445 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1446
Mathias Krause1f868402012-09-19 11:33:41 +00001447 memset(up, 0, sizeof(*up));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001449 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1451 up->reqid = kp->reqid;
1452 up->mode = kp->mode;
1453 up->share = kp->share;
1454 up->optional = kp->optional;
1455 up->aalgos = kp->aalgos;
1456 up->ealgos = kp->ealgos;
1457 up->calgos = kp->calgos;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Thomas Grafc0144be2007-08-22 13:55:43 -07001460 return nla_put(skb, XFRMA_TMPL,
1461 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001462}
1463
Serge Hallyn0d681622006-07-24 23:30:44 -07001464static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1465{
1466 if (x->security) {
1467 return copy_sec_ctx(x->security, skb);
1468 }
1469 return 0;
1470}
1471
1472static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1473{
David S. Miller1d1e34d2012-06-27 21:57:03 -07001474 if (xp->security)
Serge Hallyn0d681622006-07-24 23:30:44 -07001475 return copy_sec_ctx(xp->security, skb);
Serge Hallyn0d681622006-07-24 23:30:44 -07001476 return 0;
1477}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001478static inline size_t userpolicy_type_attrsize(void)
1479{
1480#ifdef CONFIG_XFRM_SUB_POLICY
1481 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1482#else
1483 return 0;
1484#endif
1485}
Serge Hallyn0d681622006-07-24 23:30:44 -07001486
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001487#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001488static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001489{
Thomas Grafc0144be2007-08-22 13:55:43 -07001490 struct xfrm_userpolicy_type upt = {
1491 .type = type,
1492 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001493
Thomas Grafc0144be2007-08-22 13:55:43 -07001494 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001495}
1496
1497#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001498static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001499{
1500 return 0;
1501}
1502#endif
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1505{
1506 struct xfrm_dump_info *sp = ptr;
1507 struct xfrm_userpolicy_info *p;
1508 struct sk_buff *in_skb = sp->in_skb;
1509 struct sk_buff *skb = sp->out_skb;
1510 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001511 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001513 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1514 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1515 if (nlh == NULL)
1516 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Thomas Graf7b67c852007-08-22 13:53:52 -07001518 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001520 err = copy_to_user_tmpl(xp, skb);
1521 if (!err)
1522 err = copy_to_user_sec_ctx(xp, skb);
1523 if (!err)
1524 err = copy_to_user_policy_type(xp->type, skb);
1525 if (!err)
1526 err = xfrm_mark_put(skb, &xp->mark);
1527 if (err) {
1528 nlmsg_cancel(skb, nlh);
1529 return err;
1530 }
Thomas Graf98250692007-08-22 12:47:26 -07001531 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Timo Teras4c563f72008-02-28 21:31:08 -08001535static int xfrm_dump_policy_done(struct netlink_callback *cb)
1536{
1537 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1538
1539 xfrm_policy_walk_done(walk);
1540 return 0;
1541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1544{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001545 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001546 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct xfrm_dump_info info;
1548
Timo Teras4c563f72008-02-28 21:31:08 -08001549 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1550 sizeof(cb->args) - sizeof(cb->args[0]));
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 info.in_skb = cb->skb;
1553 info.out_skb = skb;
1554 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1555 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001556
1557 if (!cb->args[0]) {
1558 cb->args[0] = 1;
1559 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1560 }
1561
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001562 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 return skb->len;
1565}
1566
1567static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1568 struct xfrm_policy *xp,
1569 int dir, u32 seq)
1570{
1571 struct xfrm_dump_info info;
1572 struct sk_buff *skb;
Mathias Krausec2546372012-09-14 09:58:32 +00001573 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Thomas Graf7deb2262007-08-22 13:57:39 -07001575 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (!skb)
1577 return ERR_PTR(-ENOMEM);
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 info.in_skb = in_skb;
1580 info.out_skb = skb;
1581 info.nlmsg_seq = seq;
1582 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Mathias Krausec2546372012-09-14 09:58:32 +00001584 err = dump_one_policy(xp, dir, 0, &info);
1585 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 kfree_skb(skb);
Mathias Krausec2546372012-09-14 09:58:32 +00001587 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589
1590 return skb;
1591}
1592
Christoph Hellwig22e70052007-01-02 15:22:30 -08001593static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001594 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001596 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct xfrm_policy *xp;
1598 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001599 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001601 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001603 struct xfrm_mark m;
1604 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Thomas Graf7b67c852007-08-22 13:53:52 -07001606 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1608
Thomas Graf35a7aa02007-08-22 14:00:40 -07001609 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001610 if (err)
1611 return err;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 err = verify_policy_dir(p->dir);
1614 if (err)
1615 return err;
1616
1617 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001618 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001619 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001620 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001621 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001622
Thomas Graf35a7aa02007-08-22 14:00:40 -07001623 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001624 if (err)
1625 return err;
1626
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001627 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001628 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001629 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001630
Paul Moore03e1ad72008-04-12 19:07:52 -07001631 err = security_xfrm_policy_alloc(&ctx, uctx);
1632 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001633 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001634 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001635 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001636 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001637 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (xp == NULL)
1640 return -ENOENT;
1641
1642 if (!delete) {
1643 struct sk_buff *resp_skb;
1644
1645 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1646 if (IS_ERR(resp_skb)) {
1647 err = PTR_ERR(resp_skb);
1648 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001649 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001650 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001652 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001653 uid_t loginuid = audit_get_loginuid(current);
1654 u32 sessionid = audit_get_sessionid(current);
1655 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001656
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001657 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001658 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1659 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001660
1661 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001662 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001663
Herbert Xue7443892005-06-18 22:44:18 -07001664 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001665 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001666 c.seq = nlh->nlmsg_seq;
1667 c.pid = nlh->nlmsg_pid;
1668 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001671out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001672 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return err;
1674}
1675
Christoph Hellwig22e70052007-01-02 15:22:30 -08001676static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001677 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001679 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001680 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001681 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001682 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001683 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001685 audit_info.loginuid = audit_get_loginuid(current);
1686 audit_info.sessionid = audit_get_sessionid(current);
1687 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001688 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001689 if (err) {
1690 if (err == -ESRCH) /* empty table */
1691 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001692 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001693 }
Herbert Xubf088672005-06-18 22:44:00 -07001694 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001695 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696 c.seq = nlh->nlmsg_seq;
1697 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001698 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699 km_state_notify(NULL, &c);
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702}
1703
Steffen Klassertd8647b72011-03-08 00:10:27 +00001704static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001705{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001706 size_t replay_size = x->replay_esn ?
1707 xfrm_replay_state_esn_len(x->replay_esn) :
1708 sizeof(struct xfrm_replay_state);
1709
Thomas Graf7deb2262007-08-22 13:57:39 -07001710 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001711 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001712 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001713 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001714 + nla_total_size(4) /* XFRM_AE_RTHR */
1715 + nla_total_size(4); /* XFRM_AE_ETHR */
1716}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001717
David S. Miller214e0052011-02-24 00:02:38 -05001718static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001719{
1720 struct xfrm_aevent_id *id;
1721 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07001722 int err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001723
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001724 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1725 if (nlh == NULL)
1726 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001727
Thomas Graf7b67c852007-08-22 13:53:52 -07001728 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001729 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001730 id->sa_id.spi = x->id.spi;
1731 id->sa_id.family = x->props.family;
1732 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001733 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1734 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001735 id->flags = c->data.aevent;
1736
David S. Millerd0fde792012-03-29 04:02:26 -04001737 if (x->replay_esn) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001738 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
1739 xfrm_replay_state_esn_len(x->replay_esn),
1740 x->replay_esn);
David S. Millerd0fde792012-03-29 04:02:26 -04001741 } else {
David S. Miller1d1e34d2012-06-27 21:57:03 -07001742 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
1743 &x->replay);
David S. Millerd0fde792012-03-29 04:02:26 -04001744 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07001745 if (err)
1746 goto out_cancel;
1747 err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
1748 if (err)
1749 goto out_cancel;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001750
David S. Miller1d1e34d2012-06-27 21:57:03 -07001751 if (id->flags & XFRM_AE_RTHR) {
1752 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
1753 if (err)
1754 goto out_cancel;
1755 }
1756 if (id->flags & XFRM_AE_ETHR) {
1757 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
1758 x->replay_maxage * 10 / HZ);
1759 if (err)
1760 goto out_cancel;
1761 }
1762 err = xfrm_mark_put(skb, &x->mark);
1763 if (err)
1764 goto out_cancel;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001765
Thomas Graf98250692007-08-22 12:47:26 -07001766 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001767
David S. Miller1d1e34d2012-06-27 21:57:03 -07001768out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07001769 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07001770 return err;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771}
1772
Christoph Hellwig22e70052007-01-02 15:22:30 -08001773static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001774 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001775{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001776 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001777 struct xfrm_state *x;
1778 struct sk_buff *r_skb;
1779 int err;
1780 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001781 u32 mark;
1782 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001783 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001784 struct xfrm_usersa_id *id = &p->sa_id;
1785
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001786 mark = xfrm_mark_get(attrs, &m);
1787
1788 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001789 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001790 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001791
1792 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1793 if (r_skb == NULL) {
1794 xfrm_state_put(x);
1795 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001796 }
1797
1798 /*
1799 * XXX: is this lock really needed - none of the other
1800 * gets lock (the concern is things getting updated
1801 * while we are still reading) - jhs
1802 */
1803 spin_lock_bh(&x->lock);
1804 c.data.aevent = p->flags;
1805 c.seq = nlh->nlmsg_seq;
1806 c.pid = nlh->nlmsg_pid;
1807
1808 if (build_aevent(r_skb, x, &c) < 0)
1809 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001810 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001811 spin_unlock_bh(&x->lock);
1812 xfrm_state_put(x);
1813 return err;
1814}
1815
Christoph Hellwig22e70052007-01-02 15:22:30 -08001816static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001817 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001818{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001819 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001820 struct xfrm_state *x;
1821 struct km_event c;
1822 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001823 u32 mark = 0;
1824 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001825 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001826 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001827 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001828 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001829
Steffen Klassertd8647b72011-03-08 00:10:27 +00001830 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001831 return err;
1832
1833 /* pedantic mode - thou shalt sayeth replaceth */
1834 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1835 return err;
1836
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001837 mark = xfrm_mark_get(attrs, &m);
1838
1839 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001840 if (x == NULL)
1841 return -ESRCH;
1842
1843 if (x->km.state != XFRM_STATE_VALID)
1844 goto out;
1845
Steffen Klasserte2b19122011-03-28 19:47:30 +00001846 err = xfrm_replay_verify_len(x->replay_esn, rp);
1847 if (err)
1848 goto out;
1849
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001850 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001851 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001852 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001853
1854 c.event = nlh->nlmsg_type;
1855 c.seq = nlh->nlmsg_seq;
1856 c.pid = nlh->nlmsg_pid;
1857 c.data.aevent = XFRM_AE_CU;
1858 km_state_notify(x, &c);
1859 err = 0;
1860out:
1861 xfrm_state_put(x);
1862 return err;
1863}
1864
Christoph Hellwig22e70052007-01-02 15:22:30 -08001865static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001866 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001868 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001869 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001870 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001871 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001872 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001873
Thomas Graf35a7aa02007-08-22 14:00:40 -07001874 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001875 if (err)
1876 return err;
1877
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001878 audit_info.loginuid = audit_get_loginuid(current);
1879 audit_info.sessionid = audit_get_sessionid(current);
1880 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001881 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001882 if (err) {
1883 if (err == -ESRCH) /* empty table */
1884 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001885 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001886 }
1887
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001888 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001889 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001890 c.seq = nlh->nlmsg_seq;
1891 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001892 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001893 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return 0;
1895}
1896
Christoph Hellwig22e70052007-01-02 15:22:30 -08001897static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001898 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001899{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001900 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001901 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001902 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001903 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001904 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001905 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001906 struct xfrm_mark m;
1907 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001908
Thomas Graf35a7aa02007-08-22 14:00:40 -07001909 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001910 if (err)
1911 return err;
1912
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001913 err = verify_policy_dir(p->dir);
1914 if (err)
1915 return err;
1916
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001917 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001918 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001919 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001920 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001921 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001922
Thomas Graf35a7aa02007-08-22 14:00:40 -07001923 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001924 if (err)
1925 return err;
1926
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001927 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001928 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001929 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001930
Paul Moore03e1ad72008-04-12 19:07:52 -07001931 err = security_xfrm_policy_alloc(&ctx, uctx);
1932 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001933 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001934 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001935 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001936 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001937 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001939 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001940 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001941
Timo Teräsea2dea92010-03-31 00:17:05 +00001942 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001943 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001944
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001945 err = 0;
1946 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001947 uid_t loginuid = audit_get_loginuid(current);
1948 u32 sessionid = audit_get_sessionid(current);
1949 u32 sid;
1950
1951 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001952 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001953 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001954
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001955 } else {
1956 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001957 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001958 }
1959 km_policy_expired(xp, p->dir, up->hard, current->pid);
1960
1961out:
1962 xfrm_pol_put(xp);
1963 return err;
1964}
1965
Christoph Hellwig22e70052007-01-02 15:22:30 -08001966static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001967 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001968{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001969 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001970 struct xfrm_state *x;
1971 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001972 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001973 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001974 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001975 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001976
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001977 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001978
David S. Miller3a765aa2007-02-26 14:52:21 -08001979 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001980 if (x == NULL)
1981 return err;
1982
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001983 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001984 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001985 if (x->km.state != XFRM_STATE_VALID)
1986 goto out;
1987 km_state_expired(x, ue->hard, current->pid);
1988
Joy Latten161a09e2006-11-27 13:11:54 -06001989 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001990 uid_t loginuid = audit_get_loginuid(current);
1991 u32 sessionid = audit_get_sessionid(current);
1992 u32 sid;
1993
1994 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001995 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001996 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001997 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001998 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001999out:
2000 spin_unlock_bh(&x->lock);
2001 xfrm_state_put(x);
2002 return err;
2003}
2004
Christoph Hellwig22e70052007-01-02 15:22:30 -08002005static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002006 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002007{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002008 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002009 struct xfrm_policy *xp;
2010 struct xfrm_user_tmpl *ut;
2011 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07002012 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002013 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002014
Thomas Graf7b67c852007-08-22 13:53:52 -07002015 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002016 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002017 int err = -ENOMEM;
2018
2019 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002020 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002021
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002022 xfrm_mark_get(attrs, &mark);
2023
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002024 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002025 if (err)
2026 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002027
2028 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002029 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002030 if (!xp)
2031 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002032
2033 memcpy(&x->id, &ua->id, sizeof(ua->id));
2034 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
2035 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002036 xp->mark.m = x->mark.m = mark.m;
2037 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07002038 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002039 /* extract the templates and for each call km_key */
2040 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
2041 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
2042 memcpy(&x->id, &t->id, sizeof(x->id));
2043 x->props.mode = t->mode;
2044 x->props.reqid = t->reqid;
2045 x->props.family = ut->family;
2046 t->aalgos = ua->aalgos;
2047 t->ealgos = ua->ealgos;
2048 t->calgos = ua->calgos;
2049 err = km_query(x, t, xp);
2050
2051 }
2052
2053 kfree(x);
2054 kfree(xp);
2055
2056 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002057
2058bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002059 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002060free_state:
2061 kfree(x);
2062nomem:
2063 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002064}
2065
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002066#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002068 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002069 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002070{
Thomas Graf5424f322007-08-22 14:01:33 -07002071 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072 struct xfrm_user_migrate *um;
2073 int i, num_migrate;
2074
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002075 if (k != NULL) {
2076 struct xfrm_user_kmaddress *uk;
2077
2078 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2079 memcpy(&k->local, &uk->local, sizeof(k->local));
2080 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2081 k->family = uk->family;
2082 k->reserved = uk->reserved;
2083 }
2084
Thomas Graf5424f322007-08-22 14:01:33 -07002085 um = nla_data(rt);
2086 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002087
2088 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2089 return -EINVAL;
2090
2091 for (i = 0; i < num_migrate; i++, um++, ma++) {
2092 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2093 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2094 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2095 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2096
2097 ma->proto = um->proto;
2098 ma->mode = um->mode;
2099 ma->reqid = um->reqid;
2100
2101 ma->old_family = um->old_family;
2102 ma->new_family = um->new_family;
2103 }
2104
2105 *num = i;
2106 return 0;
2107}
2108
2109static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002110 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002111{
Thomas Graf7b67c852007-08-22 13:53:52 -07002112 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002113 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002114 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002115 u8 type;
2116 int err;
2117 int n = 0;
2118
Thomas Graf35a7aa02007-08-22 14:00:40 -07002119 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002120 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002121
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002122 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2123
Thomas Graf5424f322007-08-22 14:01:33 -07002124 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002125 if (err)
2126 return err;
2127
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002128 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002129 if (err)
2130 return err;
2131
2132 if (!n)
2133 return 0;
2134
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002135 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002136
2137 return 0;
2138}
2139#else
2140static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002141 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142{
2143 return -ENOPROTOOPT;
2144}
2145#endif
2146
2147#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002148static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149{
2150 struct xfrm_user_migrate um;
2151
2152 memset(&um, 0, sizeof(um));
2153 um.proto = m->proto;
2154 um.mode = m->mode;
2155 um.reqid = m->reqid;
2156 um.old_family = m->old_family;
2157 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2158 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2159 um.new_family = m->new_family;
2160 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2161 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2162
Thomas Grafc0144be2007-08-22 13:55:43 -07002163 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002164}
2165
David S. Miller183cad12011-02-24 00:28:01 -05002166static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002167{
2168 struct xfrm_user_kmaddress uk;
2169
2170 memset(&uk, 0, sizeof(uk));
2171 uk.family = k->family;
2172 uk.reserved = k->reserved;
2173 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002174 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002175
2176 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2177}
2178
2179static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002180{
2181 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002182 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2183 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2184 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002185}
2186
David S. Miller183cad12011-02-24 00:28:01 -05002187static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2188 int num_migrate, const struct xfrm_kmaddress *k,
2189 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002190{
David S. Miller183cad12011-02-24 00:28:01 -05002191 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002192 struct xfrm_userpolicy_id *pol_id;
2193 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002194 int i, err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002195
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002196 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2197 if (nlh == NULL)
2198 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002199
Thomas Graf7b67c852007-08-22 13:53:52 -07002200 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002201 /* copy data from selector, dir, and type to the pol_id */
2202 memset(pol_id, 0, sizeof(*pol_id));
2203 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2204 pol_id->dir = dir;
2205
David S. Miller1d1e34d2012-06-27 21:57:03 -07002206 if (k != NULL) {
2207 err = copy_to_user_kmaddress(k, skb);
2208 if (err)
2209 goto out_cancel;
2210 }
2211 err = copy_to_user_policy_type(type, skb);
2212 if (err)
2213 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002214 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
David S. Miller1d1e34d2012-06-27 21:57:03 -07002215 err = copy_to_user_migrate(mp, skb);
2216 if (err)
2217 goto out_cancel;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002218 }
2219
Thomas Graf98250692007-08-22 12:47:26 -07002220 return nlmsg_end(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002221
2222out_cancel:
Thomas Graf98250692007-08-22 12:47:26 -07002223 nlmsg_cancel(skb, nlh);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002224 return err;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002225}
2226
David S. Miller183cad12011-02-24 00:28:01 -05002227static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2228 const struct xfrm_migrate *m, int num_migrate,
2229 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002230{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002231 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002232 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002233
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002234 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002235 if (skb == NULL)
2236 return -ENOMEM;
2237
2238 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002239 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002240 BUG();
2241
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002242 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002243}
2244#else
David S. Miller183cad12011-02-24 00:28:01 -05002245static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2246 const struct xfrm_migrate *m, int num_migrate,
2247 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002248{
2249 return -ENOPROTOOPT;
2250}
2251#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002252
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002253#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002254
2255static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002256 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002257 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2258 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2259 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2260 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2261 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2262 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002263 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002264 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002265 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002266 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002267 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002268 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002269 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002270 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2271 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002272 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002273 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002274 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2275 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276};
2277
Thomas Graf492b5582005-05-03 14:26:40 -07002278#undef XMSGSIZE
2279
Thomas Grafcf5cb792007-08-22 13:59:04 -07002280static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002281 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2282 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2283 [XFRMA_LASTUSED] = { .type = NLA_U64},
2284 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002285 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002286 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2287 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2288 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2289 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2290 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2291 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2292 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2293 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2294 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2295 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2296 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2297 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2298 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2299 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002300 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002301 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002302 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002303 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002304};
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002307 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002309 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002310} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2311 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2312 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2313 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002314 .dump = xfrm_dump_sa,
2315 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002316 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2317 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2318 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002319 .dump = xfrm_dump_policy,
2320 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002321 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002322 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002323 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002324 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2325 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002326 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002327 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2328 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002329 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2330 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002331 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002332 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002333 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334};
2335
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002336static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002338 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002339 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002341 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002345 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 type -= XFRM_MSG_BASE;
2348 link = &xfrm_dispatch[type];
2349
2350 /* All operations require privileges, even GET */
Eric Parisfd778462012-01-03 12:25:16 -05002351 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002352 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Thomas Graf492b5582005-05-03 14:26:40 -07002354 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2355 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002356 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002360 {
2361 struct netlink_dump_control c = {
2362 .dump = link->dump,
2363 .done = link->done,
2364 };
2365 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368
Thomas Graf35a7aa02007-08-22 14:00:40 -07002369 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002370 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002371 if (err < 0)
2372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Thomas Graf5424f322007-08-22 14:01:33 -07002377 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002380static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002382 mutex_lock(&xfrm_cfg_mutex);
2383 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2384 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
Thomas Graf7deb2262007-08-22 13:57:39 -07002387static inline size_t xfrm_expire_msgsize(void)
2388{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002389 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2390 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002391}
2392
David S. Miller214e0052011-02-24 00:02:38 -05002393static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 struct xfrm_user_expire *ue;
2396 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002397 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002399 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2400 if (nlh == NULL)
2401 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Thomas Graf7b67c852007-08-22 13:53:52 -07002403 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002405 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
David S. Miller1d1e34d2012-06-27 21:57:03 -07002407 err = xfrm_mark_put(skb, &x->mark);
2408 if (err)
2409 return err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002410
Thomas Graf98250692007-08-22 12:47:26 -07002411 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
David S. Miller214e0052011-02-24 00:02:38 -05002414static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002416 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 struct sk_buff *skb;
2418
Thomas Graf7deb2262007-08-22 13:57:39 -07002419 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (skb == NULL)
2421 return -ENOMEM;
2422
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002423 if (build_expire(skb, x, c) < 0) {
2424 kfree_skb(skb);
2425 return -EMSGSIZE;
2426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002428 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
David S. Miller214e0052011-02-24 00:02:38 -05002431static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002432{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002433 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002434 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002435
Steffen Klassertd8647b72011-03-08 00:10:27 +00002436 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002437 if (skb == NULL)
2438 return -ENOMEM;
2439
2440 if (build_aevent(skb, x, c) < 0)
2441 BUG();
2442
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002443 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002444}
2445
David S. Miller214e0052011-02-24 00:02:38 -05002446static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002447{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002448 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002449 struct xfrm_usersa_flush *p;
2450 struct nlmsghdr *nlh;
2451 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002452 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002453
Thomas Graf7deb2262007-08-22 13:57:39 -07002454 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002455 if (skb == NULL)
2456 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002458 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2459 if (nlh == NULL) {
2460 kfree_skb(skb);
2461 return -EMSGSIZE;
2462 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463
Thomas Graf7b67c852007-08-22 13:53:52 -07002464 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002465 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002466
Thomas Graf98250692007-08-22 12:47:26 -07002467 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002468
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002469 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002470}
2471
Thomas Graf7deb2262007-08-22 13:57:39 -07002472static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002473{
Thomas Graf7deb2262007-08-22 13:57:39 -07002474 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002475 if (x->aead)
2476 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002477 if (x->aalg) {
2478 l += nla_total_size(sizeof(struct xfrm_algo) +
2479 (x->aalg->alg_key_len + 7) / 8);
2480 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2481 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002482 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002483 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002484 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002485 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002486 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002487 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002488 if (x->tfcpad)
2489 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002490 if (x->replay_esn)
2491 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002492 if (x->security)
2493 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2494 x->security->ctx_len);
2495 if (x->coaddr)
2496 l += nla_total_size(sizeof(*x->coaddr));
2497
Herbert Xud26f3982007-11-13 21:47:08 -08002498 /* Must count x->lastused as it may become non-zero behind our back. */
2499 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002500
2501 return l;
2502}
2503
David S. Miller214e0052011-02-24 00:02:38 -05002504static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002506 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002507 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002508 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002509 struct nlmsghdr *nlh;
2510 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002511 int len = xfrm_sa_len(x);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002512 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002513
2514 headlen = sizeof(*p);
2515 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002516 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002517 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002518 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002519 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002520 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521
Thomas Graf7deb2262007-08-22 13:57:39 -07002522 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 if (skb == NULL)
2524 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002525
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002526 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002527 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002528 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002529 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002530
Thomas Graf7b67c852007-08-22 13:53:52 -07002531 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002532 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002533 struct nlattr *attr;
2534
Thomas Graf7b67c852007-08-22 13:53:52 -07002535 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002536 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2537 id->spi = x->id.spi;
2538 id->family = x->props.family;
2539 id->proto = x->id.proto;
2540
Thomas Grafc0144be2007-08-22 13:55:43 -07002541 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002542 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002543 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002544 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002545
2546 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002547 }
David S. Miller1d1e34d2012-06-27 21:57:03 -07002548 err = copy_to_user_state_extra(x, p, skb);
2549 if (err)
2550 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002551
Thomas Graf98250692007-08-22 12:47:26 -07002552 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002553
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002554 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002555
David S. Miller1d1e34d2012-06-27 21:57:03 -07002556out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002557 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002558 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002559}
2560
David S. Miller214e0052011-02-24 00:02:38 -05002561static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002562{
2563
2564 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002565 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002566 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002567 case XFRM_MSG_NEWAE:
2568 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002569 case XFRM_MSG_DELSA:
2570 case XFRM_MSG_UPDSA:
2571 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002572 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002573 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002574 return xfrm_notify_sa_flush(c);
2575 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002576 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2577 c->event);
2578 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002579 }
2580
2581 return 0;
2582
2583}
2584
Thomas Graf7deb2262007-08-22 13:57:39 -07002585static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2586 struct xfrm_policy *xp)
2587{
2588 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2589 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002590 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002591 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2592 + userpolicy_type_attrsize();
2593}
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2596 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2597 int dir)
2598{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002599 __u32 seq = xfrm_get_acqseq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 struct xfrm_user_acquire *ua;
2601 struct nlmsghdr *nlh;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002602 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002604 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2605 if (nlh == NULL)
2606 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Thomas Graf7b67c852007-08-22 13:53:52 -07002608 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 memcpy(&ua->id, &x->id, sizeof(ua->id));
2610 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2611 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2612 copy_to_user_policy(xp, &ua->policy, dir);
2613 ua->aalgos = xt->aalgos;
2614 ua->ealgos = xt->ealgos;
2615 ua->calgos = xt->calgos;
2616 ua->seq = x->km.seq = seq;
2617
David S. Miller1d1e34d2012-06-27 21:57:03 -07002618 err = copy_to_user_tmpl(xp, skb);
2619 if (!err)
2620 err = copy_to_user_state_sec_ctx(x, skb);
2621 if (!err)
2622 err = copy_to_user_policy_type(xp->type, skb);
2623 if (!err)
2624 err = xfrm_mark_put(skb, &xp->mark);
2625 if (err) {
2626 nlmsg_cancel(skb, nlh);
2627 return err;
2628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Thomas Graf98250692007-08-22 12:47:26 -07002630 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2634 struct xfrm_policy *xp, int dir)
2635{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002636 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Thomas Graf7deb2262007-08-22 13:57:39 -07002639 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (skb == NULL)
2641 return -ENOMEM;
2642
2643 if (build_acquire(skb, x, xt, xp, dir) < 0)
2644 BUG();
2645
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002646 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649/* User gives us xfrm_user_policy_info followed by an array of 0
2650 * or more templates.
2651 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002652static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 u8 *data, int len, int *dir)
2654{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002655 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2657 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2658 struct xfrm_policy *xp;
2659 int nr;
2660
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002661 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 case AF_INET:
2663 if (opt != IP_XFRM_POLICY) {
2664 *dir = -EOPNOTSUPP;
2665 return NULL;
2666 }
2667 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002668#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 case AF_INET6:
2670 if (opt != IPV6_XFRM_POLICY) {
2671 *dir = -EOPNOTSUPP;
2672 return NULL;
2673 }
2674 break;
2675#endif
2676 default:
2677 *dir = -EINVAL;
2678 return NULL;
2679 }
2680
2681 *dir = -EINVAL;
2682
2683 if (len < sizeof(*p) ||
2684 verify_newpolicy_info(p))
2685 return NULL;
2686
2687 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002688 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return NULL;
2690
Herbert Xua4f1bac2005-07-26 15:43:17 -07002691 if (p->dir > XFRM_POLICY_OUT)
2692 return NULL;
2693
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002694 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (xp == NULL) {
2696 *dir = -ENOBUFS;
2697 return NULL;
2698 }
2699
2700 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002701 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 copy_templates(xp, ut, nr);
2703
2704 *dir = p->dir;
2705
2706 return xp;
2707}
2708
Thomas Graf7deb2262007-08-22 13:57:39 -07002709static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2710{
2711 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2712 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2713 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002714 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002715 + userpolicy_type_attrsize();
2716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002719 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 struct xfrm_user_polexpire *upe;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002722 int hard = c->data.hard;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002723 struct nlmsghdr *nlh;
2724 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002726 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2727 if (nlh == NULL)
2728 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Thomas Graf7b67c852007-08-22 13:53:52 -07002730 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 copy_to_user_policy(xp, &upe->pol, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002732 err = copy_to_user_tmpl(xp, skb);
2733 if (!err)
2734 err = copy_to_user_sec_ctx(xp, skb);
2735 if (!err)
2736 err = copy_to_user_policy_type(xp->type, skb);
2737 if (!err)
2738 err = xfrm_mark_put(skb, &xp->mark);
2739 if (err) {
2740 nlmsg_cancel(skb, nlh);
2741 return err;
2742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 upe->hard = !!hard;
2744
Thomas Graf98250692007-08-22 12:47:26 -07002745 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
David S. Miller214e0052011-02-24 00:02:38 -05002748static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002750 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Thomas Graf7deb2262007-08-22 13:57:39 -07002753 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (skb == NULL)
2755 return -ENOMEM;
2756
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002757 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 BUG();
2759
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002760 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
David S. Miller214e0052011-02-24 00:02:38 -05002763static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002764{
David S. Miller1d1e34d2012-06-27 21:57:03 -07002765 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002766 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002767 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002768 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002769 struct nlmsghdr *nlh;
2770 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002771 int headlen, err;
Herbert Xu0603eac2005-06-18 22:54:36 -07002772
2773 headlen = sizeof(*p);
2774 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002775 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002776 headlen = sizeof(*id);
2777 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002778 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002779 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002780 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002781
Thomas Graf7deb2262007-08-22 13:57:39 -07002782 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002783 if (skb == NULL)
2784 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002785
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002786 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002787 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002788 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002789 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790
Thomas Graf7b67c852007-08-22 13:53:52 -07002791 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002792 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002793 struct nlattr *attr;
2794
Thomas Graf7b67c852007-08-22 13:53:52 -07002795 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002796 memset(id, 0, sizeof(*id));
2797 id->dir = dir;
2798 if (c->data.byid)
2799 id->index = xp->index;
2800 else
2801 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2802
Thomas Grafc0144be2007-08-22 13:55:43 -07002803 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
David S. Miller1d1e34d2012-06-27 21:57:03 -07002804 err = -EMSGSIZE;
Thomas Grafc0144be2007-08-22 13:55:43 -07002805 if (attr == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002806 goto out_free_skb;
Thomas Grafc0144be2007-08-22 13:55:43 -07002807
2808 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002809 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002810
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002811 copy_to_user_policy(xp, p, dir);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002812 err = copy_to_user_tmpl(xp, skb);
2813 if (!err)
2814 err = copy_to_user_policy_type(xp->type, skb);
2815 if (!err)
2816 err = xfrm_mark_put(skb, &xp->mark);
2817 if (err)
2818 goto out_free_skb;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002819
Thomas Graf98250692007-08-22 12:47:26 -07002820 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002821
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002822 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002823
David S. Miller1d1e34d2012-06-27 21:57:03 -07002824out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002825 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002826 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002827}
2828
David S. Miller214e0052011-02-24 00:02:38 -05002829static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002830{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002831 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002832 struct nlmsghdr *nlh;
2833 struct sk_buff *skb;
David S. Miller1d1e34d2012-06-27 21:57:03 -07002834 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002835
Thomas Graf7deb2262007-08-22 13:57:39 -07002836 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002837 if (skb == NULL)
2838 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002839
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002840 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002841 err = -EMSGSIZE;
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002842 if (nlh == NULL)
David S. Miller1d1e34d2012-06-27 21:57:03 -07002843 goto out_free_skb;
2844 err = copy_to_user_policy_type(c->data.type, skb);
2845 if (err)
2846 goto out_free_skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002847
Thomas Graf98250692007-08-22 12:47:26 -07002848 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002849
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002850 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002851
David S. Miller1d1e34d2012-06-27 21:57:03 -07002852out_free_skb:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002853 kfree_skb(skb);
David S. Miller1d1e34d2012-06-27 21:57:03 -07002854 return err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002855}
2856
David S. Miller214e0052011-02-24 00:02:38 -05002857static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002858{
2859
2860 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002861 case XFRM_MSG_NEWPOLICY:
2862 case XFRM_MSG_UPDPOLICY:
2863 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002864 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002865 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002866 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002867 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002868 return xfrm_exp_policy_notify(xp, dir, c);
2869 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002870 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2871 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002872 }
2873
2874 return 0;
2875
2876}
2877
Thomas Graf7deb2262007-08-22 13:57:39 -07002878static inline size_t xfrm_report_msgsize(void)
2879{
2880 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2881}
2882
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002883static int build_report(struct sk_buff *skb, u8 proto,
2884 struct xfrm_selector *sel, xfrm_address_t *addr)
2885{
2886 struct xfrm_user_report *ur;
2887 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002888
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002889 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2890 if (nlh == NULL)
2891 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002892
Thomas Graf7b67c852007-08-22 13:53:52 -07002893 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002894 ur->proto = proto;
2895 memcpy(&ur->sel, sel, sizeof(ur->sel));
2896
David S. Miller1d1e34d2012-06-27 21:57:03 -07002897 if (addr) {
2898 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
2899 if (err) {
2900 nlmsg_cancel(skb, nlh);
2901 return err;
2902 }
2903 }
Thomas Graf98250692007-08-22 12:47:26 -07002904 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002905}
2906
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002907static int xfrm_send_report(struct net *net, u8 proto,
2908 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002909{
2910 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002911
Thomas Graf7deb2262007-08-22 13:57:39 -07002912 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002913 if (skb == NULL)
2914 return -ENOMEM;
2915
2916 if (build_report(skb, proto, sel, addr) < 0)
2917 BUG();
2918
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002919 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002920}
2921
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002922static inline size_t xfrm_mapping_msgsize(void)
2923{
2924 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2925}
2926
2927static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2928 xfrm_address_t *new_saddr, __be16 new_sport)
2929{
2930 struct xfrm_user_mapping *um;
2931 struct nlmsghdr *nlh;
2932
2933 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2934 if (nlh == NULL)
2935 return -EMSGSIZE;
2936
2937 um = nlmsg_data(nlh);
2938
2939 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2940 um->id.spi = x->id.spi;
2941 um->id.family = x->props.family;
2942 um->id.proto = x->id.proto;
2943 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2944 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2945 um->new_sport = new_sport;
2946 um->old_sport = x->encap->encap_sport;
2947 um->reqid = x->props.reqid;
2948
2949 return nlmsg_end(skb, nlh);
2950}
2951
2952static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2953 __be16 sport)
2954{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002955 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002956 struct sk_buff *skb;
2957
2958 if (x->id.proto != IPPROTO_ESP)
2959 return -EINVAL;
2960
2961 if (!x->encap)
2962 return -EINVAL;
2963
2964 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2965 if (skb == NULL)
2966 return -ENOMEM;
2967
2968 if (build_mapping(skb, x, ipaddr, sport) < 0)
2969 BUG();
2970
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002971 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002972}
2973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974static struct xfrm_mgr netlink_mgr = {
2975 .id = "netlink",
2976 .notify = xfrm_send_state_notify,
2977 .acquire = xfrm_send_acquire,
2978 .compile_policy = xfrm_compile_policy,
2979 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002980 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002981 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002982 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983};
2984
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002985static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Patrick McHardybe336902006-03-20 22:40:54 -08002987 struct sock *nlsk;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002988 struct netlink_kernel_cfg cfg = {
2989 .groups = XFRMNLGRP_MAX,
2990 .input = xfrm_netlink_rcv,
2991 };
Patrick McHardybe336902006-03-20 22:40:54 -08002992
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002993 nlsk = netlink_kernel_create(net, NETLINK_XFRM, THIS_MODULE, &cfg);
Patrick McHardybe336902006-03-20 22:40:54 -08002994 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002996 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Eric Dumazetcf778b02012-01-12 04:41:32 +00002997 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return 0;
2999}
3000
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003001static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003002{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003003 struct net *net;
3004 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003005 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003006 synchronize_net();
3007 list_for_each_entry(net, net_exit_list, exit_list)
3008 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003009}
3010
3011static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00003012 .init = xfrm_user_net_init,
3013 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003014};
3015
3016static int __init xfrm_user_init(void)
3017{
3018 int rv;
3019
3020 printk(KERN_INFO "Initializing XFRM netlink socket\n");
3021
3022 rv = register_pernet_subsys(&xfrm_user_net_ops);
3023 if (rv < 0)
3024 return rv;
3025 rv = xfrm_register_km(&netlink_mgr);
3026 if (rv < 0)
3027 unregister_pernet_subsys(&xfrm_user_net_ops);
3028 return rv;
3029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031static void __exit xfrm_user_exit(void)
3032{
3033 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08003034 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
3037module_init(xfrm_user_init);
3038module_exit(xfrm_user_exit);
3039MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07003040MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08003041