blob: fc152d28753c41df6bd7e26becee2b1657a3ace8 [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>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070031#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
32#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];
126
127 if (!rt)
128 return 0;
129
130 if (p->replay_window != 0)
131 return -EINVAL;
132
133 return 0;
134}
Trent Jaegerdf718372005-12-13 23:12:27 -0800135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700137 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int err;
140
141 err = -EINVAL;
142 switch (p->family) {
143 case AF_INET:
144 break;
145
146 case AF_INET6:
147#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
148 break;
149#else
150 err = -EAFNOSUPPORT;
151 goto out;
152#endif
153
154 default:
155 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 err = -EINVAL;
159 switch (p->id.proto) {
160 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000161 if ((!attrs[XFRMA_ALG_AUTH] &&
162 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800163 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700164 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000165 attrs[XFRMA_ALG_COMP] ||
166 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto out;
168 break;
169
170 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800171 if (attrs[XFRMA_ALG_COMP])
172 goto out;
173 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000174 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800175 !attrs[XFRMA_ALG_CRYPT] &&
176 !attrs[XFRMA_ALG_AEAD])
177 goto out;
178 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000179 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800180 attrs[XFRMA_ALG_CRYPT]) &&
181 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000183 if (attrs[XFRMA_TFCPAD] &&
184 p->mode != XFRM_MODE_TUNNEL)
185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187
188 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700189 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800190 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700191 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000192 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000193 attrs[XFRMA_ALG_CRYPT] ||
194 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196 break;
197
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700198#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
199 case IPPROTO_DSTOPTS:
200 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700201 if (attrs[XFRMA_ALG_COMP] ||
202 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000203 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800204 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700205 attrs[XFRMA_ALG_CRYPT] ||
206 attrs[XFRMA_ENCAP] ||
207 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000208 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700210 goto out;
211 break;
212#endif
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 default:
215 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Herbert Xu1a6509d2008-01-28 19:37:29 -0800218 if ((err = verify_aead(attrs)))
219 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000220 if ((err = verify_auth_trunc(attrs)))
221 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700222 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700224 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700226 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700228 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800229 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000230 if ((err = verify_replay(p, attrs)))
231 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 err = -EINVAL;
234 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700235 case XFRM_MODE_TRANSPORT:
236 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700237 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700238 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240
241 default:
242 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 err = 0;
246
247out:
248 return err;
249}
250
251static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800252 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700253 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct xfrm_algo *p, *ualg;
256 struct xfrm_algo_desc *algo;
257
258 if (!rta)
259 return 0;
260
Thomas Graf5424f322007-08-22 14:01:33 -0700261 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 algo = get_byname(ualg->alg_name, 1);
264 if (!algo)
265 return -ENOSYS;
266 *props = algo->desc.sadb_alg_id;
267
Eric Dumazet0f99be02008-01-08 23:39:06 -0800268 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!p)
270 return -ENOMEM;
271
Herbert Xu04ff1262006-08-13 08:50:00 +1000272 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *algpp = p;
274 return 0;
275}
276
Martin Willi4447bb32009-11-25 00:29:52 +0000277static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
278 struct nlattr *rta)
279{
280 struct xfrm_algo *ualg;
281 struct xfrm_algo_auth *p;
282 struct xfrm_algo_desc *algo;
283
284 if (!rta)
285 return 0;
286
287 ualg = nla_data(rta);
288
289 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
290 if (!algo)
291 return -ENOSYS;
292 *props = algo->desc.sadb_alg_id;
293
294 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
295 if (!p)
296 return -ENOMEM;
297
298 strcpy(p->alg_name, algo->name);
299 p->alg_key_len = ualg->alg_key_len;
300 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
301 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
302
303 *algpp = p;
304 return 0;
305}
306
307static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
308 struct nlattr *rta)
309{
310 struct xfrm_algo_auth *p, *ualg;
311 struct xfrm_algo_desc *algo;
312
313 if (!rta)
314 return 0;
315
316 ualg = nla_data(rta);
317
318 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
319 if (!algo)
320 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000321 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
322 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000323 return -EINVAL;
324 *props = algo->desc.sadb_alg_id;
325
326 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
327 if (!p)
328 return -ENOMEM;
329
330 strcpy(p->alg_name, algo->name);
331 if (!p->alg_trunc_len)
332 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
333
334 *algpp = p;
335 return 0;
336}
337
Herbert Xu1a6509d2008-01-28 19:37:29 -0800338static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
339 struct nlattr *rta)
340{
341 struct xfrm_algo_aead *p, *ualg;
342 struct xfrm_algo_desc *algo;
343
344 if (!rta)
345 return 0;
346
347 ualg = nla_data(rta);
348
349 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
350 if (!algo)
351 return -ENOSYS;
352 *props = algo->desc.sadb_alg_id;
353
354 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
355 if (!p)
356 return -ENOMEM;
357
358 strcpy(p->alg_name, algo->name);
359 *algpp = p;
360 return 0;
361}
362
Steffen Klassertd8647b72011-03-08 00:10:27 +0000363static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
364 struct xfrm_replay_state_esn **preplay_esn,
365 struct nlattr *rta)
366{
367 struct xfrm_replay_state_esn *p, *pp, *up;
368
369 if (!rta)
370 return 0;
371
372 up = nla_data(rta);
373
374 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
375 if (!p)
376 return -ENOMEM;
377
378 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
379 if (!pp) {
380 kfree(p);
381 return -ENOMEM;
382 }
383
384 *replay_esn = p;
385 *preplay_esn = pp;
386
387 return 0;
388}
389
Joy Latten661697f2007-04-13 16:14:35 -0700390static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800391{
Trent Jaegerdf718372005-12-13 23:12:27 -0800392 int len = 0;
393
394 if (xfrm_ctx) {
395 len += sizeof(struct xfrm_user_sec_ctx);
396 len += xfrm_ctx->ctx_len;
397 }
398 return len;
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
402{
403 memcpy(&x->id, &p->id, sizeof(x->id));
404 memcpy(&x->sel, &p->sel, sizeof(x->sel));
405 memcpy(&x->lft, &p->lft, sizeof(x->lft));
406 x->props.mode = p->mode;
407 x->props.replay_window = p->replay_window;
408 x->props.reqid = p->reqid;
409 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700410 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700412
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700413 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700414 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800417/*
418 * someday when pfkey also has support, we could have the code
419 * somehow made shareable and move it to xfrm_state.c - JHS
420 *
421*/
Thomas Graf5424f322007-08-22 14:01:33 -0700422static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800423{
Thomas Graf5424f322007-08-22 14:01:33 -0700424 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +0000425 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -0700426 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
427 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
428 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800429
Steffen Klassertd8647b72011-03-08 00:10:27 +0000430 if (re) {
431 struct xfrm_replay_state_esn *replay_esn;
432 replay_esn = nla_data(re);
433 memcpy(x->replay_esn, replay_esn,
434 xfrm_replay_state_esn_len(replay_esn));
435 memcpy(x->preplay_esn, replay_esn,
436 xfrm_replay_state_esn_len(replay_esn));
437 }
438
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800439 if (rp) {
440 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700441 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800442 memcpy(&x->replay, replay, sizeof(*replay));
443 memcpy(&x->preplay, replay, sizeof(*replay));
444 }
445
446 if (lt) {
447 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700448 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800449 x->curlft.bytes = ltime->bytes;
450 x->curlft.packets = ltime->packets;
451 x->curlft.add_time = ltime->add_time;
452 x->curlft.use_time = ltime->use_time;
453 }
454
Thomas Grafcf5cb792007-08-22 13:59:04 -0700455 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700456 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800457
Thomas Grafcf5cb792007-08-22 13:59:04 -0700458 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700459 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800460}
461
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800462static struct xfrm_state *xfrm_state_construct(struct net *net,
463 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700464 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int *errp)
466{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800467 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int err = -ENOMEM;
469
470 if (!x)
471 goto error_no_put;
472
473 copy_from_user_state(x, p);
474
Herbert Xu1a6509d2008-01-28 19:37:29 -0800475 if ((err = attach_aead(&x->aead, &x->props.ealgo,
476 attrs[XFRMA_ALG_AEAD])))
477 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000478 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
479 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000481 if (!x->props.aalgo) {
482 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
483 attrs[XFRMA_ALG_AUTH])))
484 goto error;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
487 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700488 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto error;
490 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
491 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700492 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700494
495 if (attrs[XFRMA_ENCAP]) {
496 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
497 sizeof(*x->encap), GFP_KERNEL);
498 if (x->encap == NULL)
499 goto error;
500 }
501
Martin Willi35d28562010-12-08 04:37:49 +0000502 if (attrs[XFRMA_TFCPAD])
503 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
504
Thomas Graffd211502007-09-06 03:28:08 -0700505 if (attrs[XFRMA_COADDR]) {
506 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
507 sizeof(*x->coaddr), GFP_KERNEL);
508 if (x->coaddr == NULL)
509 goto error;
510 }
511
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000512 xfrm_mark_get(attrs, &x->mark);
513
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700514 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (err)
516 goto error;
517
Thomas Graffd211502007-09-06 03:28:08 -0700518 if (attrs[XFRMA_SEC_CTX] &&
519 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800520 goto error;
521
Steffen Klassertd8647b72011-03-08 00:10:27 +0000522 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
523 attrs[XFRMA_REPLAY_ESN_VAL])))
524 goto error;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800527 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800528 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800529 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800530
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000531 if ((err = xfrm_init_replay(x)))
532 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800533
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000534 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700535 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return x;
538
539error:
540 x->km.state = XFRM_STATE_DEAD;
541 xfrm_state_put(x);
542error_no_put:
543 *errp = err;
544 return NULL;
545}
546
Christoph Hellwig22e70052007-01-02 15:22:30 -0800547static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700548 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800550 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700551 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct xfrm_state *x;
553 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700554 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800555 uid_t loginuid = audit_get_loginuid(current);
556 u32 sessionid = audit_get_sessionid(current);
557 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Thomas Graf35a7aa02007-08-22 14:00:40 -0700559 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (err)
561 return err;
562
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800563 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!x)
565 return err;
566
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700567 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
569 err = xfrm_state_add(x);
570 else
571 err = xfrm_state_update(x);
572
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800573 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400574 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (err < 0) {
577 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800578 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700582 c.seq = nlh->nlmsg_seq;
583 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700584 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700585
586 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700587out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700588 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return err;
590}
591
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800592static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
593 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700594 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700595 int *errp)
596{
597 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000598 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700599 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000600 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700601
602 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
603 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000604 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700605 } else {
606 xfrm_address_t *saddr = NULL;
607
Thomas Graf35a7aa02007-08-22 14:00:40 -0700608 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700609 if (!saddr) {
610 err = -EINVAL;
611 goto out;
612 }
613
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800614 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000615 x = xfrm_state_lookup_byaddr(net, mark,
616 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800617 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700618 }
619
620 out:
621 if (!x && errp)
622 *errp = err;
623 return x;
624}
625
Christoph Hellwig22e70052007-01-02 15:22:30 -0800626static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700627 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800629 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700631 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700632 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700633 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800634 uid_t loginuid = audit_get_loginuid(current);
635 u32 sessionid = audit_get_sessionid(current);
636 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800638 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
David S. Miller6f68dc32006-06-08 23:58:52 -0700642 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700643 goto out;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700646 err = -EPERM;
647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700650 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600651
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700652 if (err < 0)
653 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700654
655 c.seq = nlh->nlmsg_seq;
656 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700657 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700658 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700660out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800661 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400662 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700663 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
668{
669 memcpy(&p->id, &x->id, sizeof(p->id));
670 memcpy(&p->sel, &x->sel, sizeof(p->sel));
671 memcpy(&p->lft, &x->lft, sizeof(p->lft));
672 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
673 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700674 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 p->mode = x->props.mode;
676 p->replay_window = x->props.replay_window;
677 p->reqid = x->props.reqid;
678 p->family = x->props.family;
679 p->flags = x->props.flags;
680 p->seq = x->km.seq;
681}
682
683struct xfrm_dump_info {
684 struct sk_buff *in_skb;
685 struct sk_buff *out_skb;
686 u32 nlmsg_seq;
687 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688};
689
Thomas Grafc0144be2007-08-22 13:55:43 -0700690static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
691{
Thomas Grafc0144be2007-08-22 13:55:43 -0700692 struct xfrm_user_sec_ctx *uctx;
693 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700694 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700695
696 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
697 if (attr == NULL)
698 return -EMSGSIZE;
699
700 uctx = nla_data(attr);
701 uctx->exttype = XFRMA_SEC_CTX;
702 uctx->len = ctx_size;
703 uctx->ctx_doi = s->ctx_doi;
704 uctx->ctx_alg = s->ctx_alg;
705 uctx->ctx_len = s->ctx_len;
706 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
707
708 return 0;
709}
710
Martin Willi4447bb32009-11-25 00:29:52 +0000711static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
712{
713 struct xfrm_algo *algo;
714 struct nlattr *nla;
715
716 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
717 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
718 if (!nla)
719 return -EMSGSIZE;
720
721 algo = nla_data(nla);
722 strcpy(algo->alg_name, auth->alg_name);
723 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
724 algo->alg_key_len = auth->alg_key_len;
725
726 return 0;
727}
728
Herbert Xu68325d32007-10-09 13:30:57 -0700729/* Don't change this without updating xfrm_sa_len! */
730static int copy_to_user_state_extra(struct xfrm_state *x,
731 struct xfrm_usersa_info *p,
732 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 copy_to_user_state(x, p);
735
Herbert Xu050f0092007-10-09 13:31:47 -0700736 if (x->coaddr)
737 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
738
739 if (x->lastused)
740 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700741
Herbert Xu1a6509d2008-01-28 19:37:29 -0800742 if (x->aead)
743 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000744 if (x->aalg) {
745 if (copy_to_user_auth(x->aalg, skb))
746 goto nla_put_failure;
747
748 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
749 xfrm_alg_auth_len(x->aalg), x->aalg);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800752 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700754 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700757 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Martin Willi35d28562010-12-08 04:37:49 +0000759 if (x->tfcpad)
760 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
761
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000762 if (xfrm_mark_put(skb, &x->mark))
763 goto nla_put_failure;
764
Steffen Klassertd8647b72011-03-08 00:10:27 +0000765 if (x->replay_esn)
766 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
767 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
768
Thomas Grafc0144be2007-08-22 13:55:43 -0700769 if (x->security && copy_sec_ctx(x->security, skb) < 0)
770 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700771
Herbert Xu68325d32007-10-09 13:30:57 -0700772 return 0;
773
774nla_put_failure:
775 return -EMSGSIZE;
776}
777
778static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
779{
780 struct xfrm_dump_info *sp = ptr;
781 struct sk_buff *in_skb = sp->in_skb;
782 struct sk_buff *skb = sp->out_skb;
783 struct xfrm_usersa_info *p;
784 struct nlmsghdr *nlh;
785 int err;
786
Herbert Xu68325d32007-10-09 13:30:57 -0700787 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
788 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
789 if (nlh == NULL)
790 return -EMSGSIZE;
791
792 p = nlmsg_data(nlh);
793
794 err = copy_to_user_state_extra(x, p, skb);
795 if (err)
796 goto nla_put_failure;
797
Thomas Graf98250692007-08-22 12:47:26 -0700798 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800
Thomas Grafc0144be2007-08-22 13:55:43 -0700801nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700802 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700803 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Timo Teras4c563f72008-02-28 21:31:08 -0800806static int xfrm_dump_sa_done(struct netlink_callback *cb)
807{
808 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
809 xfrm_state_walk_done(walk);
810 return 0;
811}
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
814{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800815 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800816 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct xfrm_dump_info info;
818
Timo Teras4c563f72008-02-28 21:31:08 -0800819 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
820 sizeof(cb->args) - sizeof(cb->args[0]));
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 info.in_skb = cb->skb;
823 info.out_skb = skb;
824 info.nlmsg_seq = cb->nlh->nlmsg_seq;
825 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800826
827 if (!cb->args[0]) {
828 cb->args[0] = 1;
829 xfrm_state_walk_init(walk, 0);
830 }
831
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800832 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 return skb->len;
835}
836
837static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
838 struct xfrm_state *x, u32 seq)
839{
840 struct xfrm_dump_info info;
841 struct sk_buff *skb;
842
Thomas Graf7deb2262007-08-22 13:57:39 -0700843 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (!skb)
845 return ERR_PTR(-ENOMEM);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 info.in_skb = in_skb;
848 info.out_skb = skb;
849 info.nlmsg_seq = seq;
850 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (dump_one_state(x, 0, &info)) {
853 kfree_skb(skb);
854 return NULL;
855 }
856
857 return skb;
858}
859
Thomas Graf7deb2262007-08-22 13:57:39 -0700860static inline size_t xfrm_spdinfo_msgsize(void)
861{
862 return NLMSG_ALIGN(4)
863 + nla_total_size(sizeof(struct xfrmu_spdinfo))
864 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
865}
866
Alexey Dobriyane0710412010-01-23 13:37:10 +0000867static int build_spdinfo(struct sk_buff *skb, struct net *net,
868 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700869{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700870 struct xfrmk_spdinfo si;
871 struct xfrmu_spdinfo spc;
872 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700873 struct nlmsghdr *nlh;
874 u32 *f;
875
876 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
877 if (nlh == NULL) /* shouldnt really happen ... */
878 return -EMSGSIZE;
879
880 f = nlmsg_data(nlh);
881 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000882 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700883 spc.incnt = si.incnt;
884 spc.outcnt = si.outcnt;
885 spc.fwdcnt = si.fwdcnt;
886 spc.inscnt = si.inscnt;
887 spc.outscnt = si.outscnt;
888 spc.fwdscnt = si.fwdscnt;
889 sph.spdhcnt = si.spdhcnt;
890 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700891
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700892 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
893 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700894
895 return nlmsg_end(skb, nlh);
896
897nla_put_failure:
898 nlmsg_cancel(skb, nlh);
899 return -EMSGSIZE;
900}
901
902static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700903 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700904{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800905 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700906 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700907 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700908 u32 spid = NETLINK_CB(skb).pid;
909 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700910
Thomas Graf7deb2262007-08-22 13:57:39 -0700911 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700912 if (r_skb == NULL)
913 return -ENOMEM;
914
Alexey Dobriyane0710412010-01-23 13:37:10 +0000915 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700916 BUG();
917
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800918 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700919}
920
Thomas Graf7deb2262007-08-22 13:57:39 -0700921static inline size_t xfrm_sadinfo_msgsize(void)
922{
923 return NLMSG_ALIGN(4)
924 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
925 + nla_total_size(4); /* XFRMA_SAD_CNT */
926}
927
Alexey Dobriyane0710412010-01-23 13:37:10 +0000928static int build_sadinfo(struct sk_buff *skb, struct net *net,
929 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700930{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700931 struct xfrmk_sadinfo si;
932 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700933 struct nlmsghdr *nlh;
934 u32 *f;
935
936 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
937 if (nlh == NULL) /* shouldnt really happen ... */
938 return -EMSGSIZE;
939
940 f = nlmsg_data(nlh);
941 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000942 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700943
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700944 sh.sadhmcnt = si.sadhmcnt;
945 sh.sadhcnt = si.sadhcnt;
946
947 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
948 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700949
950 return nlmsg_end(skb, nlh);
951
952nla_put_failure:
953 nlmsg_cancel(skb, nlh);
954 return -EMSGSIZE;
955}
956
957static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700958 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700959{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800960 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700961 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700962 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700963 u32 spid = NETLINK_CB(skb).pid;
964 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700965
Thomas Graf7deb2262007-08-22 13:57:39 -0700966 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700967 if (r_skb == NULL)
968 return -ENOMEM;
969
Alexey Dobriyane0710412010-01-23 13:37:10 +0000970 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700971 BUG();
972
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800973 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700974}
975
Christoph Hellwig22e70052007-01-02 15:22:30 -0800976static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700977 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800979 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700980 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct xfrm_state *x;
982 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700983 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800985 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (x == NULL)
987 goto out_noput;
988
989 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
990 if (IS_ERR(resp_skb)) {
991 err = PTR_ERR(resp_skb);
992 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800993 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
995 xfrm_state_put(x);
996out_noput:
997 return err;
998}
999
1000static int verify_userspi_info(struct xfrm_userspi_info *p)
1001{
1002 switch (p->info.id.proto) {
1003 case IPPROTO_AH:
1004 case IPPROTO_ESP:
1005 break;
1006
1007 case IPPROTO_COMP:
1008 /* IPCOMP spi is 16-bits. */
1009 if (p->max >= 0x10000)
1010 return -EINVAL;
1011 break;
1012
1013 default:
1014 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (p->min > p->max)
1018 return -EINVAL;
1019
1020 return 0;
1021}
1022
Christoph Hellwig22e70052007-01-02 15:22:30 -08001023static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001024 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001026 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 struct xfrm_state *x;
1028 struct xfrm_userspi_info *p;
1029 struct sk_buff *resp_skb;
1030 xfrm_address_t *daddr;
1031 int family;
1032 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001033 u32 mark;
1034 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Thomas Graf7b67c852007-08-22 13:53:52 -07001036 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 err = verify_userspi_info(p);
1038 if (err)
1039 goto out_noput;
1040
1041 family = p->info.family;
1042 daddr = &p->info.id.daddr;
1043
1044 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001045
1046 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001048 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1050 xfrm_state_put(x);
1051 x = NULL;
1052 }
1053 }
1054
1055 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001056 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 p->info.id.proto, daddr,
1058 &p->info.saddr, 1,
1059 family);
1060 err = -ENOENT;
1061 if (x == NULL)
1062 goto out_noput;
1063
Herbert Xu658b2192007-10-09 13:29:52 -07001064 err = xfrm_alloc_spi(x, p->min, p->max);
1065 if (err)
1066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Herbert Xu658b2192007-10-09 13:29:52 -07001068 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (IS_ERR(resp_skb)) {
1070 err = PTR_ERR(resp_skb);
1071 goto out;
1072 }
1073
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001074 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076out:
1077 xfrm_state_put(x);
1078out_noput:
1079 return err;
1080}
1081
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001082static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 switch (dir) {
1085 case XFRM_POLICY_IN:
1086 case XFRM_POLICY_OUT:
1087 case XFRM_POLICY_FWD:
1088 break;
1089
1090 default:
1091 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 return 0;
1095}
1096
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001097static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001098{
1099 switch (type) {
1100 case XFRM_POLICY_TYPE_MAIN:
1101#ifdef CONFIG_XFRM_SUB_POLICY
1102 case XFRM_POLICY_TYPE_SUB:
1103#endif
1104 break;
1105
1106 default:
1107 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001108 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001109
1110 return 0;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1114{
1115 switch (p->share) {
1116 case XFRM_SHARE_ANY:
1117 case XFRM_SHARE_SESSION:
1118 case XFRM_SHARE_USER:
1119 case XFRM_SHARE_UNIQUE:
1120 break;
1121
1122 default:
1123 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 switch (p->action) {
1127 case XFRM_POLICY_ALLOW:
1128 case XFRM_POLICY_BLOCK:
1129 break;
1130
1131 default:
1132 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 switch (p->sel.family) {
1136 case AF_INET:
1137 break;
1138
1139 case AF_INET6:
1140#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1141 break;
1142#else
1143 return -EAFNOSUPPORT;
1144#endif
1145
1146 default:
1147 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 return verify_policy_dir(p->dir);
1151}
1152
Thomas Graf5424f322007-08-22 14:01:33 -07001153static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001154{
Thomas Graf5424f322007-08-22 14:01:33 -07001155 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001156 struct xfrm_user_sec_ctx *uctx;
1157
1158 if (!rt)
1159 return 0;
1160
Thomas Graf5424f322007-08-22 14:01:33 -07001161 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001162 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1166 int nr)
1167{
1168 int i;
1169
1170 xp->xfrm_nr = nr;
1171 for (i = 0; i < nr; i++, ut++) {
1172 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1173
1174 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1175 memcpy(&t->saddr, &ut->saddr,
1176 sizeof(xfrm_address_t));
1177 t->reqid = ut->reqid;
1178 t->mode = ut->mode;
1179 t->share = ut->share;
1180 t->optional = ut->optional;
1181 t->aalgos = ut->aalgos;
1182 t->ealgos = ut->ealgos;
1183 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001184 /* If all masks are ~0, then we allow all algorithms. */
1185 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001186 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188}
1189
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001190static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1191{
1192 int i;
1193
1194 if (nr > XFRM_MAX_DEPTH)
1195 return -EINVAL;
1196
1197 for (i = 0; i < nr; i++) {
1198 /* We never validated the ut->family value, so many
1199 * applications simply leave it at zero. The check was
1200 * never made and ut->family was ignored because all
1201 * templates could be assumed to have the same family as
1202 * the policy itself. Now that we will have ipv4-in-ipv6
1203 * and ipv6-in-ipv4 tunnels, this is no longer true.
1204 */
1205 if (!ut[i].family)
1206 ut[i].family = family;
1207
1208 switch (ut[i].family) {
1209 case AF_INET:
1210 break;
1211#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1212 case AF_INET6:
1213 break;
1214#endif
1215 default:
1216 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001217 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001218 }
1219
1220 return 0;
1221}
1222
Thomas Graf5424f322007-08-22 14:01:33 -07001223static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Thomas Graf5424f322007-08-22 14:01:33 -07001225 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (!rt) {
1228 pol->xfrm_nr = 0;
1229 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001230 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1231 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001232 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001234 err = validate_tmpl(nr, utmpl, pol->family);
1235 if (err)
1236 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Thomas Graf5424f322007-08-22 14:01:33 -07001238 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 return 0;
1241}
1242
Thomas Graf5424f322007-08-22 14:01:33 -07001243static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001244{
Thomas Graf5424f322007-08-22 14:01:33 -07001245 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001246 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001247 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001248 int err;
1249
1250 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001251 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001252 type = upt->type;
1253 }
1254
1255 err = verify_policy_type(type);
1256 if (err)
1257 return err;
1258
1259 *tp = type;
1260 return 0;
1261}
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1264{
1265 xp->priority = p->priority;
1266 xp->index = p->index;
1267 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1268 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1269 xp->action = p->action;
1270 xp->flags = p->flags;
1271 xp->family = p->sel.family;
1272 /* XXX xp->share = p->share; */
1273}
1274
1275static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1276{
1277 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1278 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1279 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1280 p->priority = xp->priority;
1281 p->index = xp->index;
1282 p->sel.family = xp->family;
1283 p->dir = dir;
1284 p->action = xp->action;
1285 p->flags = xp->flags;
1286 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1287}
1288
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001289static 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 -07001290{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001291 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int err;
1293
1294 if (!xp) {
1295 *errp = -ENOMEM;
1296 return NULL;
1297 }
1298
1299 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001300
Thomas Graf35a7aa02007-08-22 14:00:40 -07001301 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001302 if (err)
1303 goto error;
1304
Thomas Graf35a7aa02007-08-22 14:00:40 -07001305 if (!(err = copy_from_user_tmpl(xp, attrs)))
1306 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001307 if (err)
1308 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001310 xfrm_mark_get(attrs, &xp->mark);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001313 error:
1314 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001315 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001316 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001317 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Christoph Hellwig22e70052007-01-02 15:22:30 -08001320static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001321 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001323 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001324 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001326 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int err;
1328 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001329 uid_t loginuid = audit_get_loginuid(current);
1330 u32 sessionid = audit_get_sessionid(current);
1331 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 err = verify_newpolicy_info(p);
1334 if (err)
1335 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001336 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001337 if (err)
1338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001340 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!xp)
1342 return err;
1343
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001344 /* shouldnt excl be based on nlh flags??
1345 * Aha! this is anti-netlink really i.e more pfkey derived
1346 * in netlink excl is a flag and you wouldnt need
1347 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1349 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001350 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001351 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001354 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 kfree(xp);
1356 return err;
1357 }
1358
Herbert Xuf60f6b82005-06-18 22:44:37 -07001359 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001360 c.seq = nlh->nlmsg_seq;
1361 c.pid = nlh->nlmsg_pid;
1362 km_policy_notify(xp, p->dir, &c);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 xfrm_pol_put(xp);
1365
1366 return 0;
1367}
1368
1369static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1370{
1371 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1372 int i;
1373
1374 if (xp->xfrm_nr == 0)
1375 return 0;
1376
1377 for (i = 0; i < xp->xfrm_nr; i++) {
1378 struct xfrm_user_tmpl *up = &vec[i];
1379 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1380
1381 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001382 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1384 up->reqid = kp->reqid;
1385 up->mode = kp->mode;
1386 up->share = kp->share;
1387 up->optional = kp->optional;
1388 up->aalgos = kp->aalgos;
1389 up->ealgos = kp->ealgos;
1390 up->calgos = kp->calgos;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Thomas Grafc0144be2007-08-22 13:55:43 -07001393 return nla_put(skb, XFRMA_TMPL,
1394 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001395}
1396
Serge Hallyn0d681622006-07-24 23:30:44 -07001397static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1398{
1399 if (x->security) {
1400 return copy_sec_ctx(x->security, skb);
1401 }
1402 return 0;
1403}
1404
1405static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1406{
1407 if (xp->security) {
1408 return copy_sec_ctx(xp->security, skb);
1409 }
1410 return 0;
1411}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001412static inline size_t userpolicy_type_attrsize(void)
1413{
1414#ifdef CONFIG_XFRM_SUB_POLICY
1415 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1416#else
1417 return 0;
1418#endif
1419}
Serge Hallyn0d681622006-07-24 23:30:44 -07001420
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001421#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001422static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001423{
Thomas Grafc0144be2007-08-22 13:55:43 -07001424 struct xfrm_userpolicy_type upt = {
1425 .type = type,
1426 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001427
Thomas Grafc0144be2007-08-22 13:55:43 -07001428 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001429}
1430
1431#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001432static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001433{
1434 return 0;
1435}
1436#endif
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1439{
1440 struct xfrm_dump_info *sp = ptr;
1441 struct xfrm_userpolicy_info *p;
1442 struct sk_buff *in_skb = sp->in_skb;
1443 struct sk_buff *skb = sp->out_skb;
1444 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001446 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1447 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1448 if (nlh == NULL)
1449 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Thomas Graf7b67c852007-08-22 13:53:52 -07001451 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 copy_to_user_policy(xp, p, dir);
1453 if (copy_to_user_tmpl(xp, skb) < 0)
1454 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001455 if (copy_to_user_sec_ctx(xp, skb))
1456 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001457 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001458 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001459 if (xfrm_mark_put(skb, &xp->mark))
1460 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Thomas Graf98250692007-08-22 12:47:26 -07001462 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001465nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001467 nlmsg_cancel(skb, nlh);
1468 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Timo Teras4c563f72008-02-28 21:31:08 -08001471static int xfrm_dump_policy_done(struct netlink_callback *cb)
1472{
1473 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1474
1475 xfrm_policy_walk_done(walk);
1476 return 0;
1477}
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1480{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001481 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001482 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct xfrm_dump_info info;
1484
Timo Teras4c563f72008-02-28 21:31:08 -08001485 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1486 sizeof(cb->args) - sizeof(cb->args[0]));
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 info.in_skb = cb->skb;
1489 info.out_skb = skb;
1490 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1491 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001492
1493 if (!cb->args[0]) {
1494 cb->args[0] = 1;
1495 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1496 }
1497
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001498 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 return skb->len;
1501}
1502
1503static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1504 struct xfrm_policy *xp,
1505 int dir, u32 seq)
1506{
1507 struct xfrm_dump_info info;
1508 struct sk_buff *skb;
1509
Thomas Graf7deb2262007-08-22 13:57:39 -07001510 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!skb)
1512 return ERR_PTR(-ENOMEM);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 info.in_skb = in_skb;
1515 info.out_skb = skb;
1516 info.nlmsg_seq = seq;
1517 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1520 kfree_skb(skb);
1521 return NULL;
1522 }
1523
1524 return skb;
1525}
1526
Christoph Hellwig22e70052007-01-02 15:22:30 -08001527static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001528 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001530 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct xfrm_policy *xp;
1532 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001533 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001535 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001537 struct xfrm_mark m;
1538 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Thomas Graf7b67c852007-08-22 13:53:52 -07001540 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1542
Thomas Graf35a7aa02007-08-22 14:00:40 -07001543 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001544 if (err)
1545 return err;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 err = verify_policy_dir(p->dir);
1548 if (err)
1549 return err;
1550
1551 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001552 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001553 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001554 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001555 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001556
Thomas Graf35a7aa02007-08-22 14:00:40 -07001557 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001558 if (err)
1559 return err;
1560
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001561 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001562 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001563 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001564
Paul Moore03e1ad72008-04-12 19:07:52 -07001565 err = security_xfrm_policy_alloc(&ctx, uctx);
1566 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001567 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001568 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001569 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001570 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001571 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (xp == NULL)
1574 return -ENOENT;
1575
1576 if (!delete) {
1577 struct sk_buff *resp_skb;
1578
1579 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1580 if (IS_ERR(resp_skb)) {
1581 err = PTR_ERR(resp_skb);
1582 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001583 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001584 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001586 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001587 uid_t loginuid = audit_get_loginuid(current);
1588 u32 sessionid = audit_get_sessionid(current);
1589 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001590
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001591 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001592 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1593 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001594
1595 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001596 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001597
Herbert Xue7443892005-06-18 22:44:18 -07001598 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001599 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001600 c.seq = nlh->nlmsg_seq;
1601 c.pid = nlh->nlmsg_pid;
1602 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001605out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001606 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return err;
1608}
1609
Christoph Hellwig22e70052007-01-02 15:22:30 -08001610static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001611 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001613 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001614 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001615 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001616 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001617 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001619 audit_info.loginuid = audit_get_loginuid(current);
1620 audit_info.sessionid = audit_get_sessionid(current);
1621 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001622 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001623 if (err) {
1624 if (err == -ESRCH) /* empty table */
1625 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001626 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001627 }
Herbert Xubf088672005-06-18 22:44:00 -07001628 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001629 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001630 c.seq = nlh->nlmsg_seq;
1631 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001632 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001633 km_state_notify(NULL, &c);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
1636}
1637
Steffen Klassertd8647b72011-03-08 00:10:27 +00001638static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001639{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001640 size_t replay_size = x->replay_esn ?
1641 xfrm_replay_state_esn_len(x->replay_esn) :
1642 sizeof(struct xfrm_replay_state);
1643
Thomas Graf7deb2262007-08-22 13:57:39 -07001644 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001645 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001646 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001647 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001648 + nla_total_size(4) /* XFRM_AE_RTHR */
1649 + nla_total_size(4); /* XFRM_AE_ETHR */
1650}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001651
David S. Miller214e0052011-02-24 00:02:38 -05001652static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001653{
1654 struct xfrm_aevent_id *id;
1655 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001656
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001657 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1658 if (nlh == NULL)
1659 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001660
Thomas Graf7b67c852007-08-22 13:53:52 -07001661 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001662 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001663 id->sa_id.spi = x->id.spi;
1664 id->sa_id.family = x->props.family;
1665 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001666 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1667 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001668 id->flags = c->data.aevent;
1669
Steffen Klassertd8647b72011-03-08 00:10:27 +00001670 if (x->replay_esn)
1671 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1672 xfrm_replay_state_esn_len(x->replay_esn),
1673 x->replay_esn);
1674 else
1675 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1676
Thomas Grafc0144be2007-08-22 13:55:43 -07001677 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001678
Thomas Grafc0144be2007-08-22 13:55:43 -07001679 if (id->flags & XFRM_AE_RTHR)
1680 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001681
Thomas Grafc0144be2007-08-22 13:55:43 -07001682 if (id->flags & XFRM_AE_ETHR)
1683 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1684 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001685
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001686 if (xfrm_mark_put(skb, &x->mark))
1687 goto nla_put_failure;
1688
Thomas Graf98250692007-08-22 12:47:26 -07001689 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001690
Thomas Grafc0144be2007-08-22 13:55:43 -07001691nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001692 nlmsg_cancel(skb, nlh);
1693 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001694}
1695
Christoph Hellwig22e70052007-01-02 15:22:30 -08001696static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001697 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001698{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001699 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001700 struct xfrm_state *x;
1701 struct sk_buff *r_skb;
1702 int err;
1703 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001704 u32 mark;
1705 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001706 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001707 struct xfrm_usersa_id *id = &p->sa_id;
1708
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001709 mark = xfrm_mark_get(attrs, &m);
1710
1711 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001712 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001713 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001714
1715 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1716 if (r_skb == NULL) {
1717 xfrm_state_put(x);
1718 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001719 }
1720
1721 /*
1722 * XXX: is this lock really needed - none of the other
1723 * gets lock (the concern is things getting updated
1724 * while we are still reading) - jhs
1725 */
1726 spin_lock_bh(&x->lock);
1727 c.data.aevent = p->flags;
1728 c.seq = nlh->nlmsg_seq;
1729 c.pid = nlh->nlmsg_pid;
1730
1731 if (build_aevent(r_skb, x, &c) < 0)
1732 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001733 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001734 spin_unlock_bh(&x->lock);
1735 xfrm_state_put(x);
1736 return err;
1737}
1738
Christoph Hellwig22e70052007-01-02 15:22:30 -08001739static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001740 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001741{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001742 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001743 struct xfrm_state *x;
1744 struct km_event c;
1745 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001746 u32 mark = 0;
1747 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001748 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001749 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001750 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001751 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001752
Steffen Klassertd8647b72011-03-08 00:10:27 +00001753 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001754 return err;
1755
1756 /* pedantic mode - thou shalt sayeth replaceth */
1757 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1758 return err;
1759
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001760 mark = xfrm_mark_get(attrs, &m);
1761
1762 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 -08001763 if (x == NULL)
1764 return -ESRCH;
1765
1766 if (x->km.state != XFRM_STATE_VALID)
1767 goto out;
1768
1769 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001770 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001772
1773 c.event = nlh->nlmsg_type;
1774 c.seq = nlh->nlmsg_seq;
1775 c.pid = nlh->nlmsg_pid;
1776 c.data.aevent = XFRM_AE_CU;
1777 km_state_notify(x, &c);
1778 err = 0;
1779out:
1780 xfrm_state_put(x);
1781 return err;
1782}
1783
Christoph Hellwig22e70052007-01-02 15:22:30 -08001784static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001785 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001787 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001788 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001789 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001790 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001791 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001792
Thomas Graf35a7aa02007-08-22 14:00:40 -07001793 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001794 if (err)
1795 return err;
1796
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001797 audit_info.loginuid = audit_get_loginuid(current);
1798 audit_info.sessionid = audit_get_sessionid(current);
1799 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001800 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001801 if (err) {
1802 if (err == -ESRCH) /* empty table */
1803 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001804 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001805 }
1806
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001807 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001808 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001809 c.seq = nlh->nlmsg_seq;
1810 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001811 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001812 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 0;
1814}
1815
Christoph Hellwig22e70052007-01-02 15:22:30 -08001816static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001817 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001818{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001819 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001820 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001821 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001822 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001823 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001824 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001825 struct xfrm_mark m;
1826 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001827
Thomas Graf35a7aa02007-08-22 14:00:40 -07001828 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001829 if (err)
1830 return err;
1831
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001832 err = verify_policy_dir(p->dir);
1833 if (err)
1834 return err;
1835
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001836 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001837 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001838 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001839 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001840 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001841
Thomas Graf35a7aa02007-08-22 14:00:40 -07001842 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001843 if (err)
1844 return err;
1845
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001846 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001847 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001848 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001849
Paul Moore03e1ad72008-04-12 19:07:52 -07001850 err = security_xfrm_policy_alloc(&ctx, uctx);
1851 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001852 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001853 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001854 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001855 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001856 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001857 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001858 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001859 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001860
Timo Teräsea2dea92010-03-31 00:17:05 +00001861 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001862 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001863
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001864 err = 0;
1865 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001866 uid_t loginuid = audit_get_loginuid(current);
1867 u32 sessionid = audit_get_sessionid(current);
1868 u32 sid;
1869
1870 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001871 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001872 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001873
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001874 } else {
1875 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001876 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001877 }
1878 km_policy_expired(xp, p->dir, up->hard, current->pid);
1879
1880out:
1881 xfrm_pol_put(xp);
1882 return err;
1883}
1884
Christoph Hellwig22e70052007-01-02 15:22:30 -08001885static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001886 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001887{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001888 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001889 struct xfrm_state *x;
1890 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001891 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001892 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001893 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001894 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001895
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001896 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 -08001897
David S. Miller3a765aa2007-02-26 14:52:21 -08001898 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001899 if (x == NULL)
1900 return err;
1901
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001902 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001903 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001904 if (x->km.state != XFRM_STATE_VALID)
1905 goto out;
1906 km_state_expired(x, ue->hard, current->pid);
1907
Joy Latten161a09e2006-11-27 13:11:54 -06001908 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001909 uid_t loginuid = audit_get_loginuid(current);
1910 u32 sessionid = audit_get_sessionid(current);
1911 u32 sid;
1912
1913 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001914 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001915 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001916 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001917 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001918out:
1919 spin_unlock_bh(&x->lock);
1920 xfrm_state_put(x);
1921 return err;
1922}
1923
Christoph Hellwig22e70052007-01-02 15:22:30 -08001924static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001925 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001926{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001927 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001928 struct xfrm_policy *xp;
1929 struct xfrm_user_tmpl *ut;
1930 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001931 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001932 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001933
Thomas Graf7b67c852007-08-22 13:53:52 -07001934 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001935 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001936 int err = -ENOMEM;
1937
1938 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001939 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001940
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001941 xfrm_mark_get(attrs, &mark);
1942
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001943 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001944 if (err)
1945 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001946
1947 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001948 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001949 if (!xp)
1950 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001951
1952 memcpy(&x->id, &ua->id, sizeof(ua->id));
1953 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1954 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001955 xp->mark.m = x->mark.m = mark.m;
1956 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001957 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001958 /* extract the templates and for each call km_key */
1959 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1960 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1961 memcpy(&x->id, &t->id, sizeof(x->id));
1962 x->props.mode = t->mode;
1963 x->props.reqid = t->reqid;
1964 x->props.family = ut->family;
1965 t->aalgos = ua->aalgos;
1966 t->ealgos = ua->ealgos;
1967 t->calgos = ua->calgos;
1968 err = km_query(x, t, xp);
1969
1970 }
1971
1972 kfree(x);
1973 kfree(xp);
1974
1975 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001976
1977bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001978 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001979free_state:
1980 kfree(x);
1981nomem:
1982 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001983}
1984
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001985#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001986static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001987 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001988 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001989{
Thomas Graf5424f322007-08-22 14:01:33 -07001990 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001991 struct xfrm_user_migrate *um;
1992 int i, num_migrate;
1993
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001994 if (k != NULL) {
1995 struct xfrm_user_kmaddress *uk;
1996
1997 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1998 memcpy(&k->local, &uk->local, sizeof(k->local));
1999 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2000 k->family = uk->family;
2001 k->reserved = uk->reserved;
2002 }
2003
Thomas Graf5424f322007-08-22 14:01:33 -07002004 um = nla_data(rt);
2005 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002006
2007 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2008 return -EINVAL;
2009
2010 for (i = 0; i < num_migrate; i++, um++, ma++) {
2011 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2012 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2013 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2014 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2015
2016 ma->proto = um->proto;
2017 ma->mode = um->mode;
2018 ma->reqid = um->reqid;
2019
2020 ma->old_family = um->old_family;
2021 ma->new_family = um->new_family;
2022 }
2023
2024 *num = i;
2025 return 0;
2026}
2027
2028static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002029 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002030{
Thomas Graf7b67c852007-08-22 13:53:52 -07002031 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002032 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002033 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002034 u8 type;
2035 int err;
2036 int n = 0;
2037
Thomas Graf35a7aa02007-08-22 14:00:40 -07002038 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002039 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002040
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002041 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2042
Thomas Graf5424f322007-08-22 14:01:33 -07002043 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002044 if (err)
2045 return err;
2046
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002047 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002048 if (err)
2049 return err;
2050
2051 if (!n)
2052 return 0;
2053
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002054 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002055
2056 return 0;
2057}
2058#else
2059static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002060 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002061{
2062 return -ENOPROTOOPT;
2063}
2064#endif
2065
2066#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002067static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002068{
2069 struct xfrm_user_migrate um;
2070
2071 memset(&um, 0, sizeof(um));
2072 um.proto = m->proto;
2073 um.mode = m->mode;
2074 um.reqid = m->reqid;
2075 um.old_family = m->old_family;
2076 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2077 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2078 um.new_family = m->new_family;
2079 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2080 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2081
Thomas Grafc0144be2007-08-22 13:55:43 -07002082 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002083}
2084
David S. Miller183cad12011-02-24 00:28:01 -05002085static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002086{
2087 struct xfrm_user_kmaddress uk;
2088
2089 memset(&uk, 0, sizeof(uk));
2090 uk.family = k->family;
2091 uk.reserved = k->reserved;
2092 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002093 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002094
2095 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2096}
2097
2098static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002099{
2100 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002101 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2102 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2103 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002104}
2105
David S. Miller183cad12011-02-24 00:28:01 -05002106static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2107 int num_migrate, const struct xfrm_kmaddress *k,
2108 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002109{
David S. Miller183cad12011-02-24 00:28:01 -05002110 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002111 struct xfrm_userpolicy_id *pol_id;
2112 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002113 int i;
2114
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002115 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2116 if (nlh == NULL)
2117 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002118
Thomas Graf7b67c852007-08-22 13:53:52 -07002119 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002120 /* copy data from selector, dir, and type to the pol_id */
2121 memset(pol_id, 0, sizeof(*pol_id));
2122 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2123 pol_id->dir = dir;
2124
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002125 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2126 goto nlmsg_failure;
2127
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002128 if (copy_to_user_policy_type(type, skb) < 0)
2129 goto nlmsg_failure;
2130
2131 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2132 if (copy_to_user_migrate(mp, skb) < 0)
2133 goto nlmsg_failure;
2134 }
2135
Thomas Graf98250692007-08-22 12:47:26 -07002136 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002137nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002138 nlmsg_cancel(skb, nlh);
2139 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002140}
2141
David S. Miller183cad12011-02-24 00:28:01 -05002142static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2143 const struct xfrm_migrate *m, int num_migrate,
2144 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002145{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002146 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002147 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002148
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002149 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002150 if (skb == NULL)
2151 return -ENOMEM;
2152
2153 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002154 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002155 BUG();
2156
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002157 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002158}
2159#else
David S. Miller183cad12011-02-24 00:28:01 -05002160static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2161 const struct xfrm_migrate *m, int num_migrate,
2162 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002163{
2164 return -ENOPROTOOPT;
2165}
2166#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002167
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002168#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002169
2170static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002171 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002172 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2173 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2174 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2175 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2176 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2177 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002178 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002179 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002180 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002181 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002182 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002183 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002184 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002185 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2186 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002187 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002188 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002189 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2190 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191};
2192
Thomas Graf492b5582005-05-03 14:26:40 -07002193#undef XMSGSIZE
2194
Thomas Grafcf5cb792007-08-22 13:59:04 -07002195static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002196 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2197 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2198 [XFRMA_LASTUSED] = { .type = NLA_U64},
2199 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002200 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002201 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2202 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2203 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2204 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2205 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2206 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2207 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2208 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2209 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2210 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2211 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2212 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2213 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2214 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002215 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002216 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002217 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002218 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002219};
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002222 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002224 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002225} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2226 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2227 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2228 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002229 .dump = xfrm_dump_sa,
2230 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002231 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2232 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2233 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002234 .dump = xfrm_dump_policy,
2235 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002236 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002237 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002238 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002239 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2240 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002241 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002242 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2243 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002244 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2245 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002246 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002247 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002248 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249};
2250
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002251static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002253 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002254 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002256 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002260 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 type -= XFRM_MSG_BASE;
2263 link = &xfrm_dispatch[type];
2264
2265 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002266 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2267 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Thomas Graf492b5582005-05-03 14:26:40 -07002269 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2270 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002271 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002275 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277
Thomas Graf35a7aa02007-08-22 14:00:40 -07002278 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002279 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002280 if (err < 0)
2281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Thomas Graf5424f322007-08-22 14:01:33 -07002286 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002289static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002291 mutex_lock(&xfrm_cfg_mutex);
2292 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2293 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Thomas Graf7deb2262007-08-22 13:57:39 -07002296static inline size_t xfrm_expire_msgsize(void)
2297{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002298 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2299 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002300}
2301
David S. Miller214e0052011-02-24 00:02:38 -05002302static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 struct xfrm_user_expire *ue;
2305 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002307 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2308 if (nlh == NULL)
2309 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Thomas Graf7b67c852007-08-22 13:53:52 -07002311 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002313 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002315 if (xfrm_mark_put(skb, &x->mark))
2316 goto nla_put_failure;
2317
Thomas Graf98250692007-08-22 12:47:26 -07002318 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002319
2320nla_put_failure:
2321 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
David S. Miller214e0052011-02-24 00:02:38 -05002324static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002326 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct sk_buff *skb;
2328
Thomas Graf7deb2262007-08-22 13:57:39 -07002329 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (skb == NULL)
2331 return -ENOMEM;
2332
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002333 if (build_expire(skb, x, c) < 0) {
2334 kfree_skb(skb);
2335 return -EMSGSIZE;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002338 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
David S. Miller214e0052011-02-24 00:02:38 -05002341static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002342{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002343 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002344 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002345
Steffen Klassertd8647b72011-03-08 00:10:27 +00002346 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002347 if (skb == NULL)
2348 return -ENOMEM;
2349
2350 if (build_aevent(skb, x, c) < 0)
2351 BUG();
2352
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002353 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002354}
2355
David S. Miller214e0052011-02-24 00:02:38 -05002356static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002357{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002358 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002359 struct xfrm_usersa_flush *p;
2360 struct nlmsghdr *nlh;
2361 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002362 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002363
Thomas Graf7deb2262007-08-22 13:57:39 -07002364 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365 if (skb == NULL)
2366 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002367
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002368 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2369 if (nlh == NULL) {
2370 kfree_skb(skb);
2371 return -EMSGSIZE;
2372 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002373
Thomas Graf7b67c852007-08-22 13:53:52 -07002374 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002375 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002376
Thomas Graf98250692007-08-22 12:47:26 -07002377 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002379 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380}
2381
Thomas Graf7deb2262007-08-22 13:57:39 -07002382static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002383{
Thomas Graf7deb2262007-08-22 13:57:39 -07002384 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002385 if (x->aead)
2386 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002387 if (x->aalg) {
2388 l += nla_total_size(sizeof(struct xfrm_algo) +
2389 (x->aalg->alg_key_len + 7) / 8);
2390 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2391 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002393 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002395 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002397 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002398 if (x->tfcpad)
2399 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002400 if (x->replay_esn)
2401 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002402 if (x->security)
2403 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2404 x->security->ctx_len);
2405 if (x->coaddr)
2406 l += nla_total_size(sizeof(*x->coaddr));
2407
Herbert Xud26f3982007-11-13 21:47:08 -08002408 /* Must count x->lastused as it may become non-zero behind our back. */
2409 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002410
2411 return l;
2412}
2413
David S. Miller214e0052011-02-24 00:02:38 -05002414static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002416 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002418 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002419 struct nlmsghdr *nlh;
2420 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002421 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002422 int headlen;
2423
2424 headlen = sizeof(*p);
2425 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002426 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002427 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002428 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002429 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002430 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431
Thomas Graf7deb2262007-08-22 13:57:39 -07002432 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002433 if (skb == NULL)
2434 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002435
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002436 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2437 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002438 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002439
Thomas Graf7b67c852007-08-22 13:53:52 -07002440 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002441 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002442 struct nlattr *attr;
2443
Thomas Graf7b67c852007-08-22 13:53:52 -07002444 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002445 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2446 id->spi = x->id.spi;
2447 id->family = x->props.family;
2448 id->proto = x->id.proto;
2449
Thomas Grafc0144be2007-08-22 13:55:43 -07002450 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2451 if (attr == NULL)
2452 goto nla_put_failure;
2453
2454 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002455 }
2456
Herbert Xu68325d32007-10-09 13:30:57 -07002457 if (copy_to_user_state_extra(x, p, skb))
2458 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002459
Thomas Graf98250692007-08-22 12:47:26 -07002460 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002461
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002462 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463
Thomas Grafc0144be2007-08-22 13:55:43 -07002464nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002465 /* Somebody screwed up with xfrm_sa_len! */
2466 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002467 kfree_skb(skb);
2468 return -1;
2469}
2470
David S. Miller214e0052011-02-24 00:02:38 -05002471static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002472{
2473
2474 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002475 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002476 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002477 case XFRM_MSG_NEWAE:
2478 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002479 case XFRM_MSG_DELSA:
2480 case XFRM_MSG_UPDSA:
2481 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002482 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002483 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002484 return xfrm_notify_sa_flush(c);
2485 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002486 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2487 c->event);
2488 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002489 }
2490
2491 return 0;
2492
2493}
2494
Thomas Graf7deb2262007-08-22 13:57:39 -07002495static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2496 struct xfrm_policy *xp)
2497{
2498 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2499 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002500 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002501 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2502 + userpolicy_type_attrsize();
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2506 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2507 int dir)
2508{
2509 struct xfrm_user_acquire *ua;
2510 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 __u32 seq = xfrm_get_acqseq();
2512
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002513 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2514 if (nlh == NULL)
2515 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Thomas Graf7b67c852007-08-22 13:53:52 -07002517 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 memcpy(&ua->id, &x->id, sizeof(ua->id));
2519 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2520 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2521 copy_to_user_policy(xp, &ua->policy, dir);
2522 ua->aalgos = xt->aalgos;
2523 ua->ealgos = xt->ealgos;
2524 ua->calgos = xt->calgos;
2525 ua->seq = x->km.seq = seq;
2526
2527 if (copy_to_user_tmpl(xp, skb) < 0)
2528 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002529 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002530 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002531 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002532 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002533 if (xfrm_mark_put(skb, &xp->mark))
2534 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Thomas Graf98250692007-08-22 12:47:26 -07002536 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002538nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002540 nlmsg_cancel(skb, nlh);
2541 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
2544static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2545 struct xfrm_policy *xp, int dir)
2546{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002547 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Thomas Graf7deb2262007-08-22 13:57:39 -07002550 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (skb == NULL)
2552 return -ENOMEM;
2553
2554 if (build_acquire(skb, x, xt, xp, dir) < 0)
2555 BUG();
2556
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002557 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560/* User gives us xfrm_user_policy_info followed by an array of 0
2561 * or more templates.
2562 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002563static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 u8 *data, int len, int *dir)
2565{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002566 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2568 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2569 struct xfrm_policy *xp;
2570 int nr;
2571
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002572 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 case AF_INET:
2574 if (opt != IP_XFRM_POLICY) {
2575 *dir = -EOPNOTSUPP;
2576 return NULL;
2577 }
2578 break;
2579#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2580 case AF_INET6:
2581 if (opt != IPV6_XFRM_POLICY) {
2582 *dir = -EOPNOTSUPP;
2583 return NULL;
2584 }
2585 break;
2586#endif
2587 default:
2588 *dir = -EINVAL;
2589 return NULL;
2590 }
2591
2592 *dir = -EINVAL;
2593
2594 if (len < sizeof(*p) ||
2595 verify_newpolicy_info(p))
2596 return NULL;
2597
2598 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002599 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return NULL;
2601
Herbert Xua4f1bac2005-07-26 15:43:17 -07002602 if (p->dir > XFRM_POLICY_OUT)
2603 return NULL;
2604
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002605 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (xp == NULL) {
2607 *dir = -ENOBUFS;
2608 return NULL;
2609 }
2610
2611 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002612 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 copy_templates(xp, ut, nr);
2614
2615 *dir = p->dir;
2616
2617 return xp;
2618}
2619
Thomas Graf7deb2262007-08-22 13:57:39 -07002620static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2621{
2622 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2623 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2624 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002625 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002626 + userpolicy_type_attrsize();
2627}
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002630 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
2632 struct xfrm_user_polexpire *upe;
2633 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002634 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002636 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2637 if (nlh == NULL)
2638 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Thomas Graf7b67c852007-08-22 13:53:52 -07002640 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 copy_to_user_policy(xp, &upe->pol, dir);
2642 if (copy_to_user_tmpl(xp, skb) < 0)
2643 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002644 if (copy_to_user_sec_ctx(xp, skb))
2645 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002646 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002647 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002648 if (xfrm_mark_put(skb, &xp->mark))
2649 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 upe->hard = !!hard;
2651
Thomas Graf98250692007-08-22 12:47:26 -07002652 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002654nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002656 nlmsg_cancel(skb, nlh);
2657 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
David S. Miller214e0052011-02-24 00:02:38 -05002660static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002662 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Thomas Graf7deb2262007-08-22 13:57:39 -07002665 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (skb == NULL)
2667 return -ENOMEM;
2668
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002669 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 BUG();
2671
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002672 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
David S. Miller214e0052011-02-24 00:02:38 -05002675static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002677 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002679 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002680 struct nlmsghdr *nlh;
2681 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002682 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002683 int headlen;
2684
2685 headlen = sizeof(*p);
2686 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002687 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002688 headlen = sizeof(*id);
2689 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002690 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002691 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002692 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693
Thomas Graf7deb2262007-08-22 13:57:39 -07002694 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002695 if (skb == NULL)
2696 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002698 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2699 if (nlh == NULL)
2700 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002701
Thomas Graf7b67c852007-08-22 13:53:52 -07002702 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002703 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002704 struct nlattr *attr;
2705
Thomas Graf7b67c852007-08-22 13:53:52 -07002706 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002707 memset(id, 0, sizeof(*id));
2708 id->dir = dir;
2709 if (c->data.byid)
2710 id->index = xp->index;
2711 else
2712 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2713
Thomas Grafc0144be2007-08-22 13:55:43 -07002714 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2715 if (attr == NULL)
2716 goto nlmsg_failure;
2717
2718 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002719 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002720
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002721 copy_to_user_policy(xp, p, dir);
2722 if (copy_to_user_tmpl(xp, skb) < 0)
2723 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002724 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002725 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002726
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002727 if (xfrm_mark_put(skb, &xp->mark))
2728 goto nla_put_failure;
2729
Thomas Graf98250692007-08-22 12:47:26 -07002730 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002731
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002732 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002733
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002734nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002735nlmsg_failure:
2736 kfree_skb(skb);
2737 return -1;
2738}
2739
David S. Miller214e0052011-02-24 00:02:38 -05002740static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002741{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002742 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002743 struct nlmsghdr *nlh;
2744 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002745
Thomas Graf7deb2262007-08-22 13:57:39 -07002746 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002747 if (skb == NULL)
2748 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002749
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002750 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2751 if (nlh == NULL)
2752 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002753 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2754 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755
Thomas Graf98250692007-08-22 12:47:26 -07002756 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002757
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002758 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002759
2760nlmsg_failure:
2761 kfree_skb(skb);
2762 return -1;
2763}
2764
David S. Miller214e0052011-02-24 00:02:38 -05002765static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002766{
2767
2768 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002769 case XFRM_MSG_NEWPOLICY:
2770 case XFRM_MSG_UPDPOLICY:
2771 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002772 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002773 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002774 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002775 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002776 return xfrm_exp_policy_notify(xp, dir, c);
2777 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002778 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2779 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002780 }
2781
2782 return 0;
2783
2784}
2785
Thomas Graf7deb2262007-08-22 13:57:39 -07002786static inline size_t xfrm_report_msgsize(void)
2787{
2788 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2789}
2790
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002791static int build_report(struct sk_buff *skb, u8 proto,
2792 struct xfrm_selector *sel, xfrm_address_t *addr)
2793{
2794 struct xfrm_user_report *ur;
2795 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002796
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002797 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2798 if (nlh == NULL)
2799 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002800
Thomas Graf7b67c852007-08-22 13:53:52 -07002801 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002802 ur->proto = proto;
2803 memcpy(&ur->sel, sel, sizeof(ur->sel));
2804
2805 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002806 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002807
Thomas Graf98250692007-08-22 12:47:26 -07002808 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002809
Thomas Grafc0144be2007-08-22 13:55:43 -07002810nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002811 nlmsg_cancel(skb, nlh);
2812 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002813}
2814
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002815static int xfrm_send_report(struct net *net, u8 proto,
2816 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002817{
2818 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002819
Thomas Graf7deb2262007-08-22 13:57:39 -07002820 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002821 if (skb == NULL)
2822 return -ENOMEM;
2823
2824 if (build_report(skb, proto, sel, addr) < 0)
2825 BUG();
2826
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002827 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002828}
2829
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002830static inline size_t xfrm_mapping_msgsize(void)
2831{
2832 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2833}
2834
2835static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2836 xfrm_address_t *new_saddr, __be16 new_sport)
2837{
2838 struct xfrm_user_mapping *um;
2839 struct nlmsghdr *nlh;
2840
2841 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2842 if (nlh == NULL)
2843 return -EMSGSIZE;
2844
2845 um = nlmsg_data(nlh);
2846
2847 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2848 um->id.spi = x->id.spi;
2849 um->id.family = x->props.family;
2850 um->id.proto = x->id.proto;
2851 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2852 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2853 um->new_sport = new_sport;
2854 um->old_sport = x->encap->encap_sport;
2855 um->reqid = x->props.reqid;
2856
2857 return nlmsg_end(skb, nlh);
2858}
2859
2860static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2861 __be16 sport)
2862{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002863 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002864 struct sk_buff *skb;
2865
2866 if (x->id.proto != IPPROTO_ESP)
2867 return -EINVAL;
2868
2869 if (!x->encap)
2870 return -EINVAL;
2871
2872 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2873 if (skb == NULL)
2874 return -ENOMEM;
2875
2876 if (build_mapping(skb, x, ipaddr, sport) < 0)
2877 BUG();
2878
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002879 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002880}
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882static struct xfrm_mgr netlink_mgr = {
2883 .id = "netlink",
2884 .notify = xfrm_send_state_notify,
2885 .acquire = xfrm_send_acquire,
2886 .compile_policy = xfrm_compile_policy,
2887 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002888 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002889 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002890 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891};
2892
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002893static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Patrick McHardybe336902006-03-20 22:40:54 -08002895 struct sock *nlsk;
2896
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002897 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002898 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002899 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002901 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002902 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return 0;
2904}
2905
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002906static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002907{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002908 struct net *net;
2909 list_for_each_entry(net, net_exit_list, exit_list)
2910 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2911 synchronize_net();
2912 list_for_each_entry(net, net_exit_list, exit_list)
2913 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002914}
2915
2916static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002917 .init = xfrm_user_net_init,
2918 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002919};
2920
2921static int __init xfrm_user_init(void)
2922{
2923 int rv;
2924
2925 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2926
2927 rv = register_pernet_subsys(&xfrm_user_net_ops);
2928 if (rv < 0)
2929 return rv;
2930 rv = xfrm_register_km(&netlink_mgr);
2931 if (rv < 0)
2932 unregister_pernet_subsys(&xfrm_user_net_ops);
2933 return rv;
2934}
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936static void __exit xfrm_user_exit(void)
2937{
2938 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002939 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
2942module_init(xfrm_user_init);
2943module_exit(xfrm_user_exit);
2944MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002945MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002946