blob: f7b3c857c9893f91bda07e9a31380c8f5f98d177 [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
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700124 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int err;
127
128 err = -EINVAL;
129 switch (p->family) {
130 case AF_INET:
131 break;
132
133 case AF_INET6:
134#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
135 break;
136#else
137 err = -EAFNOSUPPORT;
138 goto out;
139#endif
140
141 default:
142 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 err = -EINVAL;
146 switch (p->id.proto) {
147 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000148 if ((!attrs[XFRMA_ALG_AUTH] &&
149 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800150 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700151 attrs[XFRMA_ALG_CRYPT] ||
Martin Willi35d28562010-12-08 04:37:49 +0000152 attrs[XFRMA_ALG_COMP] ||
153 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto out;
155 break;
156
157 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800158 if (attrs[XFRMA_ALG_COMP])
159 goto out;
160 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000161 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800162 !attrs[XFRMA_ALG_CRYPT] &&
163 !attrs[XFRMA_ALG_AEAD])
164 goto out;
165 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000166 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800167 attrs[XFRMA_ALG_CRYPT]) &&
168 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000170 if (attrs[XFRMA_TFCPAD] &&
171 p->mode != XFRM_MODE_TUNNEL)
172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174
175 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700176 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800177 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700178 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000179 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000180 attrs[XFRMA_ALG_CRYPT] ||
181 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto out;
183 break;
184
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700185#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
186 case IPPROTO_DSTOPTS:
187 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700188 if (attrs[XFRMA_ALG_COMP] ||
189 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000190 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800191 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700192 attrs[XFRMA_ALG_CRYPT] ||
193 attrs[XFRMA_ENCAP] ||
194 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000195 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700196 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700197 goto out;
198 break;
199#endif
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 default:
202 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Herbert Xu1a6509d2008-01-28 19:37:29 -0800205 if ((err = verify_aead(attrs)))
206 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000207 if ((err = verify_auth_trunc(attrs)))
208 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700211 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700213 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700215 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800216 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 err = -EINVAL;
219 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700220 case XFRM_MODE_TRANSPORT:
221 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700222 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700223 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225
226 default:
227 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 err = 0;
231
232out:
233 return err;
234}
235
236static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
David S. Miller6f2f19e2011-02-27 23:04:45 -0800237 struct xfrm_algo_desc *(*get_byname)(const char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700238 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct xfrm_algo *p, *ualg;
241 struct xfrm_algo_desc *algo;
242
243 if (!rta)
244 return 0;
245
Thomas Graf5424f322007-08-22 14:01:33 -0700246 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 algo = get_byname(ualg->alg_name, 1);
249 if (!algo)
250 return -ENOSYS;
251 *props = algo->desc.sadb_alg_id;
252
Eric Dumazet0f99be02008-01-08 23:39:06 -0800253 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!p)
255 return -ENOMEM;
256
Herbert Xu04ff1262006-08-13 08:50:00 +1000257 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *algpp = p;
259 return 0;
260}
261
Martin Willi4447bb32009-11-25 00:29:52 +0000262static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
263 struct nlattr *rta)
264{
265 struct xfrm_algo *ualg;
266 struct xfrm_algo_auth *p;
267 struct xfrm_algo_desc *algo;
268
269 if (!rta)
270 return 0;
271
272 ualg = nla_data(rta);
273
274 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
275 if (!algo)
276 return -ENOSYS;
277 *props = algo->desc.sadb_alg_id;
278
279 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
280 if (!p)
281 return -ENOMEM;
282
283 strcpy(p->alg_name, algo->name);
284 p->alg_key_len = ualg->alg_key_len;
285 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
286 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
287
288 *algpp = p;
289 return 0;
290}
291
292static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
293 struct nlattr *rta)
294{
295 struct xfrm_algo_auth *p, *ualg;
296 struct xfrm_algo_desc *algo;
297
298 if (!rta)
299 return 0;
300
301 ualg = nla_data(rta);
302
303 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
304 if (!algo)
305 return -ENOSYS;
Nicolas Dichtelfa6dd8a2011-01-11 08:04:12 +0000306 if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
307 ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
Martin Willi4447bb32009-11-25 00:29:52 +0000308 return -EINVAL;
309 *props = algo->desc.sadb_alg_id;
310
311 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
312 if (!p)
313 return -ENOMEM;
314
315 strcpy(p->alg_name, algo->name);
316 if (!p->alg_trunc_len)
317 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
318
319 *algpp = p;
320 return 0;
321}
322
Herbert Xu1a6509d2008-01-28 19:37:29 -0800323static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
324 struct nlattr *rta)
325{
326 struct xfrm_algo_aead *p, *ualg;
327 struct xfrm_algo_desc *algo;
328
329 if (!rta)
330 return 0;
331
332 ualg = nla_data(rta);
333
334 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
335 if (!algo)
336 return -ENOSYS;
337 *props = algo->desc.sadb_alg_id;
338
339 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
340 if (!p)
341 return -ENOMEM;
342
343 strcpy(p->alg_name, algo->name);
344 *algpp = p;
345 return 0;
346}
347
Joy Latten661697f2007-04-13 16:14:35 -0700348static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800349{
Trent Jaegerdf718372005-12-13 23:12:27 -0800350 int len = 0;
351
352 if (xfrm_ctx) {
353 len += sizeof(struct xfrm_user_sec_ctx);
354 len += xfrm_ctx->ctx_len;
355 }
356 return len;
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
360{
361 memcpy(&x->id, &p->id, sizeof(x->id));
362 memcpy(&x->sel, &p->sel, sizeof(x->sel));
363 memcpy(&x->lft, &p->lft, sizeof(x->lft));
364 x->props.mode = p->mode;
365 x->props.replay_window = p->replay_window;
366 x->props.reqid = p->reqid;
367 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700368 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700370
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700371 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700372 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800375/*
376 * someday when pfkey also has support, we could have the code
377 * somehow made shareable and move it to xfrm_state.c - JHS
378 *
379*/
Thomas Graf5424f322007-08-22 14:01:33 -0700380static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800381{
Thomas Graf5424f322007-08-22 14:01:33 -0700382 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
383 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
384 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
385 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800386
387 if (rp) {
388 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700389 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800390 memcpy(&x->replay, replay, sizeof(*replay));
391 memcpy(&x->preplay, replay, sizeof(*replay));
392 }
393
394 if (lt) {
395 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700396 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800397 x->curlft.bytes = ltime->bytes;
398 x->curlft.packets = ltime->packets;
399 x->curlft.add_time = ltime->add_time;
400 x->curlft.use_time = ltime->use_time;
401 }
402
Thomas Grafcf5cb792007-08-22 13:59:04 -0700403 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700404 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800405
Thomas Grafcf5cb792007-08-22 13:59:04 -0700406 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700407 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800408}
409
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800410static struct xfrm_state *xfrm_state_construct(struct net *net,
411 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700412 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int *errp)
414{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800415 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int err = -ENOMEM;
417
418 if (!x)
419 goto error_no_put;
420
421 copy_from_user_state(x, p);
422
Herbert Xu1a6509d2008-01-28 19:37:29 -0800423 if ((err = attach_aead(&x->aead, &x->props.ealgo,
424 attrs[XFRMA_ALG_AEAD])))
425 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000426 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
427 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000429 if (!x->props.aalgo) {
430 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
431 attrs[XFRMA_ALG_AUTH])))
432 goto error;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
435 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700436 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto error;
438 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
439 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700440 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700442
443 if (attrs[XFRMA_ENCAP]) {
444 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
445 sizeof(*x->encap), GFP_KERNEL);
446 if (x->encap == NULL)
447 goto error;
448 }
449
Martin Willi35d28562010-12-08 04:37:49 +0000450 if (attrs[XFRMA_TFCPAD])
451 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
452
Thomas Graffd211502007-09-06 03:28:08 -0700453 if (attrs[XFRMA_COADDR]) {
454 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
455 sizeof(*x->coaddr), GFP_KERNEL);
456 if (x->coaddr == NULL)
457 goto error;
458 }
459
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000460 xfrm_mark_get(attrs, &x->mark);
461
Herbert Xu72cb6962005-06-20 13:18:08 -0700462 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (err)
464 goto error;
465
Thomas Graffd211502007-09-06 03:28:08 -0700466 if (attrs[XFRMA_SEC_CTX] &&
467 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800468 goto error;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800471 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800472 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800473 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800474 x->preplay.bitmap = 0;
475 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
476 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
477
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000478 if ((err = xfrm_init_replay(x)))
479 goto error;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800480
Steffen Klassert9fdc4882011-03-08 00:08:32 +0000481 /* override default values from above */
Thomas Graf5424f322007-08-22 14:01:33 -0700482 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return x;
485
486error:
487 x->km.state = XFRM_STATE_DEAD;
488 xfrm_state_put(x);
489error_no_put:
490 *errp = err;
491 return NULL;
492}
493
Christoph Hellwig22e70052007-01-02 15:22:30 -0800494static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700495 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800497 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700498 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct xfrm_state *x;
500 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700501 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800502 uid_t loginuid = audit_get_loginuid(current);
503 u32 sessionid = audit_get_sessionid(current);
504 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Thomas Graf35a7aa02007-08-22 14:00:40 -0700506 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (err)
508 return err;
509
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800510 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!x)
512 return err;
513
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700514 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
516 err = xfrm_state_add(x);
517 else
518 err = xfrm_state_update(x);
519
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800520 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400521 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (err < 0) {
524 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800525 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700526 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700529 c.seq = nlh->nlmsg_seq;
530 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700531 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700532
533 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700534out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700535 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return err;
537}
538
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800539static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
540 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700541 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700542 int *errp)
543{
544 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000545 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700546 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000547 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700548
549 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
550 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000551 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700552 } else {
553 xfrm_address_t *saddr = NULL;
554
Thomas Graf35a7aa02007-08-22 14:00:40 -0700555 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700556 if (!saddr) {
557 err = -EINVAL;
558 goto out;
559 }
560
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800561 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000562 x = xfrm_state_lookup_byaddr(net, mark,
563 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800564 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700565 }
566
567 out:
568 if (!x && errp)
569 *errp = err;
570 return x;
571}
572
Christoph Hellwig22e70052007-01-02 15:22:30 -0800573static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700574 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800576 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700578 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700579 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700580 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800581 uid_t loginuid = audit_get_loginuid(current);
582 u32 sessionid = audit_get_sessionid(current);
583 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800585 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
David S. Miller6f68dc32006-06-08 23:58:52 -0700589 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700590 goto out;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700593 err = -EPERM;
594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700597 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600598
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700599 if (err < 0)
600 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700601
602 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 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700607out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800608 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400609 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700610 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
615{
616 memcpy(&p->id, &x->id, sizeof(p->id));
617 memcpy(&p->sel, &x->sel, sizeof(p->sel));
618 memcpy(&p->lft, &x->lft, sizeof(p->lft));
619 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
620 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700621 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 p->mode = x->props.mode;
623 p->replay_window = x->props.replay_window;
624 p->reqid = x->props.reqid;
625 p->family = x->props.family;
626 p->flags = x->props.flags;
627 p->seq = x->km.seq;
628}
629
630struct xfrm_dump_info {
631 struct sk_buff *in_skb;
632 struct sk_buff *out_skb;
633 u32 nlmsg_seq;
634 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
Thomas Grafc0144be2007-08-22 13:55:43 -0700637static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
638{
Thomas Grafc0144be2007-08-22 13:55:43 -0700639 struct xfrm_user_sec_ctx *uctx;
640 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700641 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700642
643 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
644 if (attr == NULL)
645 return -EMSGSIZE;
646
647 uctx = nla_data(attr);
648 uctx->exttype = XFRMA_SEC_CTX;
649 uctx->len = ctx_size;
650 uctx->ctx_doi = s->ctx_doi;
651 uctx->ctx_alg = s->ctx_alg;
652 uctx->ctx_len = s->ctx_len;
653 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
654
655 return 0;
656}
657
Martin Willi4447bb32009-11-25 00:29:52 +0000658static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
659{
660 struct xfrm_algo *algo;
661 struct nlattr *nla;
662
663 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
664 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
665 if (!nla)
666 return -EMSGSIZE;
667
668 algo = nla_data(nla);
669 strcpy(algo->alg_name, auth->alg_name);
670 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
671 algo->alg_key_len = auth->alg_key_len;
672
673 return 0;
674}
675
Herbert Xu68325d32007-10-09 13:30:57 -0700676/* Don't change this without updating xfrm_sa_len! */
677static int copy_to_user_state_extra(struct xfrm_state *x,
678 struct xfrm_usersa_info *p,
679 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 copy_to_user_state(x, p);
682
Herbert Xu050f0092007-10-09 13:31:47 -0700683 if (x->coaddr)
684 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
685
686 if (x->lastused)
687 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700688
Herbert Xu1a6509d2008-01-28 19:37:29 -0800689 if (x->aead)
690 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000691 if (x->aalg) {
692 if (copy_to_user_auth(x->aalg, skb))
693 goto nla_put_failure;
694
695 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
696 xfrm_alg_auth_len(x->aalg), x->aalg);
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800699 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700701 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700704 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Martin Willi35d28562010-12-08 04:37:49 +0000706 if (x->tfcpad)
707 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
708
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000709 if (xfrm_mark_put(skb, &x->mark))
710 goto nla_put_failure;
711
Thomas Grafc0144be2007-08-22 13:55:43 -0700712 if (x->security && copy_sec_ctx(x->security, skb) < 0)
713 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700714
Herbert Xu68325d32007-10-09 13:30:57 -0700715 return 0;
716
717nla_put_failure:
718 return -EMSGSIZE;
719}
720
721static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
722{
723 struct xfrm_dump_info *sp = ptr;
724 struct sk_buff *in_skb = sp->in_skb;
725 struct sk_buff *skb = sp->out_skb;
726 struct xfrm_usersa_info *p;
727 struct nlmsghdr *nlh;
728 int err;
729
Herbert Xu68325d32007-10-09 13:30:57 -0700730 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
731 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
732 if (nlh == NULL)
733 return -EMSGSIZE;
734
735 p = nlmsg_data(nlh);
736
737 err = copy_to_user_state_extra(x, p, skb);
738 if (err)
739 goto nla_put_failure;
740
Thomas Graf98250692007-08-22 12:47:26 -0700741 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
743
Thomas Grafc0144be2007-08-22 13:55:43 -0700744nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700745 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Timo Teras4c563f72008-02-28 21:31:08 -0800749static int xfrm_dump_sa_done(struct netlink_callback *cb)
750{
751 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
752 xfrm_state_walk_done(walk);
753 return 0;
754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
757{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800758 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800759 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct xfrm_dump_info info;
761
Timo Teras4c563f72008-02-28 21:31:08 -0800762 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
763 sizeof(cb->args) - sizeof(cb->args[0]));
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 info.in_skb = cb->skb;
766 info.out_skb = skb;
767 info.nlmsg_seq = cb->nlh->nlmsg_seq;
768 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800769
770 if (!cb->args[0]) {
771 cb->args[0] = 1;
772 xfrm_state_walk_init(walk, 0);
773 }
774
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800775 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 return skb->len;
778}
779
780static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
781 struct xfrm_state *x, u32 seq)
782{
783 struct xfrm_dump_info info;
784 struct sk_buff *skb;
785
Thomas Graf7deb2262007-08-22 13:57:39 -0700786 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!skb)
788 return ERR_PTR(-ENOMEM);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 info.in_skb = in_skb;
791 info.out_skb = skb;
792 info.nlmsg_seq = seq;
793 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (dump_one_state(x, 0, &info)) {
796 kfree_skb(skb);
797 return NULL;
798 }
799
800 return skb;
801}
802
Thomas Graf7deb2262007-08-22 13:57:39 -0700803static inline size_t xfrm_spdinfo_msgsize(void)
804{
805 return NLMSG_ALIGN(4)
806 + nla_total_size(sizeof(struct xfrmu_spdinfo))
807 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
808}
809
Alexey Dobriyane0710412010-01-23 13:37:10 +0000810static int build_spdinfo(struct sk_buff *skb, struct net *net,
811 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700812{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700813 struct xfrmk_spdinfo si;
814 struct xfrmu_spdinfo spc;
815 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700816 struct nlmsghdr *nlh;
817 u32 *f;
818
819 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
820 if (nlh == NULL) /* shouldnt really happen ... */
821 return -EMSGSIZE;
822
823 f = nlmsg_data(nlh);
824 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000825 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700826 spc.incnt = si.incnt;
827 spc.outcnt = si.outcnt;
828 spc.fwdcnt = si.fwdcnt;
829 spc.inscnt = si.inscnt;
830 spc.outscnt = si.outscnt;
831 spc.fwdscnt = si.fwdscnt;
832 sph.spdhcnt = si.spdhcnt;
833 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700834
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700835 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
836 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700837
838 return nlmsg_end(skb, nlh);
839
840nla_put_failure:
841 nlmsg_cancel(skb, nlh);
842 return -EMSGSIZE;
843}
844
845static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700846 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700847{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800848 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700849 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700850 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700851 u32 spid = NETLINK_CB(skb).pid;
852 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700853
Thomas Graf7deb2262007-08-22 13:57:39 -0700854 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700855 if (r_skb == NULL)
856 return -ENOMEM;
857
Alexey Dobriyane0710412010-01-23 13:37:10 +0000858 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700859 BUG();
860
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800861 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700862}
863
Thomas Graf7deb2262007-08-22 13:57:39 -0700864static inline size_t xfrm_sadinfo_msgsize(void)
865{
866 return NLMSG_ALIGN(4)
867 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
868 + nla_total_size(4); /* XFRMA_SAD_CNT */
869}
870
Alexey Dobriyane0710412010-01-23 13:37:10 +0000871static int build_sadinfo(struct sk_buff *skb, struct net *net,
872 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700873{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700874 struct xfrmk_sadinfo si;
875 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700876 struct nlmsghdr *nlh;
877 u32 *f;
878
879 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
880 if (nlh == NULL) /* shouldnt really happen ... */
881 return -EMSGSIZE;
882
883 f = nlmsg_data(nlh);
884 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000885 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700886
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700887 sh.sadhmcnt = si.sadhmcnt;
888 sh.sadhcnt = si.sadhcnt;
889
890 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
891 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700892
893 return nlmsg_end(skb, nlh);
894
895nla_put_failure:
896 nlmsg_cancel(skb, nlh);
897 return -EMSGSIZE;
898}
899
900static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700901 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700902{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800903 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700904 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700905 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700906 u32 spid = NETLINK_CB(skb).pid;
907 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700908
Thomas Graf7deb2262007-08-22 13:57:39 -0700909 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700910 if (r_skb == NULL)
911 return -ENOMEM;
912
Alexey Dobriyane0710412010-01-23 13:37:10 +0000913 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700914 BUG();
915
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800916 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700917}
918
Christoph Hellwig22e70052007-01-02 15:22:30 -0800919static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700920 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800922 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700923 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct xfrm_state *x;
925 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700926 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800928 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (x == NULL)
930 goto out_noput;
931
932 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
933 if (IS_ERR(resp_skb)) {
934 err = PTR_ERR(resp_skb);
935 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800936 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 xfrm_state_put(x);
939out_noput:
940 return err;
941}
942
943static int verify_userspi_info(struct xfrm_userspi_info *p)
944{
945 switch (p->info.id.proto) {
946 case IPPROTO_AH:
947 case IPPROTO_ESP:
948 break;
949
950 case IPPROTO_COMP:
951 /* IPCOMP spi is 16-bits. */
952 if (p->max >= 0x10000)
953 return -EINVAL;
954 break;
955
956 default:
957 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (p->min > p->max)
961 return -EINVAL;
962
963 return 0;
964}
965
Christoph Hellwig22e70052007-01-02 15:22:30 -0800966static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700967 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800969 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct xfrm_state *x;
971 struct xfrm_userspi_info *p;
972 struct sk_buff *resp_skb;
973 xfrm_address_t *daddr;
974 int family;
975 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000976 u32 mark;
977 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Thomas Graf7b67c852007-08-22 13:53:52 -0700979 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 err = verify_userspi_info(p);
981 if (err)
982 goto out_noput;
983
984 family = p->info.family;
985 daddr = &p->info.id.daddr;
986
987 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000988
989 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000991 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
993 xfrm_state_put(x);
994 x = NULL;
995 }
996 }
997
998 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000999 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 p->info.id.proto, daddr,
1001 &p->info.saddr, 1,
1002 family);
1003 err = -ENOENT;
1004 if (x == NULL)
1005 goto out_noput;
1006
Herbert Xu658b2192007-10-09 13:29:52 -07001007 err = xfrm_alloc_spi(x, p->min, p->max);
1008 if (err)
1009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Herbert Xu658b2192007-10-09 13:29:52 -07001011 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (IS_ERR(resp_skb)) {
1013 err = PTR_ERR(resp_skb);
1014 goto out;
1015 }
1016
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001017 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019out:
1020 xfrm_state_put(x);
1021out_noput:
1022 return err;
1023}
1024
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001025static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 switch (dir) {
1028 case XFRM_POLICY_IN:
1029 case XFRM_POLICY_OUT:
1030 case XFRM_POLICY_FWD:
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 return 0;
1038}
1039
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001040static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001041{
1042 switch (type) {
1043 case XFRM_POLICY_TYPE_MAIN:
1044#ifdef CONFIG_XFRM_SUB_POLICY
1045 case XFRM_POLICY_TYPE_SUB:
1046#endif
1047 break;
1048
1049 default:
1050 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001051 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001052
1053 return 0;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1057{
1058 switch (p->share) {
1059 case XFRM_SHARE_ANY:
1060 case XFRM_SHARE_SESSION:
1061 case XFRM_SHARE_USER:
1062 case XFRM_SHARE_UNIQUE:
1063 break;
1064
1065 default:
1066 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 switch (p->action) {
1070 case XFRM_POLICY_ALLOW:
1071 case XFRM_POLICY_BLOCK:
1072 break;
1073
1074 default:
1075 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 switch (p->sel.family) {
1079 case AF_INET:
1080 break;
1081
1082 case AF_INET6:
1083#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1084 break;
1085#else
1086 return -EAFNOSUPPORT;
1087#endif
1088
1089 default:
1090 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 return verify_policy_dir(p->dir);
1094}
1095
Thomas Graf5424f322007-08-22 14:01:33 -07001096static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001097{
Thomas Graf5424f322007-08-22 14:01:33 -07001098 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001099 struct xfrm_user_sec_ctx *uctx;
1100
1101 if (!rt)
1102 return 0;
1103
Thomas Graf5424f322007-08-22 14:01:33 -07001104 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001105 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1109 int nr)
1110{
1111 int i;
1112
1113 xp->xfrm_nr = nr;
1114 for (i = 0; i < nr; i++, ut++) {
1115 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1116
1117 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1118 memcpy(&t->saddr, &ut->saddr,
1119 sizeof(xfrm_address_t));
1120 t->reqid = ut->reqid;
1121 t->mode = ut->mode;
1122 t->share = ut->share;
1123 t->optional = ut->optional;
1124 t->aalgos = ut->aalgos;
1125 t->ealgos = ut->ealgos;
1126 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001127 /* If all masks are ~0, then we allow all algorithms. */
1128 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001129 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131}
1132
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001133static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1134{
1135 int i;
1136
1137 if (nr > XFRM_MAX_DEPTH)
1138 return -EINVAL;
1139
1140 for (i = 0; i < nr; i++) {
1141 /* We never validated the ut->family value, so many
1142 * applications simply leave it at zero. The check was
1143 * never made and ut->family was ignored because all
1144 * templates could be assumed to have the same family as
1145 * the policy itself. Now that we will have ipv4-in-ipv6
1146 * and ipv6-in-ipv4 tunnels, this is no longer true.
1147 */
1148 if (!ut[i].family)
1149 ut[i].family = family;
1150
1151 switch (ut[i].family) {
1152 case AF_INET:
1153 break;
1154#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1155 case AF_INET6:
1156 break;
1157#endif
1158 default:
1159 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001160 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001161 }
1162
1163 return 0;
1164}
1165
Thomas Graf5424f322007-08-22 14:01:33 -07001166static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Thomas Graf5424f322007-08-22 14:01:33 -07001168 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 if (!rt) {
1171 pol->xfrm_nr = 0;
1172 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001173 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1174 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001175 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001177 err = validate_tmpl(nr, utmpl, pol->family);
1178 if (err)
1179 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Thomas Graf5424f322007-08-22 14:01:33 -07001181 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 return 0;
1184}
1185
Thomas Graf5424f322007-08-22 14:01:33 -07001186static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001187{
Thomas Graf5424f322007-08-22 14:01:33 -07001188 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001189 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001190 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001191 int err;
1192
1193 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001194 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001195 type = upt->type;
1196 }
1197
1198 err = verify_policy_type(type);
1199 if (err)
1200 return err;
1201
1202 *tp = type;
1203 return 0;
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1207{
1208 xp->priority = p->priority;
1209 xp->index = p->index;
1210 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1211 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1212 xp->action = p->action;
1213 xp->flags = p->flags;
1214 xp->family = p->sel.family;
1215 /* XXX xp->share = p->share; */
1216}
1217
1218static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1219{
1220 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1221 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1222 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1223 p->priority = xp->priority;
1224 p->index = xp->index;
1225 p->sel.family = xp->family;
1226 p->dir = dir;
1227 p->action = xp->action;
1228 p->flags = xp->flags;
1229 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1230}
1231
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001232static 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 -07001233{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001234 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err;
1236
1237 if (!xp) {
1238 *errp = -ENOMEM;
1239 return NULL;
1240 }
1241
1242 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001243
Thomas Graf35a7aa02007-08-22 14:00:40 -07001244 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001245 if (err)
1246 goto error;
1247
Thomas Graf35a7aa02007-08-22 14:00:40 -07001248 if (!(err = copy_from_user_tmpl(xp, attrs)))
1249 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001250 if (err)
1251 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001253 xfrm_mark_get(attrs, &xp->mark);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001256 error:
1257 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001258 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001259 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001260 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Christoph Hellwig22e70052007-01-02 15:22:30 -08001263static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001264 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001266 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001267 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001269 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int err;
1271 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001272 uid_t loginuid = audit_get_loginuid(current);
1273 u32 sessionid = audit_get_sessionid(current);
1274 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 err = verify_newpolicy_info(p);
1277 if (err)
1278 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001279 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001280 if (err)
1281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001283 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (!xp)
1285 return err;
1286
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001287 /* shouldnt excl be based on nlh flags??
1288 * Aha! this is anti-netlink really i.e more pfkey derived
1289 * in netlink excl is a flag and you wouldnt need
1290 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1292 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001293 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001294 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001297 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 kfree(xp);
1299 return err;
1300 }
1301
Herbert Xuf60f6b82005-06-18 22:44:37 -07001302 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001303 c.seq = nlh->nlmsg_seq;
1304 c.pid = nlh->nlmsg_pid;
1305 km_policy_notify(xp, p->dir, &c);
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 xfrm_pol_put(xp);
1308
1309 return 0;
1310}
1311
1312static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1313{
1314 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1315 int i;
1316
1317 if (xp->xfrm_nr == 0)
1318 return 0;
1319
1320 for (i = 0; i < xp->xfrm_nr; i++) {
1321 struct xfrm_user_tmpl *up = &vec[i];
1322 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1323
1324 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001325 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1327 up->reqid = kp->reqid;
1328 up->mode = kp->mode;
1329 up->share = kp->share;
1330 up->optional = kp->optional;
1331 up->aalgos = kp->aalgos;
1332 up->ealgos = kp->ealgos;
1333 up->calgos = kp->calgos;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Thomas Grafc0144be2007-08-22 13:55:43 -07001336 return nla_put(skb, XFRMA_TMPL,
1337 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001338}
1339
Serge Hallyn0d681622006-07-24 23:30:44 -07001340static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1341{
1342 if (x->security) {
1343 return copy_sec_ctx(x->security, skb);
1344 }
1345 return 0;
1346}
1347
1348static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1349{
1350 if (xp->security) {
1351 return copy_sec_ctx(xp->security, skb);
1352 }
1353 return 0;
1354}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001355static inline size_t userpolicy_type_attrsize(void)
1356{
1357#ifdef CONFIG_XFRM_SUB_POLICY
1358 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1359#else
1360 return 0;
1361#endif
1362}
Serge Hallyn0d681622006-07-24 23:30:44 -07001363
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001364#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001365static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001366{
Thomas Grafc0144be2007-08-22 13:55:43 -07001367 struct xfrm_userpolicy_type upt = {
1368 .type = type,
1369 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001370
Thomas Grafc0144be2007-08-22 13:55:43 -07001371 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001372}
1373
1374#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001375static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001376{
1377 return 0;
1378}
1379#endif
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1382{
1383 struct xfrm_dump_info *sp = ptr;
1384 struct xfrm_userpolicy_info *p;
1385 struct sk_buff *in_skb = sp->in_skb;
1386 struct sk_buff *skb = sp->out_skb;
1387 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001389 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1390 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1391 if (nlh == NULL)
1392 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Thomas Graf7b67c852007-08-22 13:53:52 -07001394 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 copy_to_user_policy(xp, p, dir);
1396 if (copy_to_user_tmpl(xp, skb) < 0)
1397 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001398 if (copy_to_user_sec_ctx(xp, skb))
1399 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001400 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001401 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001402 if (xfrm_mark_put(skb, &xp->mark))
1403 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Thomas Graf98250692007-08-22 12:47:26 -07001405 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return 0;
1407
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001408nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001410 nlmsg_cancel(skb, nlh);
1411 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Timo Teras4c563f72008-02-28 21:31:08 -08001414static int xfrm_dump_policy_done(struct netlink_callback *cb)
1415{
1416 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1417
1418 xfrm_policy_walk_done(walk);
1419 return 0;
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1423{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001424 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001425 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct xfrm_dump_info info;
1427
Timo Teras4c563f72008-02-28 21:31:08 -08001428 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1429 sizeof(cb->args) - sizeof(cb->args[0]));
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 info.in_skb = cb->skb;
1432 info.out_skb = skb;
1433 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1434 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001435
1436 if (!cb->args[0]) {
1437 cb->args[0] = 1;
1438 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1439 }
1440
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001441 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 return skb->len;
1444}
1445
1446static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1447 struct xfrm_policy *xp,
1448 int dir, u32 seq)
1449{
1450 struct xfrm_dump_info info;
1451 struct sk_buff *skb;
1452
Thomas Graf7deb2262007-08-22 13:57:39 -07001453 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!skb)
1455 return ERR_PTR(-ENOMEM);
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 info.in_skb = in_skb;
1458 info.out_skb = skb;
1459 info.nlmsg_seq = seq;
1460 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1463 kfree_skb(skb);
1464 return NULL;
1465 }
1466
1467 return skb;
1468}
1469
Christoph Hellwig22e70052007-01-02 15:22:30 -08001470static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001471 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001473 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct xfrm_policy *xp;
1475 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001476 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001478 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001480 struct xfrm_mark m;
1481 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Thomas Graf7b67c852007-08-22 13:53:52 -07001483 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1485
Thomas Graf35a7aa02007-08-22 14:00:40 -07001486 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001487 if (err)
1488 return err;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 err = verify_policy_dir(p->dir);
1491 if (err)
1492 return err;
1493
1494 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001495 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001496 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001497 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001498 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001499
Thomas Graf35a7aa02007-08-22 14:00:40 -07001500 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001501 if (err)
1502 return err;
1503
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001504 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001505 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001506 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001507
Paul Moore03e1ad72008-04-12 19:07:52 -07001508 err = security_xfrm_policy_alloc(&ctx, uctx);
1509 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001510 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001511 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001512 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001513 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001514 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (xp == NULL)
1517 return -ENOENT;
1518
1519 if (!delete) {
1520 struct sk_buff *resp_skb;
1521
1522 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1523 if (IS_ERR(resp_skb)) {
1524 err = PTR_ERR(resp_skb);
1525 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001526 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001527 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001529 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001530 uid_t loginuid = audit_get_loginuid(current);
1531 u32 sessionid = audit_get_sessionid(current);
1532 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001533
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001534 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001535 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1536 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001537
1538 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001539 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001540
Herbert Xue7443892005-06-18 22:44:18 -07001541 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001542 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001543 c.seq = nlh->nlmsg_seq;
1544 c.pid = nlh->nlmsg_pid;
1545 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001548out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001549 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return err;
1551}
1552
Christoph Hellwig22e70052007-01-02 15:22:30 -08001553static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001554 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001556 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001557 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001558 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001559 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001560 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001562 audit_info.loginuid = audit_get_loginuid(current);
1563 audit_info.sessionid = audit_get_sessionid(current);
1564 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001565 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001566 if (err) {
1567 if (err == -ESRCH) /* empty table */
1568 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001569 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001570 }
Herbert Xubf088672005-06-18 22:44:00 -07001571 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001572 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001573 c.seq = nlh->nlmsg_seq;
1574 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001575 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001576 km_state_notify(NULL, &c);
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
1580
Thomas Graf7deb2262007-08-22 13:57:39 -07001581static inline size_t xfrm_aevent_msgsize(void)
1582{
1583 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1584 + nla_total_size(sizeof(struct xfrm_replay_state))
1585 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001586 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001587 + nla_total_size(4) /* XFRM_AE_RTHR */
1588 + nla_total_size(4); /* XFRM_AE_ETHR */
1589}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001590
David S. Miller214e0052011-02-24 00:02:38 -05001591static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001592{
1593 struct xfrm_aevent_id *id;
1594 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001595
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001596 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1597 if (nlh == NULL)
1598 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001599
Thomas Graf7b67c852007-08-22 13:53:52 -07001600 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001601 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001602 id->sa_id.spi = x->id.spi;
1603 id->sa_id.family = x->props.family;
1604 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001605 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1606 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001607 id->flags = c->data.aevent;
1608
Thomas Grafc0144be2007-08-22 13:55:43 -07001609 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1610 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001611
Thomas Grafc0144be2007-08-22 13:55:43 -07001612 if (id->flags & XFRM_AE_RTHR)
1613 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001614
Thomas Grafc0144be2007-08-22 13:55:43 -07001615 if (id->flags & XFRM_AE_ETHR)
1616 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1617 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001618
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001619 if (xfrm_mark_put(skb, &x->mark))
1620 goto nla_put_failure;
1621
Thomas Graf98250692007-08-22 12:47:26 -07001622 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001623
Thomas Grafc0144be2007-08-22 13:55:43 -07001624nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001625 nlmsg_cancel(skb, nlh);
1626 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001627}
1628
Christoph Hellwig22e70052007-01-02 15:22:30 -08001629static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001630 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001631{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001632 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001633 struct xfrm_state *x;
1634 struct sk_buff *r_skb;
1635 int err;
1636 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001637 u32 mark;
1638 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001639 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001640 struct xfrm_usersa_id *id = &p->sa_id;
1641
Thomas Graf7deb2262007-08-22 13:57:39 -07001642 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001643 if (r_skb == NULL)
1644 return -ENOMEM;
1645
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001646 mark = xfrm_mark_get(attrs, &m);
1647
1648 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001649 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001650 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001651 return -ESRCH;
1652 }
1653
1654 /*
1655 * XXX: is this lock really needed - none of the other
1656 * gets lock (the concern is things getting updated
1657 * while we are still reading) - jhs
1658 */
1659 spin_lock_bh(&x->lock);
1660 c.data.aevent = p->flags;
1661 c.seq = nlh->nlmsg_seq;
1662 c.pid = nlh->nlmsg_pid;
1663
1664 if (build_aevent(r_skb, x, &c) < 0)
1665 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001666 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001667 spin_unlock_bh(&x->lock);
1668 xfrm_state_put(x);
1669 return err;
1670}
1671
Christoph Hellwig22e70052007-01-02 15:22:30 -08001672static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001673 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001674{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001675 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001676 struct xfrm_state *x;
1677 struct km_event c;
1678 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001679 u32 mark = 0;
1680 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001681 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001682 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1683 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001684
1685 if (!lt && !rp)
1686 return err;
1687
1688 /* pedantic mode - thou shalt sayeth replaceth */
1689 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1690 return err;
1691
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001692 mark = xfrm_mark_get(attrs, &m);
1693
1694 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 -08001695 if (x == NULL)
1696 return -ESRCH;
1697
1698 if (x->km.state != XFRM_STATE_VALID)
1699 goto out;
1700
1701 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001702 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001703 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001704
1705 c.event = nlh->nlmsg_type;
1706 c.seq = nlh->nlmsg_seq;
1707 c.pid = nlh->nlmsg_pid;
1708 c.data.aevent = XFRM_AE_CU;
1709 km_state_notify(x, &c);
1710 err = 0;
1711out:
1712 xfrm_state_put(x);
1713 return err;
1714}
1715
Christoph Hellwig22e70052007-01-02 15:22:30 -08001716static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001717 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001719 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001720 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001721 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001722 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001723 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001724
Thomas Graf35a7aa02007-08-22 14:00:40 -07001725 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001726 if (err)
1727 return err;
1728
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001729 audit_info.loginuid = audit_get_loginuid(current);
1730 audit_info.sessionid = audit_get_sessionid(current);
1731 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001732 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001733 if (err) {
1734 if (err == -ESRCH) /* empty table */
1735 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001736 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001737 }
1738
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001739 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001740 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001741 c.seq = nlh->nlmsg_seq;
1742 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001743 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001744 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return 0;
1746}
1747
Christoph Hellwig22e70052007-01-02 15:22:30 -08001748static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001749 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001750{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001751 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001752 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001753 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001754 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001755 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001756 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001757 struct xfrm_mark m;
1758 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001759
Thomas Graf35a7aa02007-08-22 14:00:40 -07001760 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001761 if (err)
1762 return err;
1763
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001764 err = verify_policy_dir(p->dir);
1765 if (err)
1766 return err;
1767
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001768 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001769 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001770 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001771 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001772 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001773
Thomas Graf35a7aa02007-08-22 14:00:40 -07001774 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001775 if (err)
1776 return err;
1777
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001778 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001779 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001780 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001781
Paul Moore03e1ad72008-04-12 19:07:52 -07001782 err = security_xfrm_policy_alloc(&ctx, uctx);
1783 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001784 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001785 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001786 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001787 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001788 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001789 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001790 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001791 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001792
Timo Teräsea2dea92010-03-31 00:17:05 +00001793 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001794 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001795
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001796 err = 0;
1797 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001798 uid_t loginuid = audit_get_loginuid(current);
1799 u32 sessionid = audit_get_sessionid(current);
1800 u32 sid;
1801
1802 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001803 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001804 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001805
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001806 } else {
1807 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001808 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001809 }
1810 km_policy_expired(xp, p->dir, up->hard, current->pid);
1811
1812out:
1813 xfrm_pol_put(xp);
1814 return err;
1815}
1816
Christoph Hellwig22e70052007-01-02 15:22:30 -08001817static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001818 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001819{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001820 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001821 struct xfrm_state *x;
1822 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001823 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001824 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001825 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001826 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001827
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001828 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 -08001829
David S. Miller3a765aa2007-02-26 14:52:21 -08001830 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001831 if (x == NULL)
1832 return err;
1833
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001834 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001835 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001836 if (x->km.state != XFRM_STATE_VALID)
1837 goto out;
1838 km_state_expired(x, ue->hard, current->pid);
1839
Joy Latten161a09e2006-11-27 13:11:54 -06001840 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001841 uid_t loginuid = audit_get_loginuid(current);
1842 u32 sessionid = audit_get_sessionid(current);
1843 u32 sid;
1844
1845 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001846 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001847 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001848 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001849 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001850out:
1851 spin_unlock_bh(&x->lock);
1852 xfrm_state_put(x);
1853 return err;
1854}
1855
Christoph Hellwig22e70052007-01-02 15:22:30 -08001856static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001857 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001858{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001859 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001860 struct xfrm_policy *xp;
1861 struct xfrm_user_tmpl *ut;
1862 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001863 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001864 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001865
Thomas Graf7b67c852007-08-22 13:53:52 -07001866 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001867 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001868 int err = -ENOMEM;
1869
1870 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001871 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001872
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001873 xfrm_mark_get(attrs, &mark);
1874
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001875 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001876 if (err)
1877 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001878
1879 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001880 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001881 if (!xp)
1882 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001883
1884 memcpy(&x->id, &ua->id, sizeof(ua->id));
1885 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1886 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001887 xp->mark.m = x->mark.m = mark.m;
1888 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001889 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001890 /* extract the templates and for each call km_key */
1891 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1892 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1893 memcpy(&x->id, &t->id, sizeof(x->id));
1894 x->props.mode = t->mode;
1895 x->props.reqid = t->reqid;
1896 x->props.family = ut->family;
1897 t->aalgos = ua->aalgos;
1898 t->ealgos = ua->ealgos;
1899 t->calgos = ua->calgos;
1900 err = km_query(x, t, xp);
1901
1902 }
1903
1904 kfree(x);
1905 kfree(xp);
1906
1907 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001908
1909bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001910 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001911free_state:
1912 kfree(x);
1913nomem:
1914 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001915}
1916
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001917#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001918static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001919 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001920 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001921{
Thomas Graf5424f322007-08-22 14:01:33 -07001922 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001923 struct xfrm_user_migrate *um;
1924 int i, num_migrate;
1925
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001926 if (k != NULL) {
1927 struct xfrm_user_kmaddress *uk;
1928
1929 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1930 memcpy(&k->local, &uk->local, sizeof(k->local));
1931 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1932 k->family = uk->family;
1933 k->reserved = uk->reserved;
1934 }
1935
Thomas Graf5424f322007-08-22 14:01:33 -07001936 um = nla_data(rt);
1937 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001938
1939 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1940 return -EINVAL;
1941
1942 for (i = 0; i < num_migrate; i++, um++, ma++) {
1943 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1944 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1945 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1946 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1947
1948 ma->proto = um->proto;
1949 ma->mode = um->mode;
1950 ma->reqid = um->reqid;
1951
1952 ma->old_family = um->old_family;
1953 ma->new_family = um->new_family;
1954 }
1955
1956 *num = i;
1957 return 0;
1958}
1959
1960static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001961 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001962{
Thomas Graf7b67c852007-08-22 13:53:52 -07001963 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001964 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001965 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001966 u8 type;
1967 int err;
1968 int n = 0;
1969
Thomas Graf35a7aa02007-08-22 14:00:40 -07001970 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001971 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001972
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001973 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1974
Thomas Graf5424f322007-08-22 14:01:33 -07001975 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001976 if (err)
1977 return err;
1978
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001979 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001980 if (err)
1981 return err;
1982
1983 if (!n)
1984 return 0;
1985
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001986 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001987
1988 return 0;
1989}
1990#else
1991static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001992 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001993{
1994 return -ENOPROTOOPT;
1995}
1996#endif
1997
1998#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001999static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002000{
2001 struct xfrm_user_migrate um;
2002
2003 memset(&um, 0, sizeof(um));
2004 um.proto = m->proto;
2005 um.mode = m->mode;
2006 um.reqid = m->reqid;
2007 um.old_family = m->old_family;
2008 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2009 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2010 um.new_family = m->new_family;
2011 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2012 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2013
Thomas Grafc0144be2007-08-22 13:55:43 -07002014 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002015}
2016
David S. Miller183cad12011-02-24 00:28:01 -05002017static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002018{
2019 struct xfrm_user_kmaddress uk;
2020
2021 memset(&uk, 0, sizeof(uk));
2022 uk.family = k->family;
2023 uk.reserved = k->reserved;
2024 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002025 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002026
2027 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2028}
2029
2030static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002031{
2032 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002033 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2034 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2035 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002036}
2037
David S. Miller183cad12011-02-24 00:28:01 -05002038static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2039 int num_migrate, const struct xfrm_kmaddress *k,
2040 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002041{
David S. Miller183cad12011-02-24 00:28:01 -05002042 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002043 struct xfrm_userpolicy_id *pol_id;
2044 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002045 int i;
2046
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002047 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2048 if (nlh == NULL)
2049 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002050
Thomas Graf7b67c852007-08-22 13:53:52 -07002051 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002052 /* copy data from selector, dir, and type to the pol_id */
2053 memset(pol_id, 0, sizeof(*pol_id));
2054 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2055 pol_id->dir = dir;
2056
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002057 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2058 goto nlmsg_failure;
2059
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002060 if (copy_to_user_policy_type(type, skb) < 0)
2061 goto nlmsg_failure;
2062
2063 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2064 if (copy_to_user_migrate(mp, skb) < 0)
2065 goto nlmsg_failure;
2066 }
2067
Thomas Graf98250692007-08-22 12:47:26 -07002068 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002070 nlmsg_cancel(skb, nlh);
2071 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072}
2073
David S. Miller183cad12011-02-24 00:28:01 -05002074static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2075 const struct xfrm_migrate *m, int num_migrate,
2076 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002077{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002078 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002079 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002080
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002081 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002082 if (skb == NULL)
2083 return -ENOMEM;
2084
2085 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002086 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002087 BUG();
2088
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002089 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002090}
2091#else
David S. Miller183cad12011-02-24 00:28:01 -05002092static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2093 const struct xfrm_migrate *m, int num_migrate,
2094 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002095{
2096 return -ENOPROTOOPT;
2097}
2098#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002099
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002100#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002101
2102static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002103 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002104 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2105 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2106 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2107 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2108 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2109 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002110 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002111 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002112 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002113 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002114 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002115 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002116 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002117 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2118 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002119 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002120 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002121 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2122 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123};
2124
Thomas Graf492b5582005-05-03 14:26:40 -07002125#undef XMSGSIZE
2126
Thomas Grafcf5cb792007-08-22 13:59:04 -07002127static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002128 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2129 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2130 [XFRMA_LASTUSED] = { .type = NLA_U64},
2131 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002132 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002133 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2134 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2135 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2136 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2137 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2138 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2139 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2140 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2141 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2142 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2143 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2144 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2145 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2146 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002147 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002148 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002149 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002150};
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002153 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002155 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002156} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2157 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2158 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2159 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002160 .dump = xfrm_dump_sa,
2161 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002162 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2163 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2164 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002165 .dump = xfrm_dump_policy,
2166 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002167 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002168 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002169 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002170 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2171 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002172 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002173 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2174 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002175 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2176 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002177 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002178 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002179 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180};
2181
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002182static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002184 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002185 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002187 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002191 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 type -= XFRM_MSG_BASE;
2194 link = &xfrm_dispatch[type];
2195
2196 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002197 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2198 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Thomas Graf492b5582005-05-03 14:26:40 -07002200 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2201 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002202 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002206 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208
Thomas Graf35a7aa02007-08-22 14:00:40 -07002209 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002210 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002211 if (err < 0)
2212 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Thomas Graf5424f322007-08-22 14:01:33 -07002217 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002220static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002222 mutex_lock(&xfrm_cfg_mutex);
2223 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2224 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Thomas Graf7deb2262007-08-22 13:57:39 -07002227static inline size_t xfrm_expire_msgsize(void)
2228{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002229 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2230 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002231}
2232
David S. Miller214e0052011-02-24 00:02:38 -05002233static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 struct xfrm_user_expire *ue;
2236 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002238 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2239 if (nlh == NULL)
2240 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Thomas Graf7b67c852007-08-22 13:53:52 -07002242 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002244 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002246 if (xfrm_mark_put(skb, &x->mark))
2247 goto nla_put_failure;
2248
Thomas Graf98250692007-08-22 12:47:26 -07002249 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002250
2251nla_put_failure:
2252 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
David S. Miller214e0052011-02-24 00:02:38 -05002255static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002257 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 struct sk_buff *skb;
2259
Thomas Graf7deb2262007-08-22 13:57:39 -07002260 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (skb == NULL)
2262 return -ENOMEM;
2263
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002264 if (build_expire(skb, x, c) < 0) {
2265 kfree_skb(skb);
2266 return -EMSGSIZE;
2267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002269 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
David S. Miller214e0052011-02-24 00:02:38 -05002272static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002273{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002274 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002275 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002276
Thomas Graf7deb2262007-08-22 13:57:39 -07002277 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002278 if (skb == NULL)
2279 return -ENOMEM;
2280
2281 if (build_aevent(skb, x, c) < 0)
2282 BUG();
2283
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002284 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002285}
2286
David S. Miller214e0052011-02-24 00:02:38 -05002287static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002288{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002289 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002290 struct xfrm_usersa_flush *p;
2291 struct nlmsghdr *nlh;
2292 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002293 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002294
Thomas Graf7deb2262007-08-22 13:57:39 -07002295 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002296 if (skb == NULL)
2297 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002298
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002299 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2300 if (nlh == NULL) {
2301 kfree_skb(skb);
2302 return -EMSGSIZE;
2303 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002304
Thomas Graf7b67c852007-08-22 13:53:52 -07002305 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002306 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002307
Thomas Graf98250692007-08-22 12:47:26 -07002308 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002310 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002311}
2312
Thomas Graf7deb2262007-08-22 13:57:39 -07002313static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002314{
Thomas Graf7deb2262007-08-22 13:57:39 -07002315 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002316 if (x->aead)
2317 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002318 if (x->aalg) {
2319 l += nla_total_size(sizeof(struct xfrm_algo) +
2320 (x->aalg->alg_key_len + 7) / 8);
2321 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2322 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002324 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002326 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002328 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002329 if (x->tfcpad)
2330 l += nla_total_size(sizeof(x->tfcpad));
Herbert Xu68325d32007-10-09 13:30:57 -07002331 if (x->security)
2332 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2333 x->security->ctx_len);
2334 if (x->coaddr)
2335 l += nla_total_size(sizeof(*x->coaddr));
2336
Herbert Xud26f3982007-11-13 21:47:08 -08002337 /* Must count x->lastused as it may become non-zero behind our back. */
2338 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002339
2340 return l;
2341}
2342
David S. Miller214e0052011-02-24 00:02:38 -05002343static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002345 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002347 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348 struct nlmsghdr *nlh;
2349 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002350 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002351 int headlen;
2352
2353 headlen = sizeof(*p);
2354 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002355 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002356 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002357 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002358 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002359 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002360
Thomas Graf7deb2262007-08-22 13:57:39 -07002361 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002362 if (skb == NULL)
2363 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002364
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002365 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2366 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002367 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002368
Thomas Graf7b67c852007-08-22 13:53:52 -07002369 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002370 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002371 struct nlattr *attr;
2372
Thomas Graf7b67c852007-08-22 13:53:52 -07002373 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002374 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2375 id->spi = x->id.spi;
2376 id->family = x->props.family;
2377 id->proto = x->id.proto;
2378
Thomas Grafc0144be2007-08-22 13:55:43 -07002379 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2380 if (attr == NULL)
2381 goto nla_put_failure;
2382
2383 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002384 }
2385
Herbert Xu68325d32007-10-09 13:30:57 -07002386 if (copy_to_user_state_extra(x, p, skb))
2387 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388
Thomas Graf98250692007-08-22 12:47:26 -07002389 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002391 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002392
Thomas Grafc0144be2007-08-22 13:55:43 -07002393nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002394 /* Somebody screwed up with xfrm_sa_len! */
2395 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396 kfree_skb(skb);
2397 return -1;
2398}
2399
David S. Miller214e0052011-02-24 00:02:38 -05002400static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401{
2402
2403 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002404 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002406 case XFRM_MSG_NEWAE:
2407 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002408 case XFRM_MSG_DELSA:
2409 case XFRM_MSG_UPDSA:
2410 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002412 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413 return xfrm_notify_sa_flush(c);
2414 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002415 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2416 c->event);
2417 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002418 }
2419
2420 return 0;
2421
2422}
2423
Thomas Graf7deb2262007-08-22 13:57:39 -07002424static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2425 struct xfrm_policy *xp)
2426{
2427 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2428 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002429 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002430 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2431 + userpolicy_type_attrsize();
2432}
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2435 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2436 int dir)
2437{
2438 struct xfrm_user_acquire *ua;
2439 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 __u32 seq = xfrm_get_acqseq();
2441
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002442 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2443 if (nlh == NULL)
2444 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Thomas Graf7b67c852007-08-22 13:53:52 -07002446 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 memcpy(&ua->id, &x->id, sizeof(ua->id));
2448 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2449 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2450 copy_to_user_policy(xp, &ua->policy, dir);
2451 ua->aalgos = xt->aalgos;
2452 ua->ealgos = xt->ealgos;
2453 ua->calgos = xt->calgos;
2454 ua->seq = x->km.seq = seq;
2455
2456 if (copy_to_user_tmpl(xp, skb) < 0)
2457 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002458 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002459 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002460 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002461 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002462 if (xfrm_mark_put(skb, &xp->mark))
2463 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Thomas Graf98250692007-08-22 12:47:26 -07002465 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002467nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002469 nlmsg_cancel(skb, nlh);
2470 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2474 struct xfrm_policy *xp, int dir)
2475{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002476 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Thomas Graf7deb2262007-08-22 13:57:39 -07002479 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (skb == NULL)
2481 return -ENOMEM;
2482
2483 if (build_acquire(skb, x, xt, xp, dir) < 0)
2484 BUG();
2485
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002486 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489/* User gives us xfrm_user_policy_info followed by an array of 0
2490 * or more templates.
2491 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002492static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 u8 *data, int len, int *dir)
2494{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002495 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2497 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2498 struct xfrm_policy *xp;
2499 int nr;
2500
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002501 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 case AF_INET:
2503 if (opt != IP_XFRM_POLICY) {
2504 *dir = -EOPNOTSUPP;
2505 return NULL;
2506 }
2507 break;
2508#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2509 case AF_INET6:
2510 if (opt != IPV6_XFRM_POLICY) {
2511 *dir = -EOPNOTSUPP;
2512 return NULL;
2513 }
2514 break;
2515#endif
2516 default:
2517 *dir = -EINVAL;
2518 return NULL;
2519 }
2520
2521 *dir = -EINVAL;
2522
2523 if (len < sizeof(*p) ||
2524 verify_newpolicy_info(p))
2525 return NULL;
2526
2527 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002528 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return NULL;
2530
Herbert Xua4f1bac2005-07-26 15:43:17 -07002531 if (p->dir > XFRM_POLICY_OUT)
2532 return NULL;
2533
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002534 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (xp == NULL) {
2536 *dir = -ENOBUFS;
2537 return NULL;
2538 }
2539
2540 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002541 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 copy_templates(xp, ut, nr);
2543
2544 *dir = p->dir;
2545
2546 return xp;
2547}
2548
Thomas Graf7deb2262007-08-22 13:57:39 -07002549static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2550{
2551 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2552 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2553 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002554 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002555 + userpolicy_type_attrsize();
2556}
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002559 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
2561 struct xfrm_user_polexpire *upe;
2562 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002563 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002565 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2566 if (nlh == NULL)
2567 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Thomas Graf7b67c852007-08-22 13:53:52 -07002569 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 copy_to_user_policy(xp, &upe->pol, dir);
2571 if (copy_to_user_tmpl(xp, skb) < 0)
2572 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002573 if (copy_to_user_sec_ctx(xp, skb))
2574 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002575 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002576 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002577 if (xfrm_mark_put(skb, &xp->mark))
2578 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 upe->hard = !!hard;
2580
Thomas Graf98250692007-08-22 12:47:26 -07002581 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002583nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002585 nlmsg_cancel(skb, nlh);
2586 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
David S. Miller214e0052011-02-24 00:02:38 -05002589static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002591 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Thomas Graf7deb2262007-08-22 13:57:39 -07002594 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (skb == NULL)
2596 return -ENOMEM;
2597
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002598 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 BUG();
2600
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002601 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
David S. Miller214e0052011-02-24 00:02:38 -05002604static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002605{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002606 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002607 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002608 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002609 struct nlmsghdr *nlh;
2610 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002611 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002612 int headlen;
2613
2614 headlen = sizeof(*p);
2615 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002616 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002617 headlen = sizeof(*id);
2618 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002619 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002620 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002621 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002622
Thomas Graf7deb2262007-08-22 13:57:39 -07002623 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002624 if (skb == NULL)
2625 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002626
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002627 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2628 if (nlh == NULL)
2629 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002630
Thomas Graf7b67c852007-08-22 13:53:52 -07002631 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002632 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002633 struct nlattr *attr;
2634
Thomas Graf7b67c852007-08-22 13:53:52 -07002635 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002636 memset(id, 0, sizeof(*id));
2637 id->dir = dir;
2638 if (c->data.byid)
2639 id->index = xp->index;
2640 else
2641 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2642
Thomas Grafc0144be2007-08-22 13:55:43 -07002643 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2644 if (attr == NULL)
2645 goto nlmsg_failure;
2646
2647 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002648 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002649
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002650 copy_to_user_policy(xp, p, dir);
2651 if (copy_to_user_tmpl(xp, skb) < 0)
2652 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002653 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002654 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002655
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002656 if (xfrm_mark_put(skb, &xp->mark))
2657 goto nla_put_failure;
2658
Thomas Graf98250692007-08-22 12:47:26 -07002659 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002661 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002662
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002663nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664nlmsg_failure:
2665 kfree_skb(skb);
2666 return -1;
2667}
2668
David S. Miller214e0052011-02-24 00:02:38 -05002669static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002670{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002671 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672 struct nlmsghdr *nlh;
2673 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674
Thomas Graf7deb2262007-08-22 13:57:39 -07002675 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676 if (skb == NULL)
2677 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002679 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2680 if (nlh == NULL)
2681 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002682 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2683 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002684
Thomas Graf98250692007-08-22 12:47:26 -07002685 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002686
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002687 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002688
2689nlmsg_failure:
2690 kfree_skb(skb);
2691 return -1;
2692}
2693
David S. Miller214e0052011-02-24 00:02:38 -05002694static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002695{
2696
2697 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002698 case XFRM_MSG_NEWPOLICY:
2699 case XFRM_MSG_UPDPOLICY:
2700 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002701 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002702 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002703 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002704 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002705 return xfrm_exp_policy_notify(xp, dir, c);
2706 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002707 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2708 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002709 }
2710
2711 return 0;
2712
2713}
2714
Thomas Graf7deb2262007-08-22 13:57:39 -07002715static inline size_t xfrm_report_msgsize(void)
2716{
2717 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2718}
2719
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002720static int build_report(struct sk_buff *skb, u8 proto,
2721 struct xfrm_selector *sel, xfrm_address_t *addr)
2722{
2723 struct xfrm_user_report *ur;
2724 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002725
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002726 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2727 if (nlh == NULL)
2728 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002729
Thomas Graf7b67c852007-08-22 13:53:52 -07002730 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002731 ur->proto = proto;
2732 memcpy(&ur->sel, sel, sizeof(ur->sel));
2733
2734 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002735 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002736
Thomas Graf98250692007-08-22 12:47:26 -07002737 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002738
Thomas Grafc0144be2007-08-22 13:55:43 -07002739nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002740 nlmsg_cancel(skb, nlh);
2741 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002742}
2743
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002744static int xfrm_send_report(struct net *net, u8 proto,
2745 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002746{
2747 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002748
Thomas Graf7deb2262007-08-22 13:57:39 -07002749 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002750 if (skb == NULL)
2751 return -ENOMEM;
2752
2753 if (build_report(skb, proto, sel, addr) < 0)
2754 BUG();
2755
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002756 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002757}
2758
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002759static inline size_t xfrm_mapping_msgsize(void)
2760{
2761 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2762}
2763
2764static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2765 xfrm_address_t *new_saddr, __be16 new_sport)
2766{
2767 struct xfrm_user_mapping *um;
2768 struct nlmsghdr *nlh;
2769
2770 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2771 if (nlh == NULL)
2772 return -EMSGSIZE;
2773
2774 um = nlmsg_data(nlh);
2775
2776 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2777 um->id.spi = x->id.spi;
2778 um->id.family = x->props.family;
2779 um->id.proto = x->id.proto;
2780 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2781 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2782 um->new_sport = new_sport;
2783 um->old_sport = x->encap->encap_sport;
2784 um->reqid = x->props.reqid;
2785
2786 return nlmsg_end(skb, nlh);
2787}
2788
2789static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2790 __be16 sport)
2791{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002792 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002793 struct sk_buff *skb;
2794
2795 if (x->id.proto != IPPROTO_ESP)
2796 return -EINVAL;
2797
2798 if (!x->encap)
2799 return -EINVAL;
2800
2801 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2802 if (skb == NULL)
2803 return -ENOMEM;
2804
2805 if (build_mapping(skb, x, ipaddr, sport) < 0)
2806 BUG();
2807
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002808 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811static struct xfrm_mgr netlink_mgr = {
2812 .id = "netlink",
2813 .notify = xfrm_send_state_notify,
2814 .acquire = xfrm_send_acquire,
2815 .compile_policy = xfrm_compile_policy,
2816 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002817 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002818 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002819 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820};
2821
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002822static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Patrick McHardybe336902006-03-20 22:40:54 -08002824 struct sock *nlsk;
2825
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002826 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002827 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002828 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002830 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002831 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return 0;
2833}
2834
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002835static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002836{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002837 struct net *net;
2838 list_for_each_entry(net, net_exit_list, exit_list)
2839 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2840 synchronize_net();
2841 list_for_each_entry(net, net_exit_list, exit_list)
2842 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002843}
2844
2845static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002846 .init = xfrm_user_net_init,
2847 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002848};
2849
2850static int __init xfrm_user_init(void)
2851{
2852 int rv;
2853
2854 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2855
2856 rv = register_pernet_subsys(&xfrm_user_net_ops);
2857 if (rv < 0)
2858 return rv;
2859 rv = xfrm_register_km(&netlink_mgr);
2860 if (rv < 0)
2861 unregister_pernet_subsys(&xfrm_user_net_ops);
2862 return rv;
2863}
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865static void __exit xfrm_user_exit(void)
2866{
2867 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002868 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871module_init(xfrm_user_init);
2872module_exit(xfrm_user_exit);
2873MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002874MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002875