blob: 468ab60d3dc0d7a9ef0afccf6ef4262531d1fcf3 [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
478 /* override default values from above */
479
Thomas Graf5424f322007-08-22 14:01:33 -0700480 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return x;
483
484error:
485 x->km.state = XFRM_STATE_DEAD;
486 xfrm_state_put(x);
487error_no_put:
488 *errp = err;
489 return NULL;
490}
491
Christoph Hellwig22e70052007-01-02 15:22:30 -0800492static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700493 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800495 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700496 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct xfrm_state *x;
498 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700499 struct km_event c;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800500 uid_t loginuid = audit_get_loginuid(current);
501 u32 sessionid = audit_get_sessionid(current);
502 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Thomas Graf35a7aa02007-08-22 14:00:40 -0700504 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (err)
506 return err;
507
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800508 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (!x)
510 return err;
511
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700512 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
514 err = xfrm_state_add(x);
515 else
516 err = xfrm_state_update(x);
517
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800518 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400519 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (err < 0) {
522 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800523 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700524 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700527 c.seq = nlh->nlmsg_seq;
528 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700529 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700530
531 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700532out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700533 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return err;
535}
536
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800537static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
538 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700539 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700540 int *errp)
541{
542 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000543 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700544 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000545 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700546
547 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
548 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000549 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700550 } else {
551 xfrm_address_t *saddr = NULL;
552
Thomas Graf35a7aa02007-08-22 14:00:40 -0700553 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700554 if (!saddr) {
555 err = -EINVAL;
556 goto out;
557 }
558
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800559 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000560 x = xfrm_state_lookup_byaddr(net, mark,
561 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800562 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700563 }
564
565 out:
566 if (!x && errp)
567 *errp = err;
568 return x;
569}
570
Christoph Hellwig22e70052007-01-02 15:22:30 -0800571static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700572 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800574 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700576 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700577 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700578 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800579 uid_t loginuid = audit_get_loginuid(current);
580 u32 sessionid = audit_get_sessionid(current);
581 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800583 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
David S. Miller6f68dc32006-06-08 23:58:52 -0700587 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700588 goto out;
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700591 err = -EPERM;
592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700595 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600596
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700597 if (err < 0)
598 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700599
600 c.seq = nlh->nlmsg_seq;
601 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700602 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700603 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700605out:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -0800606 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -0400607 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700608 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
613{
614 memcpy(&p->id, &x->id, sizeof(p->id));
615 memcpy(&p->sel, &x->sel, sizeof(p->sel));
616 memcpy(&p->lft, &x->lft, sizeof(p->lft));
617 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
618 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700619 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 p->mode = x->props.mode;
621 p->replay_window = x->props.replay_window;
622 p->reqid = x->props.reqid;
623 p->family = x->props.family;
624 p->flags = x->props.flags;
625 p->seq = x->km.seq;
626}
627
628struct xfrm_dump_info {
629 struct sk_buff *in_skb;
630 struct sk_buff *out_skb;
631 u32 nlmsg_seq;
632 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633};
634
Thomas Grafc0144be2007-08-22 13:55:43 -0700635static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
636{
Thomas Grafc0144be2007-08-22 13:55:43 -0700637 struct xfrm_user_sec_ctx *uctx;
638 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700639 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700640
641 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
642 if (attr == NULL)
643 return -EMSGSIZE;
644
645 uctx = nla_data(attr);
646 uctx->exttype = XFRMA_SEC_CTX;
647 uctx->len = ctx_size;
648 uctx->ctx_doi = s->ctx_doi;
649 uctx->ctx_alg = s->ctx_alg;
650 uctx->ctx_len = s->ctx_len;
651 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
652
653 return 0;
654}
655
Martin Willi4447bb32009-11-25 00:29:52 +0000656static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
657{
658 struct xfrm_algo *algo;
659 struct nlattr *nla;
660
661 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
662 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
663 if (!nla)
664 return -EMSGSIZE;
665
666 algo = nla_data(nla);
667 strcpy(algo->alg_name, auth->alg_name);
668 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
669 algo->alg_key_len = auth->alg_key_len;
670
671 return 0;
672}
673
Herbert Xu68325d32007-10-09 13:30:57 -0700674/* Don't change this without updating xfrm_sa_len! */
675static int copy_to_user_state_extra(struct xfrm_state *x,
676 struct xfrm_usersa_info *p,
677 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 copy_to_user_state(x, p);
680
Herbert Xu050f0092007-10-09 13:31:47 -0700681 if (x->coaddr)
682 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
683
684 if (x->lastused)
685 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700686
Herbert Xu1a6509d2008-01-28 19:37:29 -0800687 if (x->aead)
688 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000689 if (x->aalg) {
690 if (copy_to_user_auth(x->aalg, skb))
691 goto nla_put_failure;
692
693 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
694 xfrm_alg_auth_len(x->aalg), x->aalg);
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800697 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700699 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700702 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Martin Willi35d28562010-12-08 04:37:49 +0000704 if (x->tfcpad)
705 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
706
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000707 if (xfrm_mark_put(skb, &x->mark))
708 goto nla_put_failure;
709
Thomas Grafc0144be2007-08-22 13:55:43 -0700710 if (x->security && copy_sec_ctx(x->security, skb) < 0)
711 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700712
Herbert Xu68325d32007-10-09 13:30:57 -0700713 return 0;
714
715nla_put_failure:
716 return -EMSGSIZE;
717}
718
719static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
720{
721 struct xfrm_dump_info *sp = ptr;
722 struct sk_buff *in_skb = sp->in_skb;
723 struct sk_buff *skb = sp->out_skb;
724 struct xfrm_usersa_info *p;
725 struct nlmsghdr *nlh;
726 int err;
727
Herbert Xu68325d32007-10-09 13:30:57 -0700728 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
729 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
730 if (nlh == NULL)
731 return -EMSGSIZE;
732
733 p = nlmsg_data(nlh);
734
735 err = copy_to_user_state_extra(x, p, skb);
736 if (err)
737 goto nla_put_failure;
738
Thomas Graf98250692007-08-22 12:47:26 -0700739 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741
Thomas Grafc0144be2007-08-22 13:55:43 -0700742nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700743 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Timo Teras4c563f72008-02-28 21:31:08 -0800747static int xfrm_dump_sa_done(struct netlink_callback *cb)
748{
749 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
750 xfrm_state_walk_done(walk);
751 return 0;
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
755{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800756 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800757 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct xfrm_dump_info info;
759
Timo Teras4c563f72008-02-28 21:31:08 -0800760 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
761 sizeof(cb->args) - sizeof(cb->args[0]));
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 info.in_skb = cb->skb;
764 info.out_skb = skb;
765 info.nlmsg_seq = cb->nlh->nlmsg_seq;
766 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800767
768 if (!cb->args[0]) {
769 cb->args[0] = 1;
770 xfrm_state_walk_init(walk, 0);
771 }
772
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800773 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return skb->len;
776}
777
778static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
779 struct xfrm_state *x, u32 seq)
780{
781 struct xfrm_dump_info info;
782 struct sk_buff *skb;
783
Thomas Graf7deb2262007-08-22 13:57:39 -0700784 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!skb)
786 return ERR_PTR(-ENOMEM);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 info.in_skb = in_skb;
789 info.out_skb = skb;
790 info.nlmsg_seq = seq;
791 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (dump_one_state(x, 0, &info)) {
794 kfree_skb(skb);
795 return NULL;
796 }
797
798 return skb;
799}
800
Thomas Graf7deb2262007-08-22 13:57:39 -0700801static inline size_t xfrm_spdinfo_msgsize(void)
802{
803 return NLMSG_ALIGN(4)
804 + nla_total_size(sizeof(struct xfrmu_spdinfo))
805 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
806}
807
Alexey Dobriyane0710412010-01-23 13:37:10 +0000808static int build_spdinfo(struct sk_buff *skb, struct net *net,
809 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700810{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700811 struct xfrmk_spdinfo si;
812 struct xfrmu_spdinfo spc;
813 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700814 struct nlmsghdr *nlh;
815 u32 *f;
816
817 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
818 if (nlh == NULL) /* shouldnt really happen ... */
819 return -EMSGSIZE;
820
821 f = nlmsg_data(nlh);
822 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000823 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700824 spc.incnt = si.incnt;
825 spc.outcnt = si.outcnt;
826 spc.fwdcnt = si.fwdcnt;
827 spc.inscnt = si.inscnt;
828 spc.outscnt = si.outscnt;
829 spc.fwdscnt = si.fwdscnt;
830 sph.spdhcnt = si.spdhcnt;
831 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700832
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700833 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
834 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700835
836 return nlmsg_end(skb, nlh);
837
838nla_put_failure:
839 nlmsg_cancel(skb, nlh);
840 return -EMSGSIZE;
841}
842
843static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700844 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700845{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800846 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700847 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700848 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700849 u32 spid = NETLINK_CB(skb).pid;
850 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700851
Thomas Graf7deb2262007-08-22 13:57:39 -0700852 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700853 if (r_skb == NULL)
854 return -ENOMEM;
855
Alexey Dobriyane0710412010-01-23 13:37:10 +0000856 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700857 BUG();
858
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800859 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700860}
861
Thomas Graf7deb2262007-08-22 13:57:39 -0700862static inline size_t xfrm_sadinfo_msgsize(void)
863{
864 return NLMSG_ALIGN(4)
865 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
866 + nla_total_size(4); /* XFRMA_SAD_CNT */
867}
868
Alexey Dobriyane0710412010-01-23 13:37:10 +0000869static int build_sadinfo(struct sk_buff *skb, struct net *net,
870 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700871{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700872 struct xfrmk_sadinfo si;
873 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700874 struct nlmsghdr *nlh;
875 u32 *f;
876
877 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
878 if (nlh == NULL) /* shouldnt really happen ... */
879 return -EMSGSIZE;
880
881 f = nlmsg_data(nlh);
882 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000883 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700884
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700885 sh.sadhmcnt = si.sadhmcnt;
886 sh.sadhcnt = si.sadhcnt;
887
888 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
889 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700890
891 return nlmsg_end(skb, nlh);
892
893nla_put_failure:
894 nlmsg_cancel(skb, nlh);
895 return -EMSGSIZE;
896}
897
898static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700899 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700900{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800901 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700902 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700903 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700904 u32 spid = NETLINK_CB(skb).pid;
905 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700906
Thomas Graf7deb2262007-08-22 13:57:39 -0700907 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700908 if (r_skb == NULL)
909 return -ENOMEM;
910
Alexey Dobriyane0710412010-01-23 13:37:10 +0000911 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700912 BUG();
913
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800914 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700915}
916
Christoph Hellwig22e70052007-01-02 15:22:30 -0800917static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700918 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800920 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700921 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct xfrm_state *x;
923 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700924 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800926 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (x == NULL)
928 goto out_noput;
929
930 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
931 if (IS_ERR(resp_skb)) {
932 err = PTR_ERR(resp_skb);
933 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800934 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 xfrm_state_put(x);
937out_noput:
938 return err;
939}
940
941static int verify_userspi_info(struct xfrm_userspi_info *p)
942{
943 switch (p->info.id.proto) {
944 case IPPROTO_AH:
945 case IPPROTO_ESP:
946 break;
947
948 case IPPROTO_COMP:
949 /* IPCOMP spi is 16-bits. */
950 if (p->max >= 0x10000)
951 return -EINVAL;
952 break;
953
954 default:
955 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (p->min > p->max)
959 return -EINVAL;
960
961 return 0;
962}
963
Christoph Hellwig22e70052007-01-02 15:22:30 -0800964static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700965 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800967 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct xfrm_state *x;
969 struct xfrm_userspi_info *p;
970 struct sk_buff *resp_skb;
971 xfrm_address_t *daddr;
972 int family;
973 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000974 u32 mark;
975 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Thomas Graf7b67c852007-08-22 13:53:52 -0700977 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 err = verify_userspi_info(p);
979 if (err)
980 goto out_noput;
981
982 family = p->info.family;
983 daddr = &p->info.id.daddr;
984
985 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000986
987 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000989 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
991 xfrm_state_put(x);
992 x = NULL;
993 }
994 }
995
996 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000997 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 p->info.id.proto, daddr,
999 &p->info.saddr, 1,
1000 family);
1001 err = -ENOENT;
1002 if (x == NULL)
1003 goto out_noput;
1004
Herbert Xu658b2192007-10-09 13:29:52 -07001005 err = xfrm_alloc_spi(x, p->min, p->max);
1006 if (err)
1007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Herbert Xu658b2192007-10-09 13:29:52 -07001009 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (IS_ERR(resp_skb)) {
1011 err = PTR_ERR(resp_skb);
1012 goto out;
1013 }
1014
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001015 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017out:
1018 xfrm_state_put(x);
1019out_noput:
1020 return err;
1021}
1022
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001023static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 switch (dir) {
1026 case XFRM_POLICY_IN:
1027 case XFRM_POLICY_OUT:
1028 case XFRM_POLICY_FWD:
1029 break;
1030
1031 default:
1032 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 return 0;
1036}
1037
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001038static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001039{
1040 switch (type) {
1041 case XFRM_POLICY_TYPE_MAIN:
1042#ifdef CONFIG_XFRM_SUB_POLICY
1043 case XFRM_POLICY_TYPE_SUB:
1044#endif
1045 break;
1046
1047 default:
1048 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001049 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001050
1051 return 0;
1052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1055{
1056 switch (p->share) {
1057 case XFRM_SHARE_ANY:
1058 case XFRM_SHARE_SESSION:
1059 case XFRM_SHARE_USER:
1060 case XFRM_SHARE_UNIQUE:
1061 break;
1062
1063 default:
1064 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 switch (p->action) {
1068 case XFRM_POLICY_ALLOW:
1069 case XFRM_POLICY_BLOCK:
1070 break;
1071
1072 default:
1073 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 switch (p->sel.family) {
1077 case AF_INET:
1078 break;
1079
1080 case AF_INET6:
1081#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1082 break;
1083#else
1084 return -EAFNOSUPPORT;
1085#endif
1086
1087 default:
1088 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 return verify_policy_dir(p->dir);
1092}
1093
Thomas Graf5424f322007-08-22 14:01:33 -07001094static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001095{
Thomas Graf5424f322007-08-22 14:01:33 -07001096 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001097 struct xfrm_user_sec_ctx *uctx;
1098
1099 if (!rt)
1100 return 0;
1101
Thomas Graf5424f322007-08-22 14:01:33 -07001102 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001103 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1107 int nr)
1108{
1109 int i;
1110
1111 xp->xfrm_nr = nr;
1112 for (i = 0; i < nr; i++, ut++) {
1113 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1114
1115 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1116 memcpy(&t->saddr, &ut->saddr,
1117 sizeof(xfrm_address_t));
1118 t->reqid = ut->reqid;
1119 t->mode = ut->mode;
1120 t->share = ut->share;
1121 t->optional = ut->optional;
1122 t->aalgos = ut->aalgos;
1123 t->ealgos = ut->ealgos;
1124 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001125 /* If all masks are ~0, then we allow all algorithms. */
1126 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001127 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129}
1130
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001131static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1132{
1133 int i;
1134
1135 if (nr > XFRM_MAX_DEPTH)
1136 return -EINVAL;
1137
1138 for (i = 0; i < nr; i++) {
1139 /* We never validated the ut->family value, so many
1140 * applications simply leave it at zero. The check was
1141 * never made and ut->family was ignored because all
1142 * templates could be assumed to have the same family as
1143 * the policy itself. Now that we will have ipv4-in-ipv6
1144 * and ipv6-in-ipv4 tunnels, this is no longer true.
1145 */
1146 if (!ut[i].family)
1147 ut[i].family = family;
1148
1149 switch (ut[i].family) {
1150 case AF_INET:
1151 break;
1152#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1153 case AF_INET6:
1154 break;
1155#endif
1156 default:
1157 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001158 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001159 }
1160
1161 return 0;
1162}
1163
Thomas Graf5424f322007-08-22 14:01:33 -07001164static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Thomas Graf5424f322007-08-22 14:01:33 -07001166 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 if (!rt) {
1169 pol->xfrm_nr = 0;
1170 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001171 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1172 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001173 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001175 err = validate_tmpl(nr, utmpl, pol->family);
1176 if (err)
1177 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Graf5424f322007-08-22 14:01:33 -07001179 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 return 0;
1182}
1183
Thomas Graf5424f322007-08-22 14:01:33 -07001184static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185{
Thomas Graf5424f322007-08-22 14:01:33 -07001186 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001187 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001188 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001189 int err;
1190
1191 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001192 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001193 type = upt->type;
1194 }
1195
1196 err = verify_policy_type(type);
1197 if (err)
1198 return err;
1199
1200 *tp = type;
1201 return 0;
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1205{
1206 xp->priority = p->priority;
1207 xp->index = p->index;
1208 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1209 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1210 xp->action = p->action;
1211 xp->flags = p->flags;
1212 xp->family = p->sel.family;
1213 /* XXX xp->share = p->share; */
1214}
1215
1216static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1217{
1218 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1219 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1220 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1221 p->priority = xp->priority;
1222 p->index = xp->index;
1223 p->sel.family = xp->family;
1224 p->dir = dir;
1225 p->action = xp->action;
1226 p->flags = xp->flags;
1227 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1228}
1229
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001230static 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 -07001231{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001232 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int err;
1234
1235 if (!xp) {
1236 *errp = -ENOMEM;
1237 return NULL;
1238 }
1239
1240 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001241
Thomas Graf35a7aa02007-08-22 14:00:40 -07001242 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001243 if (err)
1244 goto error;
1245
Thomas Graf35a7aa02007-08-22 14:00:40 -07001246 if (!(err = copy_from_user_tmpl(xp, attrs)))
1247 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001248 if (err)
1249 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001251 xfrm_mark_get(attrs, &xp->mark);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001254 error:
1255 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001256 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001257 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Christoph Hellwig22e70052007-01-02 15:22:30 -08001261static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001262 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001264 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001265 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001267 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int err;
1269 int excl;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001270 uid_t loginuid = audit_get_loginuid(current);
1271 u32 sessionid = audit_get_sessionid(current);
1272 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 err = verify_newpolicy_info(p);
1275 if (err)
1276 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001277 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001278 if (err)
1279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001281 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!xp)
1283 return err;
1284
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001285 /* shouldnt excl be based on nlh flags??
1286 * Aha! this is anti-netlink really i.e more pfkey derived
1287 * in netlink excl is a flag and you wouldnt need
1288 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1290 err = xfrm_policy_insert(p->dir, xp, excl);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001291 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001292 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001295 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 kfree(xp);
1297 return err;
1298 }
1299
Herbert Xuf60f6b82005-06-18 22:44:37 -07001300 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001301 c.seq = nlh->nlmsg_seq;
1302 c.pid = nlh->nlmsg_pid;
1303 km_policy_notify(xp, p->dir, &c);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 xfrm_pol_put(xp);
1306
1307 return 0;
1308}
1309
1310static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1311{
1312 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1313 int i;
1314
1315 if (xp->xfrm_nr == 0)
1316 return 0;
1317
1318 for (i = 0; i < xp->xfrm_nr; i++) {
1319 struct xfrm_user_tmpl *up = &vec[i];
1320 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1321
1322 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001323 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1325 up->reqid = kp->reqid;
1326 up->mode = kp->mode;
1327 up->share = kp->share;
1328 up->optional = kp->optional;
1329 up->aalgos = kp->aalgos;
1330 up->ealgos = kp->ealgos;
1331 up->calgos = kp->calgos;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Thomas Grafc0144be2007-08-22 13:55:43 -07001334 return nla_put(skb, XFRMA_TMPL,
1335 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001336}
1337
Serge Hallyn0d681622006-07-24 23:30:44 -07001338static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1339{
1340 if (x->security) {
1341 return copy_sec_ctx(x->security, skb);
1342 }
1343 return 0;
1344}
1345
1346static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1347{
1348 if (xp->security) {
1349 return copy_sec_ctx(xp->security, skb);
1350 }
1351 return 0;
1352}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001353static inline size_t userpolicy_type_attrsize(void)
1354{
1355#ifdef CONFIG_XFRM_SUB_POLICY
1356 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1357#else
1358 return 0;
1359#endif
1360}
Serge Hallyn0d681622006-07-24 23:30:44 -07001361
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001362#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001363static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001364{
Thomas Grafc0144be2007-08-22 13:55:43 -07001365 struct xfrm_userpolicy_type upt = {
1366 .type = type,
1367 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001368
Thomas Grafc0144be2007-08-22 13:55:43 -07001369 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001370}
1371
1372#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001373static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001374{
1375 return 0;
1376}
1377#endif
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1380{
1381 struct xfrm_dump_info *sp = ptr;
1382 struct xfrm_userpolicy_info *p;
1383 struct sk_buff *in_skb = sp->in_skb;
1384 struct sk_buff *skb = sp->out_skb;
1385 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001387 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1388 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1389 if (nlh == NULL)
1390 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Thomas Graf7b67c852007-08-22 13:53:52 -07001392 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 copy_to_user_policy(xp, p, dir);
1394 if (copy_to_user_tmpl(xp, skb) < 0)
1395 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001396 if (copy_to_user_sec_ctx(xp, skb))
1397 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001398 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001399 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001400 if (xfrm_mark_put(skb, &xp->mark))
1401 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Thomas Graf98250692007-08-22 12:47:26 -07001403 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return 0;
1405
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001406nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001408 nlmsg_cancel(skb, nlh);
1409 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Timo Teras4c563f72008-02-28 21:31:08 -08001412static int xfrm_dump_policy_done(struct netlink_callback *cb)
1413{
1414 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1415
1416 xfrm_policy_walk_done(walk);
1417 return 0;
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1421{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001422 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001423 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 struct xfrm_dump_info info;
1425
Timo Teras4c563f72008-02-28 21:31:08 -08001426 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1427 sizeof(cb->args) - sizeof(cb->args[0]));
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 info.in_skb = cb->skb;
1430 info.out_skb = skb;
1431 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1432 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001433
1434 if (!cb->args[0]) {
1435 cb->args[0] = 1;
1436 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1437 }
1438
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001439 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 return skb->len;
1442}
1443
1444static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1445 struct xfrm_policy *xp,
1446 int dir, u32 seq)
1447{
1448 struct xfrm_dump_info info;
1449 struct sk_buff *skb;
1450
Thomas Graf7deb2262007-08-22 13:57:39 -07001451 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!skb)
1453 return ERR_PTR(-ENOMEM);
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 info.in_skb = in_skb;
1456 info.out_skb = skb;
1457 info.nlmsg_seq = seq;
1458 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1461 kfree_skb(skb);
1462 return NULL;
1463 }
1464
1465 return skb;
1466}
1467
Christoph Hellwig22e70052007-01-02 15:22:30 -08001468static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001469 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001471 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 struct xfrm_policy *xp;
1473 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001474 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001476 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001478 struct xfrm_mark m;
1479 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Thomas Graf7b67c852007-08-22 13:53:52 -07001481 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1483
Thomas Graf35a7aa02007-08-22 14:00:40 -07001484 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001485 if (err)
1486 return err;
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 err = verify_policy_dir(p->dir);
1489 if (err)
1490 return err;
1491
1492 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001493 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001494 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001495 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001496 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001497
Thomas Graf35a7aa02007-08-22 14:00:40 -07001498 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001499 if (err)
1500 return err;
1501
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001502 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001503 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001504 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001505
Paul Moore03e1ad72008-04-12 19:07:52 -07001506 err = security_xfrm_policy_alloc(&ctx, uctx);
1507 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001508 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001509 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001510 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001511 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001512 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (xp == NULL)
1515 return -ENOENT;
1516
1517 if (!delete) {
1518 struct sk_buff *resp_skb;
1519
1520 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1521 if (IS_ERR(resp_skb)) {
1522 err = PTR_ERR(resp_skb);
1523 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001524 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001525 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001527 } else {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001528 uid_t loginuid = audit_get_loginuid(current);
1529 u32 sessionid = audit_get_sessionid(current);
1530 u32 sid;
Eric Paris25323862008-04-18 10:09:25 -04001531
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001532 security_task_getsecid(current, &sid);
Eric Paris25323862008-04-18 10:09:25 -04001533 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1534 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001535
1536 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001537 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001538
Herbert Xue7443892005-06-18 22:44:18 -07001539 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001540 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001541 c.seq = nlh->nlmsg_seq;
1542 c.pid = nlh->nlmsg_pid;
1543 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001546out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001547 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return err;
1549}
1550
Christoph Hellwig22e70052007-01-02 15:22:30 -08001551static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001552 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001554 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001555 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001556 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001557 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001558 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001560 audit_info.loginuid = audit_get_loginuid(current);
1561 audit_info.sessionid = audit_get_sessionid(current);
1562 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001563 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001564 if (err) {
1565 if (err == -ESRCH) /* empty table */
1566 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001567 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001568 }
Herbert Xubf088672005-06-18 22:44:00 -07001569 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001570 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001571 c.seq = nlh->nlmsg_seq;
1572 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001573 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001574 km_state_notify(NULL, &c);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return 0;
1577}
1578
Thomas Graf7deb2262007-08-22 13:57:39 -07001579static inline size_t xfrm_aevent_msgsize(void)
1580{
1581 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1582 + nla_total_size(sizeof(struct xfrm_replay_state))
1583 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001584 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001585 + nla_total_size(4) /* XFRM_AE_RTHR */
1586 + nla_total_size(4); /* XFRM_AE_ETHR */
1587}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001588
David S. Miller214e0052011-02-24 00:02:38 -05001589static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001590{
1591 struct xfrm_aevent_id *id;
1592 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001593
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001594 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1595 if (nlh == NULL)
1596 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001597
Thomas Graf7b67c852007-08-22 13:53:52 -07001598 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001599 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001600 id->sa_id.spi = x->id.spi;
1601 id->sa_id.family = x->props.family;
1602 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001603 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1604 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001605 id->flags = c->data.aevent;
1606
Thomas Grafc0144be2007-08-22 13:55:43 -07001607 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1608 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001609
Thomas Grafc0144be2007-08-22 13:55:43 -07001610 if (id->flags & XFRM_AE_RTHR)
1611 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001612
Thomas Grafc0144be2007-08-22 13:55:43 -07001613 if (id->flags & XFRM_AE_ETHR)
1614 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1615 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001616
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001617 if (xfrm_mark_put(skb, &x->mark))
1618 goto nla_put_failure;
1619
Thomas Graf98250692007-08-22 12:47:26 -07001620 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001621
Thomas Grafc0144be2007-08-22 13:55:43 -07001622nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001623 nlmsg_cancel(skb, nlh);
1624 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001625}
1626
Christoph Hellwig22e70052007-01-02 15:22:30 -08001627static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001628 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001629{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001630 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001631 struct xfrm_state *x;
1632 struct sk_buff *r_skb;
1633 int err;
1634 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001635 u32 mark;
1636 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001637 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001638 struct xfrm_usersa_id *id = &p->sa_id;
1639
Thomas Graf7deb2262007-08-22 13:57:39 -07001640 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001641 if (r_skb == NULL)
1642 return -ENOMEM;
1643
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001644 mark = xfrm_mark_get(attrs, &m);
1645
1646 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001647 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001648 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001649 return -ESRCH;
1650 }
1651
1652 /*
1653 * XXX: is this lock really needed - none of the other
1654 * gets lock (the concern is things getting updated
1655 * while we are still reading) - jhs
1656 */
1657 spin_lock_bh(&x->lock);
1658 c.data.aevent = p->flags;
1659 c.seq = nlh->nlmsg_seq;
1660 c.pid = nlh->nlmsg_pid;
1661
1662 if (build_aevent(r_skb, x, &c) < 0)
1663 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001664 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001665 spin_unlock_bh(&x->lock);
1666 xfrm_state_put(x);
1667 return err;
1668}
1669
Christoph Hellwig22e70052007-01-02 15:22:30 -08001670static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001671 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001672{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001673 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001674 struct xfrm_state *x;
1675 struct km_event c;
1676 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001677 u32 mark = 0;
1678 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001679 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001680 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1681 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001682
1683 if (!lt && !rp)
1684 return err;
1685
1686 /* pedantic mode - thou shalt sayeth replaceth */
1687 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1688 return err;
1689
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001690 mark = xfrm_mark_get(attrs, &m);
1691
1692 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 -08001693 if (x == NULL)
1694 return -ESRCH;
1695
1696 if (x->km.state != XFRM_STATE_VALID)
1697 goto out;
1698
1699 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001700 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001701 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001702
1703 c.event = nlh->nlmsg_type;
1704 c.seq = nlh->nlmsg_seq;
1705 c.pid = nlh->nlmsg_pid;
1706 c.data.aevent = XFRM_AE_CU;
1707 km_state_notify(x, &c);
1708 err = 0;
1709out:
1710 xfrm_state_put(x);
1711 return err;
1712}
1713
Christoph Hellwig22e70052007-01-02 15:22:30 -08001714static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001715 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001717 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001718 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001719 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001720 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001721 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001722
Thomas Graf35a7aa02007-08-22 14:00:40 -07001723 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001724 if (err)
1725 return err;
1726
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001727 audit_info.loginuid = audit_get_loginuid(current);
1728 audit_info.sessionid = audit_get_sessionid(current);
1729 security_task_getsecid(current, &audit_info.secid);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001730 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001731 if (err) {
1732 if (err == -ESRCH) /* empty table */
1733 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001734 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001735 }
1736
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001737 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001738 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001739 c.seq = nlh->nlmsg_seq;
1740 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001741 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001742 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return 0;
1744}
1745
Christoph Hellwig22e70052007-01-02 15:22:30 -08001746static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001747 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001748{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001749 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001750 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001751 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001752 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001753 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001754 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001755 struct xfrm_mark m;
1756 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001757
Thomas Graf35a7aa02007-08-22 14:00:40 -07001758 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001759 if (err)
1760 return err;
1761
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001762 err = verify_policy_dir(p->dir);
1763 if (err)
1764 return err;
1765
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001766 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001767 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001768 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001769 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001770 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001771
Thomas Graf35a7aa02007-08-22 14:00:40 -07001772 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001773 if (err)
1774 return err;
1775
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001776 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001777 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001778 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001779
Paul Moore03e1ad72008-04-12 19:07:52 -07001780 err = security_xfrm_policy_alloc(&ctx, uctx);
1781 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001782 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001783 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001784 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001785 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001786 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001787 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001788 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001789 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001790
Timo Teräsea2dea92010-03-31 00:17:05 +00001791 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001792 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001793
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001794 err = 0;
1795 if (up->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001796 uid_t loginuid = audit_get_loginuid(current);
1797 u32 sessionid = audit_get_sessionid(current);
1798 u32 sid;
1799
1800 security_task_getsecid(current, &sid);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001801 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001802 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001803
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001804 } else {
1805 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001806 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001807 }
1808 km_policy_expired(xp, p->dir, up->hard, current->pid);
1809
1810out:
1811 xfrm_pol_put(xp);
1812 return err;
1813}
1814
Christoph Hellwig22e70052007-01-02 15:22:30 -08001815static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001816 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001817{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001818 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001819 struct xfrm_state *x;
1820 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001821 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001822 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001823 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001824 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001825
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001826 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 -08001827
David S. Miller3a765aa2007-02-26 14:52:21 -08001828 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001829 if (x == NULL)
1830 return err;
1831
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001832 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001833 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001834 if (x->km.state != XFRM_STATE_VALID)
1835 goto out;
1836 km_state_expired(x, ue->hard, current->pid);
1837
Joy Latten161a09e2006-11-27 13:11:54 -06001838 if (ue->hard) {
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001839 uid_t loginuid = audit_get_loginuid(current);
1840 u32 sessionid = audit_get_sessionid(current);
1841 u32 sid;
1842
1843 security_task_getsecid(current, &sid);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001844 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001845 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001846 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001847 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001848out:
1849 spin_unlock_bh(&x->lock);
1850 xfrm_state_put(x);
1851 return err;
1852}
1853
Christoph Hellwig22e70052007-01-02 15:22:30 -08001854static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001855 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001856{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001857 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001858 struct xfrm_policy *xp;
1859 struct xfrm_user_tmpl *ut;
1860 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001861 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001862 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001863
Thomas Graf7b67c852007-08-22 13:53:52 -07001864 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001865 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001866 int err = -ENOMEM;
1867
1868 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001869 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001870
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001871 xfrm_mark_get(attrs, &mark);
1872
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001873 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001874 if (err)
1875 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001876
1877 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001878 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001879 if (!xp)
1880 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001881
1882 memcpy(&x->id, &ua->id, sizeof(ua->id));
1883 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1884 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001885 xp->mark.m = x->mark.m = mark.m;
1886 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001887 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001888 /* extract the templates and for each call km_key */
1889 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1890 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1891 memcpy(&x->id, &t->id, sizeof(x->id));
1892 x->props.mode = t->mode;
1893 x->props.reqid = t->reqid;
1894 x->props.family = ut->family;
1895 t->aalgos = ua->aalgos;
1896 t->ealgos = ua->ealgos;
1897 t->calgos = ua->calgos;
1898 err = km_query(x, t, xp);
1899
1900 }
1901
1902 kfree(x);
1903 kfree(xp);
1904
1905 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001906
1907bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001908 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001909free_state:
1910 kfree(x);
1911nomem:
1912 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001913}
1914
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001915#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001916static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001917 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001918 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001919{
Thomas Graf5424f322007-08-22 14:01:33 -07001920 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001921 struct xfrm_user_migrate *um;
1922 int i, num_migrate;
1923
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001924 if (k != NULL) {
1925 struct xfrm_user_kmaddress *uk;
1926
1927 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1928 memcpy(&k->local, &uk->local, sizeof(k->local));
1929 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1930 k->family = uk->family;
1931 k->reserved = uk->reserved;
1932 }
1933
Thomas Graf5424f322007-08-22 14:01:33 -07001934 um = nla_data(rt);
1935 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001936
1937 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1938 return -EINVAL;
1939
1940 for (i = 0; i < num_migrate; i++, um++, ma++) {
1941 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1942 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1943 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1944 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1945
1946 ma->proto = um->proto;
1947 ma->mode = um->mode;
1948 ma->reqid = um->reqid;
1949
1950 ma->old_family = um->old_family;
1951 ma->new_family = um->new_family;
1952 }
1953
1954 *num = i;
1955 return 0;
1956}
1957
1958static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001959 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001960{
Thomas Graf7b67c852007-08-22 13:53:52 -07001961 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001962 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001963 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001964 u8 type;
1965 int err;
1966 int n = 0;
1967
Thomas Graf35a7aa02007-08-22 14:00:40 -07001968 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001969 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001970
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001971 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1972
Thomas Graf5424f322007-08-22 14:01:33 -07001973 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001974 if (err)
1975 return err;
1976
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001977 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001978 if (err)
1979 return err;
1980
1981 if (!n)
1982 return 0;
1983
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001984 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001985
1986 return 0;
1987}
1988#else
1989static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001990 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001991{
1992 return -ENOPROTOOPT;
1993}
1994#endif
1995
1996#ifdef CONFIG_XFRM_MIGRATE
David S. Miller183cad12011-02-24 00:28:01 -05001997static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001998{
1999 struct xfrm_user_migrate um;
2000
2001 memset(&um, 0, sizeof(um));
2002 um.proto = m->proto;
2003 um.mode = m->mode;
2004 um.reqid = m->reqid;
2005 um.old_family = m->old_family;
2006 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
2007 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2008 um.new_family = m->new_family;
2009 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2010 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2011
Thomas Grafc0144be2007-08-22 13:55:43 -07002012 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002013}
2014
David S. Miller183cad12011-02-24 00:28:01 -05002015static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002016{
2017 struct xfrm_user_kmaddress uk;
2018
2019 memset(&uk, 0, sizeof(uk));
2020 uk.family = k->family;
2021 uk.reserved = k->reserved;
2022 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002023 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002024
2025 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2026}
2027
2028static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002029{
2030 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002031 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2032 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2033 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002034}
2035
David S. Miller183cad12011-02-24 00:28:01 -05002036static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
2037 int num_migrate, const struct xfrm_kmaddress *k,
2038 const struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002039{
David S. Miller183cad12011-02-24 00:28:01 -05002040 const struct xfrm_migrate *mp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002041 struct xfrm_userpolicy_id *pol_id;
2042 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002043 int i;
2044
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002045 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2046 if (nlh == NULL)
2047 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002048
Thomas Graf7b67c852007-08-22 13:53:52 -07002049 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002050 /* copy data from selector, dir, and type to the pol_id */
2051 memset(pol_id, 0, sizeof(*pol_id));
2052 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2053 pol_id->dir = dir;
2054
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002055 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2056 goto nlmsg_failure;
2057
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002058 if (copy_to_user_policy_type(type, skb) < 0)
2059 goto nlmsg_failure;
2060
2061 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2062 if (copy_to_user_migrate(mp, skb) < 0)
2063 goto nlmsg_failure;
2064 }
2065
Thomas Graf98250692007-08-22 12:47:26 -07002066 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002068 nlmsg_cancel(skb, nlh);
2069 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002070}
2071
David S. Miller183cad12011-02-24 00:28:01 -05002072static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2073 const struct xfrm_migrate *m, int num_migrate,
2074 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002075{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002076 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002077 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002078
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002079 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002080 if (skb == NULL)
2081 return -ENOMEM;
2082
2083 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002084 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002085 BUG();
2086
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002087 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002088}
2089#else
David S. Miller183cad12011-02-24 00:28:01 -05002090static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
2091 const struct xfrm_migrate *m, int num_migrate,
2092 const struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002093{
2094 return -ENOPROTOOPT;
2095}
2096#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002097
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002098#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002099
2100static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002101 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002102 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2103 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2104 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2105 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2106 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2107 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002108 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002109 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002110 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002111 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002112 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002113 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002114 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002115 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2116 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002117 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002118 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002119 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2120 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121};
2122
Thomas Graf492b5582005-05-03 14:26:40 -07002123#undef XMSGSIZE
2124
Thomas Grafcf5cb792007-08-22 13:59:04 -07002125static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002126 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2127 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2128 [XFRMA_LASTUSED] = { .type = NLA_U64},
2129 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002130 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002131 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2132 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2133 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2134 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2135 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2136 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2137 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2138 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2139 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2140 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2141 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2142 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2143 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2144 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002145 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002146 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002147 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002148};
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002151 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002153 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002154} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2155 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2156 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2157 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002158 .dump = xfrm_dump_sa,
2159 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002160 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2161 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2162 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002163 .dump = xfrm_dump_policy,
2164 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002165 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002166 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002167 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002168 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2169 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002170 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002171 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2172 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002173 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2174 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002175 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002176 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002177 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178};
2179
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002180static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002182 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002183 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002185 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 type -= XFRM_MSG_BASE;
2192 link = &xfrm_dispatch[type];
2193
2194 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002195 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2196 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Thomas Graf492b5582005-05-03 14:26:40 -07002198 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2199 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002200 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002204 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206
Thomas Graf35a7aa02007-08-22 14:00:40 -07002207 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002208 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002209 if (err < 0)
2210 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
2212 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Thomas Graf5424f322007-08-22 14:01:33 -07002215 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002218static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002220 mutex_lock(&xfrm_cfg_mutex);
2221 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2222 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Thomas Graf7deb2262007-08-22 13:57:39 -07002225static inline size_t xfrm_expire_msgsize(void)
2226{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002227 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2228 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002229}
2230
David S. Miller214e0052011-02-24 00:02:38 -05002231static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 struct xfrm_user_expire *ue;
2234 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002236 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2237 if (nlh == NULL)
2238 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Thomas Graf7b67c852007-08-22 13:53:52 -07002240 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002242 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002244 if (xfrm_mark_put(skb, &x->mark))
2245 goto nla_put_failure;
2246
Thomas Graf98250692007-08-22 12:47:26 -07002247 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002248
2249nla_put_failure:
2250 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
David S. Miller214e0052011-02-24 00:02:38 -05002253static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002255 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct sk_buff *skb;
2257
Thomas Graf7deb2262007-08-22 13:57:39 -07002258 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (skb == NULL)
2260 return -ENOMEM;
2261
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002262 if (build_expire(skb, x, c) < 0) {
2263 kfree_skb(skb);
2264 return -EMSGSIZE;
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002267 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
David S. Miller214e0052011-02-24 00:02:38 -05002270static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002271{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002272 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002273 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002274
Thomas Graf7deb2262007-08-22 13:57:39 -07002275 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002276 if (skb == NULL)
2277 return -ENOMEM;
2278
2279 if (build_aevent(skb, x, c) < 0)
2280 BUG();
2281
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002282 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002283}
2284
David S. Miller214e0052011-02-24 00:02:38 -05002285static int xfrm_notify_sa_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002286{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002287 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002288 struct xfrm_usersa_flush *p;
2289 struct nlmsghdr *nlh;
2290 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002291 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002292
Thomas Graf7deb2262007-08-22 13:57:39 -07002293 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002294 if (skb == NULL)
2295 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002296
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002297 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2298 if (nlh == NULL) {
2299 kfree_skb(skb);
2300 return -EMSGSIZE;
2301 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002302
Thomas Graf7b67c852007-08-22 13:53:52 -07002303 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002304 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305
Thomas Graf98250692007-08-22 12:47:26 -07002306 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002307
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002308 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309}
2310
Thomas Graf7deb2262007-08-22 13:57:39 -07002311static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312{
Thomas Graf7deb2262007-08-22 13:57:39 -07002313 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002314 if (x->aead)
2315 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002316 if (x->aalg) {
2317 l += nla_total_size(sizeof(struct xfrm_algo) +
2318 (x->aalg->alg_key_len + 7) / 8);
2319 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2320 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002322 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002324 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002326 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002327 if (x->tfcpad)
2328 l += nla_total_size(sizeof(x->tfcpad));
Herbert Xu68325d32007-10-09 13:30:57 -07002329 if (x->security)
2330 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2331 x->security->ctx_len);
2332 if (x->coaddr)
2333 l += nla_total_size(sizeof(*x->coaddr));
2334
Herbert Xud26f3982007-11-13 21:47:08 -08002335 /* Must count x->lastused as it may become non-zero behind our back. */
2336 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002337
2338 return l;
2339}
2340
David S. Miller214e0052011-02-24 00:02:38 -05002341static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002342{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002343 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002344 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002345 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002346 struct nlmsghdr *nlh;
2347 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002349 int headlen;
2350
2351 headlen = sizeof(*p);
2352 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002353 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002354 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002355 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002356 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002357 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358
Thomas Graf7deb2262007-08-22 13:57:39 -07002359 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002360 if (skb == NULL)
2361 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002362
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002363 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2364 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002365 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002366
Thomas Graf7b67c852007-08-22 13:53:52 -07002367 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002368 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002369 struct nlattr *attr;
2370
Thomas Graf7b67c852007-08-22 13:53:52 -07002371 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002372 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2373 id->spi = x->id.spi;
2374 id->family = x->props.family;
2375 id->proto = x->id.proto;
2376
Thomas Grafc0144be2007-08-22 13:55:43 -07002377 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2378 if (attr == NULL)
2379 goto nla_put_failure;
2380
2381 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002382 }
2383
Herbert Xu68325d32007-10-09 13:30:57 -07002384 if (copy_to_user_state_extra(x, p, skb))
2385 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386
Thomas Graf98250692007-08-22 12:47:26 -07002387 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002389 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002390
Thomas Grafc0144be2007-08-22 13:55:43 -07002391nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002392 /* Somebody screwed up with xfrm_sa_len! */
2393 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394 kfree_skb(skb);
2395 return -1;
2396}
2397
David S. Miller214e0052011-02-24 00:02:38 -05002398static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399{
2400
2401 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002402 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002404 case XFRM_MSG_NEWAE:
2405 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002406 case XFRM_MSG_DELSA:
2407 case XFRM_MSG_UPDSA:
2408 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002409 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002410 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411 return xfrm_notify_sa_flush(c);
2412 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002413 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2414 c->event);
2415 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002416 }
2417
2418 return 0;
2419
2420}
2421
Thomas Graf7deb2262007-08-22 13:57:39 -07002422static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2423 struct xfrm_policy *xp)
2424{
2425 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2426 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002427 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002428 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2429 + userpolicy_type_attrsize();
2430}
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2433 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2434 int dir)
2435{
2436 struct xfrm_user_acquire *ua;
2437 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 __u32 seq = xfrm_get_acqseq();
2439
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002440 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2441 if (nlh == NULL)
2442 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Thomas Graf7b67c852007-08-22 13:53:52 -07002444 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 memcpy(&ua->id, &x->id, sizeof(ua->id));
2446 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2447 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2448 copy_to_user_policy(xp, &ua->policy, dir);
2449 ua->aalgos = xt->aalgos;
2450 ua->ealgos = xt->ealgos;
2451 ua->calgos = xt->calgos;
2452 ua->seq = x->km.seq = seq;
2453
2454 if (copy_to_user_tmpl(xp, skb) < 0)
2455 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002456 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002457 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002458 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002459 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002460 if (xfrm_mark_put(skb, &xp->mark))
2461 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Thomas Graf98250692007-08-22 12:47:26 -07002463 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002465nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002467 nlmsg_cancel(skb, nlh);
2468 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
2471static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2472 struct xfrm_policy *xp, int dir)
2473{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002474 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Thomas Graf7deb2262007-08-22 13:57:39 -07002477 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (skb == NULL)
2479 return -ENOMEM;
2480
2481 if (build_acquire(skb, x, xt, xp, dir) < 0)
2482 BUG();
2483
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002484 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
2487/* User gives us xfrm_user_policy_info followed by an array of 0
2488 * or more templates.
2489 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002490static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 u8 *data, int len, int *dir)
2492{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002493 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2495 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2496 struct xfrm_policy *xp;
2497 int nr;
2498
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002499 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 case AF_INET:
2501 if (opt != IP_XFRM_POLICY) {
2502 *dir = -EOPNOTSUPP;
2503 return NULL;
2504 }
2505 break;
2506#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2507 case AF_INET6:
2508 if (opt != IPV6_XFRM_POLICY) {
2509 *dir = -EOPNOTSUPP;
2510 return NULL;
2511 }
2512 break;
2513#endif
2514 default:
2515 *dir = -EINVAL;
2516 return NULL;
2517 }
2518
2519 *dir = -EINVAL;
2520
2521 if (len < sizeof(*p) ||
2522 verify_newpolicy_info(p))
2523 return NULL;
2524
2525 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002526 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return NULL;
2528
Herbert Xua4f1bac2005-07-26 15:43:17 -07002529 if (p->dir > XFRM_POLICY_OUT)
2530 return NULL;
2531
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002532 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (xp == NULL) {
2534 *dir = -ENOBUFS;
2535 return NULL;
2536 }
2537
2538 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002539 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 copy_templates(xp, ut, nr);
2541
2542 *dir = p->dir;
2543
2544 return xp;
2545}
2546
Thomas Graf7deb2262007-08-22 13:57:39 -07002547static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2548{
2549 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2550 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2551 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002552 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002553 + userpolicy_type_attrsize();
2554}
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
David S. Miller214e0052011-02-24 00:02:38 -05002557 int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 struct xfrm_user_polexpire *upe;
2560 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002561 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002563 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2564 if (nlh == NULL)
2565 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Thomas Graf7b67c852007-08-22 13:53:52 -07002567 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 copy_to_user_policy(xp, &upe->pol, dir);
2569 if (copy_to_user_tmpl(xp, skb) < 0)
2570 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002571 if (copy_to_user_sec_ctx(xp, skb))
2572 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002573 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002574 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002575 if (xfrm_mark_put(skb, &xp->mark))
2576 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 upe->hard = !!hard;
2578
Thomas Graf98250692007-08-22 12:47:26 -07002579 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002581nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002583 nlmsg_cancel(skb, nlh);
2584 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
David S. Miller214e0052011-02-24 00:02:38 -05002587static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002589 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Thomas Graf7deb2262007-08-22 13:57:39 -07002592 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (skb == NULL)
2594 return -ENOMEM;
2595
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002596 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 BUG();
2598
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002599 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
David S. Miller214e0052011-02-24 00:02:38 -05002602static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002603{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002604 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002605 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002606 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002607 struct nlmsghdr *nlh;
2608 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002609 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002610 int headlen;
2611
2612 headlen = sizeof(*p);
2613 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002614 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002615 headlen = sizeof(*id);
2616 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002617 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002618 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002619 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002620
Thomas Graf7deb2262007-08-22 13:57:39 -07002621 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002622 if (skb == NULL)
2623 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002624
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002625 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2626 if (nlh == NULL)
2627 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002628
Thomas Graf7b67c852007-08-22 13:53:52 -07002629 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002630 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002631 struct nlattr *attr;
2632
Thomas Graf7b67c852007-08-22 13:53:52 -07002633 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002634 memset(id, 0, sizeof(*id));
2635 id->dir = dir;
2636 if (c->data.byid)
2637 id->index = xp->index;
2638 else
2639 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2640
Thomas Grafc0144be2007-08-22 13:55:43 -07002641 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2642 if (attr == NULL)
2643 goto nlmsg_failure;
2644
2645 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002646 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002647
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002648 copy_to_user_policy(xp, p, dir);
2649 if (copy_to_user_tmpl(xp, skb) < 0)
2650 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002651 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002652 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002653
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002654 if (xfrm_mark_put(skb, &xp->mark))
2655 goto nla_put_failure;
2656
Thomas Graf98250692007-08-22 12:47:26 -07002657 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002658
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002659 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002661nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002662nlmsg_failure:
2663 kfree_skb(skb);
2664 return -1;
2665}
2666
David S. Miller214e0052011-02-24 00:02:38 -05002667static int xfrm_notify_policy_flush(const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002668{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002669 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002670 struct nlmsghdr *nlh;
2671 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672
Thomas Graf7deb2262007-08-22 13:57:39 -07002673 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674 if (skb == NULL)
2675 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002677 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2678 if (nlh == NULL)
2679 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002680 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2681 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002682
Thomas Graf98250692007-08-22 12:47:26 -07002683 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002684
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002685 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002686
2687nlmsg_failure:
2688 kfree_skb(skb);
2689 return -1;
2690}
2691
David S. Miller214e0052011-02-24 00:02:38 -05002692static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693{
2694
2695 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002696 case XFRM_MSG_NEWPOLICY:
2697 case XFRM_MSG_UPDPOLICY:
2698 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002699 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002700 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002701 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002702 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002703 return xfrm_exp_policy_notify(xp, dir, c);
2704 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002705 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2706 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002707 }
2708
2709 return 0;
2710
2711}
2712
Thomas Graf7deb2262007-08-22 13:57:39 -07002713static inline size_t xfrm_report_msgsize(void)
2714{
2715 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2716}
2717
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002718static int build_report(struct sk_buff *skb, u8 proto,
2719 struct xfrm_selector *sel, xfrm_address_t *addr)
2720{
2721 struct xfrm_user_report *ur;
2722 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002723
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002724 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2725 if (nlh == NULL)
2726 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002727
Thomas Graf7b67c852007-08-22 13:53:52 -07002728 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002729 ur->proto = proto;
2730 memcpy(&ur->sel, sel, sizeof(ur->sel));
2731
2732 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002733 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002734
Thomas Graf98250692007-08-22 12:47:26 -07002735 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002736
Thomas Grafc0144be2007-08-22 13:55:43 -07002737nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002738 nlmsg_cancel(skb, nlh);
2739 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002740}
2741
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002742static int xfrm_send_report(struct net *net, u8 proto,
2743 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002744{
2745 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002746
Thomas Graf7deb2262007-08-22 13:57:39 -07002747 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002748 if (skb == NULL)
2749 return -ENOMEM;
2750
2751 if (build_report(skb, proto, sel, addr) < 0)
2752 BUG();
2753
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002754 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002755}
2756
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002757static inline size_t xfrm_mapping_msgsize(void)
2758{
2759 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2760}
2761
2762static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2763 xfrm_address_t *new_saddr, __be16 new_sport)
2764{
2765 struct xfrm_user_mapping *um;
2766 struct nlmsghdr *nlh;
2767
2768 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2769 if (nlh == NULL)
2770 return -EMSGSIZE;
2771
2772 um = nlmsg_data(nlh);
2773
2774 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2775 um->id.spi = x->id.spi;
2776 um->id.family = x->props.family;
2777 um->id.proto = x->id.proto;
2778 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2779 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2780 um->new_sport = new_sport;
2781 um->old_sport = x->encap->encap_sport;
2782 um->reqid = x->props.reqid;
2783
2784 return nlmsg_end(skb, nlh);
2785}
2786
2787static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2788 __be16 sport)
2789{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002790 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002791 struct sk_buff *skb;
2792
2793 if (x->id.proto != IPPROTO_ESP)
2794 return -EINVAL;
2795
2796 if (!x->encap)
2797 return -EINVAL;
2798
2799 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2800 if (skb == NULL)
2801 return -ENOMEM;
2802
2803 if (build_mapping(skb, x, ipaddr, sport) < 0)
2804 BUG();
2805
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002806 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002807}
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809static struct xfrm_mgr netlink_mgr = {
2810 .id = "netlink",
2811 .notify = xfrm_send_state_notify,
2812 .acquire = xfrm_send_acquire,
2813 .compile_policy = xfrm_compile_policy,
2814 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002815 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002816 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002817 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818};
2819
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002820static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Patrick McHardybe336902006-03-20 22:40:54 -08002822 struct sock *nlsk;
2823
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002824 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002825 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002826 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002828 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002829 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return 0;
2831}
2832
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002833static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002834{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002835 struct net *net;
2836 list_for_each_entry(net, net_exit_list, exit_list)
2837 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2838 synchronize_net();
2839 list_for_each_entry(net, net_exit_list, exit_list)
2840 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002841}
2842
2843static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002844 .init = xfrm_user_net_init,
2845 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002846};
2847
2848static int __init xfrm_user_init(void)
2849{
2850 int rv;
2851
2852 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2853
2854 rv = register_pernet_subsys(&xfrm_user_net_ops);
2855 if (rv < 0)
2856 return rv;
2857 rv = xfrm_register_km(&netlink_mgr);
2858 if (rv < 0)
2859 unregister_pernet_subsys(&xfrm_user_net_ops);
2860 return rv;
2861}
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863static void __exit xfrm_user_exit(void)
2864{
2865 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002866 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867}
2868
2869module_init(xfrm_user_init);
2870module_exit(xfrm_user_exit);
2871MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002872MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002873