blob: 3d15d3e1b2c49b7c124cd08d42ed5a1a77849c57 [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
Steffen Klassert02aadf72011-03-28 19:48:09 +0000130 if (p->id.proto != IPPROTO_ESP)
131 return -EINVAL;
132
Steffen Klassertd8647b72011-03-08 00:10:27 +0000133 if (p->replay_window != 0)
134 return -EINVAL;
135
136 return 0;
137}
Trent Jaegerdf718372005-12-13 23:12:27 -0800138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700140 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 int err;
143
144 err = -EINVAL;
145 switch (p->family) {
146 case AF_INET:
147 break;
148
149 case AF_INET6:
150#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
151 break;
152#else
153 err = -EAFNOSUPPORT;
154 goto out;
155#endif
156
157 default:
158 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 err = -EINVAL;
162 switch (p->id.proto) {
163 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000164 if ((!attrs[XFRMA_ALG_AUTH] &&
165 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800166 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700167 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000168 attrs[XFRMA_ALG_COMP] ||
169 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out;
171 break;
172
173 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800174 if (attrs[XFRMA_ALG_COMP])
175 goto out;
176 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000177 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800178 !attrs[XFRMA_ALG_CRYPT] &&
179 !attrs[XFRMA_ALG_AEAD])
180 goto out;
181 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000182 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800183 attrs[XFRMA_ALG_CRYPT]) &&
184 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000186 if (attrs[XFRMA_TFCPAD] &&
187 p->mode != XFRM_MODE_TUNNEL)
188 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190
191 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700192 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800193 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700194 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000195 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000196 attrs[XFRMA_ALG_CRYPT] ||
197 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out;
199 break;
200
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700201#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
202 case IPPROTO_DSTOPTS:
203 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if (attrs[XFRMA_ALG_COMP] ||
205 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000206 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800207 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700208 attrs[XFRMA_ALG_CRYPT] ||
209 attrs[XFRMA_ENCAP] ||
210 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000211 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700212 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700213 goto out;
214 break;
215#endif
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 default:
218 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Herbert Xu1a6509d2008-01-28 19:37:29 -0800221 if ((err = verify_aead(attrs)))
222 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000223 if ((err = verify_auth_trunc(attrs)))
224 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700225 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700227 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700229 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700231 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800232 goto out;
Steffen Klassertd8647b72011-03-08 00:10:27 +0000233 if ((err = verify_replay(p, attrs)))
234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 err = -EINVAL;
237 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700238 case XFRM_MODE_TRANSPORT:
239 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700240 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700241 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243
244 default:
245 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 err = 0;
249
250out:
251 return err;
252}
253
254static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800255 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700256 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct xfrm_algo *p, *ualg;
259 struct xfrm_algo_desc *algo;
260
261 if (!rta)
262 return 0;
263
Thomas Graf5424f322007-08-22 14:01:33 -0700264 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 algo = get_byname(ualg->alg_name, 1);
267 if (!algo)
268 return -ENOSYS;
269 *props = algo->desc.sadb_alg_id;
270
Eric Dumazet0f99be02008-01-08 23:39:06 -0800271 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!p)
273 return -ENOMEM;
274
Herbert Xu04ff1262006-08-13 08:50:00 +1000275 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *algpp = p;
277 return 0;
278}
279
Martin Willi4447bb32009-11-25 00:29:52 +0000280static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
281 struct nlattr *rta)
282{
283 struct xfrm_algo *ualg;
284 struct xfrm_algo_auth *p;
285 struct xfrm_algo_desc *algo;
286
287 if (!rta)
288 return 0;
289
290 ualg = nla_data(rta);
291
292 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
293 if (!algo)
294 return -ENOSYS;
295 *props = algo->desc.sadb_alg_id;
296
297 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
298 if (!p)
299 return -ENOMEM;
300
301 strcpy(p->alg_name, algo->name);
302 p->alg_key_len = ualg->alg_key_len;
303 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
304 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
305
306 *algpp = p;
307 return 0;
308}
309
310static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
311 struct nlattr *rta)
312{
313 struct xfrm_algo_auth *p, *ualg;
314 struct xfrm_algo_desc *algo;
315
316 if (!rta)
317 return 0;
318
319 ualg = nla_data(rta);
320
321 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
322 if (!algo)
323 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000324 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
325 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000326 return -EINVAL;
327 *props = algo->desc.sadb_alg_id;
328
329 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
330 if (!p)
331 return -ENOMEM;
332
333 strcpy(p->alg_name, algo->name);
334 if (!p->alg_trunc_len)
335 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
336
337 *algpp = p;
338 return 0;
339}
340
Herbert Xu1a6509d2008-01-28 19:37:29 -0800341static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
342 struct nlattr *rta)
343{
344 struct xfrm_algo_aead *p, *ualg;
345 struct xfrm_algo_desc *algo;
346
347 if (!rta)
348 return 0;
349
350 ualg = nla_data(rta);
351
352 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
353 if (!algo)
354 return -ENOSYS;
355 *props = algo->desc.sadb_alg_id;
356
357 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
358 if (!p)
359 return -ENOMEM;
360
361 strcpy(p->alg_name, algo->name);
362 *algpp = p;
363 return 0;
364}
365
Steffen Klasserte2b19122011-03-28 19:47:30 +0000366static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
367 struct nlattr *rp)
368{
369 struct xfrm_replay_state_esn *up;
370
371 if (!replay_esn || !rp)
372 return 0;
373
374 up = nla_data(rp);
375
376 if (xfrm_replay_state_esn_len(replay_esn) !=
377 xfrm_replay_state_esn_len(up))
378 return -EINVAL;
379
380 return 0;
381}
382
Steffen Klassertd8647b72011-03-08 00:10:27 +0000383static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
384 struct xfrm_replay_state_esn **preplay_esn,
385 struct nlattr *rta)
386{
387 struct xfrm_replay_state_esn *p, *pp, *up;
388
389 if (!rta)
390 return 0;
391
392 up = nla_data(rta);
393
394 p = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
395 if (!p)
396 return -ENOMEM;
397
398 pp = kmemdup(up, xfrm_replay_state_esn_len(up), GFP_KERNEL);
399 if (!pp) {
400 kfree(p);
401 return -ENOMEM;
402 }
403
404 *replay_esn = p;
405 *preplay_esn = pp;
406
407 return 0;
408}
409
Joy Latten661697f2007-04-13 16:14:35 -0700410static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800411{
Trent Jaegerdf718372005-12-13 23:12:27 -0800412 int len = 0;
413
414 if (xfrm_ctx) {
415 len += sizeof(struct xfrm_user_sec_ctx);
416 len += xfrm_ctx->ctx_len;
417 }
418 return len;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
422{
423 memcpy(&x->id, &p->id, sizeof(x->id));
424 memcpy(&x->sel, &p->sel, sizeof(x->sel));
425 memcpy(&x->lft, &p->lft, sizeof(x->lft));
426 x->props.mode = p->mode;
427 x->props.replay_window = p->replay_window;
428 x->props.reqid = p->reqid;
429 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700430 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700432
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700433 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700434 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800437/*
438 * someday when pfkey also has support, we could have the code
439 * somehow made shareable and move it to xfrm_state.c - JHS
440 *
441*/
Thomas Graf5424f322007-08-22 14:01:33 -0700442static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800443{
Thomas Graf5424f322007-08-22 14:01:33 -0700444 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +0000445 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -0700446 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
447 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
448 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800449
Steffen Klassertd8647b72011-03-08 00:10:27 +0000450 if (re) {
451 struct xfrm_replay_state_esn *replay_esn;
452 replay_esn = nla_data(re);
453 memcpy(x->replay_esn, replay_esn,
454 xfrm_replay_state_esn_len(replay_esn));
455 memcpy(x->preplay_esn, replay_esn,
456 xfrm_replay_state_esn_len(replay_esn));
457 }
458
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800459 if (rp) {
460 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700461 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800462 memcpy(&x->replay, replay, sizeof(*replay));
463 memcpy(&x->preplay, replay, sizeof(*replay));
464 }
465
466 if (lt) {
467 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700468 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800469 x->curlft.bytes = ltime->bytes;
470 x->curlft.packets = ltime->packets;
471 x->curlft.add_time = ltime->add_time;
472 x->curlft.use_time = ltime->use_time;
473 }
474
Thomas Grafcf5cb792007-08-22 13:59:04 -0700475 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700476 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800477
Thomas Grafcf5cb792007-08-22 13:59:04 -0700478 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700479 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800480}
481
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800482static struct xfrm_state *xfrm_state_construct(struct net *net,
483 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700484 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int *errp)
486{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800487 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int err = -ENOMEM;
489
490 if (!x)
491 goto error_no_put;
492
493 copy_from_user_state(x, p);
494
Herbert Xu1a6509d2008-01-28 19:37:29 -0800495 if ((err = attach_aead(&x->aead, &x->props.ealgo,
496 attrs[XFRMA_ALG_AEAD])))
497 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000498 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
499 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000501 if (!x->props.aalgo) {
502 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
503 attrs[XFRMA_ALG_AUTH])))
504 goto error;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
507 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700508 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 goto error;
510 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
511 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700512 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700514
515 if (attrs[XFRMA_ENCAP]) {
516 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
517 sizeof(*x->encap), GFP_KERNEL);
518 if (x->encap == NULL)
519 goto error;
520 }
521
Martin Willi35d28562010-12-08 04:37:49 +0000522 if (attrs[XFRMA_TFCPAD])
523 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
524
Thomas Graffd211502007-09-06 03:28:08 -0700525 if (attrs[XFRMA_COADDR]) {
526 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
527 sizeof(*x->coaddr), GFP_KERNEL);
528 if (x->coaddr == NULL)
529 goto error;
530 }
531
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000532 xfrm_mark_get(attrs, &x->mark);
533
Wei Yongjuna454f0c2011-03-21 18:08:28 -0700534 err = __xfrm_init_state(x, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (err)
536 goto error;
537
Thomas Graffd211502007-09-06 03:28:08 -0700538 if (attrs[XFRMA_SEC_CTX] &&
539 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800540 goto error;
541
Steffen Klassertd8647b72011-03-08 00:10:27 +0000542 if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
543 attrs[XFRMA_REPLAY_ESN_VAL])))
544 goto error;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800547 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800548 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800549 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800550
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000551 if ((err = xfrm_init_replay(x)))
552 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800553
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000554 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700555 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 return x;
558
559error:
560 x->km.state = XFRM_STATE_DEAD;
561 xfrm_state_put(x);
562error_no_put:
563 *errp = err;
564 return NULL;
565}
566
Christoph Hellwig22e70052007-01-02 15:22:30 -0800567static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700568 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800570 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700571 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct xfrm_state *x;
573 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700574 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800575 uid_t loginuid = audit_get_loginuid(current);
576 u32 sessionid = audit_get_sessionid(current);
577 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Thomas Graf35a7aa02007-08-22 14:00:40 -0700579 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (err)
581 return err;
582
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800583 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!x)
585 return err;
586
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700587 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
589 err = xfrm_state_add(x);
590 else
591 err = xfrm_state_update(x);
592
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800593 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400594 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (err < 0) {
597 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800598 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700599 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700602 c.seq = nlh->nlmsg_seq;
603 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700604 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700605
606 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700607out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700608 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return err;
610}
611
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800612static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
613 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700614 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700615 int *errp)
616{
617 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000618 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700619 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000620 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700621
622 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
623 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000624 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700625 } else {
626 xfrm_address_t *saddr = NULL;
627
Thomas Graf35a7aa02007-08-22 14:00:40 -0700628 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700629 if (!saddr) {
630 err = -EINVAL;
631 goto out;
632 }
633
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800634 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000635 x = xfrm_state_lookup_byaddr(net, mark,
636 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800637 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700638 }
639
640 out:
641 if (!x && errp)
642 *errp = err;
643 return x;
644}
645
Christoph Hellwig22e70052007-01-02 15:22:30 -0800646static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700647 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800649 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700651 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700652 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700653 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800654 uid_t loginuid = audit_get_loginuid(current);
655 u32 sessionid = audit_get_sessionid(current);
656 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800658 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700660 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
David S. Miller6f68dc32006-06-08 23:58:52 -0700662 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700663 goto out;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700666 err = -EPERM;
667 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700670 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600671
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700672 if (err < 0)
673 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700674
675 c.seq = nlh->nlmsg_seq;
676 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700677 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700678 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700680out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800681 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400682 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700683 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
688{
689 memcpy(&p->id, &x->id, sizeof(p->id));
690 memcpy(&p->sel, &x->sel, sizeof(p->sel));
691 memcpy(&p->lft, &x->lft, sizeof(p->lft));
692 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
693 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700694 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 p->mode = x->props.mode;
696 p->replay_window = x->props.replay_window;
697 p->reqid = x->props.reqid;
698 p->family = x->props.family;
699 p->flags = x->props.flags;
700 p->seq = x->km.seq;
701}
702
703struct xfrm_dump_info {
704 struct sk_buff *in_skb;
705 struct sk_buff *out_skb;
706 u32 nlmsg_seq;
707 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708};
709
Thomas Grafc0144be2007-08-22 13:55:43 -0700710static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
711{
Thomas Grafc0144be2007-08-22 13:55:43 -0700712 struct xfrm_user_sec_ctx *uctx;
713 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700714 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700715
716 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
717 if (attr == NULL)
718 return -EMSGSIZE;
719
720 uctx = nla_data(attr);
721 uctx->exttype = XFRMA_SEC_CTX;
722 uctx->len = ctx_size;
723 uctx->ctx_doi = s->ctx_doi;
724 uctx->ctx_alg = s->ctx_alg;
725 uctx->ctx_len = s->ctx_len;
726 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
727
728 return 0;
729}
730
Martin Willi4447bb32009-11-25 00:29:52 +0000731static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
732{
733 struct xfrm_algo *algo;
734 struct nlattr *nla;
735
736 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
737 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
738 if (!nla)
739 return -EMSGSIZE;
740
741 algo = nla_data(nla);
742 strcpy(algo->alg_name, auth->alg_name);
743 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
744 algo->alg_key_len = auth->alg_key_len;
745
746 return 0;
747}
748
Herbert Xu68325d32007-10-09 13:30:57 -0700749/* Don't change this without updating xfrm_sa_len! */
750static int copy_to_user_state_extra(struct xfrm_state *x,
751 struct xfrm_usersa_info *p,
752 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 copy_to_user_state(x, p);
755
Herbert Xu050f0092007-10-09 13:31:47 -0700756 if (x->coaddr)
757 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
758
759 if (x->lastused)
760 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700761
Herbert Xu1a6509d2008-01-28 19:37:29 -0800762 if (x->aead)
763 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000764 if (x->aalg) {
765 if (copy_to_user_auth(x->aalg, skb))
766 goto nla_put_failure;
767
768 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
769 xfrm_alg_auth_len(x->aalg), x->aalg);
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800772 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700774 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700777 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Martin Willi35d28562010-12-08 04:37:49 +0000779 if (x->tfcpad)
780 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
781
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000782 if (xfrm_mark_put(skb, &x->mark))
783 goto nla_put_failure;
784
Steffen Klassertd8647b72011-03-08 00:10:27 +0000785 if (x->replay_esn)
786 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
787 xfrm_replay_state_esn_len(x->replay_esn), x->replay_esn);
788
Thomas Grafc0144be2007-08-22 13:55:43 -0700789 if (x->security && copy_sec_ctx(x->security, skb) < 0)
790 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700791
Herbert Xu68325d32007-10-09 13:30:57 -0700792 return 0;
793
794nla_put_failure:
795 return -EMSGSIZE;
796}
797
798static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
799{
800 struct xfrm_dump_info *sp = ptr;
801 struct sk_buff *in_skb = sp->in_skb;
802 struct sk_buff *skb = sp->out_skb;
803 struct xfrm_usersa_info *p;
804 struct nlmsghdr *nlh;
805 int err;
806
Herbert Xu68325d32007-10-09 13:30:57 -0700807 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
808 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
809 if (nlh == NULL)
810 return -EMSGSIZE;
811
812 p = nlmsg_data(nlh);
813
814 err = copy_to_user_state_extra(x, p, skb);
815 if (err)
816 goto nla_put_failure;
817
Thomas Graf98250692007-08-22 12:47:26 -0700818 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820
Thomas Grafc0144be2007-08-22 13:55:43 -0700821nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700822 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Timo Teras4c563f72008-02-28 21:31:08 -0800826static int xfrm_dump_sa_done(struct netlink_callback *cb)
827{
828 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
829 xfrm_state_walk_done(walk);
830 return 0;
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
834{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800835 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800836 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct xfrm_dump_info info;
838
Timo Teras4c563f72008-02-28 21:31:08 -0800839 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
840 sizeof(cb->args) - sizeof(cb->args[0]));
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 info.in_skb = cb->skb;
843 info.out_skb = skb;
844 info.nlmsg_seq = cb->nlh->nlmsg_seq;
845 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800846
847 if (!cb->args[0]) {
848 cb->args[0] = 1;
849 xfrm_state_walk_init(walk, 0);
850 }
851
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800852 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 return skb->len;
855}
856
857static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
858 struct xfrm_state *x, u32 seq)
859{
860 struct xfrm_dump_info info;
861 struct sk_buff *skb;
862
Thomas Graf7deb2262007-08-22 13:57:39 -0700863 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!skb)
865 return ERR_PTR(-ENOMEM);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 info.in_skb = in_skb;
868 info.out_skb = skb;
869 info.nlmsg_seq = seq;
870 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 if (dump_one_state(x, 0, &info)) {
873 kfree_skb(skb);
874 return NULL;
875 }
876
877 return skb;
878}
879
Thomas Graf7deb2262007-08-22 13:57:39 -0700880static inline size_t xfrm_spdinfo_msgsize(void)
881{
882 return NLMSG_ALIGN(4)
883 + nla_total_size(sizeof(struct xfrmu_spdinfo))
884 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
885}
886
Alexey Dobriyane0710412010-01-23 13:37:10 +0000887static int build_spdinfo(struct sk_buff *skb, struct net *net,
888 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700889{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700890 struct xfrmk_spdinfo si;
891 struct xfrmu_spdinfo spc;
892 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700893 struct nlmsghdr *nlh;
894 u32 *f;
895
896 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
897 if (nlh == NULL) /* shouldnt really happen ... */
898 return -EMSGSIZE;
899
900 f = nlmsg_data(nlh);
901 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000902 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700903 spc.incnt = si.incnt;
904 spc.outcnt = si.outcnt;
905 spc.fwdcnt = si.fwdcnt;
906 spc.inscnt = si.inscnt;
907 spc.outscnt = si.outscnt;
908 spc.fwdscnt = si.fwdscnt;
909 sph.spdhcnt = si.spdhcnt;
910 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700911
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700912 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
913 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700914
915 return nlmsg_end(skb, nlh);
916
917nla_put_failure:
918 nlmsg_cancel(skb, nlh);
919 return -EMSGSIZE;
920}
921
922static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700923 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700924{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800925 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700926 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700927 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700928 u32 spid = NETLINK_CB(skb).pid;
929 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700930
Thomas Graf7deb2262007-08-22 13:57:39 -0700931 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700932 if (r_skb == NULL)
933 return -ENOMEM;
934
Alexey Dobriyane0710412010-01-23 13:37:10 +0000935 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700936 BUG();
937
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800938 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700939}
940
Thomas Graf7deb2262007-08-22 13:57:39 -0700941static inline size_t xfrm_sadinfo_msgsize(void)
942{
943 return NLMSG_ALIGN(4)
944 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
945 + nla_total_size(4); /* XFRMA_SAD_CNT */
946}
947
Alexey Dobriyane0710412010-01-23 13:37:10 +0000948static int build_sadinfo(struct sk_buff *skb, struct net *net,
949 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700950{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700951 struct xfrmk_sadinfo si;
952 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700953 struct nlmsghdr *nlh;
954 u32 *f;
955
956 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
957 if (nlh == NULL) /* shouldnt really happen ... */
958 return -EMSGSIZE;
959
960 f = nlmsg_data(nlh);
961 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000962 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700963
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700964 sh.sadhmcnt = si.sadhmcnt;
965 sh.sadhcnt = si.sadhcnt;
966
967 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
968 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700969
970 return nlmsg_end(skb, nlh);
971
972nla_put_failure:
973 nlmsg_cancel(skb, nlh);
974 return -EMSGSIZE;
975}
976
977static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700978 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700979{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800980 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700981 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700982 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700983 u32 spid = NETLINK_CB(skb).pid;
984 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700985
Thomas Graf7deb2262007-08-22 13:57:39 -0700986 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700987 if (r_skb == NULL)
988 return -ENOMEM;
989
Alexey Dobriyane0710412010-01-23 13:37:10 +0000990 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700991 BUG();
992
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800993 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700994}
995
Christoph Hellwig22e70052007-01-02 15:22:30 -0800996static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700997 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800999 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001000 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 struct xfrm_state *x;
1002 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -07001003 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001005 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (x == NULL)
1007 goto out_noput;
1008
1009 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
1010 if (IS_ERR(resp_skb)) {
1011 err = PTR_ERR(resp_skb);
1012 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001013 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015 xfrm_state_put(x);
1016out_noput:
1017 return err;
1018}
1019
1020static int verify_userspi_info(struct xfrm_userspi_info *p)
1021{
1022 switch (p->info.id.proto) {
1023 case IPPROTO_AH:
1024 case IPPROTO_ESP:
1025 break;
1026
1027 case IPPROTO_COMP:
1028 /* IPCOMP spi is 16-bits. */
1029 if (p->max >= 0x10000)
1030 return -EINVAL;
1031 break;
1032
1033 default:
1034 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (p->min > p->max)
1038 return -EINVAL;
1039
1040 return 0;
1041}
1042
Christoph Hellwig22e70052007-01-02 15:22:30 -08001043static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001044 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001046 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct xfrm_state *x;
1048 struct xfrm_userspi_info *p;
1049 struct sk_buff *resp_skb;
1050 xfrm_address_t *daddr;
1051 int family;
1052 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001053 u32 mark;
1054 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Thomas Graf7b67c852007-08-22 13:53:52 -07001056 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 err = verify_userspi_info(p);
1058 if (err)
1059 goto out_noput;
1060
1061 family = p->info.family;
1062 daddr = &p->info.id.daddr;
1063
1064 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001065
1066 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001068 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
1070 xfrm_state_put(x);
1071 x = NULL;
1072 }
1073 }
1074
1075 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001076 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 p->info.id.proto, daddr,
1078 &p->info.saddr, 1,
1079 family);
1080 err = -ENOENT;
1081 if (x == NULL)
1082 goto out_noput;
1083
Herbert Xu658b2192007-10-09 13:29:52 -07001084 err = xfrm_alloc_spi(x, p->min, p->max);
1085 if (err)
1086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Herbert Xu658b2192007-10-09 13:29:52 -07001088 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (IS_ERR(resp_skb)) {
1090 err = PTR_ERR(resp_skb);
1091 goto out;
1092 }
1093
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001094 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096out:
1097 xfrm_state_put(x);
1098out_noput:
1099 return err;
1100}
1101
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001102static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 switch (dir) {
1105 case XFRM_POLICY_IN:
1106 case XFRM_POLICY_OUT:
1107 case XFRM_POLICY_FWD:
1108 break;
1109
1110 default:
1111 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return 0;
1115}
1116
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001117static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001118{
1119 switch (type) {
1120 case XFRM_POLICY_TYPE_MAIN:
1121#ifdef CONFIG_XFRM_SUB_POLICY
1122 case XFRM_POLICY_TYPE_SUB:
1123#endif
1124 break;
1125
1126 default:
1127 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001128 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001129
1130 return 0;
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1134{
1135 switch (p->share) {
1136 case XFRM_SHARE_ANY:
1137 case XFRM_SHARE_SESSION:
1138 case XFRM_SHARE_USER:
1139 case XFRM_SHARE_UNIQUE:
1140 break;
1141
1142 default:
1143 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 switch (p->action) {
1147 case XFRM_POLICY_ALLOW:
1148 case XFRM_POLICY_BLOCK:
1149 break;
1150
1151 default:
1152 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 switch (p->sel.family) {
1156 case AF_INET:
1157 break;
1158
1159 case AF_INET6:
1160#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1161 break;
1162#else
1163 return -EAFNOSUPPORT;
1164#endif
1165
1166 default:
1167 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 return verify_policy_dir(p->dir);
1171}
1172
Thomas Graf5424f322007-08-22 14:01:33 -07001173static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001174{
Thomas Graf5424f322007-08-22 14:01:33 -07001175 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001176 struct xfrm_user_sec_ctx *uctx;
1177
1178 if (!rt)
1179 return 0;
1180
Thomas Graf5424f322007-08-22 14:01:33 -07001181 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001182 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1186 int nr)
1187{
1188 int i;
1189
1190 xp->xfrm_nr = nr;
1191 for (i = 0; i < nr; i++, ut++) {
1192 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1193
1194 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1195 memcpy(&t->saddr, &ut->saddr,
1196 sizeof(xfrm_address_t));
1197 t->reqid = ut->reqid;
1198 t->mode = ut->mode;
1199 t->share = ut->share;
1200 t->optional = ut->optional;
1201 t->aalgos = ut->aalgos;
1202 t->ealgos = ut->ealgos;
1203 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001204 /* If all masks are ~0, then we allow all algorithms. */
1205 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001206 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208}
1209
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001210static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1211{
1212 int i;
1213
1214 if (nr > XFRM_MAX_DEPTH)
1215 return -EINVAL;
1216
1217 for (i = 0; i < nr; i++) {
1218 /* We never validated the ut->family value, so many
1219 * applications simply leave it at zero. The check was
1220 * never made and ut->family was ignored because all
1221 * templates could be assumed to have the same family as
1222 * the policy itself. Now that we will have ipv4-in-ipv6
1223 * and ipv6-in-ipv4 tunnels, this is no longer true.
1224 */
1225 if (!ut[i].family)
1226 ut[i].family = family;
1227
1228 switch (ut[i].family) {
1229 case AF_INET:
1230 break;
1231#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1232 case AF_INET6:
1233 break;
1234#endif
1235 default:
1236 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001237 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001238 }
1239
1240 return 0;
1241}
1242
Thomas Graf5424f322007-08-22 14:01:33 -07001243static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Thomas Graf5424f322007-08-22 14:01:33 -07001245 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (!rt) {
1248 pol->xfrm_nr = 0;
1249 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001250 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1251 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001252 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001254 err = validate_tmpl(nr, utmpl, pol->family);
1255 if (err)
1256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Thomas Graf5424f322007-08-22 14:01:33 -07001258 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 return 0;
1261}
1262
Thomas Graf5424f322007-08-22 14:01:33 -07001263static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001264{
Thomas Graf5424f322007-08-22 14:01:33 -07001265 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001266 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001267 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001268 int err;
1269
1270 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001271 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001272 type = upt->type;
1273 }
1274
1275 err = verify_policy_type(type);
1276 if (err)
1277 return err;
1278
1279 *tp = type;
1280 return 0;
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1284{
1285 xp->priority = p->priority;
1286 xp->index = p->index;
1287 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1288 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1289 xp->action = p->action;
1290 xp->flags = p->flags;
1291 xp->family = p->sel.family;
1292 /* XXX xp->share = p->share; */
1293}
1294
1295static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1296{
1297 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1298 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1299 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1300 p->priority = xp->priority;
1301 p->index = xp->index;
1302 p->sel.family = xp->family;
1303 p->dir = dir;
1304 p->action = xp->action;
1305 p->flags = xp->flags;
1306 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1307}
1308
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001309static 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 -07001310{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001311 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 int err;
1313
1314 if (!xp) {
1315 *errp = -ENOMEM;
1316 return NULL;
1317 }
1318
1319 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001320
Thomas Graf35a7aa02007-08-22 14:00:40 -07001321 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001322 if (err)
1323 goto error;
1324
Thomas Graf35a7aa02007-08-22 14:00:40 -07001325 if (!(err = copy_from_user_tmpl(xp, attrs)))
1326 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001327 if (err)
1328 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001330 xfrm_mark_get(attrs, &xp->mark);
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001333 error:
1334 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001335 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001336 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001337 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Christoph Hellwig22e70052007-01-02 15:22:30 -08001340static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001341 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001343 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001344 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001346 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int err;
1348 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001349 uid_t loginuid = audit_get_loginuid(current);
1350 u32 sessionid = audit_get_sessionid(current);
1351 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 err = verify_newpolicy_info(p);
1354 if (err)
1355 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001356 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001357 if (err)
1358 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001360 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!xp)
1362 return err;
1363
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001364 /* shouldnt excl be based on nlh flags??
1365 * Aha! this is anti-netlink really i.e more pfkey derived
1366 * in netlink excl is a flag and you wouldnt need
1367 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1369 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001370 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001371 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001374 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 kfree(xp);
1376 return err;
1377 }
1378
Herbert Xuf60f6b82005-06-18 22:44:37 -07001379 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001380 c.seq = nlh->nlmsg_seq;
1381 c.pid = nlh->nlmsg_pid;
1382 km_policy_notify(xp, p->dir, &c);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 xfrm_pol_put(xp);
1385
1386 return 0;
1387}
1388
1389static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1390{
1391 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1392 int i;
1393
1394 if (xp->xfrm_nr == 0)
1395 return 0;
1396
1397 for (i = 0; i < xp->xfrm_nr; i++) {
1398 struct xfrm_user_tmpl *up = &vec[i];
1399 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1400
1401 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001402 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1404 up->reqid = kp->reqid;
1405 up->mode = kp->mode;
1406 up->share = kp->share;
1407 up->optional = kp->optional;
1408 up->aalgos = kp->aalgos;
1409 up->ealgos = kp->ealgos;
1410 up->calgos = kp->calgos;
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Thomas Grafc0144be2007-08-22 13:55:43 -07001413 return nla_put(skb, XFRMA_TMPL,
1414 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001415}
1416
Serge Hallyn0d681622006-07-24 23:30:44 -07001417static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1418{
1419 if (x->security) {
1420 return copy_sec_ctx(x->security, skb);
1421 }
1422 return 0;
1423}
1424
1425static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1426{
1427 if (xp->security) {
1428 return copy_sec_ctx(xp->security, skb);
1429 }
1430 return 0;
1431}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001432static inline size_t userpolicy_type_attrsize(void)
1433{
1434#ifdef CONFIG_XFRM_SUB_POLICY
1435 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1436#else
1437 return 0;
1438#endif
1439}
Serge Hallyn0d681622006-07-24 23:30:44 -07001440
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001441#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001442static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001443{
Thomas Grafc0144be2007-08-22 13:55:43 -07001444 struct xfrm_userpolicy_type upt = {
1445 .type = type,
1446 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001447
Thomas Grafc0144be2007-08-22 13:55:43 -07001448 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001449}
1450
1451#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001452static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001453{
1454 return 0;
1455}
1456#endif
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1459{
1460 struct xfrm_dump_info *sp = ptr;
1461 struct xfrm_userpolicy_info *p;
1462 struct sk_buff *in_skb = sp->in_skb;
1463 struct sk_buff *skb = sp->out_skb;
1464 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001466 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1467 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1468 if (nlh == NULL)
1469 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Thomas Graf7b67c852007-08-22 13:53:52 -07001471 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 copy_to_user_policy(xp, p, dir);
1473 if (copy_to_user_tmpl(xp, skb) < 0)
1474 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001475 if (copy_to_user_sec_ctx(xp, skb))
1476 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001477 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001478 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001479 if (xfrm_mark_put(skb, &xp->mark))
1480 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Thomas Graf98250692007-08-22 12:47:26 -07001482 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
1484
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001485nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001487 nlmsg_cancel(skb, nlh);
1488 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Timo Teras4c563f72008-02-28 21:31:08 -08001491static int xfrm_dump_policy_done(struct netlink_callback *cb)
1492{
1493 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1494
1495 xfrm_policy_walk_done(walk);
1496 return 0;
1497}
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1500{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001501 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001502 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct xfrm_dump_info info;
1504
Timo Teras4c563f72008-02-28 21:31:08 -08001505 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1506 sizeof(cb->args) - sizeof(cb->args[0]));
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 info.in_skb = cb->skb;
1509 info.out_skb = skb;
1510 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1511 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001512
1513 if (!cb->args[0]) {
1514 cb->args[0] = 1;
1515 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1516 }
1517
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001518 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 return skb->len;
1521}
1522
1523static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1524 struct xfrm_policy *xp,
1525 int dir, u32 seq)
1526{
1527 struct xfrm_dump_info info;
1528 struct sk_buff *skb;
1529
Thomas Graf7deb2262007-08-22 13:57:39 -07001530 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (!skb)
1532 return ERR_PTR(-ENOMEM);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 info.in_skb = in_skb;
1535 info.out_skb = skb;
1536 info.nlmsg_seq = seq;
1537 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1540 kfree_skb(skb);
1541 return NULL;
1542 }
1543
1544 return skb;
1545}
1546
Christoph Hellwig22e70052007-01-02 15:22:30 -08001547static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001548 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001550 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct xfrm_policy *xp;
1552 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001553 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001555 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001557 struct xfrm_mark m;
1558 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Thomas Graf7b67c852007-08-22 13:53:52 -07001560 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1562
Thomas Graf35a7aa02007-08-22 14:00:40 -07001563 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001564 if (err)
1565 return err;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 err = verify_policy_dir(p->dir);
1568 if (err)
1569 return err;
1570
1571 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001572 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001573 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001574 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001575 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001576
Thomas Graf35a7aa02007-08-22 14:00:40 -07001577 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001578 if (err)
1579 return err;
1580
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001581 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001582 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001583 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001584
Paul Moore03e1ad72008-04-12 19:07:52 -07001585 err = security_xfrm_policy_alloc(&ctx, uctx);
1586 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001587 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001588 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001589 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001590 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001591 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (xp == NULL)
1594 return -ENOENT;
1595
1596 if (!delete) {
1597 struct sk_buff *resp_skb;
1598
1599 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1600 if (IS_ERR(resp_skb)) {
1601 err = PTR_ERR(resp_skb);
1602 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001603 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001604 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001606 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001607 uid_t loginuid = audit_get_loginuid(current);
1608 u32 sessionid = audit_get_sessionid(current);
1609 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001610
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001611 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001612 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1613 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001614
1615 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001616 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001617
Herbert Xue7443892005-06-18 22:44:18 -07001618 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001619 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001620 c.seq = nlh->nlmsg_seq;
1621 c.pid = nlh->nlmsg_pid;
1622 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001625out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001626 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return err;
1628}
1629
Christoph Hellwig22e70052007-01-02 15:22:30 -08001630static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001631 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001633 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001634 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001635 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001636 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001637 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001639 audit_info.loginuid = audit_get_loginuid(current);
1640 audit_info.sessionid = audit_get_sessionid(current);
1641 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001642 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001643 if (err) {
1644 if (err == -ESRCH) /* empty table */
1645 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001646 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001647 }
Herbert Xubf088672005-06-18 22:44:00 -07001648 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001649 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001650 c.seq = nlh->nlmsg_seq;
1651 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001652 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001653 km_state_notify(NULL, &c);
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return 0;
1656}
1657
Steffen Klassertd8647b72011-03-08 00:10:27 +00001658static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
Thomas Graf7deb2262007-08-22 13:57:39 -07001659{
Steffen Klassertd8647b72011-03-08 00:10:27 +00001660 size_t replay_size = x->replay_esn ?
1661 xfrm_replay_state_esn_len(x->replay_esn) :
1662 sizeof(struct xfrm_replay_state);
1663
Thomas Graf7deb2262007-08-22 13:57:39 -07001664 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
Steffen Klassertd8647b72011-03-08 00:10:27 +00001665 + nla_total_size(replay_size)
Thomas Graf7deb2262007-08-22 13:57:39 -07001666 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001667 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001668 + nla_total_size(4) /* XFRM_AE_RTHR */
1669 + nla_total_size(4); /* XFRM_AE_ETHR */
1670}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001671
David S. Miller214e0052011-02-24 00:02:38 -05001672static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001673{
1674 struct xfrm_aevent_id *id;
1675 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001676
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001677 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1678 if (nlh == NULL)
1679 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001680
Thomas Graf7b67c852007-08-22 13:53:52 -07001681 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001682 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001683 id->sa_id.spi = x->id.spi;
1684 id->sa_id.family = x->props.family;
1685 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001686 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1687 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001688 id->flags = c->data.aevent;
1689
Steffen Klassertd8647b72011-03-08 00:10:27 +00001690 if (x->replay_esn)
1691 NLA_PUT(skb, XFRMA_REPLAY_ESN_VAL,
1692 xfrm_replay_state_esn_len(x->replay_esn),
1693 x->replay_esn);
1694 else
1695 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1696
Thomas Grafc0144be2007-08-22 13:55:43 -07001697 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001698
Thomas Grafc0144be2007-08-22 13:55:43 -07001699 if (id->flags & XFRM_AE_RTHR)
1700 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001701
Thomas Grafc0144be2007-08-22 13:55:43 -07001702 if (id->flags & XFRM_AE_ETHR)
1703 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1704 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001705
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001706 if (xfrm_mark_put(skb, &x->mark))
1707 goto nla_put_failure;
1708
Thomas Graf98250692007-08-22 12:47:26 -07001709 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001710
Thomas Grafc0144be2007-08-22 13:55:43 -07001711nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001712 nlmsg_cancel(skb, nlh);
1713 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001714}
1715
Christoph Hellwig22e70052007-01-02 15:22:30 -08001716static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001717 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001718{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001719 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001720 struct xfrm_state *x;
1721 struct sk_buff *r_skb;
1722 int err;
1723 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001724 u32 mark;
1725 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001726 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001727 struct xfrm_usersa_id *id = &p->sa_id;
1728
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001729 mark = xfrm_mark_get(attrs, &m);
1730
1731 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Steffen Klassertd8647b72011-03-08 00:10:27 +00001732 if (x == NULL)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001733 return -ESRCH;
Steffen Klassertd8647b72011-03-08 00:10:27 +00001734
1735 r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
1736 if (r_skb == NULL) {
1737 xfrm_state_put(x);
1738 return -ENOMEM;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001739 }
1740
1741 /*
1742 * XXX: is this lock really needed - none of the other
1743 * gets lock (the concern is things getting updated
1744 * while we are still reading) - jhs
1745 */
1746 spin_lock_bh(&x->lock);
1747 c.data.aevent = p->flags;
1748 c.seq = nlh->nlmsg_seq;
1749 c.pid = nlh->nlmsg_pid;
1750
1751 if (build_aevent(r_skb, x, &c) < 0)
1752 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001753 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001754 spin_unlock_bh(&x->lock);
1755 xfrm_state_put(x);
1756 return err;
1757}
1758
Christoph Hellwig22e70052007-01-02 15:22:30 -08001759static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001760 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001761{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001762 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001763 struct xfrm_state *x;
1764 struct km_event c;
1765 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001766 u32 mark = 0;
1767 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001768 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001769 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
Steffen Klassertd8647b72011-03-08 00:10:27 +00001770 struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
Thomas Graf5424f322007-08-22 14:01:33 -07001771 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001772
Steffen Klassertd8647b72011-03-08 00:10:27 +00001773 if (!lt && !rp && !re)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001774 return err;
1775
1776 /* pedantic mode - thou shalt sayeth replaceth */
1777 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1778 return err;
1779
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001780 mark = xfrm_mark_get(attrs, &m);
1781
1782 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 -08001783 if (x == NULL)
1784 return -ESRCH;
1785
1786 if (x->km.state != XFRM_STATE_VALID)
1787 goto out;
1788
Steffen Klasserte2b19122011-03-28 19:47:30 +00001789 err = xfrm_replay_verify_len(x->replay_esn, rp);
1790 if (err)
1791 goto out;
1792
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001793 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001794 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001795 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001796
1797 c.event = nlh->nlmsg_type;
1798 c.seq = nlh->nlmsg_seq;
1799 c.pid = nlh->nlmsg_pid;
1800 c.data.aevent = XFRM_AE_CU;
1801 km_state_notify(x, &c);
1802 err = 0;
1803out:
1804 xfrm_state_put(x);
1805 return err;
1806}
1807
Christoph Hellwig22e70052007-01-02 15:22:30 -08001808static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001809 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001811 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001812 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001813 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001814 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001815 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001816
Thomas Graf35a7aa02007-08-22 14:00:40 -07001817 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001818 if (err)
1819 return err;
1820
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001821 audit_info.loginuid = audit_get_loginuid(current);
1822 audit_info.sessionid = audit_get_sessionid(current);
1823 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001824 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001825 if (err) {
1826 if (err == -ESRCH) /* empty table */
1827 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001828 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001829 }
1830
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001831 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001832 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001833 c.seq = nlh->nlmsg_seq;
1834 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001835 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001836 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
1838}
1839
Christoph Hellwig22e70052007-01-02 15:22:30 -08001840static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001841 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001842{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001843 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001844 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001845 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001846 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001847 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001848 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001849 struct xfrm_mark m;
1850 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001851
Thomas Graf35a7aa02007-08-22 14:00:40 -07001852 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001853 if (err)
1854 return err;
1855
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001856 err = verify_policy_dir(p->dir);
1857 if (err)
1858 return err;
1859
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001860 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001861 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001862 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001863 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001864 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001865
Thomas Graf35a7aa02007-08-22 14:00:40 -07001866 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001867 if (err)
1868 return err;
1869
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001870 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001871 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001872 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001873
Paul Moore03e1ad72008-04-12 19:07:52 -07001874 err = security_xfrm_policy_alloc(&ctx, uctx);
1875 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001876 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001877 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001878 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001879 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001880 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001881 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001882 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001883 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001884
Timo Teräsea2dea92010-03-31 00:17:05 +00001885 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001886 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001887
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001888 err = 0;
1889 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001890 uid_t loginuid = audit_get_loginuid(current);
1891 u32 sessionid = audit_get_sessionid(current);
1892 u32 sid;
1893
1894 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001895 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001896 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001897
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001898 } else {
1899 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001900 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001901 }
1902 km_policy_expired(xp, p->dir, up->hard, current->pid);
1903
1904out:
1905 xfrm_pol_put(xp);
1906 return err;
1907}
1908
Christoph Hellwig22e70052007-01-02 15:22:30 -08001909static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001910 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001911{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001912 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001913 struct xfrm_state *x;
1914 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001915 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001916 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001917 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001918 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001919
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001920 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 -08001921
David S. Miller3a765aa2007-02-26 14:52:21 -08001922 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001923 if (x == NULL)
1924 return err;
1925
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001926 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001927 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001928 if (x->km.state != XFRM_STATE_VALID)
1929 goto out;
1930 km_state_expired(x, ue->hard, current->pid);
1931
Joy Latten161a09e2006-11-27 13:11:54 -06001932 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001933 uid_t loginuid = audit_get_loginuid(current);
1934 u32 sessionid = audit_get_sessionid(current);
1935 u32 sid;
1936
1937 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001938 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001939 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001940 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001941 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001942out:
1943 spin_unlock_bh(&x->lock);
1944 xfrm_state_put(x);
1945 return err;
1946}
1947
Christoph Hellwig22e70052007-01-02 15:22:30 -08001948static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001949 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001950{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001951 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001952 struct xfrm_policy *xp;
1953 struct xfrm_user_tmpl *ut;
1954 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001955 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001956 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001957
Thomas Graf7b67c852007-08-22 13:53:52 -07001958 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001959 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001960 int err = -ENOMEM;
1961
1962 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001963 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001964
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001965 xfrm_mark_get(attrs, &mark);
1966
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001967 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001968 if (err)
1969 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001970
1971 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001972 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001973 if (!xp)
1974 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001975
1976 memcpy(&x->id, &ua->id, sizeof(ua->id));
1977 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1978 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001979 xp->mark.m = x->mark.m = mark.m;
1980 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001981 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001982 /* extract the templates and for each call km_key */
1983 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1984 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1985 memcpy(&x->id, &t->id, sizeof(x->id));
1986 x->props.mode = t->mode;
1987 x->props.reqid = t->reqid;
1988 x->props.family = ut->family;
1989 t->aalgos = ua->aalgos;
1990 t->ealgos = ua->ealgos;
1991 t->calgos = ua->calgos;
1992 err = km_query(x, t, xp);
1993
1994 }
1995
1996 kfree(x);
1997 kfree(xp);
1998
1999 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002000
2001bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002002 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08002003free_state:
2004 kfree(x);
2005nomem:
2006 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002007}
2008
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002009#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002010static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002011 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07002012 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002013{
Thomas Graf5424f322007-08-22 14:01:33 -07002014 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002015 struct xfrm_user_migrate *um;
2016 int i, num_migrate;
2017
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002018 if (k != NULL) {
2019 struct xfrm_user_kmaddress *uk;
2020
2021 uk = nla_data(attrs[XFRMA_KMADDRESS]);
2022 memcpy(&k->local, &uk->local, sizeof(k->local));
2023 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
2024 k->family = uk->family;
2025 k->reserved = uk->reserved;
2026 }
2027
Thomas Graf5424f322007-08-22 14:01:33 -07002028 um = nla_data(rt);
2029 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002030
2031 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
2032 return -EINVAL;
2033
2034 for (i = 0; i < num_migrate; i++, um++, ma++) {
2035 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
2036 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
2037 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
2038 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
2039
2040 ma->proto = um->proto;
2041 ma->mode = um->mode;
2042 ma->reqid = um->reqid;
2043
2044 ma->old_family = um->old_family;
2045 ma->new_family = um->new_family;
2046 }
2047
2048 *num = i;
2049 return 0;
2050}
2051
2052static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002053 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002054{
Thomas Graf7b67c852007-08-22 13:53:52 -07002055 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002056 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002057 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002058 u8 type;
2059 int err;
2060 int n = 0;
2061
Thomas Graf35a7aa02007-08-22 14:00:40 -07002062 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07002063 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002064
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002065 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
2066
Thomas Graf5424f322007-08-22 14:01:33 -07002067 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002068 if (err)
2069 return err;
2070
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002071 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072 if (err)
2073 return err;
2074
2075 if (!n)
2076 return 0;
2077
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002078 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002079
2080 return 0;
2081}
2082#else
2083static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07002084 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002085{
2086 return -ENOPROTOOPT;
2087}
2088#endif
2089
2090#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05002091static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002092{
2093 struct xfrm_user_migrate um;
2094
2095 memset(&um, 0, sizeof(um));
2096 um.proto = m->proto;
2097 um.mode = m->mode;
2098 um.reqid = m->reqid;
2099 um.old_family = m->old_family;
2100 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2101 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2102 um.new_family = m->new_family;
2103 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2104 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2105
Thomas Grafc0144be2007-08-22 13:55:43 -07002106 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002107}
2108
David S. Miller183cad12011-02-24 00:28:01 -05002109static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002110{
2111 struct xfrm_user_kmaddress uk;
2112
2113 memset(&uk, 0, sizeof(uk));
2114 uk.family = k->family;
2115 uk.reserved = k->reserved;
2116 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002117 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002118
2119 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2120}
2121
2122static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002123{
2124 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002125 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2126 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2127 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002128}
2129
David S. Miller183cad12011-02-24 00:28:01 -05002130static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2131 int num_migrate, const struct xfrm_kmaddress *k,
2132 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002133{
David S. Miller183cad12011-02-24 00:28:01 -05002134 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002135 struct xfrm_userpolicy_id *pol_id;
2136 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002137 int i;
2138
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002139 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2140 if (nlh == NULL)
2141 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002142
Thomas Graf7b67c852007-08-22 13:53:52 -07002143 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002144 /* copy data from selector, dir, and type to the pol_id */
2145 memset(pol_id, 0, sizeof(*pol_id));
2146 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2147 pol_id->dir = dir;
2148
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002149 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2150 goto nlmsg_failure;
2151
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002152 if (copy_to_user_policy_type(type, skb) < 0)
2153 goto nlmsg_failure;
2154
2155 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2156 if (copy_to_user_migrate(mp, skb) < 0)
2157 goto nlmsg_failure;
2158 }
2159
Thomas Graf98250692007-08-22 12:47:26 -07002160 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002161nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002162 nlmsg_cancel(skb, nlh);
2163 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002164}
2165
David S. Miller183cad12011-02-24 00:28:01 -05002166static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2167 const struct xfrm_migrate *m, int num_migrate,
2168 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002169{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002170 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002171 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002172
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002173 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002174 if (skb == NULL)
2175 return -ENOMEM;
2176
2177 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002178 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002179 BUG();
2180
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002181 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002182}
2183#else
David S. Miller183cad12011-02-24 00:28:01 -05002184static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2185 const struct xfrm_migrate *m, int num_migrate,
2186 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002187{
2188 return -ENOPROTOOPT;
2189}
2190#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002191
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002192#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002193
2194static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002195 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002196 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2197 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2198 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2199 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2200 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2201 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002202 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002203 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002204 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002205 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002206 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002207 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002208 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002209 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2210 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002211 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002212 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002213 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2214 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215};
2216
Thomas Graf492b5582005-05-03 14:26:40 -07002217#undef XMSGSIZE
2218
Thomas Grafcf5cb792007-08-22 13:59:04 -07002219static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002220 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2221 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2222 [XFRMA_LASTUSED] = { .type = NLA_U64},
2223 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002224 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002225 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2226 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2227 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2228 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2229 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2230 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2231 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2232 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2233 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2234 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2235 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2236 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2237 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2238 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002239 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002240 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002241 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Steffen Klassertd8647b72011-03-08 00:10:27 +00002242 [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002243};
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002246 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002248 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002249} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2250 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2251 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2252 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002253 .dump = xfrm_dump_sa,
2254 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002255 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2256 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2257 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002258 .dump = xfrm_dump_policy,
2259 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002260 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002261 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002262 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002263 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2264 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002265 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002266 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2267 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002268 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2269 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002270 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002271 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002272 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273};
2274
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002275static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002277 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002278 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002280 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 type -= XFRM_MSG_BASE;
2287 link = &xfrm_dispatch[type];
2288
2289 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002290 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2291 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Thomas Graf492b5582005-05-03 14:26:40 -07002293 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2294 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002295 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002299 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301
Thomas Graf35a7aa02007-08-22 14:00:40 -07002302 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002303 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002304 if (err < 0)
2305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Thomas Graf5424f322007-08-22 14:01:33 -07002310 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002313static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002315 mutex_lock(&xfrm_cfg_mutex);
2316 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2317 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Thomas Graf7deb2262007-08-22 13:57:39 -07002320static inline size_t xfrm_expire_msgsize(void)
2321{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002322 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2323 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002324}
2325
David S. Miller214e0052011-02-24 00:02:38 -05002326static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct xfrm_user_expire *ue;
2329 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002331 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2332 if (nlh == NULL)
2333 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Thomas Graf7b67c852007-08-22 13:53:52 -07002335 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002337 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002339 if (xfrm_mark_put(skb, &x->mark))
2340 goto nla_put_failure;
2341
Thomas Graf98250692007-08-22 12:47:26 -07002342 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002343
2344nla_put_failure:
2345 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
David S. Miller214e0052011-02-24 00:02:38 -05002348static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002350 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct sk_buff *skb;
2352
Thomas Graf7deb2262007-08-22 13:57:39 -07002353 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (skb == NULL)
2355 return -ENOMEM;
2356
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002357 if (build_expire(skb, x, c) < 0) {
2358 kfree_skb(skb);
2359 return -EMSGSIZE;
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002362 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
David S. Miller214e0052011-02-24 00:02:38 -05002365static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002366{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002367 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002368 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002369
Steffen Klassertd8647b72011-03-08 00:10:27 +00002370 skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002371 if (skb == NULL)
2372 return -ENOMEM;
2373
2374 if (build_aevent(skb, x, c) < 0)
2375 BUG();
2376
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002377 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002378}
2379
David S. Miller214e0052011-02-24 00:02:38 -05002380static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002381{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002382 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002383 struct xfrm_usersa_flush *p;
2384 struct nlmsghdr *nlh;
2385 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002386 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002387
Thomas Graf7deb2262007-08-22 13:57:39 -07002388 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002389 if (skb == NULL)
2390 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002392 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2393 if (nlh == NULL) {
2394 kfree_skb(skb);
2395 return -EMSGSIZE;
2396 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397
Thomas Graf7b67c852007-08-22 13:53:52 -07002398 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002399 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002400
Thomas Graf98250692007-08-22 12:47:26 -07002401 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002402
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002403 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002404}
2405
Thomas Graf7deb2262007-08-22 13:57:39 -07002406static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002407{
Thomas Graf7deb2262007-08-22 13:57:39 -07002408 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002409 if (x->aead)
2410 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002411 if (x->aalg) {
2412 l += nla_total_size(sizeof(struct xfrm_algo) +
2413 (x->aalg->alg_key_len + 7) / 8);
2414 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2415 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002417 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002419 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002420 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002421 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002422 if (x->tfcpad)
2423 l += nla_total_size(sizeof(x->tfcpad));
Steffen Klassertd8647b72011-03-08 00:10:27 +00002424 if (x->replay_esn)
2425 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
Herbert Xu68325d32007-10-09 13:30:57 -07002426 if (x->security)
2427 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2428 x->security->ctx_len);
2429 if (x->coaddr)
2430 l += nla_total_size(sizeof(*x->coaddr));
2431
Herbert Xud26f3982007-11-13 21:47:08 -08002432 /* Must count x->lastused as it may become non-zero behind our back. */
2433 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002434
2435 return l;
2436}
2437
David S. Miller214e0052011-02-24 00:02:38 -05002438static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002439{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002440 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002441 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002442 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002443 struct nlmsghdr *nlh;
2444 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002445 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002446 int headlen;
2447
2448 headlen = sizeof(*p);
2449 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002450 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002451 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002452 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002453 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002454 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002455
Thomas Graf7deb2262007-08-22 13:57:39 -07002456 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002457 if (skb == NULL)
2458 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002459
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002460 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2461 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002462 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002463
Thomas Graf7b67c852007-08-22 13:53:52 -07002464 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002465 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002466 struct nlattr *attr;
2467
Thomas Graf7b67c852007-08-22 13:53:52 -07002468 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002469 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2470 id->spi = x->id.spi;
2471 id->family = x->props.family;
2472 id->proto = x->id.proto;
2473
Thomas Grafc0144be2007-08-22 13:55:43 -07002474 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2475 if (attr == NULL)
2476 goto nla_put_failure;
2477
2478 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002479 }
2480
Herbert Xu68325d32007-10-09 13:30:57 -07002481 if (copy_to_user_state_extra(x, p, skb))
2482 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002483
Thomas Graf98250692007-08-22 12:47:26 -07002484 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002485
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002486 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002487
Thomas Grafc0144be2007-08-22 13:55:43 -07002488nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002489 /* Somebody screwed up with xfrm_sa_len! */
2490 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002491 kfree_skb(skb);
2492 return -1;
2493}
2494
David S. Miller214e0052011-02-24 00:02:38 -05002495static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002496{
2497
2498 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002499 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002500 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002501 case XFRM_MSG_NEWAE:
2502 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002503 case XFRM_MSG_DELSA:
2504 case XFRM_MSG_UPDSA:
2505 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002506 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002507 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002508 return xfrm_notify_sa_flush(c);
2509 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002510 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2511 c->event);
2512 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002513 }
2514
2515 return 0;
2516
2517}
2518
Thomas Graf7deb2262007-08-22 13:57:39 -07002519static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2520 struct xfrm_policy *xp)
2521{
2522 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2523 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002524 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002525 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2526 + userpolicy_type_attrsize();
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2530 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2531 int dir)
2532{
2533 struct xfrm_user_acquire *ua;
2534 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 __u32 seq = xfrm_get_acqseq();
2536
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002537 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2538 if (nlh == NULL)
2539 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Thomas Graf7b67c852007-08-22 13:53:52 -07002541 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 memcpy(&ua->id, &x->id, sizeof(ua->id));
2543 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2544 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2545 copy_to_user_policy(xp, &ua->policy, dir);
2546 ua->aalgos = xt->aalgos;
2547 ua->ealgos = xt->ealgos;
2548 ua->calgos = xt->calgos;
2549 ua->seq = x->km.seq = seq;
2550
2551 if (copy_to_user_tmpl(xp, skb) < 0)
2552 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002553 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002554 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002555 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002556 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002557 if (xfrm_mark_put(skb, &xp->mark))
2558 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Thomas Graf98250692007-08-22 12:47:26 -07002560 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002562nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002564 nlmsg_cancel(skb, nlh);
2565 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2569 struct xfrm_policy *xp, int dir)
2570{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002571 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Thomas Graf7deb2262007-08-22 13:57:39 -07002574 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (skb == NULL)
2576 return -ENOMEM;
2577
2578 if (build_acquire(skb, x, xt, xp, dir) < 0)
2579 BUG();
2580
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002581 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584/* User gives us xfrm_user_policy_info followed by an array of 0
2585 * or more templates.
2586 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002587static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 u8 *data, int len, int *dir)
2589{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002590 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2592 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2593 struct xfrm_policy *xp;
2594 int nr;
2595
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002596 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 case AF_INET:
2598 if (opt != IP_XFRM_POLICY) {
2599 *dir = -EOPNOTSUPP;
2600 return NULL;
2601 }
2602 break;
2603#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2604 case AF_INET6:
2605 if (opt != IPV6_XFRM_POLICY) {
2606 *dir = -EOPNOTSUPP;
2607 return NULL;
2608 }
2609 break;
2610#endif
2611 default:
2612 *dir = -EINVAL;
2613 return NULL;
2614 }
2615
2616 *dir = -EINVAL;
2617
2618 if (len < sizeof(*p) ||
2619 verify_newpolicy_info(p))
2620 return NULL;
2621
2622 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002623 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return NULL;
2625
Herbert Xua4f1bac2005-07-26 15:43:17 -07002626 if (p->dir > XFRM_POLICY_OUT)
2627 return NULL;
2628
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002629 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (xp == NULL) {
2631 *dir = -ENOBUFS;
2632 return NULL;
2633 }
2634
2635 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002636 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 copy_templates(xp, ut, nr);
2638
2639 *dir = p->dir;
2640
2641 return xp;
2642}
2643
Thomas Graf7deb2262007-08-22 13:57:39 -07002644static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2645{
2646 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2647 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2648 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002649 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002650 + userpolicy_type_attrsize();
2651}
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002654 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 struct xfrm_user_polexpire *upe;
2657 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002658 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002660 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2661 if (nlh == NULL)
2662 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Thomas Graf7b67c852007-08-22 13:53:52 -07002664 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 copy_to_user_policy(xp, &upe->pol, dir);
2666 if (copy_to_user_tmpl(xp, skb) < 0)
2667 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002668 if (copy_to_user_sec_ctx(xp, skb))
2669 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002670 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002671 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002672 if (xfrm_mark_put(skb, &xp->mark))
2673 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 upe->hard = !!hard;
2675
Thomas Graf98250692007-08-22 12:47:26 -07002676 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002678nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002680 nlmsg_cancel(skb, nlh);
2681 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
David S. Miller214e0052011-02-24 00:02:38 -05002684static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002686 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Thomas Graf7deb2262007-08-22 13:57:39 -07002689 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if (skb == NULL)
2691 return -ENOMEM;
2692
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002693 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 BUG();
2695
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002696 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
David S. Miller214e0052011-02-24 00:02:38 -05002699static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002700{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002701 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002702 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002703 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002704 struct nlmsghdr *nlh;
2705 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002706 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002707 int headlen;
2708
2709 headlen = sizeof(*p);
2710 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002711 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002712 headlen = sizeof(*id);
2713 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002714 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002715 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002716 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002717
Thomas Graf7deb2262007-08-22 13:57:39 -07002718 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002719 if (skb == NULL)
2720 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002721
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002722 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2723 if (nlh == NULL)
2724 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002725
Thomas Graf7b67c852007-08-22 13:53:52 -07002726 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002727 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002728 struct nlattr *attr;
2729
Thomas Graf7b67c852007-08-22 13:53:52 -07002730 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002731 memset(id, 0, sizeof(*id));
2732 id->dir = dir;
2733 if (c->data.byid)
2734 id->index = xp->index;
2735 else
2736 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2737
Thomas Grafc0144be2007-08-22 13:55:43 -07002738 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2739 if (attr == NULL)
2740 goto nlmsg_failure;
2741
2742 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002743 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002744
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002745 copy_to_user_policy(xp, p, dir);
2746 if (copy_to_user_tmpl(xp, skb) < 0)
2747 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002748 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002749 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002750
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002751 if (xfrm_mark_put(skb, &xp->mark))
2752 goto nla_put_failure;
2753
Thomas Graf98250692007-08-22 12:47:26 -07002754 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002755
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002756 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002757
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002758nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002759nlmsg_failure:
2760 kfree_skb(skb);
2761 return -1;
2762}
2763
David S. Miller214e0052011-02-24 00:02:38 -05002764static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002765{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002766 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002767 struct nlmsghdr *nlh;
2768 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002769
Thomas Graf7deb2262007-08-22 13:57:39 -07002770 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002771 if (skb == NULL)
2772 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002773
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002774 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2775 if (nlh == NULL)
2776 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002777 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2778 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002779
Thomas Graf98250692007-08-22 12:47:26 -07002780 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002781
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002782 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002783
2784nlmsg_failure:
2785 kfree_skb(skb);
2786 return -1;
2787}
2788
David S. Miller214e0052011-02-24 00:02:38 -05002789static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002790{
2791
2792 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002793 case XFRM_MSG_NEWPOLICY:
2794 case XFRM_MSG_UPDPOLICY:
2795 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002796 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002797 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002798 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002799 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002800 return xfrm_exp_policy_notify(xp, dir, c);
2801 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002802 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2803 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002804 }
2805
2806 return 0;
2807
2808}
2809
Thomas Graf7deb2262007-08-22 13:57:39 -07002810static inline size_t xfrm_report_msgsize(void)
2811{
2812 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2813}
2814
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002815static int build_report(struct sk_buff *skb, u8 proto,
2816 struct xfrm_selector *sel, xfrm_address_t *addr)
2817{
2818 struct xfrm_user_report *ur;
2819 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002820
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002821 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2822 if (nlh == NULL)
2823 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002824
Thomas Graf7b67c852007-08-22 13:53:52 -07002825 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002826 ur->proto = proto;
2827 memcpy(&ur->sel, sel, sizeof(ur->sel));
2828
2829 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002830 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002831
Thomas Graf98250692007-08-22 12:47:26 -07002832 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002833
Thomas Grafc0144be2007-08-22 13:55:43 -07002834nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002835 nlmsg_cancel(skb, nlh);
2836 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002837}
2838
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002839static int xfrm_send_report(struct net *net, u8 proto,
2840 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002841{
2842 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002843
Thomas Graf7deb2262007-08-22 13:57:39 -07002844 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002845 if (skb == NULL)
2846 return -ENOMEM;
2847
2848 if (build_report(skb, proto, sel, addr) < 0)
2849 BUG();
2850
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002851 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002852}
2853
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002854static inline size_t xfrm_mapping_msgsize(void)
2855{
2856 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2857}
2858
2859static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2860 xfrm_address_t *new_saddr, __be16 new_sport)
2861{
2862 struct xfrm_user_mapping *um;
2863 struct nlmsghdr *nlh;
2864
2865 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2866 if (nlh == NULL)
2867 return -EMSGSIZE;
2868
2869 um = nlmsg_data(nlh);
2870
2871 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2872 um->id.spi = x->id.spi;
2873 um->id.family = x->props.family;
2874 um->id.proto = x->id.proto;
2875 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2876 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2877 um->new_sport = new_sport;
2878 um->old_sport = x->encap->encap_sport;
2879 um->reqid = x->props.reqid;
2880
2881 return nlmsg_end(skb, nlh);
2882}
2883
2884static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2885 __be16 sport)
2886{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002887 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002888 struct sk_buff *skb;
2889
2890 if (x->id.proto != IPPROTO_ESP)
2891 return -EINVAL;
2892
2893 if (!x->encap)
2894 return -EINVAL;
2895
2896 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2897 if (skb == NULL)
2898 return -ENOMEM;
2899
2900 if (build_mapping(skb, x, ipaddr, sport) < 0)
2901 BUG();
2902
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002903 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002904}
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906static struct xfrm_mgr netlink_mgr = {
2907 .id = "netlink",
2908 .notify = xfrm_send_state_notify,
2909 .acquire = xfrm_send_acquire,
2910 .compile_policy = xfrm_compile_policy,
2911 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002912 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002913 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002914 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915};
2916
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002917static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Patrick McHardybe336902006-03-20 22:40:54 -08002919 struct sock *nlsk;
2920
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002921 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002922 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002923 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002925 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002926 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return 0;
2928}
2929
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002930static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002931{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002932 struct net *net;
2933 list_for_each_entry(net, net_exit_list, exit_list)
2934 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2935 synchronize_net();
2936 list_for_each_entry(net, net_exit_list, exit_list)
2937 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002938}
2939
2940static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002941 .init = xfrm_user_net_init,
2942 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002943};
2944
2945static int __init xfrm_user_init(void)
2946{
2947 int rv;
2948
2949 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2950
2951 rv = register_pernet_subsys(&xfrm_user_net_ops);
2952 if (rv < 0)
2953 return rv;
2954 rv = xfrm_register_km(&netlink_mgr);
2955 if (rv < 0)
2956 unregister_pernet_subsys(&xfrm_user_net_ops);
2957 return rv;
2958}
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960static void __exit xfrm_user_exit(void)
2961{
2962 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002963 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
2966module_init(xfrm_user_init);
2967module_exit(xfrm_user_exit);
2968MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002969MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002970