blob: 61291965c5f60a0354ee499f664df340845bc9e7 [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,
237 struct xfrm_algo_desc *(*get_byname)(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;
Eric Paris25323862008-04-18 10:09:25 -0400500 uid_t loginuid = NETLINK_CB(skb).loginuid;
501 u32 sessionid = NETLINK_CB(skb).sessionid;
502 u32 sid = NETLINK_CB(skb).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
Eric Paris25323862008-04-18 10:09:25 -0400518 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (err < 0) {
521 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800522 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700526 c.seq = nlh->nlmsg_seq;
527 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700528 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700529
530 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700531out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700532 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return err;
534}
535
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800536static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
537 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700538 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700539 int *errp)
540{
541 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000542 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700543 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000544 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700545
546 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
547 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000548 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700549 } else {
550 xfrm_address_t *saddr = NULL;
551
Thomas Graf35a7aa02007-08-22 14:00:40 -0700552 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700553 if (!saddr) {
554 err = -EINVAL;
555 goto out;
556 }
557
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800558 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000559 x = xfrm_state_lookup_byaddr(net, mark,
560 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800561 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700562 }
563
564 out:
565 if (!x && errp)
566 *errp = err;
567 return x;
568}
569
Christoph Hellwig22e70052007-01-02 15:22:30 -0800570static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700571 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800573 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700575 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700576 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700577 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400578 uid_t loginuid = NETLINK_CB(skb).loginuid;
579 u32 sessionid = NETLINK_CB(skb).sessionid;
580 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800582 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
David S. Miller6f68dc32006-06-08 23:58:52 -0700586 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700587 goto out;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700590 err = -EPERM;
591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700594 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600595
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700596 if (err < 0)
597 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700598
599 c.seq = nlh->nlmsg_seq;
600 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700601 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700602 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700604out:
Eric Paris25323862008-04-18 10:09:25 -0400605 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700606 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
611{
612 memcpy(&p->id, &x->id, sizeof(p->id));
613 memcpy(&p->sel, &x->sel, sizeof(p->sel));
614 memcpy(&p->lft, &x->lft, sizeof(p->lft));
615 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
616 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700617 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 p->mode = x->props.mode;
619 p->replay_window = x->props.replay_window;
620 p->reqid = x->props.reqid;
621 p->family = x->props.family;
622 p->flags = x->props.flags;
623 p->seq = x->km.seq;
624}
625
626struct xfrm_dump_info {
627 struct sk_buff *in_skb;
628 struct sk_buff *out_skb;
629 u32 nlmsg_seq;
630 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631};
632
Thomas Grafc0144be2007-08-22 13:55:43 -0700633static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
634{
Thomas Grafc0144be2007-08-22 13:55:43 -0700635 struct xfrm_user_sec_ctx *uctx;
636 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700637 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700638
639 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
640 if (attr == NULL)
641 return -EMSGSIZE;
642
643 uctx = nla_data(attr);
644 uctx->exttype = XFRMA_SEC_CTX;
645 uctx->len = ctx_size;
646 uctx->ctx_doi = s->ctx_doi;
647 uctx->ctx_alg = s->ctx_alg;
648 uctx->ctx_len = s->ctx_len;
649 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
650
651 return 0;
652}
653
Martin Willi4447bb32009-11-25 00:29:52 +0000654static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
655{
656 struct xfrm_algo *algo;
657 struct nlattr *nla;
658
659 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
660 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
661 if (!nla)
662 return -EMSGSIZE;
663
664 algo = nla_data(nla);
665 strcpy(algo->alg_name, auth->alg_name);
666 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
667 algo->alg_key_len = auth->alg_key_len;
668
669 return 0;
670}
671
Herbert Xu68325d32007-10-09 13:30:57 -0700672/* Don't change this without updating xfrm_sa_len! */
673static int copy_to_user_state_extra(struct xfrm_state *x,
674 struct xfrm_usersa_info *p,
675 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 copy_to_user_state(x, p);
678
Herbert Xu050f0092007-10-09 13:31:47 -0700679 if (x->coaddr)
680 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
681
682 if (x->lastused)
683 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700684
Herbert Xu1a6509d2008-01-28 19:37:29 -0800685 if (x->aead)
686 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000687 if (x->aalg) {
688 if (copy_to_user_auth(x->aalg, skb))
689 goto nla_put_failure;
690
691 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
692 xfrm_alg_auth_len(x->aalg), x->aalg);
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800695 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700697 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700700 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Martin Willi35d28562010-12-08 04:37:49 +0000702 if (x->tfcpad)
703 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
704
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000705 if (xfrm_mark_put(skb, &x->mark))
706 goto nla_put_failure;
707
Thomas Grafc0144be2007-08-22 13:55:43 -0700708 if (x->security && copy_sec_ctx(x->security, skb) < 0)
709 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700710
Herbert Xu68325d32007-10-09 13:30:57 -0700711 return 0;
712
713nla_put_failure:
714 return -EMSGSIZE;
715}
716
717static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
718{
719 struct xfrm_dump_info *sp = ptr;
720 struct sk_buff *in_skb = sp->in_skb;
721 struct sk_buff *skb = sp->out_skb;
722 struct xfrm_usersa_info *p;
723 struct nlmsghdr *nlh;
724 int err;
725
Herbert Xu68325d32007-10-09 13:30:57 -0700726 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
727 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
728 if (nlh == NULL)
729 return -EMSGSIZE;
730
731 p = nlmsg_data(nlh);
732
733 err = copy_to_user_state_extra(x, p, skb);
734 if (err)
735 goto nla_put_failure;
736
Thomas Graf98250692007-08-22 12:47:26 -0700737 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739
Thomas Grafc0144be2007-08-22 13:55:43 -0700740nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700741 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700742 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Timo Teras4c563f72008-02-28 21:31:08 -0800745static int xfrm_dump_sa_done(struct netlink_callback *cb)
746{
747 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
748 xfrm_state_walk_done(walk);
749 return 0;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
753{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800754 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800755 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct xfrm_dump_info info;
757
Timo Teras4c563f72008-02-28 21:31:08 -0800758 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
759 sizeof(cb->args) - sizeof(cb->args[0]));
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 info.in_skb = cb->skb;
762 info.out_skb = skb;
763 info.nlmsg_seq = cb->nlh->nlmsg_seq;
764 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800765
766 if (!cb->args[0]) {
767 cb->args[0] = 1;
768 xfrm_state_walk_init(walk, 0);
769 }
770
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800771 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return skb->len;
774}
775
776static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
777 struct xfrm_state *x, u32 seq)
778{
779 struct xfrm_dump_info info;
780 struct sk_buff *skb;
781
Thomas Graf7deb2262007-08-22 13:57:39 -0700782 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!skb)
784 return ERR_PTR(-ENOMEM);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 info.in_skb = in_skb;
787 info.out_skb = skb;
788 info.nlmsg_seq = seq;
789 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (dump_one_state(x, 0, &info)) {
792 kfree_skb(skb);
793 return NULL;
794 }
795
796 return skb;
797}
798
Thomas Graf7deb2262007-08-22 13:57:39 -0700799static inline size_t xfrm_spdinfo_msgsize(void)
800{
801 return NLMSG_ALIGN(4)
802 + nla_total_size(sizeof(struct xfrmu_spdinfo))
803 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
804}
805
Alexey Dobriyane0710412010-01-23 13:37:10 +0000806static int build_spdinfo(struct sk_buff *skb, struct net *net,
807 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700808{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700809 struct xfrmk_spdinfo si;
810 struct xfrmu_spdinfo spc;
811 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700812 struct nlmsghdr *nlh;
813 u32 *f;
814
815 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
816 if (nlh == NULL) /* shouldnt really happen ... */
817 return -EMSGSIZE;
818
819 f = nlmsg_data(nlh);
820 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000821 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700822 spc.incnt = si.incnt;
823 spc.outcnt = si.outcnt;
824 spc.fwdcnt = si.fwdcnt;
825 spc.inscnt = si.inscnt;
826 spc.outscnt = si.outscnt;
827 spc.fwdscnt = si.fwdscnt;
828 sph.spdhcnt = si.spdhcnt;
829 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700830
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700831 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
832 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700833
834 return nlmsg_end(skb, nlh);
835
836nla_put_failure:
837 nlmsg_cancel(skb, nlh);
838 return -EMSGSIZE;
839}
840
841static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700842 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700843{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800844 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700845 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700846 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700847 u32 spid = NETLINK_CB(skb).pid;
848 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700849
Thomas Graf7deb2262007-08-22 13:57:39 -0700850 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700851 if (r_skb == NULL)
852 return -ENOMEM;
853
Alexey Dobriyane0710412010-01-23 13:37:10 +0000854 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700855 BUG();
856
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800857 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700858}
859
Thomas Graf7deb2262007-08-22 13:57:39 -0700860static inline size_t xfrm_sadinfo_msgsize(void)
861{
862 return NLMSG_ALIGN(4)
863 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
864 + nla_total_size(4); /* XFRMA_SAD_CNT */
865}
866
Alexey Dobriyane0710412010-01-23 13:37:10 +0000867static int build_sadinfo(struct sk_buff *skb, struct net *net,
868 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700869{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700870 struct xfrmk_sadinfo si;
871 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700872 struct nlmsghdr *nlh;
873 u32 *f;
874
875 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
876 if (nlh == NULL) /* shouldnt really happen ... */
877 return -EMSGSIZE;
878
879 f = nlmsg_data(nlh);
880 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000881 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700882
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700883 sh.sadhmcnt = si.sadhmcnt;
884 sh.sadhcnt = si.sadhcnt;
885
886 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
887 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700888
889 return nlmsg_end(skb, nlh);
890
891nla_put_failure:
892 nlmsg_cancel(skb, nlh);
893 return -EMSGSIZE;
894}
895
896static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700897 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700898{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800899 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700900 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700901 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700902 u32 spid = NETLINK_CB(skb).pid;
903 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700904
Thomas Graf7deb2262007-08-22 13:57:39 -0700905 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700906 if (r_skb == NULL)
907 return -ENOMEM;
908
Alexey Dobriyane0710412010-01-23 13:37:10 +0000909 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700910 BUG();
911
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800912 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700913}
914
Christoph Hellwig22e70052007-01-02 15:22:30 -0800915static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700916 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800918 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700919 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct xfrm_state *x;
921 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700922 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800924 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (x == NULL)
926 goto out_noput;
927
928 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
929 if (IS_ERR(resp_skb)) {
930 err = PTR_ERR(resp_skb);
931 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800932 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 xfrm_state_put(x);
935out_noput:
936 return err;
937}
938
939static int verify_userspi_info(struct xfrm_userspi_info *p)
940{
941 switch (p->info.id.proto) {
942 case IPPROTO_AH:
943 case IPPROTO_ESP:
944 break;
945
946 case IPPROTO_COMP:
947 /* IPCOMP spi is 16-bits. */
948 if (p->max >= 0x10000)
949 return -EINVAL;
950 break;
951
952 default:
953 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (p->min > p->max)
957 return -EINVAL;
958
959 return 0;
960}
961
Christoph Hellwig22e70052007-01-02 15:22:30 -0800962static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700963 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800965 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct xfrm_state *x;
967 struct xfrm_userspi_info *p;
968 struct sk_buff *resp_skb;
969 xfrm_address_t *daddr;
970 int family;
971 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000972 u32 mark;
973 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Thomas Graf7b67c852007-08-22 13:53:52 -0700975 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 err = verify_userspi_info(p);
977 if (err)
978 goto out_noput;
979
980 family = p->info.family;
981 daddr = &p->info.id.daddr;
982
983 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000984
985 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000987 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
989 xfrm_state_put(x);
990 x = NULL;
991 }
992 }
993
994 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000995 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 p->info.id.proto, daddr,
997 &p->info.saddr, 1,
998 family);
999 err = -ENOENT;
1000 if (x == NULL)
1001 goto out_noput;
1002
Herbert Xu658b2192007-10-09 13:29:52 -07001003 err = xfrm_alloc_spi(x, p->min, p->max);
1004 if (err)
1005 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Herbert Xu658b2192007-10-09 13:29:52 -07001007 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (IS_ERR(resp_skb)) {
1009 err = PTR_ERR(resp_skb);
1010 goto out;
1011 }
1012
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001013 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015out:
1016 xfrm_state_put(x);
1017out_noput:
1018 return err;
1019}
1020
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001021static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 switch (dir) {
1024 case XFRM_POLICY_IN:
1025 case XFRM_POLICY_OUT:
1026 case XFRM_POLICY_FWD:
1027 break;
1028
1029 default:
1030 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 return 0;
1034}
1035
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001036static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001037{
1038 switch (type) {
1039 case XFRM_POLICY_TYPE_MAIN:
1040#ifdef CONFIG_XFRM_SUB_POLICY
1041 case XFRM_POLICY_TYPE_SUB:
1042#endif
1043 break;
1044
1045 default:
1046 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001047 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001048
1049 return 0;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1053{
1054 switch (p->share) {
1055 case XFRM_SHARE_ANY:
1056 case XFRM_SHARE_SESSION:
1057 case XFRM_SHARE_USER:
1058 case XFRM_SHARE_UNIQUE:
1059 break;
1060
1061 default:
1062 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 switch (p->action) {
1066 case XFRM_POLICY_ALLOW:
1067 case XFRM_POLICY_BLOCK:
1068 break;
1069
1070 default:
1071 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 switch (p->sel.family) {
1075 case AF_INET:
1076 break;
1077
1078 case AF_INET6:
1079#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1080 break;
1081#else
1082 return -EAFNOSUPPORT;
1083#endif
1084
1085 default:
1086 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 return verify_policy_dir(p->dir);
1090}
1091
Thomas Graf5424f322007-08-22 14:01:33 -07001092static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001093{
Thomas Graf5424f322007-08-22 14:01:33 -07001094 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001095 struct xfrm_user_sec_ctx *uctx;
1096
1097 if (!rt)
1098 return 0;
1099
Thomas Graf5424f322007-08-22 14:01:33 -07001100 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001101 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1105 int nr)
1106{
1107 int i;
1108
1109 xp->xfrm_nr = nr;
1110 for (i = 0; i < nr; i++, ut++) {
1111 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1112
1113 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1114 memcpy(&t->saddr, &ut->saddr,
1115 sizeof(xfrm_address_t));
1116 t->reqid = ut->reqid;
1117 t->mode = ut->mode;
1118 t->share = ut->share;
1119 t->optional = ut->optional;
1120 t->aalgos = ut->aalgos;
1121 t->ealgos = ut->ealgos;
1122 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001123 /* If all masks are ~0, then we allow all algorithms. */
1124 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001125 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127}
1128
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001129static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1130{
1131 int i;
1132
1133 if (nr > XFRM_MAX_DEPTH)
1134 return -EINVAL;
1135
1136 for (i = 0; i < nr; i++) {
1137 /* We never validated the ut->family value, so many
1138 * applications simply leave it at zero. The check was
1139 * never made and ut->family was ignored because all
1140 * templates could be assumed to have the same family as
1141 * the policy itself. Now that we will have ipv4-in-ipv6
1142 * and ipv6-in-ipv4 tunnels, this is no longer true.
1143 */
1144 if (!ut[i].family)
1145 ut[i].family = family;
1146
1147 switch (ut[i].family) {
1148 case AF_INET:
1149 break;
1150#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1151 case AF_INET6:
1152 break;
1153#endif
1154 default:
1155 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001156 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001157 }
1158
1159 return 0;
1160}
1161
Thomas Graf5424f322007-08-22 14:01:33 -07001162static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Thomas Graf5424f322007-08-22 14:01:33 -07001164 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!rt) {
1167 pol->xfrm_nr = 0;
1168 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001169 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1170 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001171 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001173 err = validate_tmpl(nr, utmpl, pol->family);
1174 if (err)
1175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Thomas Graf5424f322007-08-22 14:01:33 -07001177 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 return 0;
1180}
1181
Thomas Graf5424f322007-08-22 14:01:33 -07001182static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001183{
Thomas Graf5424f322007-08-22 14:01:33 -07001184 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001186 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001187 int err;
1188
1189 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001190 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001191 type = upt->type;
1192 }
1193
1194 err = verify_policy_type(type);
1195 if (err)
1196 return err;
1197
1198 *tp = type;
1199 return 0;
1200}
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1203{
1204 xp->priority = p->priority;
1205 xp->index = p->index;
1206 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1207 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1208 xp->action = p->action;
1209 xp->flags = p->flags;
1210 xp->family = p->sel.family;
1211 /* XXX xp->share = p->share; */
1212}
1213
1214static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1215{
1216 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1217 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1218 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1219 p->priority = xp->priority;
1220 p->index = xp->index;
1221 p->sel.family = xp->family;
1222 p->dir = dir;
1223 p->action = xp->action;
1224 p->flags = xp->flags;
1225 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1226}
1227
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001228static 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 -07001229{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001230 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int err;
1232
1233 if (!xp) {
1234 *errp = -ENOMEM;
1235 return NULL;
1236 }
1237
1238 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001239
Thomas Graf35a7aa02007-08-22 14:00:40 -07001240 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001241 if (err)
1242 goto error;
1243
Thomas Graf35a7aa02007-08-22 14:00:40 -07001244 if (!(err = copy_from_user_tmpl(xp, attrs)))
1245 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001246 if (err)
1247 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001249 xfrm_mark_get(attrs, &xp->mark);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001252 error:
1253 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001254 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001255 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001256 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Christoph Hellwig22e70052007-01-02 15:22:30 -08001259static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001260 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001262 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001263 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001265 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int err;
1267 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001268 uid_t loginuid = NETLINK_CB(skb).loginuid;
1269 u32 sessionid = NETLINK_CB(skb).sessionid;
1270 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 err = verify_newpolicy_info(p);
1273 if (err)
1274 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001275 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001276 if (err)
1277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001279 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!xp)
1281 return err;
1282
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001283 /* shouldnt excl be based on nlh flags??
1284 * Aha! this is anti-netlink really i.e more pfkey derived
1285 * in netlink excl is a flag and you wouldnt need
1286 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1288 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001289 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001292 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 kfree(xp);
1294 return err;
1295 }
1296
Herbert Xuf60f6b82005-06-18 22:44:37 -07001297 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001298 c.seq = nlh->nlmsg_seq;
1299 c.pid = nlh->nlmsg_pid;
1300 km_policy_notify(xp, p->dir, &c);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 xfrm_pol_put(xp);
1303
1304 return 0;
1305}
1306
1307static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1308{
1309 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1310 int i;
1311
1312 if (xp->xfrm_nr == 0)
1313 return 0;
1314
1315 for (i = 0; i < xp->xfrm_nr; i++) {
1316 struct xfrm_user_tmpl *up = &vec[i];
1317 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1318
1319 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001320 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1322 up->reqid = kp->reqid;
1323 up->mode = kp->mode;
1324 up->share = kp->share;
1325 up->optional = kp->optional;
1326 up->aalgos = kp->aalgos;
1327 up->ealgos = kp->ealgos;
1328 up->calgos = kp->calgos;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Thomas Grafc0144be2007-08-22 13:55:43 -07001331 return nla_put(skb, XFRMA_TMPL,
1332 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001333}
1334
Serge Hallyn0d681622006-07-24 23:30:44 -07001335static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1336{
1337 if (x->security) {
1338 return copy_sec_ctx(x->security, skb);
1339 }
1340 return 0;
1341}
1342
1343static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1344{
1345 if (xp->security) {
1346 return copy_sec_ctx(xp->security, skb);
1347 }
1348 return 0;
1349}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001350static inline size_t userpolicy_type_attrsize(void)
1351{
1352#ifdef CONFIG_XFRM_SUB_POLICY
1353 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1354#else
1355 return 0;
1356#endif
1357}
Serge Hallyn0d681622006-07-24 23:30:44 -07001358
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001359#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001360static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001361{
Thomas Grafc0144be2007-08-22 13:55:43 -07001362 struct xfrm_userpolicy_type upt = {
1363 .type = type,
1364 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001365
Thomas Grafc0144be2007-08-22 13:55:43 -07001366 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001367}
1368
1369#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001370static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001371{
1372 return 0;
1373}
1374#endif
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1377{
1378 struct xfrm_dump_info *sp = ptr;
1379 struct xfrm_userpolicy_info *p;
1380 struct sk_buff *in_skb = sp->in_skb;
1381 struct sk_buff *skb = sp->out_skb;
1382 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001384 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1385 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1386 if (nlh == NULL)
1387 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Thomas Graf7b67c852007-08-22 13:53:52 -07001389 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 copy_to_user_policy(xp, p, dir);
1391 if (copy_to_user_tmpl(xp, skb) < 0)
1392 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001393 if (copy_to_user_sec_ctx(xp, skb))
1394 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001395 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001396 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001397 if (xfrm_mark_put(skb, &xp->mark))
1398 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Thomas Graf98250692007-08-22 12:47:26 -07001400 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return 0;
1402
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001403nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001405 nlmsg_cancel(skb, nlh);
1406 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Timo Teras4c563f72008-02-28 21:31:08 -08001409static int xfrm_dump_policy_done(struct netlink_callback *cb)
1410{
1411 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1412
1413 xfrm_policy_walk_done(walk);
1414 return 0;
1415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1418{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001419 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001420 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct xfrm_dump_info info;
1422
Timo Teras4c563f72008-02-28 21:31:08 -08001423 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1424 sizeof(cb->args) - sizeof(cb->args[0]));
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 info.in_skb = cb->skb;
1427 info.out_skb = skb;
1428 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1429 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001430
1431 if (!cb->args[0]) {
1432 cb->args[0] = 1;
1433 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1434 }
1435
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001436 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 return skb->len;
1439}
1440
1441static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1442 struct xfrm_policy *xp,
1443 int dir, u32 seq)
1444{
1445 struct xfrm_dump_info info;
1446 struct sk_buff *skb;
1447
Thomas Graf7deb2262007-08-22 13:57:39 -07001448 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!skb)
1450 return ERR_PTR(-ENOMEM);
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 info.in_skb = in_skb;
1453 info.out_skb = skb;
1454 info.nlmsg_seq = seq;
1455 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1458 kfree_skb(skb);
1459 return NULL;
1460 }
1461
1462 return skb;
1463}
1464
Christoph Hellwig22e70052007-01-02 15:22:30 -08001465static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001466 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001468 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct xfrm_policy *xp;
1470 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001471 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001473 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001475 struct xfrm_mark m;
1476 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Thomas Graf7b67c852007-08-22 13:53:52 -07001478 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1480
Thomas Graf35a7aa02007-08-22 14:00:40 -07001481 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001482 if (err)
1483 return err;
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 err = verify_policy_dir(p->dir);
1486 if (err)
1487 return err;
1488
1489 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001490 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001491 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001492 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001493 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001494
Thomas Graf35a7aa02007-08-22 14:00:40 -07001495 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001496 if (err)
1497 return err;
1498
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001499 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001500 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001501 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001502
Paul Moore03e1ad72008-04-12 19:07:52 -07001503 err = security_xfrm_policy_alloc(&ctx, uctx);
1504 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001505 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001506 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001507 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001508 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001509 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (xp == NULL)
1512 return -ENOENT;
1513
1514 if (!delete) {
1515 struct sk_buff *resp_skb;
1516
1517 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1518 if (IS_ERR(resp_skb)) {
1519 err = PTR_ERR(resp_skb);
1520 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001521 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001522 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001524 } else {
Eric Paris25323862008-04-18 10:09:25 -04001525 uid_t loginuid = NETLINK_CB(skb).loginuid;
1526 u32 sessionid = NETLINK_CB(skb).sessionid;
1527 u32 sid = NETLINK_CB(skb).sid;
1528
1529 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1530 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001531
1532 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001533 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001534
Herbert Xue7443892005-06-18 22:44:18 -07001535 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001536 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001537 c.seq = nlh->nlmsg_seq;
1538 c.pid = nlh->nlmsg_pid;
1539 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001542out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001543 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return err;
1545}
1546
Christoph Hellwig22e70052007-01-02 15:22:30 -08001547static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001548 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001550 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001551 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001552 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001553 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Joy Latten161a09e2006-11-27 13:11:54 -06001556 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001557 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001558 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001559 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001560 if (err) {
1561 if (err == -ESRCH) /* empty table */
1562 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001563 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001564 }
Herbert Xubf088672005-06-18 22:44:00 -07001565 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001566 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001567 c.seq = nlh->nlmsg_seq;
1568 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001569 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001570 km_state_notify(NULL, &c);
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return 0;
1573}
1574
Thomas Graf7deb2262007-08-22 13:57:39 -07001575static inline size_t xfrm_aevent_msgsize(void)
1576{
1577 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1578 + nla_total_size(sizeof(struct xfrm_replay_state))
1579 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001580 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001581 + nla_total_size(4) /* XFRM_AE_RTHR */
1582 + nla_total_size(4); /* XFRM_AE_ETHR */
1583}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001584
1585static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1586{
1587 struct xfrm_aevent_id *id;
1588 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001589
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001590 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1591 if (nlh == NULL)
1592 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001593
Thomas Graf7b67c852007-08-22 13:53:52 -07001594 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001595 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001596 id->sa_id.spi = x->id.spi;
1597 id->sa_id.family = x->props.family;
1598 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001599 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1600 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001601 id->flags = c->data.aevent;
1602
Thomas Grafc0144be2007-08-22 13:55:43 -07001603 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1604 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001605
Thomas Grafc0144be2007-08-22 13:55:43 -07001606 if (id->flags & XFRM_AE_RTHR)
1607 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001608
Thomas Grafc0144be2007-08-22 13:55:43 -07001609 if (id->flags & XFRM_AE_ETHR)
1610 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1611 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001612
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001613 if (xfrm_mark_put(skb, &x->mark))
1614 goto nla_put_failure;
1615
Thomas Graf98250692007-08-22 12:47:26 -07001616 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001617
Thomas Grafc0144be2007-08-22 13:55:43 -07001618nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001619 nlmsg_cancel(skb, nlh);
1620 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001621}
1622
Christoph Hellwig22e70052007-01-02 15:22:30 -08001623static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001624 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001625{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001626 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001627 struct xfrm_state *x;
1628 struct sk_buff *r_skb;
1629 int err;
1630 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001631 u32 mark;
1632 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001633 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001634 struct xfrm_usersa_id *id = &p->sa_id;
1635
Thomas Graf7deb2262007-08-22 13:57:39 -07001636 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001637 if (r_skb == NULL)
1638 return -ENOMEM;
1639
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001640 mark = xfrm_mark_get(attrs, &m);
1641
1642 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001643 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001644 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001645 return -ESRCH;
1646 }
1647
1648 /*
1649 * XXX: is this lock really needed - none of the other
1650 * gets lock (the concern is things getting updated
1651 * while we are still reading) - jhs
1652 */
1653 spin_lock_bh(&x->lock);
1654 c.data.aevent = p->flags;
1655 c.seq = nlh->nlmsg_seq;
1656 c.pid = nlh->nlmsg_pid;
1657
1658 if (build_aevent(r_skb, x, &c) < 0)
1659 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001660 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001661 spin_unlock_bh(&x->lock);
1662 xfrm_state_put(x);
1663 return err;
1664}
1665
Christoph Hellwig22e70052007-01-02 15:22:30 -08001666static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001667 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001668{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001669 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001670 struct xfrm_state *x;
1671 struct km_event c;
1672 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001673 u32 mark = 0;
1674 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001675 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001676 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1677 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001678
1679 if (!lt && !rp)
1680 return err;
1681
1682 /* pedantic mode - thou shalt sayeth replaceth */
1683 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1684 return err;
1685
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001686 mark = xfrm_mark_get(attrs, &m);
1687
1688 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 -08001689 if (x == NULL)
1690 return -ESRCH;
1691
1692 if (x->km.state != XFRM_STATE_VALID)
1693 goto out;
1694
1695 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001696 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001697 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001698
1699 c.event = nlh->nlmsg_type;
1700 c.seq = nlh->nlmsg_seq;
1701 c.pid = nlh->nlmsg_pid;
1702 c.data.aevent = XFRM_AE_CU;
1703 km_state_notify(x, &c);
1704 err = 0;
1705out:
1706 xfrm_state_put(x);
1707 return err;
1708}
1709
Christoph Hellwig22e70052007-01-02 15:22:30 -08001710static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001711 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001713 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001714 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001715 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001716 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001717 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001718
Thomas Graf35a7aa02007-08-22 14:00:40 -07001719 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001720 if (err)
1721 return err;
1722
Joy Latten161a09e2006-11-27 13:11:54 -06001723 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001724 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001725 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001726 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001727 if (err) {
1728 if (err == -ESRCH) /* empty table */
1729 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001730 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001731 }
1732
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001733 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001734 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001735 c.seq = nlh->nlmsg_seq;
1736 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001737 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001738 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
1740}
1741
Christoph Hellwig22e70052007-01-02 15:22:30 -08001742static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001743 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001744{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001745 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001746 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001747 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001748 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001749 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001750 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001751 struct xfrm_mark m;
1752 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001753
Thomas Graf35a7aa02007-08-22 14:00:40 -07001754 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001755 if (err)
1756 return err;
1757
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001758 err = verify_policy_dir(p->dir);
1759 if (err)
1760 return err;
1761
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001762 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001763 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001764 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001765 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001766 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001767
Thomas Graf35a7aa02007-08-22 14:00:40 -07001768 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001769 if (err)
1770 return err;
1771
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001772 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001773 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001774 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001775
Paul Moore03e1ad72008-04-12 19:07:52 -07001776 err = security_xfrm_policy_alloc(&ctx, uctx);
1777 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001778 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001779 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001780 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001781 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001782 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001783 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001784 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001785 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001786
Timo Teräsea2dea92010-03-31 00:17:05 +00001787 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001788 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001789
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001790 err = 0;
1791 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001792 uid_t loginuid = NETLINK_CB(skb).loginuid;
1793 uid_t sessionid = NETLINK_CB(skb).sessionid;
1794 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001795 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001796 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001797
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001798 } else {
1799 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001800 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001801 }
1802 km_policy_expired(xp, p->dir, up->hard, current->pid);
1803
1804out:
1805 xfrm_pol_put(xp);
1806 return err;
1807}
1808
Christoph Hellwig22e70052007-01-02 15:22:30 -08001809static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001810 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001811{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001812 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001813 struct xfrm_state *x;
1814 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001815 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001816 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001817 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001818 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001819
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001820 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 -08001821
David S. Miller3a765aa2007-02-26 14:52:21 -08001822 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001823 if (x == NULL)
1824 return err;
1825
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001826 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001827 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001828 if (x->km.state != XFRM_STATE_VALID)
1829 goto out;
1830 km_state_expired(x, ue->hard, current->pid);
1831
Joy Latten161a09e2006-11-27 13:11:54 -06001832 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001833 uid_t loginuid = NETLINK_CB(skb).loginuid;
1834 uid_t sessionid = NETLINK_CB(skb).sessionid;
1835 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001836 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001837 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001838 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001839 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001840out:
1841 spin_unlock_bh(&x->lock);
1842 xfrm_state_put(x);
1843 return err;
1844}
1845
Christoph Hellwig22e70052007-01-02 15:22:30 -08001846static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001847 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001848{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001849 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001850 struct xfrm_policy *xp;
1851 struct xfrm_user_tmpl *ut;
1852 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001853 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001854 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001855
Thomas Graf7b67c852007-08-22 13:53:52 -07001856 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001857 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001858 int err = -ENOMEM;
1859
1860 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001861 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001862
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001863 xfrm_mark_get(attrs, &mark);
1864
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001865 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001866 if (err)
1867 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001868
1869 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001870 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001871 if (!xp)
1872 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001873
1874 memcpy(&x->id, &ua->id, sizeof(ua->id));
1875 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1876 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001877 xp->mark.m = x->mark.m = mark.m;
1878 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001879 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001880 /* extract the templates and for each call km_key */
1881 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1882 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1883 memcpy(&x->id, &t->id, sizeof(x->id));
1884 x->props.mode = t->mode;
1885 x->props.reqid = t->reqid;
1886 x->props.family = ut->family;
1887 t->aalgos = ua->aalgos;
1888 t->ealgos = ua->ealgos;
1889 t->calgos = ua->calgos;
1890 err = km_query(x, t, xp);
1891
1892 }
1893
1894 kfree(x);
1895 kfree(xp);
1896
1897 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001898
1899bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001900 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001901free_state:
1902 kfree(x);
1903nomem:
1904 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001905}
1906
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001907#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001908static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001909 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001910 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001911{
Thomas Graf5424f322007-08-22 14:01:33 -07001912 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001913 struct xfrm_user_migrate *um;
1914 int i, num_migrate;
1915
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001916 if (k != NULL) {
1917 struct xfrm_user_kmaddress *uk;
1918
1919 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1920 memcpy(&k->local, &uk->local, sizeof(k->local));
1921 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1922 k->family = uk->family;
1923 k->reserved = uk->reserved;
1924 }
1925
Thomas Graf5424f322007-08-22 14:01:33 -07001926 um = nla_data(rt);
1927 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001928
1929 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1930 return -EINVAL;
1931
1932 for (i = 0; i < num_migrate; i++, um++, ma++) {
1933 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1934 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1935 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1936 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1937
1938 ma->proto = um->proto;
1939 ma->mode = um->mode;
1940 ma->reqid = um->reqid;
1941
1942 ma->old_family = um->old_family;
1943 ma->new_family = um->new_family;
1944 }
1945
1946 *num = i;
1947 return 0;
1948}
1949
1950static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001951 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001952{
Thomas Graf7b67c852007-08-22 13:53:52 -07001953 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001954 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001955 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001956 u8 type;
1957 int err;
1958 int n = 0;
1959
Thomas Graf35a7aa02007-08-22 14:00:40 -07001960 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001961 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001962
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001963 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1964
Thomas Graf5424f322007-08-22 14:01:33 -07001965 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001966 if (err)
1967 return err;
1968
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001969 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001970 if (err)
1971 return err;
1972
1973 if (!n)
1974 return 0;
1975
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001976 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001977
1978 return 0;
1979}
1980#else
1981static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001982 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001983{
1984 return -ENOPROTOOPT;
1985}
1986#endif
1987
1988#ifdef CONFIG_XFRM_MIGRATE
1989static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1990{
1991 struct xfrm_user_migrate um;
1992
1993 memset(&um, 0, sizeof(um));
1994 um.proto = m->proto;
1995 um.mode = m->mode;
1996 um.reqid = m->reqid;
1997 um.old_family = m->old_family;
1998 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1999 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
2000 um.new_family = m->new_family;
2001 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2002 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2003
Thomas Grafc0144be2007-08-22 13:55:43 -07002004 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002005}
2006
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002007static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
2008{
2009 struct xfrm_user_kmaddress uk;
2010
2011 memset(&uk, 0, sizeof(uk));
2012 uk.family = k->family;
2013 uk.reserved = k->reserved;
2014 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002015 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002016
2017 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2018}
2019
2020static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002021{
2022 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002023 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2024 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2025 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002026}
2027
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002028static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002029 int num_migrate, struct xfrm_kmaddress *k,
2030 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002031{
2032 struct xfrm_migrate *mp;
2033 struct xfrm_userpolicy_id *pol_id;
2034 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002035 int i;
2036
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002037 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2038 if (nlh == NULL)
2039 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002040
Thomas Graf7b67c852007-08-22 13:53:52 -07002041 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002042 /* copy data from selector, dir, and type to the pol_id */
2043 memset(pol_id, 0, sizeof(*pol_id));
2044 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2045 pol_id->dir = dir;
2046
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002047 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2048 goto nlmsg_failure;
2049
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002050 if (copy_to_user_policy_type(type, skb) < 0)
2051 goto nlmsg_failure;
2052
2053 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2054 if (copy_to_user_migrate(mp, skb) < 0)
2055 goto nlmsg_failure;
2056 }
2057
Thomas Graf98250692007-08-22 12:47:26 -07002058 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002059nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002060 nlmsg_cancel(skb, nlh);
2061 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002062}
2063
2064static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002065 struct xfrm_migrate *m, int num_migrate,
2066 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002068 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002069 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002070
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002071 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002072 if (skb == NULL)
2073 return -ENOMEM;
2074
2075 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002076 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002077 BUG();
2078
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002079 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002080}
2081#else
2082static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002083 struct xfrm_migrate *m, int num_migrate,
2084 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002085{
2086 return -ENOPROTOOPT;
2087}
2088#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002089
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002090#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002091
2092static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002093 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002094 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2095 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2096 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2097 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2098 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2099 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002100 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002101 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002102 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002103 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002104 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002105 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002106 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002107 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2108 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002109 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002110 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002111 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2112 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113};
2114
Thomas Graf492b5582005-05-03 14:26:40 -07002115#undef XMSGSIZE
2116
Thomas Grafcf5cb792007-08-22 13:59:04 -07002117static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002118 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2119 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2120 [XFRMA_LASTUSED] = { .type = NLA_U64},
2121 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002122 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002123 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2124 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2125 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2126 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2127 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2128 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2129 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2130 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2131 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2132 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2133 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2134 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2135 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2136 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002137 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002138 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002139 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002140};
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002143 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002145 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002146} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2147 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2148 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2149 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002150 .dump = xfrm_dump_sa,
2151 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002152 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2153 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2154 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002155 .dump = xfrm_dump_policy,
2156 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002157 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002158 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002159 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002160 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2161 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002162 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002163 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2164 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002165 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2166 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002167 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002168 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002169 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170};
2171
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002172static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002174 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002175 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002177 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002181 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 type -= XFRM_MSG_BASE;
2184 link = &xfrm_dispatch[type];
2185
2186 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002187 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2188 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Thomas Graf492b5582005-05-03 14:26:40 -07002190 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2191 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
David S. Millerb8f3ab42011-01-18 12:40:38 -08002192 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002194 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002196 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198
Thomas Graf35a7aa02007-08-22 14:00:40 -07002199 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002200 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002201 if (err < 0)
2202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Graf5424f322007-08-22 14:01:33 -07002207 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002210static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002212 mutex_lock(&xfrm_cfg_mutex);
2213 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2214 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Thomas Graf7deb2262007-08-22 13:57:39 -07002217static inline size_t xfrm_expire_msgsize(void)
2218{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002219 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2220 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002221}
2222
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002223static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 struct xfrm_user_expire *ue;
2226 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002228 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2229 if (nlh == NULL)
2230 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Thomas Graf7b67c852007-08-22 13:53:52 -07002232 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002234 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002236 if (xfrm_mark_put(skb, &x->mark))
2237 goto nla_put_failure;
2238
Thomas Graf98250692007-08-22 12:47:26 -07002239 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002240
2241nla_put_failure:
2242 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002245static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002247 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 struct sk_buff *skb;
2249
Thomas Graf7deb2262007-08-22 13:57:39 -07002250 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (skb == NULL)
2252 return -ENOMEM;
2253
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002254 if (build_expire(skb, x, c) < 0) {
2255 kfree_skb(skb);
2256 return -EMSGSIZE;
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002259 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002262static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2263{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002264 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002265 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002266
Thomas Graf7deb2262007-08-22 13:57:39 -07002267 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002268 if (skb == NULL)
2269 return -ENOMEM;
2270
2271 if (build_aevent(skb, x, c) < 0)
2272 BUG();
2273
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002274 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002275}
2276
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277static int xfrm_notify_sa_flush(struct km_event *c)
2278{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002279 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002280 struct xfrm_usersa_flush *p;
2281 struct nlmsghdr *nlh;
2282 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002283 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002284
Thomas Graf7deb2262007-08-22 13:57:39 -07002285 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002286 if (skb == NULL)
2287 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002288
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002289 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2290 if (nlh == NULL) {
2291 kfree_skb(skb);
2292 return -EMSGSIZE;
2293 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002294
Thomas Graf7b67c852007-08-22 13:53:52 -07002295 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002296 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002297
Thomas Graf98250692007-08-22 12:47:26 -07002298 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002299
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002300 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002301}
2302
Thomas Graf7deb2262007-08-22 13:57:39 -07002303static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002304{
Thomas Graf7deb2262007-08-22 13:57:39 -07002305 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002306 if (x->aead)
2307 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002308 if (x->aalg) {
2309 l += nla_total_size(sizeof(struct xfrm_algo) +
2310 (x->aalg->alg_key_len + 7) / 8);
2311 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2312 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002313 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002314 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002316 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002317 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002318 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002319 if (x->tfcpad)
2320 l += nla_total_size(sizeof(x->tfcpad));
Herbert Xu68325d32007-10-09 13:30:57 -07002321 if (x->security)
2322 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2323 x->security->ctx_len);
2324 if (x->coaddr)
2325 l += nla_total_size(sizeof(*x->coaddr));
2326
Herbert Xud26f3982007-11-13 21:47:08 -08002327 /* Must count x->lastused as it may become non-zero behind our back. */
2328 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002329
2330 return l;
2331}
2332
2333static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2334{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002335 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002336 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002337 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338 struct nlmsghdr *nlh;
2339 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002340 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002341 int headlen;
2342
2343 headlen = sizeof(*p);
2344 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002345 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002346 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002347 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002348 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002349 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002350
Thomas Graf7deb2262007-08-22 13:57:39 -07002351 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002352 if (skb == NULL)
2353 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002354
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002355 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2356 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002357 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358
Thomas Graf7b67c852007-08-22 13:53:52 -07002359 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002360 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002361 struct nlattr *attr;
2362
Thomas Graf7b67c852007-08-22 13:53:52 -07002363 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002364 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2365 id->spi = x->id.spi;
2366 id->family = x->props.family;
2367 id->proto = x->id.proto;
2368
Thomas Grafc0144be2007-08-22 13:55:43 -07002369 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2370 if (attr == NULL)
2371 goto nla_put_failure;
2372
2373 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002374 }
2375
Herbert Xu68325d32007-10-09 13:30:57 -07002376 if (copy_to_user_state_extra(x, p, skb))
2377 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378
Thomas Graf98250692007-08-22 12:47:26 -07002379 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002381 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002382
Thomas Grafc0144be2007-08-22 13:55:43 -07002383nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002384 /* Somebody screwed up with xfrm_sa_len! */
2385 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386 kfree_skb(skb);
2387 return -1;
2388}
2389
2390static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2391{
2392
2393 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002394 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002395 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002396 case XFRM_MSG_NEWAE:
2397 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002398 case XFRM_MSG_DELSA:
2399 case XFRM_MSG_UPDSA:
2400 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002402 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403 return xfrm_notify_sa_flush(c);
2404 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002405 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2406 c->event);
2407 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002408 }
2409
2410 return 0;
2411
2412}
2413
Thomas Graf7deb2262007-08-22 13:57:39 -07002414static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2415 struct xfrm_policy *xp)
2416{
2417 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2418 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002419 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002420 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2421 + userpolicy_type_attrsize();
2422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2425 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2426 int dir)
2427{
2428 struct xfrm_user_acquire *ua;
2429 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 __u32 seq = xfrm_get_acqseq();
2431
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002432 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2433 if (nlh == NULL)
2434 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Thomas Graf7b67c852007-08-22 13:53:52 -07002436 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 memcpy(&ua->id, &x->id, sizeof(ua->id));
2438 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2439 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2440 copy_to_user_policy(xp, &ua->policy, dir);
2441 ua->aalgos = xt->aalgos;
2442 ua->ealgos = xt->ealgos;
2443 ua->calgos = xt->calgos;
2444 ua->seq = x->km.seq = seq;
2445
2446 if (copy_to_user_tmpl(xp, skb) < 0)
2447 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002448 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002449 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002450 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002451 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002452 if (xfrm_mark_put(skb, &xp->mark))
2453 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Thomas Graf98250692007-08-22 12:47:26 -07002455 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002457nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002459 nlmsg_cancel(skb, nlh);
2460 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
2463static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2464 struct xfrm_policy *xp, int dir)
2465{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002466 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Thomas Graf7deb2262007-08-22 13:57:39 -07002469 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (skb == NULL)
2471 return -ENOMEM;
2472
2473 if (build_acquire(skb, x, xt, xp, dir) < 0)
2474 BUG();
2475
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002476 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479/* User gives us xfrm_user_policy_info followed by an array of 0
2480 * or more templates.
2481 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002482static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 u8 *data, int len, int *dir)
2484{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002485 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2487 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2488 struct xfrm_policy *xp;
2489 int nr;
2490
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002491 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 case AF_INET:
2493 if (opt != IP_XFRM_POLICY) {
2494 *dir = -EOPNOTSUPP;
2495 return NULL;
2496 }
2497 break;
2498#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2499 case AF_INET6:
2500 if (opt != IPV6_XFRM_POLICY) {
2501 *dir = -EOPNOTSUPP;
2502 return NULL;
2503 }
2504 break;
2505#endif
2506 default:
2507 *dir = -EINVAL;
2508 return NULL;
2509 }
2510
2511 *dir = -EINVAL;
2512
2513 if (len < sizeof(*p) ||
2514 verify_newpolicy_info(p))
2515 return NULL;
2516
2517 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002518 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return NULL;
2520
Herbert Xua4f1bac2005-07-26 15:43:17 -07002521 if (p->dir > XFRM_POLICY_OUT)
2522 return NULL;
2523
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002524 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (xp == NULL) {
2526 *dir = -ENOBUFS;
2527 return NULL;
2528 }
2529
2530 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002531 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 copy_templates(xp, ut, nr);
2533
2534 *dir = p->dir;
2535
2536 return xp;
2537}
2538
Thomas Graf7deb2262007-08-22 13:57:39 -07002539static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2540{
2541 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2542 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2543 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002544 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002545 + userpolicy_type_attrsize();
2546}
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002549 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
2551 struct xfrm_user_polexpire *upe;
2552 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002553 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002555 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2556 if (nlh == NULL)
2557 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Thomas Graf7b67c852007-08-22 13:53:52 -07002559 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 copy_to_user_policy(xp, &upe->pol, dir);
2561 if (copy_to_user_tmpl(xp, skb) < 0)
2562 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002563 if (copy_to_user_sec_ctx(xp, skb))
2564 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002565 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002566 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002567 if (xfrm_mark_put(skb, &xp->mark))
2568 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 upe->hard = !!hard;
2570
Thomas Graf98250692007-08-22 12:47:26 -07002571 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002573nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002575 nlmsg_cancel(skb, nlh);
2576 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002579static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002581 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Thomas Graf7deb2262007-08-22 13:57:39 -07002584 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (skb == NULL)
2586 return -ENOMEM;
2587
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002588 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 BUG();
2590
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002591 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002594static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2595{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002596 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002598 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002599 struct nlmsghdr *nlh;
2600 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002601 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002602 int headlen;
2603
2604 headlen = sizeof(*p);
2605 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002606 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002607 headlen = sizeof(*id);
2608 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002609 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002610 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002611 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612
Thomas Graf7deb2262007-08-22 13:57:39 -07002613 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002614 if (skb == NULL)
2615 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002616
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002617 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2618 if (nlh == NULL)
2619 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002620
Thomas Graf7b67c852007-08-22 13:53:52 -07002621 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002622 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002623 struct nlattr *attr;
2624
Thomas Graf7b67c852007-08-22 13:53:52 -07002625 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002626 memset(id, 0, sizeof(*id));
2627 id->dir = dir;
2628 if (c->data.byid)
2629 id->index = xp->index;
2630 else
2631 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2632
Thomas Grafc0144be2007-08-22 13:55:43 -07002633 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2634 if (attr == NULL)
2635 goto nlmsg_failure;
2636
2637 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002638 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002639
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002640 copy_to_user_policy(xp, p, dir);
2641 if (copy_to_user_tmpl(xp, skb) < 0)
2642 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002643 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002644 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002645
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002646 if (xfrm_mark_put(skb, &xp->mark))
2647 goto nla_put_failure;
2648
Thomas Graf98250692007-08-22 12:47:26 -07002649 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002650
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002651 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002652
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002653nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654nlmsg_failure:
2655 kfree_skb(skb);
2656 return -1;
2657}
2658
2659static int xfrm_notify_policy_flush(struct km_event *c)
2660{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002661 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002662 struct nlmsghdr *nlh;
2663 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664
Thomas Graf7deb2262007-08-22 13:57:39 -07002665 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002666 if (skb == NULL)
2667 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002668
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002669 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2670 if (nlh == NULL)
2671 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002672 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2673 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674
Thomas Graf98250692007-08-22 12:47:26 -07002675 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002677 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002678
2679nlmsg_failure:
2680 kfree_skb(skb);
2681 return -1;
2682}
2683
2684static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2685{
2686
2687 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002688 case XFRM_MSG_NEWPOLICY:
2689 case XFRM_MSG_UPDPOLICY:
2690 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002691 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002692 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002694 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002695 return xfrm_exp_policy_notify(xp, dir, c);
2696 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002697 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2698 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002699 }
2700
2701 return 0;
2702
2703}
2704
Thomas Graf7deb2262007-08-22 13:57:39 -07002705static inline size_t xfrm_report_msgsize(void)
2706{
2707 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2708}
2709
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002710static int build_report(struct sk_buff *skb, u8 proto,
2711 struct xfrm_selector *sel, xfrm_address_t *addr)
2712{
2713 struct xfrm_user_report *ur;
2714 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002715
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002716 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2717 if (nlh == NULL)
2718 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002719
Thomas Graf7b67c852007-08-22 13:53:52 -07002720 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002721 ur->proto = proto;
2722 memcpy(&ur->sel, sel, sizeof(ur->sel));
2723
2724 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002725 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002726
Thomas Graf98250692007-08-22 12:47:26 -07002727 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002728
Thomas Grafc0144be2007-08-22 13:55:43 -07002729nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002730 nlmsg_cancel(skb, nlh);
2731 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002732}
2733
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002734static int xfrm_send_report(struct net *net, u8 proto,
2735 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002736{
2737 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002738
Thomas Graf7deb2262007-08-22 13:57:39 -07002739 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002740 if (skb == NULL)
2741 return -ENOMEM;
2742
2743 if (build_report(skb, proto, sel, addr) < 0)
2744 BUG();
2745
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002746 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002747}
2748
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002749static inline size_t xfrm_mapping_msgsize(void)
2750{
2751 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2752}
2753
2754static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2755 xfrm_address_t *new_saddr, __be16 new_sport)
2756{
2757 struct xfrm_user_mapping *um;
2758 struct nlmsghdr *nlh;
2759
2760 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2761 if (nlh == NULL)
2762 return -EMSGSIZE;
2763
2764 um = nlmsg_data(nlh);
2765
2766 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2767 um->id.spi = x->id.spi;
2768 um->id.family = x->props.family;
2769 um->id.proto = x->id.proto;
2770 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2771 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2772 um->new_sport = new_sport;
2773 um->old_sport = x->encap->encap_sport;
2774 um->reqid = x->props.reqid;
2775
2776 return nlmsg_end(skb, nlh);
2777}
2778
2779static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2780 __be16 sport)
2781{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002782 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002783 struct sk_buff *skb;
2784
2785 if (x->id.proto != IPPROTO_ESP)
2786 return -EINVAL;
2787
2788 if (!x->encap)
2789 return -EINVAL;
2790
2791 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2792 if (skb == NULL)
2793 return -ENOMEM;
2794
2795 if (build_mapping(skb, x, ipaddr, sport) < 0)
2796 BUG();
2797
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002798 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801static struct xfrm_mgr netlink_mgr = {
2802 .id = "netlink",
2803 .notify = xfrm_send_state_notify,
2804 .acquire = xfrm_send_acquire,
2805 .compile_policy = xfrm_compile_policy,
2806 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002807 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002808 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002809 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810};
2811
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002812static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Patrick McHardybe336902006-03-20 22:40:54 -08002814 struct sock *nlsk;
2815
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002816 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002817 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002818 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002820 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002821 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return 0;
2823}
2824
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002825static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002826{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002827 struct net *net;
2828 list_for_each_entry(net, net_exit_list, exit_list)
2829 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2830 synchronize_net();
2831 list_for_each_entry(net, net_exit_list, exit_list)
2832 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002833}
2834
2835static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002836 .init = xfrm_user_net_init,
2837 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002838};
2839
2840static int __init xfrm_user_init(void)
2841{
2842 int rv;
2843
2844 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2845
2846 rv = register_pernet_subsys(&xfrm_user_net_ops);
2847 if (rv < 0)
2848 return rv;
2849 rv = xfrm_register_km(&netlink_mgr);
2850 if (rv < 0)
2851 unregister_pernet_subsys(&xfrm_user_net_ops);
2852 return rv;
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855static void __exit xfrm_user_exit(void)
2856{
2857 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002858 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
2861module_init(xfrm_user_init);
2862module_exit(xfrm_user_exit);
2863MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002864MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002865