blob: 1ada6186933c24256c92bfee4db98eafde653a03 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31#include <linux/in6.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Herbert Xu1a6509d2008-01-28 19:37:29 -080034static inline int aead_len(struct xfrm_algo_aead *alg)
35{
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
37}
38
Thomas Graf5424f322007-08-22 14:01:33 -070039static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Graf5424f322007-08-22 14:01:33 -070041 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
Thomas Graf5424f322007-08-22 14:01:33 -070047 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080048 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 break;
56
57 default:
58 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
62 return 0;
63}
64
Martin Willi4447bb32009-11-25 00:29:52 +000065static int verify_auth_trunc(struct nlattr **attrs)
66{
67 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
68 struct xfrm_algo_auth *algp;
69
70 if (!rt)
71 return 0;
72
73 algp = nla_data(rt);
74 if (nla_len(rt) < xfrm_alg_auth_len(algp))
75 return -EINVAL;
76
77 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
78 return 0;
79}
80
Herbert Xu1a6509d2008-01-28 19:37:29 -080081static int verify_aead(struct nlattr **attrs)
82{
83 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
84 struct xfrm_algo_aead *algp;
85
86 if (!rt)
87 return 0;
88
89 algp = nla_data(rt);
90 if (nla_len(rt) < aead_len(algp))
91 return -EINVAL;
92
93 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
94 return 0;
95}
96
Thomas Graf5424f322007-08-22 14:01:33 -070097static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070098 xfrm_address_t **addrp)
99{
Thomas Graf5424f322007-08-22 14:01:33 -0700100 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101
Thomas Grafcf5cb792007-08-22 13:59:04 -0700102 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700103 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700104}
Trent Jaegerdf718372005-12-13 23:12:27 -0800105
Thomas Graf5424f322007-08-22 14:01:33 -0700106static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800107{
Thomas Graf5424f322007-08-22 14:01:33 -0700108 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800109 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800110
111 if (!rt)
112 return 0;
113
Thomas Graf5424f322007-08-22 14:01:33 -0700114 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700115 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800116 return -EINVAL;
117
118 return 0;
119}
120
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700123 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 int err;
126
127 err = -EINVAL;
128 switch (p->family) {
129 case AF_INET:
130 break;
131
132 case AF_INET6:
133#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
134 break;
135#else
136 err = -EAFNOSUPPORT;
137 goto out;
138#endif
139
140 default:
141 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 err = -EINVAL;
145 switch (p->id.proto) {
146 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000147 if ((!attrs[XFRMA_ALG_AUTH] &&
148 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800149 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700150 attrs[XFRMA_ALG_CRYPT] ||
151 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto out;
153 break;
154
155 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800156 if (attrs[XFRMA_ALG_COMP])
157 goto out;
158 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000159 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800160 !attrs[XFRMA_ALG_CRYPT] &&
161 !attrs[XFRMA_ALG_AEAD])
162 goto out;
163 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000164 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800165 attrs[XFRMA_ALG_CRYPT]) &&
166 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto out;
168 break;
169
170 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700171 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800172 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700173 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000174 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700175 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto out;
177 break;
178
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700179#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
180 case IPPROTO_DSTOPTS:
181 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700182 if (attrs[XFRMA_ALG_COMP] ||
183 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000184 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800185 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700186 attrs[XFRMA_ALG_CRYPT] ||
187 attrs[XFRMA_ENCAP] ||
188 attrs[XFRMA_SEC_CTX] ||
189 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700190 goto out;
191 break;
192#endif
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 default:
195 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Herbert Xu1a6509d2008-01-28 19:37:29 -0800198 if ((err = verify_aead(attrs)))
199 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000200 if ((err = verify_auth_trunc(attrs)))
201 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700202 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700206 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700208 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800209 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 err = -EINVAL;
212 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700213 case XFRM_MODE_TRANSPORT:
214 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700215 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700216 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
218
219 default:
220 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 err = 0;
224
225out:
226 return err;
227}
228
229static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
230 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700231 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct xfrm_algo *p, *ualg;
234 struct xfrm_algo_desc *algo;
235
236 if (!rta)
237 return 0;
238
Thomas Graf5424f322007-08-22 14:01:33 -0700239 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 algo = get_byname(ualg->alg_name, 1);
242 if (!algo)
243 return -ENOSYS;
244 *props = algo->desc.sadb_alg_id;
245
Eric Dumazet0f99be02008-01-08 23:39:06 -0800246 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!p)
248 return -ENOMEM;
249
Herbert Xu04ff1262006-08-13 08:50:00 +1000250 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *algpp = p;
252 return 0;
253}
254
Martin Willi4447bb32009-11-25 00:29:52 +0000255static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
256 struct nlattr *rta)
257{
258 struct xfrm_algo *ualg;
259 struct xfrm_algo_auth *p;
260 struct xfrm_algo_desc *algo;
261
262 if (!rta)
263 return 0;
264
265 ualg = nla_data(rta);
266
267 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
268 if (!algo)
269 return -ENOSYS;
270 *props = algo->desc.sadb_alg_id;
271
272 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
273 if (!p)
274 return -ENOMEM;
275
276 strcpy(p->alg_name, algo->name);
277 p->alg_key_len = ualg->alg_key_len;
278 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
279 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
280
281 *algpp = p;
282 return 0;
283}
284
285static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
286 struct nlattr *rta)
287{
288 struct xfrm_algo_auth *p, *ualg;
289 struct xfrm_algo_desc *algo;
290
291 if (!rta)
292 return 0;
293
294 ualg = nla_data(rta);
295
296 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
297 if (!algo)
298 return -ENOSYS;
299 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
300 return -EINVAL;
301 *props = algo->desc.sadb_alg_id;
302
303 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
304 if (!p)
305 return -ENOMEM;
306
307 strcpy(p->alg_name, algo->name);
308 if (!p->alg_trunc_len)
309 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
310
311 *algpp = p;
312 return 0;
313}
314
Herbert Xu1a6509d2008-01-28 19:37:29 -0800315static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
316 struct nlattr *rta)
317{
318 struct xfrm_algo_aead *p, *ualg;
319 struct xfrm_algo_desc *algo;
320
321 if (!rta)
322 return 0;
323
324 ualg = nla_data(rta);
325
326 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
327 if (!algo)
328 return -ENOSYS;
329 *props = algo->desc.sadb_alg_id;
330
331 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
332 if (!p)
333 return -ENOMEM;
334
335 strcpy(p->alg_name, algo->name);
336 *algpp = p;
337 return 0;
338}
339
Joy Latten661697f2007-04-13 16:14:35 -0700340static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800341{
Trent Jaegerdf718372005-12-13 23:12:27 -0800342 int len = 0;
343
344 if (xfrm_ctx) {
345 len += sizeof(struct xfrm_user_sec_ctx);
346 len += xfrm_ctx->ctx_len;
347 }
348 return len;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
352{
353 memcpy(&x->id, &p->id, sizeof(x->id));
354 memcpy(&x->sel, &p->sel, sizeof(x->sel));
355 memcpy(&x->lft, &p->lft, sizeof(x->lft));
356 x->props.mode = p->mode;
357 x->props.replay_window = p->replay_window;
358 x->props.reqid = p->reqid;
359 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700360 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700362
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700363 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700364 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800367/*
368 * someday when pfkey also has support, we could have the code
369 * somehow made shareable and move it to xfrm_state.c - JHS
370 *
371*/
Thomas Graf5424f322007-08-22 14:01:33 -0700372static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800373{
Thomas Graf5424f322007-08-22 14:01:33 -0700374 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
375 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
376 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
377 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800378
379 if (rp) {
380 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700381 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800382 memcpy(&x->replay, replay, sizeof(*replay));
383 memcpy(&x->preplay, replay, sizeof(*replay));
384 }
385
386 if (lt) {
387 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700388 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800389 x->curlft.bytes = ltime->bytes;
390 x->curlft.packets = ltime->packets;
391 x->curlft.add_time = ltime->add_time;
392 x->curlft.use_time = ltime->use_time;
393 }
394
Thomas Grafcf5cb792007-08-22 13:59:04 -0700395 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700396 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800397
Thomas Grafcf5cb792007-08-22 13:59:04 -0700398 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700399 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800400}
401
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800402static struct xfrm_state *xfrm_state_construct(struct net *net,
403 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700404 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int *errp)
406{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800407 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int err = -ENOMEM;
409
410 if (!x)
411 goto error_no_put;
412
413 copy_from_user_state(x, p);
414
Herbert Xu1a6509d2008-01-28 19:37:29 -0800415 if ((err = attach_aead(&x->aead, &x->props.ealgo,
416 attrs[XFRMA_ALG_AEAD])))
417 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000418 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
419 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000421 if (!x->props.aalgo) {
422 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
423 attrs[XFRMA_ALG_AUTH])))
424 goto error;
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
427 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700428 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto error;
430 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
431 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700432 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700434
435 if (attrs[XFRMA_ENCAP]) {
436 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
437 sizeof(*x->encap), GFP_KERNEL);
438 if (x->encap == NULL)
439 goto error;
440 }
441
442 if (attrs[XFRMA_COADDR]) {
443 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
444 sizeof(*x->coaddr), GFP_KERNEL);
445 if (x->coaddr == NULL)
446 goto error;
447 }
448
Herbert Xu72cb6962005-06-20 13:18:08 -0700449 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (err)
451 goto error;
452
Thomas Graffd211502007-09-06 03:28:08 -0700453 if (attrs[XFRMA_SEC_CTX] &&
454 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800455 goto error;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800458 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800459 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800460 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800461 x->preplay.bitmap = 0;
462 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
463 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
464
465 /* override default values from above */
466
Thomas Graf5424f322007-08-22 14:01:33 -0700467 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 return x;
470
471error:
472 x->km.state = XFRM_STATE_DEAD;
473 xfrm_state_put(x);
474error_no_put:
475 *errp = err;
476 return NULL;
477}
478
Christoph Hellwig22e70052007-01-02 15:22:30 -0800479static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700480 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800482 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700483 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct xfrm_state *x;
485 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700486 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400487 uid_t loginuid = NETLINK_CB(skb).loginuid;
488 u32 sessionid = NETLINK_CB(skb).sessionid;
489 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Thomas Graf35a7aa02007-08-22 14:00:40 -0700491 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (err)
493 return err;
494
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800495 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!x)
497 return err;
498
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700499 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
501 err = xfrm_state_add(x);
502 else
503 err = xfrm_state_update(x);
504
Eric Paris25323862008-04-18 10:09:25 -0400505 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (err < 0) {
508 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800509 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700513 c.seq = nlh->nlmsg_seq;
514 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700515 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700516
517 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700518out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700519 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return err;
521}
522
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800523static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
524 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700525 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700526 int *errp)
527{
528 struct xfrm_state *x = NULL;
529 int err;
530
531 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
532 err = -ESRCH;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800533 x = xfrm_state_lookup(net, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700534 } else {
535 xfrm_address_t *saddr = NULL;
536
Thomas Graf35a7aa02007-08-22 14:00:40 -0700537 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700538 if (!saddr) {
539 err = -EINVAL;
540 goto out;
541 }
542
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800543 err = -ESRCH;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800544 x = xfrm_state_lookup_byaddr(net, &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800545 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700546 }
547
548 out:
549 if (!x && errp)
550 *errp = err;
551 return x;
552}
553
Christoph Hellwig22e70052007-01-02 15:22:30 -0800554static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700555 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800557 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700559 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700560 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700561 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400562 uid_t loginuid = NETLINK_CB(skb).loginuid;
563 u32 sessionid = NETLINK_CB(skb).sessionid;
564 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800566 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David S. Miller6f68dc32006-06-08 23:58:52 -0700570 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700571 goto out;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700574 err = -EPERM;
575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700578 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600579
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700580 if (err < 0)
581 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700582
583 c.seq = nlh->nlmsg_seq;
584 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700585 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700586 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700588out:
Eric Paris25323862008-04-18 10:09:25 -0400589 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700590 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
595{
596 memcpy(&p->id, &x->id, sizeof(p->id));
597 memcpy(&p->sel, &x->sel, sizeof(p->sel));
598 memcpy(&p->lft, &x->lft, sizeof(p->lft));
599 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
600 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700601 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 p->mode = x->props.mode;
603 p->replay_window = x->props.replay_window;
604 p->reqid = x->props.reqid;
605 p->family = x->props.family;
606 p->flags = x->props.flags;
607 p->seq = x->km.seq;
608}
609
610struct xfrm_dump_info {
611 struct sk_buff *in_skb;
612 struct sk_buff *out_skb;
613 u32 nlmsg_seq;
614 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615};
616
Thomas Grafc0144be2007-08-22 13:55:43 -0700617static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
618{
Thomas Grafc0144be2007-08-22 13:55:43 -0700619 struct xfrm_user_sec_ctx *uctx;
620 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700621 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700622
623 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
624 if (attr == NULL)
625 return -EMSGSIZE;
626
627 uctx = nla_data(attr);
628 uctx->exttype = XFRMA_SEC_CTX;
629 uctx->len = ctx_size;
630 uctx->ctx_doi = s->ctx_doi;
631 uctx->ctx_alg = s->ctx_alg;
632 uctx->ctx_len = s->ctx_len;
633 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
634
635 return 0;
636}
637
Martin Willi4447bb32009-11-25 00:29:52 +0000638static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
639{
640 struct xfrm_algo *algo;
641 struct nlattr *nla;
642
643 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
644 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
645 if (!nla)
646 return -EMSGSIZE;
647
648 algo = nla_data(nla);
649 strcpy(algo->alg_name, auth->alg_name);
650 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
651 algo->alg_key_len = auth->alg_key_len;
652
653 return 0;
654}
655
Herbert Xu68325d32007-10-09 13:30:57 -0700656/* Don't change this without updating xfrm_sa_len! */
657static int copy_to_user_state_extra(struct xfrm_state *x,
658 struct xfrm_usersa_info *p,
659 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 copy_to_user_state(x, p);
662
Herbert Xu050f0092007-10-09 13:31:47 -0700663 if (x->coaddr)
664 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
665
666 if (x->lastused)
667 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700668
Herbert Xu1a6509d2008-01-28 19:37:29 -0800669 if (x->aead)
670 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000671 if (x->aalg) {
672 if (copy_to_user_auth(x->aalg, skb))
673 goto nla_put_failure;
674
675 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
676 xfrm_alg_auth_len(x->aalg), x->aalg);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800679 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700681 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700684 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Thomas Grafc0144be2007-08-22 13:55:43 -0700686 if (x->security && copy_sec_ctx(x->security, skb) < 0)
687 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700688
Herbert Xu68325d32007-10-09 13:30:57 -0700689 return 0;
690
691nla_put_failure:
692 return -EMSGSIZE;
693}
694
695static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
696{
697 struct xfrm_dump_info *sp = ptr;
698 struct sk_buff *in_skb = sp->in_skb;
699 struct sk_buff *skb = sp->out_skb;
700 struct xfrm_usersa_info *p;
701 struct nlmsghdr *nlh;
702 int err;
703
Herbert Xu68325d32007-10-09 13:30:57 -0700704 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
705 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
706 if (nlh == NULL)
707 return -EMSGSIZE;
708
709 p = nlmsg_data(nlh);
710
711 err = copy_to_user_state_extra(x, p, skb);
712 if (err)
713 goto nla_put_failure;
714
Thomas Graf98250692007-08-22 12:47:26 -0700715 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
717
Thomas Grafc0144be2007-08-22 13:55:43 -0700718nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700719 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700720 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Timo Teras4c563f72008-02-28 21:31:08 -0800723static int xfrm_dump_sa_done(struct netlink_callback *cb)
724{
725 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
726 xfrm_state_walk_done(walk);
727 return 0;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
731{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800732 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800733 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct xfrm_dump_info info;
735
Timo Teras4c563f72008-02-28 21:31:08 -0800736 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
737 sizeof(cb->args) - sizeof(cb->args[0]));
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 info.in_skb = cb->skb;
740 info.out_skb = skb;
741 info.nlmsg_seq = cb->nlh->nlmsg_seq;
742 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800743
744 if (!cb->args[0]) {
745 cb->args[0] = 1;
746 xfrm_state_walk_init(walk, 0);
747 }
748
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800749 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 return skb->len;
752}
753
754static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
755 struct xfrm_state *x, u32 seq)
756{
757 struct xfrm_dump_info info;
758 struct sk_buff *skb;
759
Thomas Graf7deb2262007-08-22 13:57:39 -0700760 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!skb)
762 return ERR_PTR(-ENOMEM);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 info.in_skb = in_skb;
765 info.out_skb = skb;
766 info.nlmsg_seq = seq;
767 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 if (dump_one_state(x, 0, &info)) {
770 kfree_skb(skb);
771 return NULL;
772 }
773
774 return skb;
775}
776
Thomas Graf7deb2262007-08-22 13:57:39 -0700777static inline size_t xfrm_spdinfo_msgsize(void)
778{
779 return NLMSG_ALIGN(4)
780 + nla_total_size(sizeof(struct xfrmu_spdinfo))
781 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
782}
783
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700784static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
785{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700786 struct xfrmk_spdinfo si;
787 struct xfrmu_spdinfo spc;
788 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700789 struct nlmsghdr *nlh;
790 u32 *f;
791
792 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
793 if (nlh == NULL) /* shouldnt really happen ... */
794 return -EMSGSIZE;
795
796 f = nlmsg_data(nlh);
797 *f = flags;
798 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700799 spc.incnt = si.incnt;
800 spc.outcnt = si.outcnt;
801 spc.fwdcnt = si.fwdcnt;
802 spc.inscnt = si.inscnt;
803 spc.outscnt = si.outscnt;
804 spc.fwdscnt = si.fwdscnt;
805 sph.spdhcnt = si.spdhcnt;
806 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700807
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700808 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
809 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700810
811 return nlmsg_end(skb, nlh);
812
813nla_put_failure:
814 nlmsg_cancel(skb, nlh);
815 return -EMSGSIZE;
816}
817
818static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700819 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700820{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800821 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700822 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700823 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700824 u32 spid = NETLINK_CB(skb).pid;
825 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700826
Thomas Graf7deb2262007-08-22 13:57:39 -0700827 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700828 if (r_skb == NULL)
829 return -ENOMEM;
830
831 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
832 BUG();
833
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800834 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700835}
836
Thomas Graf7deb2262007-08-22 13:57:39 -0700837static inline size_t xfrm_sadinfo_msgsize(void)
838{
839 return NLMSG_ALIGN(4)
840 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
841 + nla_total_size(4); /* XFRMA_SAD_CNT */
842}
843
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700844static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
845{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700846 struct xfrmk_sadinfo si;
847 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700848 struct nlmsghdr *nlh;
849 u32 *f;
850
851 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
852 if (nlh == NULL) /* shouldnt really happen ... */
853 return -EMSGSIZE;
854
855 f = nlmsg_data(nlh);
856 *f = flags;
857 xfrm_sad_getinfo(&si);
858
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700859 sh.sadhmcnt = si.sadhmcnt;
860 sh.sadhcnt = si.sadhcnt;
861
862 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
863 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700864
865 return nlmsg_end(skb, nlh);
866
867nla_put_failure:
868 nlmsg_cancel(skb, nlh);
869 return -EMSGSIZE;
870}
871
872static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700873 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700874{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800875 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700876 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700877 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700878 u32 spid = NETLINK_CB(skb).pid;
879 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700880
Thomas Graf7deb2262007-08-22 13:57:39 -0700881 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700882 if (r_skb == NULL)
883 return -ENOMEM;
884
885 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
886 BUG();
887
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800888 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700889}
890
Christoph Hellwig22e70052007-01-02 15:22:30 -0800891static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700892 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800894 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700895 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 struct xfrm_state *x;
897 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700898 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800900 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (x == NULL)
902 goto out_noput;
903
904 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
905 if (IS_ERR(resp_skb)) {
906 err = PTR_ERR(resp_skb);
907 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800908 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 xfrm_state_put(x);
911out_noput:
912 return err;
913}
914
915static int verify_userspi_info(struct xfrm_userspi_info *p)
916{
917 switch (p->info.id.proto) {
918 case IPPROTO_AH:
919 case IPPROTO_ESP:
920 break;
921
922 case IPPROTO_COMP:
923 /* IPCOMP spi is 16-bits. */
924 if (p->max >= 0x10000)
925 return -EINVAL;
926 break;
927
928 default:
929 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (p->min > p->max)
933 return -EINVAL;
934
935 return 0;
936}
937
Christoph Hellwig22e70052007-01-02 15:22:30 -0800938static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700939 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800941 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct xfrm_state *x;
943 struct xfrm_userspi_info *p;
944 struct sk_buff *resp_skb;
945 xfrm_address_t *daddr;
946 int family;
947 int err;
948
Thomas Graf7b67c852007-08-22 13:53:52 -0700949 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 err = verify_userspi_info(p);
951 if (err)
952 goto out_noput;
953
954 family = p->info.family;
955 daddr = &p->info.id.daddr;
956
957 x = NULL;
958 if (p->info.seq) {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800959 x = xfrm_find_acq_byseq(net, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
961 xfrm_state_put(x);
962 x = NULL;
963 }
964 }
965
966 if (!x)
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800967 x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 p->info.id.proto, daddr,
969 &p->info.saddr, 1,
970 family);
971 err = -ENOENT;
972 if (x == NULL)
973 goto out_noput;
974
Herbert Xu658b2192007-10-09 13:29:52 -0700975 err = xfrm_alloc_spi(x, p->min, p->max);
976 if (err)
977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Herbert Xu658b2192007-10-09 13:29:52 -0700979 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (IS_ERR(resp_skb)) {
981 err = PTR_ERR(resp_skb);
982 goto out;
983 }
984
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800985 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987out:
988 xfrm_state_put(x);
989out_noput:
990 return err;
991}
992
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800993static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 switch (dir) {
996 case XFRM_POLICY_IN:
997 case XFRM_POLICY_OUT:
998 case XFRM_POLICY_FWD:
999 break;
1000
1001 default:
1002 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 return 0;
1006}
1007
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001008static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001009{
1010 switch (type) {
1011 case XFRM_POLICY_TYPE_MAIN:
1012#ifdef CONFIG_XFRM_SUB_POLICY
1013 case XFRM_POLICY_TYPE_SUB:
1014#endif
1015 break;
1016
1017 default:
1018 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001019 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001020
1021 return 0;
1022}
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1025{
1026 switch (p->share) {
1027 case XFRM_SHARE_ANY:
1028 case XFRM_SHARE_SESSION:
1029 case XFRM_SHARE_USER:
1030 case XFRM_SHARE_UNIQUE:
1031 break;
1032
1033 default:
1034 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 switch (p->action) {
1038 case XFRM_POLICY_ALLOW:
1039 case XFRM_POLICY_BLOCK:
1040 break;
1041
1042 default:
1043 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 switch (p->sel.family) {
1047 case AF_INET:
1048 break;
1049
1050 case AF_INET6:
1051#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1052 break;
1053#else
1054 return -EAFNOSUPPORT;
1055#endif
1056
1057 default:
1058 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 return verify_policy_dir(p->dir);
1062}
1063
Thomas Graf5424f322007-08-22 14:01:33 -07001064static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001065{
Thomas Graf5424f322007-08-22 14:01:33 -07001066 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001067 struct xfrm_user_sec_ctx *uctx;
1068
1069 if (!rt)
1070 return 0;
1071
Thomas Graf5424f322007-08-22 14:01:33 -07001072 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001073 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1077 int nr)
1078{
1079 int i;
1080
1081 xp->xfrm_nr = nr;
1082 for (i = 0; i < nr; i++, ut++) {
1083 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1084
1085 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1086 memcpy(&t->saddr, &ut->saddr,
1087 sizeof(xfrm_address_t));
1088 t->reqid = ut->reqid;
1089 t->mode = ut->mode;
1090 t->share = ut->share;
1091 t->optional = ut->optional;
1092 t->aalgos = ut->aalgos;
1093 t->ealgos = ut->ealgos;
1094 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001095 /* If all masks are ~0, then we allow all algorithms. */
1096 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001097 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099}
1100
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001101static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1102{
1103 int i;
1104
1105 if (nr > XFRM_MAX_DEPTH)
1106 return -EINVAL;
1107
1108 for (i = 0; i < nr; i++) {
1109 /* We never validated the ut->family value, so many
1110 * applications simply leave it at zero. The check was
1111 * never made and ut->family was ignored because all
1112 * templates could be assumed to have the same family as
1113 * the policy itself. Now that we will have ipv4-in-ipv6
1114 * and ipv6-in-ipv4 tunnels, this is no longer true.
1115 */
1116 if (!ut[i].family)
1117 ut[i].family = family;
1118
1119 switch (ut[i].family) {
1120 case AF_INET:
1121 break;
1122#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1123 case AF_INET6:
1124 break;
1125#endif
1126 default:
1127 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001128 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001129 }
1130
1131 return 0;
1132}
1133
Thomas Graf5424f322007-08-22 14:01:33 -07001134static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Thomas Graf5424f322007-08-22 14:01:33 -07001136 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (!rt) {
1139 pol->xfrm_nr = 0;
1140 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001141 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1142 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001143 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001145 err = validate_tmpl(nr, utmpl, pol->family);
1146 if (err)
1147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Thomas Graf5424f322007-08-22 14:01:33 -07001149 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 return 0;
1152}
1153
Thomas Graf5424f322007-08-22 14:01:33 -07001154static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001155{
Thomas Graf5424f322007-08-22 14:01:33 -07001156 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001157 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001158 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001159 int err;
1160
1161 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001162 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001163 type = upt->type;
1164 }
1165
1166 err = verify_policy_type(type);
1167 if (err)
1168 return err;
1169
1170 *tp = type;
1171 return 0;
1172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1175{
1176 xp->priority = p->priority;
1177 xp->index = p->index;
1178 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1179 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1180 xp->action = p->action;
1181 xp->flags = p->flags;
1182 xp->family = p->sel.family;
1183 /* XXX xp->share = p->share; */
1184}
1185
1186static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1187{
1188 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1189 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1190 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1191 p->priority = xp->priority;
1192 p->index = xp->index;
1193 p->sel.family = xp->family;
1194 p->dir = dir;
1195 p->action = xp->action;
1196 p->flags = xp->flags;
1197 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1198}
1199
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001200static 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 -07001201{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001202 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int err;
1204
1205 if (!xp) {
1206 *errp = -ENOMEM;
1207 return NULL;
1208 }
1209
1210 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001211
Thomas Graf35a7aa02007-08-22 14:00:40 -07001212 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001213 if (err)
1214 goto error;
1215
Thomas Graf35a7aa02007-08-22 14:00:40 -07001216 if (!(err = copy_from_user_tmpl(xp, attrs)))
1217 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001218 if (err)
1219 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001222 error:
1223 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001224 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001225 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001226 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Christoph Hellwig22e70052007-01-02 15:22:30 -08001229static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001230 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001232 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001233 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001235 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int err;
1237 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001238 uid_t loginuid = NETLINK_CB(skb).loginuid;
1239 u32 sessionid = NETLINK_CB(skb).sessionid;
1240 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 err = verify_newpolicy_info(p);
1243 if (err)
1244 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001245 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001246 if (err)
1247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001249 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (!xp)
1251 return err;
1252
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001253 /* shouldnt excl be based on nlh flags??
1254 * Aha! this is anti-netlink really i.e more pfkey derived
1255 * in netlink excl is a flag and you wouldnt need
1256 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1258 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001259 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001262 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 kfree(xp);
1264 return err;
1265 }
1266
Herbert Xuf60f6b82005-06-18 22:44:37 -07001267 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001268 c.seq = nlh->nlmsg_seq;
1269 c.pid = nlh->nlmsg_pid;
1270 km_policy_notify(xp, p->dir, &c);
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 xfrm_pol_put(xp);
1273
1274 return 0;
1275}
1276
1277static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1278{
1279 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1280 int i;
1281
1282 if (xp->xfrm_nr == 0)
1283 return 0;
1284
1285 for (i = 0; i < xp->xfrm_nr; i++) {
1286 struct xfrm_user_tmpl *up = &vec[i];
1287 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1288
1289 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001290 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1292 up->reqid = kp->reqid;
1293 up->mode = kp->mode;
1294 up->share = kp->share;
1295 up->optional = kp->optional;
1296 up->aalgos = kp->aalgos;
1297 up->ealgos = kp->ealgos;
1298 up->calgos = kp->calgos;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Thomas Grafc0144be2007-08-22 13:55:43 -07001301 return nla_put(skb, XFRMA_TMPL,
1302 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001303}
1304
Serge Hallyn0d681622006-07-24 23:30:44 -07001305static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1306{
1307 if (x->security) {
1308 return copy_sec_ctx(x->security, skb);
1309 }
1310 return 0;
1311}
1312
1313static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1314{
1315 if (xp->security) {
1316 return copy_sec_ctx(xp->security, skb);
1317 }
1318 return 0;
1319}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001320static inline size_t userpolicy_type_attrsize(void)
1321{
1322#ifdef CONFIG_XFRM_SUB_POLICY
1323 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1324#else
1325 return 0;
1326#endif
1327}
Serge Hallyn0d681622006-07-24 23:30:44 -07001328
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001329#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001330static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001331{
Thomas Grafc0144be2007-08-22 13:55:43 -07001332 struct xfrm_userpolicy_type upt = {
1333 .type = type,
1334 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001335
Thomas Grafc0144be2007-08-22 13:55:43 -07001336 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001337}
1338
1339#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001340static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001341{
1342 return 0;
1343}
1344#endif
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1347{
1348 struct xfrm_dump_info *sp = ptr;
1349 struct xfrm_userpolicy_info *p;
1350 struct sk_buff *in_skb = sp->in_skb;
1351 struct sk_buff *skb = sp->out_skb;
1352 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001354 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1355 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1356 if (nlh == NULL)
1357 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Thomas Graf7b67c852007-08-22 13:53:52 -07001359 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 copy_to_user_policy(xp, p, dir);
1361 if (copy_to_user_tmpl(xp, skb) < 0)
1362 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001363 if (copy_to_user_sec_ctx(xp, skb))
1364 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001365 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001366 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Thomas Graf98250692007-08-22 12:47:26 -07001368 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370
1371nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001372 nlmsg_cancel(skb, nlh);
1373 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Timo Teras4c563f72008-02-28 21:31:08 -08001376static int xfrm_dump_policy_done(struct netlink_callback *cb)
1377{
1378 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1379
1380 xfrm_policy_walk_done(walk);
1381 return 0;
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1385{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001386 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001387 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct xfrm_dump_info info;
1389
Timo Teras4c563f72008-02-28 21:31:08 -08001390 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1391 sizeof(cb->args) - sizeof(cb->args[0]));
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 info.in_skb = cb->skb;
1394 info.out_skb = skb;
1395 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1396 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001397
1398 if (!cb->args[0]) {
1399 cb->args[0] = 1;
1400 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1401 }
1402
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001403 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return skb->len;
1406}
1407
1408static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1409 struct xfrm_policy *xp,
1410 int dir, u32 seq)
1411{
1412 struct xfrm_dump_info info;
1413 struct sk_buff *skb;
1414
Thomas Graf7deb2262007-08-22 13:57:39 -07001415 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!skb)
1417 return ERR_PTR(-ENOMEM);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 info.in_skb = in_skb;
1420 info.out_skb = skb;
1421 info.nlmsg_seq = seq;
1422 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1425 kfree_skb(skb);
1426 return NULL;
1427 }
1428
1429 return skb;
1430}
1431
Christoph Hellwig22e70052007-01-02 15:22:30 -08001432static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001433 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001435 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 struct xfrm_policy *xp;
1437 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001438 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001440 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int delete;
1442
Thomas Graf7b67c852007-08-22 13:53:52 -07001443 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1445
Thomas Graf35a7aa02007-08-22 14:00:40 -07001446 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001447 if (err)
1448 return err;
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 err = verify_policy_dir(p->dir);
1451 if (err)
1452 return err;
1453
1454 if (p->index)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001455 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001456 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001457 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001458 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001459
Thomas Graf35a7aa02007-08-22 14:00:40 -07001460 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001461 if (err)
1462 return err;
1463
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001464 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001465 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001466 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001467
Paul Moore03e1ad72008-04-12 19:07:52 -07001468 err = security_xfrm_policy_alloc(&ctx, uctx);
1469 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001470 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001471 }
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001472 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001473 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001474 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (xp == NULL)
1477 return -ENOENT;
1478
1479 if (!delete) {
1480 struct sk_buff *resp_skb;
1481
1482 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1483 if (IS_ERR(resp_skb)) {
1484 err = PTR_ERR(resp_skb);
1485 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001486 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001487 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001489 } else {
Eric Paris25323862008-04-18 10:09:25 -04001490 uid_t loginuid = NETLINK_CB(skb).loginuid;
1491 u32 sessionid = NETLINK_CB(skb).sessionid;
1492 u32 sid = NETLINK_CB(skb).sid;
1493
1494 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1495 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001496
1497 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001498 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001499
Herbert Xue7443892005-06-18 22:44:18 -07001500 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001501 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001502 c.seq = nlh->nlmsg_seq;
1503 c.pid = nlh->nlmsg_pid;
1504 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001507out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001508 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return err;
1510}
1511
Christoph Hellwig22e70052007-01-02 15:22:30 -08001512static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001513 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001515 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001516 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001517 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001518 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001519 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Joy Latten161a09e2006-11-27 13:11:54 -06001521 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001522 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001523 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001524 err = xfrm_state_flush(net, p->proto, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001525 if (err)
1526 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001527 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001528 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001529 c.seq = nlh->nlmsg_seq;
1530 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001531 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001532 km_state_notify(NULL, &c);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
1535}
1536
Thomas Graf7deb2262007-08-22 13:57:39 -07001537static inline size_t xfrm_aevent_msgsize(void)
1538{
1539 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1540 + nla_total_size(sizeof(struct xfrm_replay_state))
1541 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1542 + nla_total_size(4) /* XFRM_AE_RTHR */
1543 + nla_total_size(4); /* XFRM_AE_ETHR */
1544}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001545
1546static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1547{
1548 struct xfrm_aevent_id *id;
1549 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001550
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001551 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1552 if (nlh == NULL)
1553 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001554
Thomas Graf7b67c852007-08-22 13:53:52 -07001555 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001556 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001557 id->sa_id.spi = x->id.spi;
1558 id->sa_id.family = x->props.family;
1559 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001560 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1561 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001562 id->flags = c->data.aevent;
1563
Thomas Grafc0144be2007-08-22 13:55:43 -07001564 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1565 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001566
Thomas Grafc0144be2007-08-22 13:55:43 -07001567 if (id->flags & XFRM_AE_RTHR)
1568 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001569
Thomas Grafc0144be2007-08-22 13:55:43 -07001570 if (id->flags & XFRM_AE_ETHR)
1571 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1572 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001573
Thomas Graf98250692007-08-22 12:47:26 -07001574 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001575
Thomas Grafc0144be2007-08-22 13:55:43 -07001576nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001577 nlmsg_cancel(skb, nlh);
1578 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001579}
1580
Christoph Hellwig22e70052007-01-02 15:22:30 -08001581static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001582 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001583{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001584 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001585 struct xfrm_state *x;
1586 struct sk_buff *r_skb;
1587 int err;
1588 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001589 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001590 struct xfrm_usersa_id *id = &p->sa_id;
1591
Thomas Graf7deb2262007-08-22 13:57:39 -07001592 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001593 if (r_skb == NULL)
1594 return -ENOMEM;
1595
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001596 x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001597 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001598 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001599 return -ESRCH;
1600 }
1601
1602 /*
1603 * XXX: is this lock really needed - none of the other
1604 * gets lock (the concern is things getting updated
1605 * while we are still reading) - jhs
1606 */
1607 spin_lock_bh(&x->lock);
1608 c.data.aevent = p->flags;
1609 c.seq = nlh->nlmsg_seq;
1610 c.pid = nlh->nlmsg_pid;
1611
1612 if (build_aevent(r_skb, x, &c) < 0)
1613 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001614 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001615 spin_unlock_bh(&x->lock);
1616 xfrm_state_put(x);
1617 return err;
1618}
1619
Christoph Hellwig22e70052007-01-02 15:22:30 -08001620static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001621 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001622{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001623 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001624 struct xfrm_state *x;
1625 struct km_event c;
1626 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001627 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001628 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1629 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001630
1631 if (!lt && !rp)
1632 return err;
1633
1634 /* pedantic mode - thou shalt sayeth replaceth */
1635 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1636 return err;
1637
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001638 x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001639 if (x == NULL)
1640 return -ESRCH;
1641
1642 if (x->km.state != XFRM_STATE_VALID)
1643 goto out;
1644
1645 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001646 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001647 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001648
1649 c.event = nlh->nlmsg_type;
1650 c.seq = nlh->nlmsg_seq;
1651 c.pid = nlh->nlmsg_pid;
1652 c.data.aevent = XFRM_AE_CU;
1653 km_state_notify(x, &c);
1654 err = 0;
1655out:
1656 xfrm_state_put(x);
1657 return err;
1658}
1659
Christoph Hellwig22e70052007-01-02 15:22:30 -08001660static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001661 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001663 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001664 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001665 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001666 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001667 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001668
Thomas Graf35a7aa02007-08-22 14:00:40 -07001669 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001670 if (err)
1671 return err;
1672
Joy Latten161a09e2006-11-27 13:11:54 -06001673 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001674 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001675 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001676 err = xfrm_policy_flush(net, type, &audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -04001677 if (err)
1678 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001679 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001680 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001681 c.seq = nlh->nlmsg_seq;
1682 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001683 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001684 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
1687
Christoph Hellwig22e70052007-01-02 15:22:30 -08001688static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001689 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001690{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001691 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001692 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001693 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001694 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001695 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001696 int err = -ENOENT;
1697
Thomas Graf35a7aa02007-08-22 14:00:40 -07001698 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001699 if (err)
1700 return err;
1701
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001702 if (p->index)
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001703 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001704 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001705 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001706 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001707
Thomas Graf35a7aa02007-08-22 14:00:40 -07001708 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001709 if (err)
1710 return err;
1711
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001712 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001713 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001714 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001715
Paul Moore03e1ad72008-04-12 19:07:52 -07001716 err = security_xfrm_policy_alloc(&ctx, uctx);
1717 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001718 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001719 }
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001720 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001721 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001722 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001723 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001724 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001725
Eric Parisef41aaa2007-03-07 15:37:58 -08001726 read_lock(&xp->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001727 if (xp->walk.dead) {
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001728 read_unlock(&xp->lock);
1729 goto out;
1730 }
1731
1732 read_unlock(&xp->lock);
1733 err = 0;
1734 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001735 uid_t loginuid = NETLINK_CB(skb).loginuid;
1736 uid_t sessionid = NETLINK_CB(skb).sessionid;
1737 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001738 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001739 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001740
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001741 } else {
1742 // reset the timers here?
1743 printk("Dont know what to do with soft policy expire\n");
1744 }
1745 km_policy_expired(xp, p->dir, up->hard, current->pid);
1746
1747out:
1748 xfrm_pol_put(xp);
1749 return err;
1750}
1751
Christoph Hellwig22e70052007-01-02 15:22:30 -08001752static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001753 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001754{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001755 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001756 struct xfrm_state *x;
1757 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001758 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001759 struct xfrm_usersa_info *p = &ue->state;
1760
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001761 x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001762
David S. Miller3a765aa2007-02-26 14:52:21 -08001763 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001764 if (x == NULL)
1765 return err;
1766
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001767 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001768 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001769 if (x->km.state != XFRM_STATE_VALID)
1770 goto out;
1771 km_state_expired(x, ue->hard, current->pid);
1772
Joy Latten161a09e2006-11-27 13:11:54 -06001773 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001774 uid_t loginuid = NETLINK_CB(skb).loginuid;
1775 uid_t sessionid = NETLINK_CB(skb).sessionid;
1776 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001777 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001778 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001779 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001780 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001781out:
1782 spin_unlock_bh(&x->lock);
1783 xfrm_state_put(x);
1784 return err;
1785}
1786
Christoph Hellwig22e70052007-01-02 15:22:30 -08001787static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001788 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001789{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001790 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001791 struct xfrm_policy *xp;
1792 struct xfrm_user_tmpl *ut;
1793 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001794 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001795
Thomas Graf7b67c852007-08-22 13:53:52 -07001796 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001797 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001798 int err = -ENOMEM;
1799
1800 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001801 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001802
1803 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001804 if (err)
1805 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001806
1807 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001808 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001809 if (!xp)
1810 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001811
1812 memcpy(&x->id, &ua->id, sizeof(ua->id));
1813 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1814 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1815
Thomas Graf5424f322007-08-22 14:01:33 -07001816 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001817 /* extract the templates and for each call km_key */
1818 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1819 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1820 memcpy(&x->id, &t->id, sizeof(x->id));
1821 x->props.mode = t->mode;
1822 x->props.reqid = t->reqid;
1823 x->props.family = ut->family;
1824 t->aalgos = ua->aalgos;
1825 t->ealgos = ua->ealgos;
1826 t->calgos = ua->calgos;
1827 err = km_query(x, t, xp);
1828
1829 }
1830
1831 kfree(x);
1832 kfree(xp);
1833
1834 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001835
1836bad_policy:
1837 printk("BAD policy passed\n");
1838free_state:
1839 kfree(x);
1840nomem:
1841 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001842}
1843
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001844#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001845static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001846 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001847 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001848{
Thomas Graf5424f322007-08-22 14:01:33 -07001849 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001850 struct xfrm_user_migrate *um;
1851 int i, num_migrate;
1852
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001853 if (k != NULL) {
1854 struct xfrm_user_kmaddress *uk;
1855
1856 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1857 memcpy(&k->local, &uk->local, sizeof(k->local));
1858 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1859 k->family = uk->family;
1860 k->reserved = uk->reserved;
1861 }
1862
Thomas Graf5424f322007-08-22 14:01:33 -07001863 um = nla_data(rt);
1864 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001865
1866 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1867 return -EINVAL;
1868
1869 for (i = 0; i < num_migrate; i++, um++, ma++) {
1870 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1871 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1872 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1873 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1874
1875 ma->proto = um->proto;
1876 ma->mode = um->mode;
1877 ma->reqid = um->reqid;
1878
1879 ma->old_family = um->old_family;
1880 ma->new_family = um->new_family;
1881 }
1882
1883 *num = i;
1884 return 0;
1885}
1886
1887static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001888 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001889{
Thomas Graf7b67c852007-08-22 13:53:52 -07001890 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001891 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001892 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001893 u8 type;
1894 int err;
1895 int n = 0;
1896
Thomas Graf35a7aa02007-08-22 14:00:40 -07001897 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001898 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001899
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001900 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1901
Thomas Graf5424f322007-08-22 14:01:33 -07001902 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001903 if (err)
1904 return err;
1905
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001906 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001907 if (err)
1908 return err;
1909
1910 if (!n)
1911 return 0;
1912
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001913 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001914
1915 return 0;
1916}
1917#else
1918static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001919 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001920{
1921 return -ENOPROTOOPT;
1922}
1923#endif
1924
1925#ifdef CONFIG_XFRM_MIGRATE
1926static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1927{
1928 struct xfrm_user_migrate um;
1929
1930 memset(&um, 0, sizeof(um));
1931 um.proto = m->proto;
1932 um.mode = m->mode;
1933 um.reqid = m->reqid;
1934 um.old_family = m->old_family;
1935 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1936 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1937 um.new_family = m->new_family;
1938 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1939 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1940
Thomas Grafc0144be2007-08-22 13:55:43 -07001941 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001942}
1943
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001944static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1945{
1946 struct xfrm_user_kmaddress uk;
1947
1948 memset(&uk, 0, sizeof(uk));
1949 uk.family = k->family;
1950 uk.reserved = k->reserved;
1951 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08001952 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001953
1954 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1955}
1956
1957static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07001958{
1959 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001960 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1961 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1962 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07001963}
1964
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001965static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001966 int num_migrate, struct xfrm_kmaddress *k,
1967 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001968{
1969 struct xfrm_migrate *mp;
1970 struct xfrm_userpolicy_id *pol_id;
1971 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001972 int i;
1973
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001974 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1975 if (nlh == NULL)
1976 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001977
Thomas Graf7b67c852007-08-22 13:53:52 -07001978 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001979 /* copy data from selector, dir, and type to the pol_id */
1980 memset(pol_id, 0, sizeof(*pol_id));
1981 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1982 pol_id->dir = dir;
1983
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001984 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1985 goto nlmsg_failure;
1986
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001987 if (copy_to_user_policy_type(type, skb) < 0)
1988 goto nlmsg_failure;
1989
1990 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1991 if (copy_to_user_migrate(mp, skb) < 0)
1992 goto nlmsg_failure;
1993 }
1994
Thomas Graf98250692007-08-22 12:47:26 -07001995 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001996nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001997 nlmsg_cancel(skb, nlh);
1998 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001999}
2000
2001static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002002 struct xfrm_migrate *m, int num_migrate,
2003 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002004{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002005 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002006 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002007
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002008 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002009 if (skb == NULL)
2010 return -ENOMEM;
2011
2012 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002013 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002014 BUG();
2015
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002016 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002017}
2018#else
2019static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002020 struct xfrm_migrate *m, int num_migrate,
2021 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002022{
2023 return -ENOPROTOOPT;
2024}
2025#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002026
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002027#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002028
2029static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002030 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002031 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2032 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2033 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2034 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2035 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2036 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002037 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002038 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002039 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002040 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002041 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002042 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002043 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002044 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2045 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002046 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002047 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002048 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2049 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050};
2051
Thomas Graf492b5582005-05-03 14:26:40 -07002052#undef XMSGSIZE
2053
Thomas Grafcf5cb792007-08-22 13:59:04 -07002054static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08002055 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002056 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2057 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2058 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2059 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2060 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2061 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2062 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2063 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2064 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2065 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2066 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2067 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2068 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2069 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002070 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002071};
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002074 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002076 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002077} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2078 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2079 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2080 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002081 .dump = xfrm_dump_sa,
2082 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002083 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2084 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2085 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002086 .dump = xfrm_dump_policy,
2087 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002088 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002089 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002090 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002091 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2092 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002093 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002094 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2095 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002096 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2097 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002098 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002099 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002100 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101};
2102
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002103static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002105 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002106 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002108 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002112 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 type -= XFRM_MSG_BASE;
2115 link = &xfrm_dispatch[type];
2116
2117 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002118 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2119 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Thomas Graf492b5582005-05-03 14:26:40 -07002121 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2122 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2123 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002127 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129
Thomas Graf35a7aa02007-08-22 14:00:40 -07002130 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002131 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002132 if (err < 0)
2133 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002136 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Thomas Graf5424f322007-08-22 14:01:33 -07002138 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002141static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002143 mutex_lock(&xfrm_cfg_mutex);
2144 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2145 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Thomas Graf7deb2262007-08-22 13:57:39 -07002148static inline size_t xfrm_expire_msgsize(void)
2149{
2150 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2151}
2152
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002153static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 struct xfrm_user_expire *ue;
2156 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002158 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2159 if (nlh == NULL)
2160 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Thomas Graf7b67c852007-08-22 13:53:52 -07002162 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002164 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Thomas Graf98250692007-08-22 12:47:26 -07002166 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002169static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002171 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 struct sk_buff *skb;
2173
Thomas Graf7deb2262007-08-22 13:57:39 -07002174 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (skb == NULL)
2176 return -ENOMEM;
2177
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002178 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 BUG();
2180
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002181 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002184static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2185{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002186 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002187 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002188
Thomas Graf7deb2262007-08-22 13:57:39 -07002189 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002190 if (skb == NULL)
2191 return -ENOMEM;
2192
2193 if (build_aevent(skb, x, c) < 0)
2194 BUG();
2195
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002196 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002197}
2198
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002199static int xfrm_notify_sa_flush(struct km_event *c)
2200{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002201 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002202 struct xfrm_usersa_flush *p;
2203 struct nlmsghdr *nlh;
2204 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002205 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002206
Thomas Graf7deb2262007-08-22 13:57:39 -07002207 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002208 if (skb == NULL)
2209 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002210
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002211 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2212 if (nlh == NULL) {
2213 kfree_skb(skb);
2214 return -EMSGSIZE;
2215 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002216
Thomas Graf7b67c852007-08-22 13:53:52 -07002217 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002218 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002219
Thomas Graf98250692007-08-22 12:47:26 -07002220 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002221
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002222 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002223}
2224
Thomas Graf7deb2262007-08-22 13:57:39 -07002225static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002226{
Thomas Graf7deb2262007-08-22 13:57:39 -07002227 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002228 if (x->aead)
2229 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002230 if (x->aalg) {
2231 l += nla_total_size(sizeof(struct xfrm_algo) +
2232 (x->aalg->alg_key_len + 7) / 8);
2233 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2234 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002235 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002236 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002237 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002238 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002239 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002240 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002241 if (x->security)
2242 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2243 x->security->ctx_len);
2244 if (x->coaddr)
2245 l += nla_total_size(sizeof(*x->coaddr));
2246
Herbert Xud26f3982007-11-13 21:47:08 -08002247 /* Must count x->lastused as it may become non-zero behind our back. */
2248 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002249
2250 return l;
2251}
2252
2253static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2254{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002255 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002256 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002257 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258 struct nlmsghdr *nlh;
2259 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002260 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002261 int headlen;
2262
2263 headlen = sizeof(*p);
2264 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002265 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002266 headlen = sizeof(*id);
2267 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002268 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002269
Thomas Graf7deb2262007-08-22 13:57:39 -07002270 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002271 if (skb == NULL)
2272 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002273
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002274 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2275 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002276 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277
Thomas Graf7b67c852007-08-22 13:53:52 -07002278 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002279 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002280 struct nlattr *attr;
2281
Thomas Graf7b67c852007-08-22 13:53:52 -07002282 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002283 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2284 id->spi = x->id.spi;
2285 id->family = x->props.family;
2286 id->proto = x->id.proto;
2287
Thomas Grafc0144be2007-08-22 13:55:43 -07002288 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2289 if (attr == NULL)
2290 goto nla_put_failure;
2291
2292 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002293 }
2294
Herbert Xu68325d32007-10-09 13:30:57 -07002295 if (copy_to_user_state_extra(x, p, skb))
2296 goto nla_put_failure;
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
Thomas Grafc0144be2007-08-22 13:55:43 -07002302nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002303 /* Somebody screwed up with xfrm_sa_len! */
2304 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305 kfree_skb(skb);
2306 return -1;
2307}
2308
2309static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2310{
2311
2312 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002313 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002314 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002315 case XFRM_MSG_NEWAE:
2316 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002317 case XFRM_MSG_DELSA:
2318 case XFRM_MSG_UPDSA:
2319 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002320 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002321 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002322 return xfrm_notify_sa_flush(c);
2323 default:
2324 printk("xfrm_user: Unknown SA event %d\n", c->event);
2325 break;
2326 }
2327
2328 return 0;
2329
2330}
2331
Thomas Graf7deb2262007-08-22 13:57:39 -07002332static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2333 struct xfrm_policy *xp)
2334{
2335 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2336 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2337 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2338 + userpolicy_type_attrsize();
2339}
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2342 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2343 int dir)
2344{
2345 struct xfrm_user_acquire *ua;
2346 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 __u32 seq = xfrm_get_acqseq();
2348
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002349 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2350 if (nlh == NULL)
2351 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Thomas Graf7b67c852007-08-22 13:53:52 -07002353 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 memcpy(&ua->id, &x->id, sizeof(ua->id));
2355 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2356 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2357 copy_to_user_policy(xp, &ua->policy, dir);
2358 ua->aalgos = xt->aalgos;
2359 ua->ealgos = xt->ealgos;
2360 ua->calgos = xt->calgos;
2361 ua->seq = x->km.seq = seq;
2362
2363 if (copy_to_user_tmpl(xp, skb) < 0)
2364 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002365 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002366 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002367 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002368 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Thomas Graf98250692007-08-22 12:47:26 -07002370 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002373 nlmsg_cancel(skb, nlh);
2374 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2378 struct xfrm_policy *xp, int dir)
2379{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002380 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Thomas Graf7deb2262007-08-22 13:57:39 -07002383 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (skb == NULL)
2385 return -ENOMEM;
2386
2387 if (build_acquire(skb, x, xt, xp, dir) < 0)
2388 BUG();
2389
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002390 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393/* User gives us xfrm_user_policy_info followed by an array of 0
2394 * or more templates.
2395 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002396static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 u8 *data, int len, int *dir)
2398{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002399 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2401 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2402 struct xfrm_policy *xp;
2403 int nr;
2404
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002405 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 case AF_INET:
2407 if (opt != IP_XFRM_POLICY) {
2408 *dir = -EOPNOTSUPP;
2409 return NULL;
2410 }
2411 break;
2412#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2413 case AF_INET6:
2414 if (opt != IPV6_XFRM_POLICY) {
2415 *dir = -EOPNOTSUPP;
2416 return NULL;
2417 }
2418 break;
2419#endif
2420 default:
2421 *dir = -EINVAL;
2422 return NULL;
2423 }
2424
2425 *dir = -EINVAL;
2426
2427 if (len < sizeof(*p) ||
2428 verify_newpolicy_info(p))
2429 return NULL;
2430
2431 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002432 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return NULL;
2434
Herbert Xua4f1bac2005-07-26 15:43:17 -07002435 if (p->dir > XFRM_POLICY_OUT)
2436 return NULL;
2437
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002438 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (xp == NULL) {
2440 *dir = -ENOBUFS;
2441 return NULL;
2442 }
2443
2444 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002445 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 copy_templates(xp, ut, nr);
2447
2448 *dir = p->dir;
2449
2450 return xp;
2451}
2452
Thomas Graf7deb2262007-08-22 13:57:39 -07002453static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2454{
2455 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2456 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2457 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2458 + userpolicy_type_attrsize();
2459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002462 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct xfrm_user_polexpire *upe;
2465 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002466 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002468 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2469 if (nlh == NULL)
2470 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Thomas Graf7b67c852007-08-22 13:53:52 -07002472 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 copy_to_user_policy(xp, &upe->pol, dir);
2474 if (copy_to_user_tmpl(xp, skb) < 0)
2475 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002476 if (copy_to_user_sec_ctx(xp, skb))
2477 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002478 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002479 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 upe->hard = !!hard;
2481
Thomas Graf98250692007-08-22 12:47:26 -07002482 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002485 nlmsg_cancel(skb, nlh);
2486 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002489static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002491 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Thomas Graf7deb2262007-08-22 13:57:39 -07002494 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (skb == NULL)
2496 return -ENOMEM;
2497
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002498 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 BUG();
2500
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002501 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002504static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2505{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002506 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002507 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002508 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002509 struct nlmsghdr *nlh;
2510 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002511 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002512 int headlen;
2513
2514 headlen = sizeof(*p);
2515 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002516 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002517 headlen = sizeof(*id);
2518 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002519 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002520 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002521
Thomas Graf7deb2262007-08-22 13:57:39 -07002522 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 if (skb == NULL)
2524 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002525
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002526 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2527 if (nlh == NULL)
2528 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002529
Thomas Graf7b67c852007-08-22 13:53:52 -07002530 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002531 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002532 struct nlattr *attr;
2533
Thomas Graf7b67c852007-08-22 13:53:52 -07002534 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002535 memset(id, 0, sizeof(*id));
2536 id->dir = dir;
2537 if (c->data.byid)
2538 id->index = xp->index;
2539 else
2540 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2541
Thomas Grafc0144be2007-08-22 13:55:43 -07002542 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2543 if (attr == NULL)
2544 goto nlmsg_failure;
2545
2546 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002547 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002548
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002549 copy_to_user_policy(xp, p, dir);
2550 if (copy_to_user_tmpl(xp, skb) < 0)
2551 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002552 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002553 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002554
Thomas Graf98250692007-08-22 12:47:26 -07002555 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002556
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002557 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002558
2559nlmsg_failure:
2560 kfree_skb(skb);
2561 return -1;
2562}
2563
2564static int xfrm_notify_policy_flush(struct km_event *c)
2565{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002566 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567 struct nlmsghdr *nlh;
2568 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002569
Thomas Graf7deb2262007-08-22 13:57:39 -07002570 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002571 if (skb == NULL)
2572 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002573
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002574 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2575 if (nlh == NULL)
2576 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002577 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2578 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002579
Thomas Graf98250692007-08-22 12:47:26 -07002580 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002581
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002582 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583
2584nlmsg_failure:
2585 kfree_skb(skb);
2586 return -1;
2587}
2588
2589static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2590{
2591
2592 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002593 case XFRM_MSG_NEWPOLICY:
2594 case XFRM_MSG_UPDPOLICY:
2595 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002596 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002597 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002598 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002599 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002600 return xfrm_exp_policy_notify(xp, dir, c);
2601 default:
2602 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2603 }
2604
2605 return 0;
2606
2607}
2608
Thomas Graf7deb2262007-08-22 13:57:39 -07002609static inline size_t xfrm_report_msgsize(void)
2610{
2611 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2612}
2613
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002614static int build_report(struct sk_buff *skb, u8 proto,
2615 struct xfrm_selector *sel, xfrm_address_t *addr)
2616{
2617 struct xfrm_user_report *ur;
2618 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002619
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002620 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2621 if (nlh == NULL)
2622 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002623
Thomas Graf7b67c852007-08-22 13:53:52 -07002624 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002625 ur->proto = proto;
2626 memcpy(&ur->sel, sel, sizeof(ur->sel));
2627
2628 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002629 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002630
Thomas Graf98250692007-08-22 12:47:26 -07002631 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002632
Thomas Grafc0144be2007-08-22 13:55:43 -07002633nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002634 nlmsg_cancel(skb, nlh);
2635 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002636}
2637
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002638static int xfrm_send_report(struct net *net, u8 proto,
2639 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002640{
2641 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002642
Thomas Graf7deb2262007-08-22 13:57:39 -07002643 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002644 if (skb == NULL)
2645 return -ENOMEM;
2646
2647 if (build_report(skb, proto, sel, addr) < 0)
2648 BUG();
2649
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002650 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002651}
2652
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002653static inline size_t xfrm_mapping_msgsize(void)
2654{
2655 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2656}
2657
2658static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2659 xfrm_address_t *new_saddr, __be16 new_sport)
2660{
2661 struct xfrm_user_mapping *um;
2662 struct nlmsghdr *nlh;
2663
2664 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2665 if (nlh == NULL)
2666 return -EMSGSIZE;
2667
2668 um = nlmsg_data(nlh);
2669
2670 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2671 um->id.spi = x->id.spi;
2672 um->id.family = x->props.family;
2673 um->id.proto = x->id.proto;
2674 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2675 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2676 um->new_sport = new_sport;
2677 um->old_sport = x->encap->encap_sport;
2678 um->reqid = x->props.reqid;
2679
2680 return nlmsg_end(skb, nlh);
2681}
2682
2683static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2684 __be16 sport)
2685{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002686 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002687 struct sk_buff *skb;
2688
2689 if (x->id.proto != IPPROTO_ESP)
2690 return -EINVAL;
2691
2692 if (!x->encap)
2693 return -EINVAL;
2694
2695 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2696 if (skb == NULL)
2697 return -ENOMEM;
2698
2699 if (build_mapping(skb, x, ipaddr, sport) < 0)
2700 BUG();
2701
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002702 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002703}
2704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705static struct xfrm_mgr netlink_mgr = {
2706 .id = "netlink",
2707 .notify = xfrm_send_state_notify,
2708 .acquire = xfrm_send_acquire,
2709 .compile_policy = xfrm_compile_policy,
2710 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002711 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002712 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002713 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714};
2715
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002716static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
Patrick McHardybe336902006-03-20 22:40:54 -08002718 struct sock *nlsk;
2719
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002720 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002721 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002722 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002724 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002725 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return 0;
2727}
2728
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002729static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002730{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002731 struct net *net;
2732 list_for_each_entry(net, net_exit_list, exit_list)
2733 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2734 synchronize_net();
2735 list_for_each_entry(net, net_exit_list, exit_list)
2736 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002737}
2738
2739static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002740 .init = xfrm_user_net_init,
2741 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002742};
2743
2744static int __init xfrm_user_init(void)
2745{
2746 int rv;
2747
2748 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2749
2750 rv = register_pernet_subsys(&xfrm_user_net_ops);
2751 if (rv < 0)
2752 return rv;
2753 rv = xfrm_register_km(&netlink_mgr);
2754 if (rv < 0)
2755 unregister_pernet_subsys(&xfrm_user_net_ops);
2756 return rv;
2757}
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759static void __exit xfrm_user_exit(void)
2760{
2761 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002762 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765module_init(xfrm_user_init);
2766module_exit(xfrm_user_exit);
2767MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002768MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002769