blob: ccc4c0c8ef000fd9635f949bc28c6c866f80ca42 [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 Klasserte2b19122011-03-28 19:47:30 +0000363static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
364 struct nlattr *rp)
365{
366 struct xfrm_replay_state_esn *up;
367
368 if (!replay_esn || !rp)
369 return 0;
370
371 up = nla_data(rp);
372
373 if (xfrm_replay_state_esn_len(replay_esn) !=
374 xfrm_replay_state_esn_len(up))
375 return -EINVAL;
376
377 return 0;
378}
379
Steffen Klassertd8647b72011-03-08 00:10:27 +0000380static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
381 struct xfrm_replay_state_esn **preplay_esn,
382 struct nlattr *rta)
383{
384 struct xfrm_replay_state_esn *p, *pp, *up;
385
386 if (!rta)
387 return 0;
388
389 up = nla_data(rta);
390
391 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
392 if (!p)
393 return -ENOMEM;
394
395 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
396 if (!pp) {
397 kfree(p);
398 return -ENOMEM;
399 }
400
401 *replay_esn = p;
402 *preplay_esn = pp;
403
404 return 0;
405}
406
Joy Latten661697f2007-04-13 16:14:35 -0700407static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800408{
Trent Jaegerdf718372005-12-13 23:12:27 -0800409 int len = 0;
410
411 if (xfrm_ctx) {
412 len += sizeof(struct xfrm_user_sec_ctx);
413 len += xfrm_ctx->ctx_len;
414 }
415 return len;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
419{
420 memcpy(&x->id, &p->id, sizeof(x->id));
421 memcpy(&x->sel, &p->sel, sizeof(x->sel));
422 memcpy(&x->lft, &p->lft, sizeof(x->lft));
423 x->props.mode = p->mode;
424 x->props.replay_window = p->replay_window;
425 x->props.reqid = p->reqid;
426 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700427 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700429
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700430 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700431 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800434/*
435 * someday when pfkey also has support, we could have the code
436 * somehow made shareable and move it to xfrm_state.c - JHS
437 *
438*/
Thomas Graf5424f322007-08-22 14:01:33 -0700439static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800440{
Thomas Graf5424f322007-08-22 14:01:33 -0700441 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +0000442 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -0700443 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
444 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
445 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800446
Steffen Klassertd8647b72011-03-08 00:10:27 +0000447 if (re) {
448 struct xfrm_replay_state_esn *replay_esn;
449 replay_esn = nla_data(re);
450 memcpy(x->replay_esn, replay_esn,
451 xfrm_replay_state_esn_len(replay_esn));
452 memcpy(x->preplay_esn, replay_esn,
453 xfrm_replay_state_esn_len(replay_esn));
454 }
455
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800456 if (rp) {
457 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700458 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800459 memcpy(&x->replay, replay, sizeof(*replay));
460 memcpy(&x->preplay, replay, sizeof(*replay));
461 }
462
463 if (lt) {
464 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700465 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800466 x->curlft.bytes = ltime->bytes;
467 x->curlft.packets = ltime->packets;
468 x->curlft.add_time = ltime->add_time;
469 x->curlft.use_time = ltime->use_time;
470 }
471
Thomas Grafcf5cb792007-08-22 13:59:04 -0700472 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700473 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800474
Thomas Grafcf5cb792007-08-22 13:59:04 -0700475 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700476 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800477}
478
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800479static struct xfrm_state *xfrm_state_construct(struct net *net,
480 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700481 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int *errp)
483{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800484 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int err = -ENOMEM;
486
487 if (!x)
488 goto error_no_put;
489
490 copy_from_user_state(x, p);
491
Herbert Xu1a6509d2008-01-28 19:37:29 -0800492 if ((err = attach_aead(&x->aead, &x->props.ealgo,
493 attrs[XFRMA_ALG_AEAD])))
494 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000495 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
496 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000498 if (!x->props.aalgo) {
499 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
500 attrs[XFRMA_ALG_AUTH])))
501 goto error;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
504 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700505 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto error;
507 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
508 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700509 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700511
512 if (attrs[XFRMA_ENCAP]) {
513 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
514 sizeof(*x->encap), GFP_KERNEL);
515 if (x->encap == NULL)
516 goto error;
517 }
518
Martin Willi35d28562010-12-08 04:37:49 +0000519 if (attrs[XFRMA_TFCPAD])
520 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
521
Thomas Graffd211502007-09-06 03:28:08 -0700522 if (attrs[XFRMA_COADDR]) {
523 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
524 sizeof(*x->coaddr), GFP_KERNEL);
525 if (x->coaddr == NULL)
526 goto error;
527 }
528
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000529 xfrm_mark_get(attrs, &x->mark);
530
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700531 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (err)
533 goto error;
534
Thomas Graffd211502007-09-06 03:28:08 -0700535 if (attrs[XFRMA_SEC_CTX] &&
536 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800537 goto error;
538
Steffen Klassertd8647b72011-03-08 00:10:27 +0000539 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
540 attrs[XFRMA_REPLAY_ESN_VAL])))
541 goto error;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800544 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800545 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800546 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800547
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000548 if ((err = xfrm_init_replay(x)))
549 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800550
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000551 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700552 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 return x;
555
556error:
557 x->km.state = XFRM_STATE_DEAD;
558 xfrm_state_put(x);
559error_no_put:
560 *errp = err;
561 return NULL;
562}
563
Christoph Hellwig22e70052007-01-02 15:22:30 -0800564static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700565 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800567 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700568 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct xfrm_state *x;
570 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700571 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800572 uid_t loginuid = audit_get_loginuid(current);
573 u32 sessionid = audit_get_sessionid(current);
574 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Thomas Graf35a7aa02007-08-22 14:00:40 -0700576 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (err)
578 return err;
579
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800580 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!x)
582 return err;
583
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700584 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
586 err = xfrm_state_add(x);
587 else
588 err = xfrm_state_update(x);
589
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800590 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400591 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (err < 0) {
594 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800595 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700599 c.seq = nlh->nlmsg_seq;
600 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700601 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700602
603 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700604out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700605 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return err;
607}
608
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800609static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
610 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700611 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700612 int *errp)
613{
614 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000615 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700616 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000617 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700618
619 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
620 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000621 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700622 } else {
623 xfrm_address_t *saddr = NULL;
624
Thomas Graf35a7aa02007-08-22 14:00:40 -0700625 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700626 if (!saddr) {
627 err = -EINVAL;
628 goto out;
629 }
630
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800631 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000632 x = xfrm_state_lookup_byaddr(net, mark,
633 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800634 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700635 }
636
637 out:
638 if (!x && errp)
639 *errp = err;
640 return x;
641}
642
Christoph Hellwig22e70052007-01-02 15:22:30 -0800643static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700644 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800646 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700648 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700649 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700650 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800651 uid_t loginuid = audit_get_loginuid(current);
652 u32 sessionid = audit_get_sessionid(current);
653 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800655 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
David S. Miller6f68dc32006-06-08 23:58:52 -0700659 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700660 goto out;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700663 err = -EPERM;
664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700667 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600668
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700669 if (err < 0)
670 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700671
672 c.seq = nlh->nlmsg_seq;
673 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700674 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700675 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700677out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800678 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400679 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700680 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
685{
686 memcpy(&p->id, &x->id, sizeof(p->id));
687 memcpy(&p->sel, &x->sel, sizeof(p->sel));
688 memcpy(&p->lft, &x->lft, sizeof(p->lft));
689 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
690 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700691 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 p->mode = x->props.mode;
693 p->replay_window = x->props.replay_window;
694 p->reqid = x->props.reqid;
695 p->family = x->props.family;
696 p->flags = x->props.flags;
697 p->seq = x->km.seq;
698}
699
700struct xfrm_dump_info {
701 struct sk_buff *in_skb;
702 struct sk_buff *out_skb;
703 u32 nlmsg_seq;
704 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705};
706
Thomas Grafc0144be2007-08-22 13:55:43 -0700707static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
708{
Thomas Grafc0144be2007-08-22 13:55:43 -0700709 struct xfrm_user_sec_ctx *uctx;
710 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700711 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700712
713 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
714 if (attr == NULL)
715 return -EMSGSIZE;
716
717 uctx = nla_data(attr);
718 uctx->exttype = XFRMA_SEC_CTX;
719 uctx->len = ctx_size;
720 uctx->ctx_doi = s->ctx_doi;
721 uctx->ctx_alg = s->ctx_alg;
722 uctx->ctx_len = s->ctx_len;
723 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
724
725 return 0;
726}
727
Martin Willi4447bb32009-11-25 00:29:52 +0000728static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
729{
730 struct xfrm_algo *algo;
731 struct nlattr *nla;
732
733 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
734 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
735 if (!nla)
736 return -EMSGSIZE;
737
738 algo = nla_data(nla);
739 strcpy(algo->alg_name, auth->alg_name);
740 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
741 algo->alg_key_len = auth->alg_key_len;
742
743 return 0;
744}
745
Herbert Xu68325d32007-10-09 13:30:57 -0700746/* Don't change this without updating xfrm_sa_len! */
747static int copy_to_user_state_extra(struct xfrm_state *x,
748 struct xfrm_usersa_info *p,
749 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 copy_to_user_state(x, p);
752
Herbert Xu050f0092007-10-09 13:31:47 -0700753 if (x->coaddr)
754 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
755
756 if (x->lastused)
757 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700758
Herbert Xu1a6509d2008-01-28 19:37:29 -0800759 if (x->aead)
760 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000761 if (x->aalg) {
762 if (copy_to_user_auth(x->aalg, skb))
763 goto nla_put_failure;
764
765 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
766 xfrm_alg_auth_len(x->aalg), x->aalg);
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800769 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700771 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700774 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Martin Willi35d28562010-12-08 04:37:49 +0000776 if (x->tfcpad)
777 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
778
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000779 if (xfrm_mark_put(skb, &x->mark))
780 goto nla_put_failure;
781
Steffen Klassertd8647b72011-03-08 00:10:27 +0000782 if (x->replay_esn)
783 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
784 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
785
Thomas Grafc0144be2007-08-22 13:55:43 -0700786 if (x->security && copy_sec_ctx(x->security, skb) < 0)
787 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700788
Herbert Xu68325d32007-10-09 13:30:57 -0700789 return 0;
790
791nla_put_failure:
792 return -EMSGSIZE;
793}
794
795static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
796{
797 struct xfrm_dump_info *sp = ptr;
798 struct sk_buff *in_skb = sp->in_skb;
799 struct sk_buff *skb = sp->out_skb;
800 struct xfrm_usersa_info *p;
801 struct nlmsghdr *nlh;
802 int err;
803
Herbert Xu68325d32007-10-09 13:30:57 -0700804 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
805 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
806 if (nlh == NULL)
807 return -EMSGSIZE;
808
809 p = nlmsg_data(nlh);
810
811 err = copy_to_user_state_extra(x, p, skb);
812 if (err)
813 goto nla_put_failure;
814
Thomas Graf98250692007-08-22 12:47:26 -0700815 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817
Thomas Grafc0144be2007-08-22 13:55:43 -0700818nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700819 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700820 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Timo Teras4c563f72008-02-28 21:31:08 -0800823static int xfrm_dump_sa_done(struct netlink_callback *cb)
824{
825 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
826 xfrm_state_walk_done(walk);
827 return 0;
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
831{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800832 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800833 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct xfrm_dump_info info;
835
Timo Teras4c563f72008-02-28 21:31:08 -0800836 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
837 sizeof(cb->args) - sizeof(cb->args[0]));
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 info.in_skb = cb->skb;
840 info.out_skb = skb;
841 info.nlmsg_seq = cb->nlh->nlmsg_seq;
842 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800843
844 if (!cb->args[0]) {
845 cb->args[0] = 1;
846 xfrm_state_walk_init(walk, 0);
847 }
848
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800849 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 return skb->len;
852}
853
854static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
855 struct xfrm_state *x, u32 seq)
856{
857 struct xfrm_dump_info info;
858 struct sk_buff *skb;
859
Thomas Graf7deb2262007-08-22 13:57:39 -0700860 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!skb)
862 return ERR_PTR(-ENOMEM);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 info.in_skb = in_skb;
865 info.out_skb = skb;
866 info.nlmsg_seq = seq;
867 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (dump_one_state(x, 0, &info)) {
870 kfree_skb(skb);
871 return NULL;
872 }
873
874 return skb;
875}
876
Thomas Graf7deb2262007-08-22 13:57:39 -0700877static inline size_t xfrm_spdinfo_msgsize(void)
878{
879 return NLMSG_ALIGN(4)
880 + nla_total_size(sizeof(struct xfrmu_spdinfo))
881 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
882}
883
Alexey Dobriyane0710412010-01-23 13:37:10 +0000884static int build_spdinfo(struct sk_buff *skb, struct net *net,
885 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700886{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700887 struct xfrmk_spdinfo si;
888 struct xfrmu_spdinfo spc;
889 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700890 struct nlmsghdr *nlh;
891 u32 *f;
892
893 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
894 if (nlh == NULL) /* shouldnt really happen ... */
895 return -EMSGSIZE;
896
897 f = nlmsg_data(nlh);
898 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000899 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700900 spc.incnt = si.incnt;
901 spc.outcnt = si.outcnt;
902 spc.fwdcnt = si.fwdcnt;
903 spc.inscnt = si.inscnt;
904 spc.outscnt = si.outscnt;
905 spc.fwdscnt = si.fwdscnt;
906 sph.spdhcnt = si.spdhcnt;
907 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700908
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700909 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
910 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700911
912 return nlmsg_end(skb, nlh);
913
914nla_put_failure:
915 nlmsg_cancel(skb, nlh);
916 return -EMSGSIZE;
917}
918
919static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700920 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700921{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800922 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700923 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700924 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700925 u32 spid = NETLINK_CB(skb).pid;
926 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700927
Thomas Graf7deb2262007-08-22 13:57:39 -0700928 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700929 if (r_skb == NULL)
930 return -ENOMEM;
931
Alexey Dobriyane0710412010-01-23 13:37:10 +0000932 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700933 BUG();
934
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800935 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700936}
937
Thomas Graf7deb2262007-08-22 13:57:39 -0700938static inline size_t xfrm_sadinfo_msgsize(void)
939{
940 return NLMSG_ALIGN(4)
941 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
942 + nla_total_size(4); /* XFRMA_SAD_CNT */
943}
944
Alexey Dobriyane0710412010-01-23 13:37:10 +0000945static int build_sadinfo(struct sk_buff *skb, struct net *net,
946 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700947{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700948 struct xfrmk_sadinfo si;
949 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700950 struct nlmsghdr *nlh;
951 u32 *f;
952
953 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
954 if (nlh == NULL) /* shouldnt really happen ... */
955 return -EMSGSIZE;
956
957 f = nlmsg_data(nlh);
958 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000959 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700960
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700961 sh.sadhmcnt = si.sadhmcnt;
962 sh.sadhcnt = si.sadhcnt;
963
964 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
965 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700966
967 return nlmsg_end(skb, nlh);
968
969nla_put_failure:
970 nlmsg_cancel(skb, nlh);
971 return -EMSGSIZE;
972}
973
974static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700975 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700976{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800977 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700978 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700979 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700980 u32 spid = NETLINK_CB(skb).pid;
981 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700982
Thomas Graf7deb2262007-08-22 13:57:39 -0700983 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700984 if (r_skb == NULL)
985 return -ENOMEM;
986
Alexey Dobriyane0710412010-01-23 13:37:10 +0000987 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700988 BUG();
989
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800990 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700991}
992
Christoph Hellwig22e70052007-01-02 15:22:30 -0800993static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700994 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800996 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700997 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 struct xfrm_state *x;
999 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001000 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001002 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (x == NULL)
1004 goto out_noput;
1005
1006 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1007 if (IS_ERR(resp_skb)) {
1008 err = PTR_ERR(resp_skb);
1009 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001010 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 xfrm_state_put(x);
1013out_noput:
1014 return err;
1015}
1016
1017static int verify_userspi_info(struct xfrm_userspi_info *p)
1018{
1019 switch (p->info.id.proto) {
1020 case IPPROTO_AH:
1021 case IPPROTO_ESP:
1022 break;
1023
1024 case IPPROTO_COMP:
1025 /* IPCOMP spi is 16-bits. */
1026 if (p->max >= 0x10000)
1027 return -EINVAL;
1028 break;
1029
1030 default:
1031 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (p->min > p->max)
1035 return -EINVAL;
1036
1037 return 0;
1038}
1039
Christoph Hellwig22e70052007-01-02 15:22:30 -08001040static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001041 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001043 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct xfrm_state *x;
1045 struct xfrm_userspi_info *p;
1046 struct sk_buff *resp_skb;
1047 xfrm_address_t *daddr;
1048 int family;
1049 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001050 u32 mark;
1051 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Thomas Graf7b67c852007-08-22 13:53:52 -07001053 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 err = verify_userspi_info(p);
1055 if (err)
1056 goto out_noput;
1057
1058 family = p->info.family;
1059 daddr = &p->info.id.daddr;
1060
1061 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001062
1063 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001065 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1067 xfrm_state_put(x);
1068 x = NULL;
1069 }
1070 }
1071
1072 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001073 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 p->info.id.proto, daddr,
1075 &p->info.saddr, 1,
1076 family);
1077 err = -ENOENT;
1078 if (x == NULL)
1079 goto out_noput;
1080
Herbert Xu658b2192007-10-09 13:29:52 -07001081 err = xfrm_alloc_spi(x, p->min, p->max);
1082 if (err)
1083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Herbert Xu658b2192007-10-09 13:29:52 -07001085 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (IS_ERR(resp_skb)) {
1087 err = PTR_ERR(resp_skb);
1088 goto out;
1089 }
1090
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001091 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093out:
1094 xfrm_state_put(x);
1095out_noput:
1096 return err;
1097}
1098
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001099static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 switch (dir) {
1102 case XFRM_POLICY_IN:
1103 case XFRM_POLICY_OUT:
1104 case XFRM_POLICY_FWD:
1105 break;
1106
1107 default:
1108 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 return 0;
1112}
1113
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001114static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001115{
1116 switch (type) {
1117 case XFRM_POLICY_TYPE_MAIN:
1118#ifdef CONFIG_XFRM_SUB_POLICY
1119 case XFRM_POLICY_TYPE_SUB:
1120#endif
1121 break;
1122
1123 default:
1124 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001125 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001126
1127 return 0;
1128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1131{
1132 switch (p->share) {
1133 case XFRM_SHARE_ANY:
1134 case XFRM_SHARE_SESSION:
1135 case XFRM_SHARE_USER:
1136 case XFRM_SHARE_UNIQUE:
1137 break;
1138
1139 default:
1140 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 switch (p->action) {
1144 case XFRM_POLICY_ALLOW:
1145 case XFRM_POLICY_BLOCK:
1146 break;
1147
1148 default:
1149 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 switch (p->sel.family) {
1153 case AF_INET:
1154 break;
1155
1156 case AF_INET6:
1157#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1158 break;
1159#else
1160 return -EAFNOSUPPORT;
1161#endif
1162
1163 default:
1164 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 return verify_policy_dir(p->dir);
1168}
1169
Thomas Graf5424f322007-08-22 14:01:33 -07001170static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001171{
Thomas Graf5424f322007-08-22 14:01:33 -07001172 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001173 struct xfrm_user_sec_ctx *uctx;
1174
1175 if (!rt)
1176 return 0;
1177
Thomas Graf5424f322007-08-22 14:01:33 -07001178 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001179 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1183 int nr)
1184{
1185 int i;
1186
1187 xp->xfrm_nr = nr;
1188 for (i = 0; i < nr; i++, ut++) {
1189 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1190
1191 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1192 memcpy(&t->saddr, &ut->saddr,
1193 sizeof(xfrm_address_t));
1194 t->reqid = ut->reqid;
1195 t->mode = ut->mode;
1196 t->share = ut->share;
1197 t->optional = ut->optional;
1198 t->aalgos = ut->aalgos;
1199 t->ealgos = ut->ealgos;
1200 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001201 /* If all masks are ~0, then we allow all algorithms. */
1202 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001203 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205}
1206
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001207static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1208{
1209 int i;
1210
1211 if (nr > XFRM_MAX_DEPTH)
1212 return -EINVAL;
1213
1214 for (i = 0; i < nr; i++) {
1215 /* We never validated the ut->family value, so many
1216 * applications simply leave it at zero. The check was
1217 * never made and ut->family was ignored because all
1218 * templates could be assumed to have the same family as
1219 * the policy itself. Now that we will have ipv4-in-ipv6
1220 * and ipv6-in-ipv4 tunnels, this is no longer true.
1221 */
1222 if (!ut[i].family)
1223 ut[i].family = family;
1224
1225 switch (ut[i].family) {
1226 case AF_INET:
1227 break;
1228#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1229 case AF_INET6:
1230 break;
1231#endif
1232 default:
1233 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001234 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001235 }
1236
1237 return 0;
1238}
1239
Thomas Graf5424f322007-08-22 14:01:33 -07001240static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Thomas Graf5424f322007-08-22 14:01:33 -07001242 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (!rt) {
1245 pol->xfrm_nr = 0;
1246 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001247 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1248 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001249 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001251 err = validate_tmpl(nr, utmpl, pol->family);
1252 if (err)
1253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Thomas Graf5424f322007-08-22 14:01:33 -07001255 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 return 0;
1258}
1259
Thomas Graf5424f322007-08-22 14:01:33 -07001260static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001261{
Thomas Graf5424f322007-08-22 14:01:33 -07001262 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001263 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001264 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001265 int err;
1266
1267 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001268 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001269 type = upt->type;
1270 }
1271
1272 err = verify_policy_type(type);
1273 if (err)
1274 return err;
1275
1276 *tp = type;
1277 return 0;
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1281{
1282 xp->priority = p->priority;
1283 xp->index = p->index;
1284 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1285 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1286 xp->action = p->action;
1287 xp->flags = p->flags;
1288 xp->family = p->sel.family;
1289 /* XXX xp->share = p->share; */
1290}
1291
1292static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1293{
1294 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1295 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1296 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1297 p->priority = xp->priority;
1298 p->index = xp->index;
1299 p->sel.family = xp->family;
1300 p->dir = dir;
1301 p->action = xp->action;
1302 p->flags = xp->flags;
1303 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1304}
1305
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001306static 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 -07001307{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001308 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int err;
1310
1311 if (!xp) {
1312 *errp = -ENOMEM;
1313 return NULL;
1314 }
1315
1316 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001317
Thomas Graf35a7aa02007-08-22 14:00:40 -07001318 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001319 if (err)
1320 goto error;
1321
Thomas Graf35a7aa02007-08-22 14:00:40 -07001322 if (!(err = copy_from_user_tmpl(xp, attrs)))
1323 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001324 if (err)
1325 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001327 xfrm_mark_get(attrs, &xp->mark);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001330 error:
1331 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001332 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001333 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001334 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Christoph Hellwig22e70052007-01-02 15:22:30 -08001337static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001338 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001340 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001341 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001343 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int err;
1345 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001346 uid_t loginuid = audit_get_loginuid(current);
1347 u32 sessionid = audit_get_sessionid(current);
1348 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 err = verify_newpolicy_info(p);
1351 if (err)
1352 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001353 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001354 if (err)
1355 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001357 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!xp)
1359 return err;
1360
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001361 /* shouldnt excl be based on nlh flags??
1362 * Aha! this is anti-netlink really i.e more pfkey derived
1363 * in netlink excl is a flag and you wouldnt need
1364 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1366 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001367 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001368 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001371 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 kfree(xp);
1373 return err;
1374 }
1375
Herbert Xuf60f6b82005-06-18 22:44:37 -07001376 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001377 c.seq = nlh->nlmsg_seq;
1378 c.pid = nlh->nlmsg_pid;
1379 km_policy_notify(xp, p->dir, &c);
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 xfrm_pol_put(xp);
1382
1383 return 0;
1384}
1385
1386static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1387{
1388 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1389 int i;
1390
1391 if (xp->xfrm_nr == 0)
1392 return 0;
1393
1394 for (i = 0; i < xp->xfrm_nr; i++) {
1395 struct xfrm_user_tmpl *up = &vec[i];
1396 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1397
1398 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001399 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1401 up->reqid = kp->reqid;
1402 up->mode = kp->mode;
1403 up->share = kp->share;
1404 up->optional = kp->optional;
1405 up->aalgos = kp->aalgos;
1406 up->ealgos = kp->ealgos;
1407 up->calgos = kp->calgos;
1408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Thomas Grafc0144be2007-08-22 13:55:43 -07001410 return nla_put(skb, XFRMA_TMPL,
1411 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001412}
1413
Serge Hallyn0d681622006-07-24 23:30:44 -07001414static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1415{
1416 if (x->security) {
1417 return copy_sec_ctx(x->security, skb);
1418 }
1419 return 0;
1420}
1421
1422static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1423{
1424 if (xp->security) {
1425 return copy_sec_ctx(xp->security, skb);
1426 }
1427 return 0;
1428}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001429static inline size_t userpolicy_type_attrsize(void)
1430{
1431#ifdef CONFIG_XFRM_SUB_POLICY
1432 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1433#else
1434 return 0;
1435#endif
1436}
Serge Hallyn0d681622006-07-24 23:30:44 -07001437
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001438#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001439static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001440{
Thomas Grafc0144be2007-08-22 13:55:43 -07001441 struct xfrm_userpolicy_type upt = {
1442 .type = type,
1443 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001444
Thomas Grafc0144be2007-08-22 13:55:43 -07001445 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001446}
1447
1448#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001449static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001450{
1451 return 0;
1452}
1453#endif
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1456{
1457 struct xfrm_dump_info *sp = ptr;
1458 struct xfrm_userpolicy_info *p;
1459 struct sk_buff *in_skb = sp->in_skb;
1460 struct sk_buff *skb = sp->out_skb;
1461 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001463 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1464 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1465 if (nlh == NULL)
1466 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Thomas Graf7b67c852007-08-22 13:53:52 -07001468 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 copy_to_user_policy(xp, p, dir);
1470 if (copy_to_user_tmpl(xp, skb) < 0)
1471 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001472 if (copy_to_user_sec_ctx(xp, skb))
1473 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001474 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001475 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001476 if (xfrm_mark_put(skb, &xp->mark))
1477 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Thomas Graf98250692007-08-22 12:47:26 -07001479 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return 0;
1481
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001482nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001484 nlmsg_cancel(skb, nlh);
1485 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Timo Teras4c563f72008-02-28 21:31:08 -08001488static int xfrm_dump_policy_done(struct netlink_callback *cb)
1489{
1490 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1491
1492 xfrm_policy_walk_done(walk);
1493 return 0;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1497{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001498 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001499 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct xfrm_dump_info info;
1501
Timo Teras4c563f72008-02-28 21:31:08 -08001502 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1503 sizeof(cb->args) - sizeof(cb->args[0]));
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 info.in_skb = cb->skb;
1506 info.out_skb = skb;
1507 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1508 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001509
1510 if (!cb->args[0]) {
1511 cb->args[0] = 1;
1512 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1513 }
1514
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001515 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 return skb->len;
1518}
1519
1520static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1521 struct xfrm_policy *xp,
1522 int dir, u32 seq)
1523{
1524 struct xfrm_dump_info info;
1525 struct sk_buff *skb;
1526
Thomas Graf7deb2262007-08-22 13:57:39 -07001527 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (!skb)
1529 return ERR_PTR(-ENOMEM);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 info.in_skb = in_skb;
1532 info.out_skb = skb;
1533 info.nlmsg_seq = seq;
1534 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1537 kfree_skb(skb);
1538 return NULL;
1539 }
1540
1541 return skb;
1542}
1543
Christoph Hellwig22e70052007-01-02 15:22:30 -08001544static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001545 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001547 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 struct xfrm_policy *xp;
1549 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001550 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001552 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001554 struct xfrm_mark m;
1555 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Thomas Graf7b67c852007-08-22 13:53:52 -07001557 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1559
Thomas Graf35a7aa02007-08-22 14:00:40 -07001560 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001561 if (err)
1562 return err;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 err = verify_policy_dir(p->dir);
1565 if (err)
1566 return err;
1567
1568 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001569 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001570 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001571 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001572 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001573
Thomas Graf35a7aa02007-08-22 14:00:40 -07001574 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001575 if (err)
1576 return err;
1577
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001578 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001579 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001580 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001581
Paul Moore03e1ad72008-04-12 19:07:52 -07001582 err = security_xfrm_policy_alloc(&ctx, uctx);
1583 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001584 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001585 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001586 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001587 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001588 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (xp == NULL)
1591 return -ENOENT;
1592
1593 if (!delete) {
1594 struct sk_buff *resp_skb;
1595
1596 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1597 if (IS_ERR(resp_skb)) {
1598 err = PTR_ERR(resp_skb);
1599 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001600 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001601 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001603 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001604 uid_t loginuid = audit_get_loginuid(current);
1605 u32 sessionid = audit_get_sessionid(current);
1606 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001607
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001608 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001609 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1610 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001611
1612 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001613 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001614
Herbert Xue7443892005-06-18 22:44:18 -07001615 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001616 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001617 c.seq = nlh->nlmsg_seq;
1618 c.pid = nlh->nlmsg_pid;
1619 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001622out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001623 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return err;
1625}
1626
Christoph Hellwig22e70052007-01-02 15:22:30 -08001627static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001628 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001630 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001631 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001632 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001633 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001634 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001636 audit_info.loginuid = audit_get_loginuid(current);
1637 audit_info.sessionid = audit_get_sessionid(current);
1638 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001639 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001640 if (err) {
1641 if (err == -ESRCH) /* empty table */
1642 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001643 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001644 }
Herbert Xubf088672005-06-18 22:44:00 -07001645 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001646 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001647 c.seq = nlh->nlmsg_seq;
1648 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001649 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001650 km_state_notify(NULL, &c);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 return 0;
1653}
1654
Steffen Klassertd8647b72011-03-08 00:10:27 +00001655static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001656{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001657 size_t replay_size = x->replay_esn ?
1658 xfrm_replay_state_esn_len(x->replay_esn) :
1659 sizeof(struct xfrm_replay_state);
1660
Thomas Graf7deb2262007-08-22 13:57:39 -07001661 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001662 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001663 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001664 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001665 + nla_total_size(4) /* XFRM_AE_RTHR */
1666 + nla_total_size(4); /* XFRM_AE_ETHR */
1667}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001668
David S. Miller214e0052011-02-24 00:02:38 -05001669static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001670{
1671 struct xfrm_aevent_id *id;
1672 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001673
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001674 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1675 if (nlh == NULL)
1676 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001677
Thomas Graf7b67c852007-08-22 13:53:52 -07001678 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001679 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001680 id->sa_id.spi = x->id.spi;
1681 id->sa_id.family = x->props.family;
1682 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001683 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1684 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001685 id->flags = c->data.aevent;
1686
Steffen Klassertd8647b72011-03-08 00:10:27 +00001687 if (x->replay_esn)
1688 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1689 xfrm_replay_state_esn_len(x->replay_esn),
1690 x->replay_esn);
1691 else
1692 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1693
Thomas Grafc0144be2007-08-22 13:55:43 -07001694 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001695
Thomas Grafc0144be2007-08-22 13:55:43 -07001696 if (id->flags & XFRM_AE_RTHR)
1697 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001698
Thomas Grafc0144be2007-08-22 13:55:43 -07001699 if (id->flags & XFRM_AE_ETHR)
1700 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1701 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001702
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001703 if (xfrm_mark_put(skb, &x->mark))
1704 goto nla_put_failure;
1705
Thomas Graf98250692007-08-22 12:47:26 -07001706 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001707
Thomas Grafc0144be2007-08-22 13:55:43 -07001708nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001709 nlmsg_cancel(skb, nlh);
1710 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001711}
1712
Christoph Hellwig22e70052007-01-02 15:22:30 -08001713static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001714 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001715{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001716 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001717 struct xfrm_state *x;
1718 struct sk_buff *r_skb;
1719 int err;
1720 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001721 u32 mark;
1722 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001723 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001724 struct xfrm_usersa_id *id = &p->sa_id;
1725
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001726 mark = xfrm_mark_get(attrs, &m);
1727
1728 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001729 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001730 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001731
1732 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1733 if (r_skb == NULL) {
1734 xfrm_state_put(x);
1735 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001736 }
1737
1738 /*
1739 * XXX: is this lock really needed - none of the other
1740 * gets lock (the concern is things getting updated
1741 * while we are still reading) - jhs
1742 */
1743 spin_lock_bh(&x->lock);
1744 c.data.aevent = p->flags;
1745 c.seq = nlh->nlmsg_seq;
1746 c.pid = nlh->nlmsg_pid;
1747
1748 if (build_aevent(r_skb, x, &c) < 0)
1749 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001750 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001751 spin_unlock_bh(&x->lock);
1752 xfrm_state_put(x);
1753 return err;
1754}
1755
Christoph Hellwig22e70052007-01-02 15:22:30 -08001756static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001757 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001758{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001759 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001760 struct xfrm_state *x;
1761 struct km_event c;
1762 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001763 u32 mark = 0;
1764 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001765 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001766 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001767 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001768 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001769
Steffen Klassertd8647b72011-03-08 00:10:27 +00001770 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001771 return err;
1772
1773 /* pedantic mode - thou shalt sayeth replaceth */
1774 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1775 return err;
1776
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001777 mark = xfrm_mark_get(attrs, &m);
1778
1779 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 -08001780 if (x == NULL)
1781 return -ESRCH;
1782
1783 if (x->km.state != XFRM_STATE_VALID)
1784 goto out;
1785
Steffen Klasserte2b19122011-03-28 19:47:30 +00001786 err = xfrm_replay_verify_len(x->replay_esn, rp);
1787 if (err)
1788 goto out;
1789
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001790 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001791 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001792 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001793
1794 c.event = nlh->nlmsg_type;
1795 c.seq = nlh->nlmsg_seq;
1796 c.pid = nlh->nlmsg_pid;
1797 c.data.aevent = XFRM_AE_CU;
1798 km_state_notify(x, &c);
1799 err = 0;
1800out:
1801 xfrm_state_put(x);
1802 return err;
1803}
1804
Christoph Hellwig22e70052007-01-02 15:22:30 -08001805static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001806 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001808 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001809 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001810 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001811 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001812 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001813
Thomas Graf35a7aa02007-08-22 14:00:40 -07001814 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001815 if (err)
1816 return err;
1817
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001818 audit_info.loginuid = audit_get_loginuid(current);
1819 audit_info.sessionid = audit_get_sessionid(current);
1820 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001821 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001822 if (err) {
1823 if (err == -ESRCH) /* empty table */
1824 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001825 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001826 }
1827
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001828 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001829 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001830 c.seq = nlh->nlmsg_seq;
1831 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001832 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001833 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 0;
1835}
1836
Christoph Hellwig22e70052007-01-02 15:22:30 -08001837static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001838 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001839{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001840 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001841 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001842 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001843 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001844 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001845 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001846 struct xfrm_mark m;
1847 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001848
Thomas Graf35a7aa02007-08-22 14:00:40 -07001849 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001850 if (err)
1851 return err;
1852
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001853 err = verify_policy_dir(p->dir);
1854 if (err)
1855 return err;
1856
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001857 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001858 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001859 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001860 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001861 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001862
Thomas Graf35a7aa02007-08-22 14:00:40 -07001863 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001864 if (err)
1865 return err;
1866
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001867 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001868 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001869 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001870
Paul Moore03e1ad72008-04-12 19:07:52 -07001871 err = security_xfrm_policy_alloc(&ctx, uctx);
1872 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001873 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001874 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001875 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001876 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001877 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001878 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001879 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001880 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001881
Timo Teräsea2dea92010-03-31 00:17:05 +00001882 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001883 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001884
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001885 err = 0;
1886 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001887 uid_t loginuid = audit_get_loginuid(current);
1888 u32 sessionid = audit_get_sessionid(current);
1889 u32 sid;
1890
1891 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001892 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001893 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001894
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001895 } else {
1896 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001897 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001898 }
1899 km_policy_expired(xp, p->dir, up->hard, current->pid);
1900
1901out:
1902 xfrm_pol_put(xp);
1903 return err;
1904}
1905
Christoph Hellwig22e70052007-01-02 15:22:30 -08001906static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001907 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001908{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001909 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001910 struct xfrm_state *x;
1911 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001912 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001913 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001914 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001915 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001916
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001917 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 -08001918
David S. Miller3a765aa2007-02-26 14:52:21 -08001919 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001920 if (x == NULL)
1921 return err;
1922
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001923 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001924 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001925 if (x->km.state != XFRM_STATE_VALID)
1926 goto out;
1927 km_state_expired(x, ue->hard, current->pid);
1928
Joy Latten161a09e2006-11-27 13:11:54 -06001929 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001930 uid_t loginuid = audit_get_loginuid(current);
1931 u32 sessionid = audit_get_sessionid(current);
1932 u32 sid;
1933
1934 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001935 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001936 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001937 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001938 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001939out:
1940 spin_unlock_bh(&x->lock);
1941 xfrm_state_put(x);
1942 return err;
1943}
1944
Christoph Hellwig22e70052007-01-02 15:22:30 -08001945static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001946 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001947{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001948 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001949 struct xfrm_policy *xp;
1950 struct xfrm_user_tmpl *ut;
1951 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001952 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001953 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001954
Thomas Graf7b67c852007-08-22 13:53:52 -07001955 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001956 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001957 int err = -ENOMEM;
1958
1959 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001960 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001961
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001962 xfrm_mark_get(attrs, &mark);
1963
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001964 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001965 if (err)
1966 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001967
1968 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001969 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001970 if (!xp)
1971 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001972
1973 memcpy(&x->id, &ua->id, sizeof(ua->id));
1974 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1975 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001976 xp->mark.m = x->mark.m = mark.m;
1977 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001978 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001979 /* extract the templates and for each call km_key */
1980 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1981 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1982 memcpy(&x->id, &t->id, sizeof(x->id));
1983 x->props.mode = t->mode;
1984 x->props.reqid = t->reqid;
1985 x->props.family = ut->family;
1986 t->aalgos = ua->aalgos;
1987 t->ealgos = ua->ealgos;
1988 t->calgos = ua->calgos;
1989 err = km_query(x, t, xp);
1990
1991 }
1992
1993 kfree(x);
1994 kfree(xp);
1995
1996 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001997
1998bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001999 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002000free_state:
2001 kfree(x);
2002nomem:
2003 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002004}
2005
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002006#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002007static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002008 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002009 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002010{
Thomas Graf5424f322007-08-22 14:01:33 -07002011 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002012 struct xfrm_user_migrate *um;
2013 int i, num_migrate;
2014
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002015 if (k != NULL) {
2016 struct xfrm_user_kmaddress *uk;
2017
2018 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2019 memcpy(&k->local, &uk->local, sizeof(k->local));
2020 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2021 k->family = uk->family;
2022 k->reserved = uk->reserved;
2023 }
2024
Thomas Graf5424f322007-08-22 14:01:33 -07002025 um = nla_data(rt);
2026 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002027
2028 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2029 return -EINVAL;
2030
2031 for (i = 0; i < num_migrate; i++, um++, ma++) {
2032 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2033 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2034 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2035 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2036
2037 ma->proto = um->proto;
2038 ma->mode = um->mode;
2039 ma->reqid = um->reqid;
2040
2041 ma->old_family = um->old_family;
2042 ma->new_family = um->new_family;
2043 }
2044
2045 *num = i;
2046 return 0;
2047}
2048
2049static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002050 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002051{
Thomas Graf7b67c852007-08-22 13:53:52 -07002052 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002053 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002054 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002055 u8 type;
2056 int err;
2057 int n = 0;
2058
Thomas Graf35a7aa02007-08-22 14:00:40 -07002059 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002060 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002061
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002062 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2063
Thomas Graf5424f322007-08-22 14:01:33 -07002064 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002065 if (err)
2066 return err;
2067
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002068 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069 if (err)
2070 return err;
2071
2072 if (!n)
2073 return 0;
2074
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002075 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002076
2077 return 0;
2078}
2079#else
2080static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002081 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002082{
2083 return -ENOPROTOOPT;
2084}
2085#endif
2086
2087#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002088static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002089{
2090 struct xfrm_user_migrate um;
2091
2092 memset(&um, 0, sizeof(um));
2093 um.proto = m->proto;
2094 um.mode = m->mode;
2095 um.reqid = m->reqid;
2096 um.old_family = m->old_family;
2097 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2098 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2099 um.new_family = m->new_family;
2100 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2101 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2102
Thomas Grafc0144be2007-08-22 13:55:43 -07002103 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002104}
2105
David S. Miller183cad12011-02-24 00:28:01 -05002106static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002107{
2108 struct xfrm_user_kmaddress uk;
2109
2110 memset(&uk, 0, sizeof(uk));
2111 uk.family = k->family;
2112 uk.reserved = k->reserved;
2113 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002114 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002115
2116 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2117}
2118
2119static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002120{
2121 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002122 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2123 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2124 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002125}
2126
David S. Miller183cad12011-02-24 00:28:01 -05002127static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2128 int num_migrate, const struct xfrm_kmaddress *k,
2129 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002130{
David S. Miller183cad12011-02-24 00:28:01 -05002131 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002132 struct xfrm_userpolicy_id *pol_id;
2133 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002134 int i;
2135
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002136 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2137 if (nlh == NULL)
2138 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002139
Thomas Graf7b67c852007-08-22 13:53:52 -07002140 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002141 /* copy data from selector, dir, and type to the pol_id */
2142 memset(pol_id, 0, sizeof(*pol_id));
2143 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2144 pol_id->dir = dir;
2145
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002146 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2147 goto nlmsg_failure;
2148
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002149 if (copy_to_user_policy_type(type, skb) < 0)
2150 goto nlmsg_failure;
2151
2152 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2153 if (copy_to_user_migrate(mp, skb) < 0)
2154 goto nlmsg_failure;
2155 }
2156
Thomas Graf98250692007-08-22 12:47:26 -07002157 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002158nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002159 nlmsg_cancel(skb, nlh);
2160 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002161}
2162
David S. Miller183cad12011-02-24 00:28:01 -05002163static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2164 const struct xfrm_migrate *m, int num_migrate,
2165 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002166{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002167 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002168 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002169
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002170 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002171 if (skb == NULL)
2172 return -ENOMEM;
2173
2174 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002175 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002176 BUG();
2177
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002178 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179}
2180#else
David S. Miller183cad12011-02-24 00:28:01 -05002181static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2182 const struct xfrm_migrate *m, int num_migrate,
2183 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002184{
2185 return -ENOPROTOOPT;
2186}
2187#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002188
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002189#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002190
2191static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002192 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002193 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2194 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2195 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2196 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2197 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2198 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002199 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002200 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002201 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002202 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002203 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002204 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002205 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002206 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2207 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002208 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002209 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002210 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2211 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212};
2213
Thomas Graf492b5582005-05-03 14:26:40 -07002214#undef XMSGSIZE
2215
Thomas Grafcf5cb792007-08-22 13:59:04 -07002216static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002217 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2218 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2219 [XFRMA_LASTUSED] = { .type = NLA_U64},
2220 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002221 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002222 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2223 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2224 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2225 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2226 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2227 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2228 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2229 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2230 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2231 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2232 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2233 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2234 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2235 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002236 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002237 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002238 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002239 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002240};
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002243 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002245 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002246} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2247 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2248 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2249 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002250 .dump = xfrm_dump_sa,
2251 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002252 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2253 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2254 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002255 .dump = xfrm_dump_policy,
2256 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002257 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002258 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002259 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002260 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2261 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002262 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002263 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2264 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002265 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2266 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002267 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002268 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002269 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270};
2271
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002272static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002274 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002275 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002277 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 type -= XFRM_MSG_BASE;
2284 link = &xfrm_dispatch[type];
2285
2286 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002287 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2288 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Thomas Graf492b5582005-05-03 14:26:40 -07002290 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2291 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002292 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002296 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
2298
Thomas Graf35a7aa02007-08-22 14:00:40 -07002299 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002300 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002301 if (err < 0)
2302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002305 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Thomas Graf5424f322007-08-22 14:01:33 -07002307 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002310static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002312 mutex_lock(&xfrm_cfg_mutex);
2313 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2314 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Thomas Graf7deb2262007-08-22 13:57:39 -07002317static inline size_t xfrm_expire_msgsize(void)
2318{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002319 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2320 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002321}
2322
David S. Miller214e0052011-02-24 00:02:38 -05002323static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 struct xfrm_user_expire *ue;
2326 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002328 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2329 if (nlh == NULL)
2330 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Thomas Graf7b67c852007-08-22 13:53:52 -07002332 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002334 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002336 if (xfrm_mark_put(skb, &x->mark))
2337 goto nla_put_failure;
2338
Thomas Graf98250692007-08-22 12:47:26 -07002339 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002340
2341nla_put_failure:
2342 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
David S. Miller214e0052011-02-24 00:02:38 -05002345static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002347 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 struct sk_buff *skb;
2349
Thomas Graf7deb2262007-08-22 13:57:39 -07002350 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (skb == NULL)
2352 return -ENOMEM;
2353
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002354 if (build_expire(skb, x, c) < 0) {
2355 kfree_skb(skb);
2356 return -EMSGSIZE;
2357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002359 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
David S. Miller214e0052011-02-24 00:02:38 -05002362static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002363{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002364 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002365 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002366
Steffen Klassertd8647b72011-03-08 00:10:27 +00002367 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002368 if (skb == NULL)
2369 return -ENOMEM;
2370
2371 if (build_aevent(skb, x, c) < 0)
2372 BUG();
2373
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002374 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002375}
2376
David S. Miller214e0052011-02-24 00:02:38 -05002377static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002379 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380 struct xfrm_usersa_flush *p;
2381 struct nlmsghdr *nlh;
2382 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002383 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384
Thomas Graf7deb2262007-08-22 13:57:39 -07002385 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386 if (skb == NULL)
2387 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002389 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2390 if (nlh == NULL) {
2391 kfree_skb(skb);
2392 return -EMSGSIZE;
2393 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394
Thomas Graf7b67c852007-08-22 13:53:52 -07002395 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002396 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397
Thomas Graf98250692007-08-22 12:47:26 -07002398 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002400 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401}
2402
Thomas Graf7deb2262007-08-22 13:57:39 -07002403static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404{
Thomas Graf7deb2262007-08-22 13:57:39 -07002405 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002406 if (x->aead)
2407 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002408 if (x->aalg) {
2409 l += nla_total_size(sizeof(struct xfrm_algo) +
2410 (x->aalg->alg_key_len + 7) / 8);
2411 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2412 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002414 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002416 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002417 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002418 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002419 if (x->tfcpad)
2420 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002421 if (x->replay_esn)
2422 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002423 if (x->security)
2424 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2425 x->security->ctx_len);
2426 if (x->coaddr)
2427 l += nla_total_size(sizeof(*x->coaddr));
2428
Herbert Xud26f3982007-11-13 21:47:08 -08002429 /* Must count x->lastused as it may become non-zero behind our back. */
2430 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002431
2432 return l;
2433}
2434
David S. Miller214e0052011-02-24 00:02:38 -05002435static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002436{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002437 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002438 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002439 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002440 struct nlmsghdr *nlh;
2441 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002442 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002443 int headlen;
2444
2445 headlen = sizeof(*p);
2446 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002447 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002448 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002449 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002450 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002451 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002452
Thomas Graf7deb2262007-08-22 13:57:39 -07002453 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002454 if (skb == NULL)
2455 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002456
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002457 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2458 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002459 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002460
Thomas Graf7b67c852007-08-22 13:53:52 -07002461 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002462 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002463 struct nlattr *attr;
2464
Thomas Graf7b67c852007-08-22 13:53:52 -07002465 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002466 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2467 id->spi = x->id.spi;
2468 id->family = x->props.family;
2469 id->proto = x->id.proto;
2470
Thomas Grafc0144be2007-08-22 13:55:43 -07002471 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2472 if (attr == NULL)
2473 goto nla_put_failure;
2474
2475 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002476 }
2477
Herbert Xu68325d32007-10-09 13:30:57 -07002478 if (copy_to_user_state_extra(x, p, skb))
2479 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002480
Thomas Graf98250692007-08-22 12:47:26 -07002481 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002482
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002483 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002484
Thomas Grafc0144be2007-08-22 13:55:43 -07002485nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002486 /* Somebody screwed up with xfrm_sa_len! */
2487 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002488 kfree_skb(skb);
2489 return -1;
2490}
2491
David S. Miller214e0052011-02-24 00:02:38 -05002492static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002493{
2494
2495 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002496 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002497 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002498 case XFRM_MSG_NEWAE:
2499 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002500 case XFRM_MSG_DELSA:
2501 case XFRM_MSG_UPDSA:
2502 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002503 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002504 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505 return xfrm_notify_sa_flush(c);
2506 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002507 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2508 c->event);
2509 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002510 }
2511
2512 return 0;
2513
2514}
2515
Thomas Graf7deb2262007-08-22 13:57:39 -07002516static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2517 struct xfrm_policy *xp)
2518{
2519 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2520 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002521 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002522 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2523 + userpolicy_type_attrsize();
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2527 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2528 int dir)
2529{
2530 struct xfrm_user_acquire *ua;
2531 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 __u32 seq = xfrm_get_acqseq();
2533
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002534 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2535 if (nlh == NULL)
2536 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Thomas Graf7b67c852007-08-22 13:53:52 -07002538 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 memcpy(&ua->id, &x->id, sizeof(ua->id));
2540 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2541 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2542 copy_to_user_policy(xp, &ua->policy, dir);
2543 ua->aalgos = xt->aalgos;
2544 ua->ealgos = xt->ealgos;
2545 ua->calgos = xt->calgos;
2546 ua->seq = x->km.seq = seq;
2547
2548 if (copy_to_user_tmpl(xp, skb) < 0)
2549 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002550 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002551 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002552 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002553 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002554 if (xfrm_mark_put(skb, &xp->mark))
2555 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Thomas Graf98250692007-08-22 12:47:26 -07002557 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002559nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002561 nlmsg_cancel(skb, nlh);
2562 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
2565static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2566 struct xfrm_policy *xp, int dir)
2567{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002568 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Thomas Graf7deb2262007-08-22 13:57:39 -07002571 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (skb == NULL)
2573 return -ENOMEM;
2574
2575 if (build_acquire(skb, x, xt, xp, dir) < 0)
2576 BUG();
2577
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002578 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
2581/* User gives us xfrm_user_policy_info followed by an array of 0
2582 * or more templates.
2583 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002584static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 u8 *data, int len, int *dir)
2586{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002587 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2589 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2590 struct xfrm_policy *xp;
2591 int nr;
2592
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002593 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 case AF_INET:
2595 if (opt != IP_XFRM_POLICY) {
2596 *dir = -EOPNOTSUPP;
2597 return NULL;
2598 }
2599 break;
2600#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2601 case AF_INET6:
2602 if (opt != IPV6_XFRM_POLICY) {
2603 *dir = -EOPNOTSUPP;
2604 return NULL;
2605 }
2606 break;
2607#endif
2608 default:
2609 *dir = -EINVAL;
2610 return NULL;
2611 }
2612
2613 *dir = -EINVAL;
2614
2615 if (len < sizeof(*p) ||
2616 verify_newpolicy_info(p))
2617 return NULL;
2618
2619 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002620 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return NULL;
2622
Herbert Xua4f1bac2005-07-26 15:43:17 -07002623 if (p->dir > XFRM_POLICY_OUT)
2624 return NULL;
2625
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002626 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (xp == NULL) {
2628 *dir = -ENOBUFS;
2629 return NULL;
2630 }
2631
2632 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002633 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 copy_templates(xp, ut, nr);
2635
2636 *dir = p->dir;
2637
2638 return xp;
2639}
2640
Thomas Graf7deb2262007-08-22 13:57:39 -07002641static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2642{
2643 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2644 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2645 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002646 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002647 + userpolicy_type_attrsize();
2648}
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002651 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 struct xfrm_user_polexpire *upe;
2654 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002655 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002657 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2658 if (nlh == NULL)
2659 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Thomas Graf7b67c852007-08-22 13:53:52 -07002661 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 copy_to_user_policy(xp, &upe->pol, dir);
2663 if (copy_to_user_tmpl(xp, skb) < 0)
2664 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002665 if (copy_to_user_sec_ctx(xp, skb))
2666 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002667 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002668 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002669 if (xfrm_mark_put(skb, &xp->mark))
2670 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 upe->hard = !!hard;
2672
Thomas Graf98250692007-08-22 12:47:26 -07002673 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002675nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002677 nlmsg_cancel(skb, nlh);
2678 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
David S. Miller214e0052011-02-24 00:02:38 -05002681static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002683 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Thomas Graf7deb2262007-08-22 13:57:39 -07002686 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 if (skb == NULL)
2688 return -ENOMEM;
2689
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002690 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 BUG();
2692
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002693 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
David S. Miller214e0052011-02-24 00:02:38 -05002696static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002698 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002699 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002700 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002701 struct nlmsghdr *nlh;
2702 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002703 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002704 int headlen;
2705
2706 headlen = sizeof(*p);
2707 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002708 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002709 headlen = sizeof(*id);
2710 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002711 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002712 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002713 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002714
Thomas Graf7deb2262007-08-22 13:57:39 -07002715 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002716 if (skb == NULL)
2717 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002718
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002719 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2720 if (nlh == NULL)
2721 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002722
Thomas Graf7b67c852007-08-22 13:53:52 -07002723 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002724 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002725 struct nlattr *attr;
2726
Thomas Graf7b67c852007-08-22 13:53:52 -07002727 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002728 memset(id, 0, sizeof(*id));
2729 id->dir = dir;
2730 if (c->data.byid)
2731 id->index = xp->index;
2732 else
2733 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2734
Thomas Grafc0144be2007-08-22 13:55:43 -07002735 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2736 if (attr == NULL)
2737 goto nlmsg_failure;
2738
2739 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002740 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002741
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002742 copy_to_user_policy(xp, p, dir);
2743 if (copy_to_user_tmpl(xp, skb) < 0)
2744 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002745 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002746 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002747
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002748 if (xfrm_mark_put(skb, &xp->mark))
2749 goto nla_put_failure;
2750
Thomas Graf98250692007-08-22 12:47:26 -07002751 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002752
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002753 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002754
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002755nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002756nlmsg_failure:
2757 kfree_skb(skb);
2758 return -1;
2759}
2760
David S. Miller214e0052011-02-24 00:02:38 -05002761static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002762{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002763 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002764 struct nlmsghdr *nlh;
2765 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002766
Thomas Graf7deb2262007-08-22 13:57:39 -07002767 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002768 if (skb == NULL)
2769 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002770
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002771 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2772 if (nlh == NULL)
2773 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002774 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2775 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002776
Thomas Graf98250692007-08-22 12:47:26 -07002777 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002778
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002779 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002780
2781nlmsg_failure:
2782 kfree_skb(skb);
2783 return -1;
2784}
2785
David S. Miller214e0052011-02-24 00:02:38 -05002786static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002787{
2788
2789 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002790 case XFRM_MSG_NEWPOLICY:
2791 case XFRM_MSG_UPDPOLICY:
2792 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002793 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002794 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002795 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002796 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002797 return xfrm_exp_policy_notify(xp, dir, c);
2798 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002799 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2800 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002801 }
2802
2803 return 0;
2804
2805}
2806
Thomas Graf7deb2262007-08-22 13:57:39 -07002807static inline size_t xfrm_report_msgsize(void)
2808{
2809 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2810}
2811
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002812static int build_report(struct sk_buff *skb, u8 proto,
2813 struct xfrm_selector *sel, xfrm_address_t *addr)
2814{
2815 struct xfrm_user_report *ur;
2816 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002817
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002818 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2819 if (nlh == NULL)
2820 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002821
Thomas Graf7b67c852007-08-22 13:53:52 -07002822 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002823 ur->proto = proto;
2824 memcpy(&ur->sel, sel, sizeof(ur->sel));
2825
2826 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002827 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002828
Thomas Graf98250692007-08-22 12:47:26 -07002829 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002830
Thomas Grafc0144be2007-08-22 13:55:43 -07002831nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002832 nlmsg_cancel(skb, nlh);
2833 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002834}
2835
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002836static int xfrm_send_report(struct net *net, u8 proto,
2837 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002838{
2839 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002840
Thomas Graf7deb2262007-08-22 13:57:39 -07002841 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002842 if (skb == NULL)
2843 return -ENOMEM;
2844
2845 if (build_report(skb, proto, sel, addr) < 0)
2846 BUG();
2847
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002848 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002849}
2850
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002851static inline size_t xfrm_mapping_msgsize(void)
2852{
2853 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2854}
2855
2856static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2857 xfrm_address_t *new_saddr, __be16 new_sport)
2858{
2859 struct xfrm_user_mapping *um;
2860 struct nlmsghdr *nlh;
2861
2862 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2863 if (nlh == NULL)
2864 return -EMSGSIZE;
2865
2866 um = nlmsg_data(nlh);
2867
2868 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2869 um->id.spi = x->id.spi;
2870 um->id.family = x->props.family;
2871 um->id.proto = x->id.proto;
2872 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2873 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2874 um->new_sport = new_sport;
2875 um->old_sport = x->encap->encap_sport;
2876 um->reqid = x->props.reqid;
2877
2878 return nlmsg_end(skb, nlh);
2879}
2880
2881static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2882 __be16 sport)
2883{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002884 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002885 struct sk_buff *skb;
2886
2887 if (x->id.proto != IPPROTO_ESP)
2888 return -EINVAL;
2889
2890 if (!x->encap)
2891 return -EINVAL;
2892
2893 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2894 if (skb == NULL)
2895 return -ENOMEM;
2896
2897 if (build_mapping(skb, x, ipaddr, sport) < 0)
2898 BUG();
2899
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002900 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static struct xfrm_mgr netlink_mgr = {
2904 .id = "netlink",
2905 .notify = xfrm_send_state_notify,
2906 .acquire = xfrm_send_acquire,
2907 .compile_policy = xfrm_compile_policy,
2908 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002909 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002910 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002911 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912};
2913
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002914static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Patrick McHardybe336902006-03-20 22:40:54 -08002916 struct sock *nlsk;
2917
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002918 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002919 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002920 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002922 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002923 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 0;
2925}
2926
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002927static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002928{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002929 struct net *net;
2930 list_for_each_entry(net, net_exit_list, exit_list)
2931 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2932 synchronize_net();
2933 list_for_each_entry(net, net_exit_list, exit_list)
2934 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002935}
2936
2937static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002938 .init = xfrm_user_net_init,
2939 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002940};
2941
2942static int __init xfrm_user_init(void)
2943{
2944 int rv;
2945
2946 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2947
2948 rv = register_pernet_subsys(&xfrm_user_net_ops);
2949 if (rv < 0)
2950 return rv;
2951 rv = xfrm_register_km(&netlink_mgr);
2952 if (rv < 0)
2953 unregister_pernet_subsys(&xfrm_user_net_ops);
2954 return rv;
2955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957static void __exit xfrm_user_exit(void)
2958{
2959 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002960 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961}
2962
2963module_init(xfrm_user_init);
2964module_exit(xfrm_user_exit);
2965MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002966MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002967