blob: 8eb889510916408526ccc5b3c5a297d87e37719f [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] ||
Martin Willi35d28562010-12-08 04:37:49 +0000151 attrs[XFRMA_ALG_COMP] ||
152 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto out;
154 break;
155
156 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800157 if (attrs[XFRMA_ALG_COMP])
158 goto out;
159 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000160 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800161 !attrs[XFRMA_ALG_CRYPT] &&
162 !attrs[XFRMA_ALG_AEAD])
163 goto out;
164 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000165 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800166 attrs[XFRMA_ALG_CRYPT]) &&
167 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto out;
Martin Willi35d28562010-12-08 04:37:49 +0000169 if (attrs[XFRMA_TFCPAD] &&
170 p->mode != XFRM_MODE_TUNNEL)
171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173
174 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700175 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800176 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700177 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000178 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Martin Willi35d28562010-12-08 04:37:49 +0000179 attrs[XFRMA_ALG_CRYPT] ||
180 attrs[XFRMA_TFCPAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out;
182 break;
183
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700184#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
185 case IPPROTO_DSTOPTS:
186 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700187 if (attrs[XFRMA_ALG_COMP] ||
188 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000189 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800190 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700191 attrs[XFRMA_ALG_CRYPT] ||
192 attrs[XFRMA_ENCAP] ||
193 attrs[XFRMA_SEC_CTX] ||
Martin Willi35d28562010-12-08 04:37:49 +0000194 attrs[XFRMA_TFCPAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700195 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700196 goto out;
197 break;
198#endif
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 default:
201 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Herbert Xu1a6509d2008-01-28 19:37:29 -0800204 if ((err = verify_aead(attrs)))
205 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000206 if ((err = verify_auth_trunc(attrs)))
207 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700208 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700210 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700212 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700214 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800215 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 err = -EINVAL;
218 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700219 case XFRM_MODE_TRANSPORT:
220 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700221 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700222 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
224
225 default:
226 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 err = 0;
230
231out:
232 return err;
233}
234
235static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
236 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700237 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct xfrm_algo *p, *ualg;
240 struct xfrm_algo_desc *algo;
241
242 if (!rta)
243 return 0;
244
Thomas Graf5424f322007-08-22 14:01:33 -0700245 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 algo = get_byname(ualg->alg_name, 1);
248 if (!algo)
249 return -ENOSYS;
250 *props = algo->desc.sadb_alg_id;
251
Eric Dumazet0f99be02008-01-08 23:39:06 -0800252 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!p)
254 return -ENOMEM;
255
Herbert Xu04ff1262006-08-13 08:50:00 +1000256 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *algpp = p;
258 return 0;
259}
260
Martin Willi4447bb32009-11-25 00:29:52 +0000261static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
262 struct nlattr *rta)
263{
264 struct xfrm_algo *ualg;
265 struct xfrm_algo_auth *p;
266 struct xfrm_algo_desc *algo;
267
268 if (!rta)
269 return 0;
270
271 ualg = nla_data(rta);
272
273 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
274 if (!algo)
275 return -ENOSYS;
276 *props = algo->desc.sadb_alg_id;
277
278 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
279 if (!p)
280 return -ENOMEM;
281
282 strcpy(p->alg_name, algo->name);
283 p->alg_key_len = ualg->alg_key_len;
284 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
285 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
286
287 *algpp = p;
288 return 0;
289}
290
291static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
292 struct nlattr *rta)
293{
294 struct xfrm_algo_auth *p, *ualg;
295 struct xfrm_algo_desc *algo;
296
297 if (!rta)
298 return 0;
299
300 ualg = nla_data(rta);
301
302 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
303 if (!algo)
304 return -ENOSYS;
305 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
306 return -EINVAL;
307 *props = algo->desc.sadb_alg_id;
308
309 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
310 if (!p)
311 return -ENOMEM;
312
313 strcpy(p->alg_name, algo->name);
314 if (!p->alg_trunc_len)
315 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
316
317 *algpp = p;
318 return 0;
319}
320
Herbert Xu1a6509d2008-01-28 19:37:29 -0800321static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
322 struct nlattr *rta)
323{
324 struct xfrm_algo_aead *p, *ualg;
325 struct xfrm_algo_desc *algo;
326
327 if (!rta)
328 return 0;
329
330 ualg = nla_data(rta);
331
332 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
333 if (!algo)
334 return -ENOSYS;
335 *props = algo->desc.sadb_alg_id;
336
337 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
338 if (!p)
339 return -ENOMEM;
340
341 strcpy(p->alg_name, algo->name);
342 *algpp = p;
343 return 0;
344}
345
Joy Latten661697f2007-04-13 16:14:35 -0700346static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800347{
Trent Jaegerdf718372005-12-13 23:12:27 -0800348 int len = 0;
349
350 if (xfrm_ctx) {
351 len += sizeof(struct xfrm_user_sec_ctx);
352 len += xfrm_ctx->ctx_len;
353 }
354 return len;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
358{
359 memcpy(&x->id, &p->id, sizeof(x->id));
360 memcpy(&x->sel, &p->sel, sizeof(x->sel));
361 memcpy(&x->lft, &p->lft, sizeof(x->lft));
362 x->props.mode = p->mode;
363 x->props.replay_window = p->replay_window;
364 x->props.reqid = p->reqid;
365 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700366 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700368
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700369 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700370 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800373/*
374 * someday when pfkey also has support, we could have the code
375 * somehow made shareable and move it to xfrm_state.c - JHS
376 *
377*/
Thomas Graf5424f322007-08-22 14:01:33 -0700378static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800379{
Thomas Graf5424f322007-08-22 14:01:33 -0700380 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
381 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
382 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
383 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800384
385 if (rp) {
386 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700387 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800388 memcpy(&x->replay, replay, sizeof(*replay));
389 memcpy(&x->preplay, replay, sizeof(*replay));
390 }
391
392 if (lt) {
393 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700394 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800395 x->curlft.bytes = ltime->bytes;
396 x->curlft.packets = ltime->packets;
397 x->curlft.add_time = ltime->add_time;
398 x->curlft.use_time = ltime->use_time;
399 }
400
Thomas Grafcf5cb792007-08-22 13:59:04 -0700401 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700402 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800403
Thomas Grafcf5cb792007-08-22 13:59:04 -0700404 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700405 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800406}
407
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800408static struct xfrm_state *xfrm_state_construct(struct net *net,
409 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700410 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int *errp)
412{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800413 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int err = -ENOMEM;
415
416 if (!x)
417 goto error_no_put;
418
419 copy_from_user_state(x, p);
420
Herbert Xu1a6509d2008-01-28 19:37:29 -0800421 if ((err = attach_aead(&x->aead, &x->props.ealgo,
422 attrs[XFRMA_ALG_AEAD])))
423 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000424 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
425 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000427 if (!x->props.aalgo) {
428 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
429 attrs[XFRMA_ALG_AUTH])))
430 goto error;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
433 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700434 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto error;
436 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
437 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700438 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700440
441 if (attrs[XFRMA_ENCAP]) {
442 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
443 sizeof(*x->encap), GFP_KERNEL);
444 if (x->encap == NULL)
445 goto error;
446 }
447
Martin Willi35d28562010-12-08 04:37:49 +0000448 if (attrs[XFRMA_TFCPAD])
449 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
450
Thomas Graffd211502007-09-06 03:28:08 -0700451 if (attrs[XFRMA_COADDR]) {
452 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
453 sizeof(*x->coaddr), GFP_KERNEL);
454 if (x->coaddr == NULL)
455 goto error;
456 }
457
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000458 xfrm_mark_get(attrs, &x->mark);
459
Herbert Xu72cb6962005-06-20 13:18:08 -0700460 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (err)
462 goto error;
463
Thomas Graffd211502007-09-06 03:28:08 -0700464 if (attrs[XFRMA_SEC_CTX] &&
465 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800466 goto error;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800469 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800470 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800471 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800472 x->preplay.bitmap = 0;
473 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
474 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
475
476 /* override default values from above */
477
Thomas Graf5424f322007-08-22 14:01:33 -0700478 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 return x;
481
482error:
483 x->km.state = XFRM_STATE_DEAD;
484 xfrm_state_put(x);
485error_no_put:
486 *errp = err;
487 return NULL;
488}
489
Christoph Hellwig22e70052007-01-02 15:22:30 -0800490static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700491 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800493 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700494 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct xfrm_state *x;
496 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700497 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400498 uid_t loginuid = NETLINK_CB(skb).loginuid;
499 u32 sessionid = NETLINK_CB(skb).sessionid;
500 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Thomas Graf35a7aa02007-08-22 14:00:40 -0700502 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (err)
504 return err;
505
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800506 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!x)
508 return err;
509
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700510 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
512 err = xfrm_state_add(x);
513 else
514 err = xfrm_state_update(x);
515
Eric Paris25323862008-04-18 10:09:25 -0400516 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (err < 0) {
519 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800520 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700524 c.seq = nlh->nlmsg_seq;
525 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700526 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700527
528 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700529out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700530 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return err;
532}
533
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800534static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
535 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700536 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700537 int *errp)
538{
539 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000540 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700541 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000542 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700543
544 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
545 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000546 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700547 } else {
548 xfrm_address_t *saddr = NULL;
549
Thomas Graf35a7aa02007-08-22 14:00:40 -0700550 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700551 if (!saddr) {
552 err = -EINVAL;
553 goto out;
554 }
555
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800556 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000557 x = xfrm_state_lookup_byaddr(net, mark,
558 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800559 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700560 }
561
562 out:
563 if (!x && errp)
564 *errp = err;
565 return x;
566}
567
Christoph Hellwig22e70052007-01-02 15:22:30 -0800568static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700569 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800571 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700573 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700574 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700575 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400576 uid_t loginuid = NETLINK_CB(skb).loginuid;
577 u32 sessionid = NETLINK_CB(skb).sessionid;
578 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800580 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
David S. Miller6f68dc32006-06-08 23:58:52 -0700584 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700585 goto out;
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700588 err = -EPERM;
589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700592 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600593
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700594 if (err < 0)
595 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700596
597 c.seq = nlh->nlmsg_seq;
598 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700599 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700600 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700602out:
Eric Paris25323862008-04-18 10:09:25 -0400603 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700604 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
609{
610 memcpy(&p->id, &x->id, sizeof(p->id));
611 memcpy(&p->sel, &x->sel, sizeof(p->sel));
612 memcpy(&p->lft, &x->lft, sizeof(p->lft));
613 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
614 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700615 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 p->mode = x->props.mode;
617 p->replay_window = x->props.replay_window;
618 p->reqid = x->props.reqid;
619 p->family = x->props.family;
620 p->flags = x->props.flags;
621 p->seq = x->km.seq;
622}
623
624struct xfrm_dump_info {
625 struct sk_buff *in_skb;
626 struct sk_buff *out_skb;
627 u32 nlmsg_seq;
628 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629};
630
Thomas Grafc0144be2007-08-22 13:55:43 -0700631static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
632{
Thomas Grafc0144be2007-08-22 13:55:43 -0700633 struct xfrm_user_sec_ctx *uctx;
634 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700635 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700636
637 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
638 if (attr == NULL)
639 return -EMSGSIZE;
640
641 uctx = nla_data(attr);
642 uctx->exttype = XFRMA_SEC_CTX;
643 uctx->len = ctx_size;
644 uctx->ctx_doi = s->ctx_doi;
645 uctx->ctx_alg = s->ctx_alg;
646 uctx->ctx_len = s->ctx_len;
647 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
648
649 return 0;
650}
651
Martin Willi4447bb32009-11-25 00:29:52 +0000652static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
653{
654 struct xfrm_algo *algo;
655 struct nlattr *nla;
656
657 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
658 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
659 if (!nla)
660 return -EMSGSIZE;
661
662 algo = nla_data(nla);
663 strcpy(algo->alg_name, auth->alg_name);
664 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
665 algo->alg_key_len = auth->alg_key_len;
666
667 return 0;
668}
669
Herbert Xu68325d32007-10-09 13:30:57 -0700670/* Don't change this without updating xfrm_sa_len! */
671static int copy_to_user_state_extra(struct xfrm_state *x,
672 struct xfrm_usersa_info *p,
673 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 copy_to_user_state(x, p);
676
Herbert Xu050f0092007-10-09 13:31:47 -0700677 if (x->coaddr)
678 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
679
680 if (x->lastused)
681 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700682
Herbert Xu1a6509d2008-01-28 19:37:29 -0800683 if (x->aead)
684 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000685 if (x->aalg) {
686 if (copy_to_user_auth(x->aalg, skb))
687 goto nla_put_failure;
688
689 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
690 xfrm_alg_auth_len(x->aalg), x->aalg);
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800693 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700695 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700698 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Martin Willi35d28562010-12-08 04:37:49 +0000700 if (x->tfcpad)
701 NLA_PUT_U32(skb, XFRMA_TFCPAD, x->tfcpad);
702
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000703 if (xfrm_mark_put(skb, &x->mark))
704 goto nla_put_failure;
705
Thomas Grafc0144be2007-08-22 13:55:43 -0700706 if (x->security && copy_sec_ctx(x->security, skb) < 0)
707 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700708
Herbert Xu68325d32007-10-09 13:30:57 -0700709 return 0;
710
711nla_put_failure:
712 return -EMSGSIZE;
713}
714
715static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
716{
717 struct xfrm_dump_info *sp = ptr;
718 struct sk_buff *in_skb = sp->in_skb;
719 struct sk_buff *skb = sp->out_skb;
720 struct xfrm_usersa_info *p;
721 struct nlmsghdr *nlh;
722 int err;
723
Herbert Xu68325d32007-10-09 13:30:57 -0700724 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
725 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
726 if (nlh == NULL)
727 return -EMSGSIZE;
728
729 p = nlmsg_data(nlh);
730
731 err = copy_to_user_state_extra(x, p, skb);
732 if (err)
733 goto nla_put_failure;
734
Thomas Graf98250692007-08-22 12:47:26 -0700735 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 0;
737
Thomas Grafc0144be2007-08-22 13:55:43 -0700738nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700739 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Timo Teras4c563f72008-02-28 21:31:08 -0800743static int xfrm_dump_sa_done(struct netlink_callback *cb)
744{
745 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
746 xfrm_state_walk_done(walk);
747 return 0;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
751{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800752 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800753 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct xfrm_dump_info info;
755
Timo Teras4c563f72008-02-28 21:31:08 -0800756 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
757 sizeof(cb->args) - sizeof(cb->args[0]));
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 info.in_skb = cb->skb;
760 info.out_skb = skb;
761 info.nlmsg_seq = cb->nlh->nlmsg_seq;
762 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800763
764 if (!cb->args[0]) {
765 cb->args[0] = 1;
766 xfrm_state_walk_init(walk, 0);
767 }
768
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800769 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 return skb->len;
772}
773
774static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
775 struct xfrm_state *x, u32 seq)
776{
777 struct xfrm_dump_info info;
778 struct sk_buff *skb;
779
Thomas Graf7deb2262007-08-22 13:57:39 -0700780 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!skb)
782 return ERR_PTR(-ENOMEM);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 info.in_skb = in_skb;
785 info.out_skb = skb;
786 info.nlmsg_seq = seq;
787 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (dump_one_state(x, 0, &info)) {
790 kfree_skb(skb);
791 return NULL;
792 }
793
794 return skb;
795}
796
Thomas Graf7deb2262007-08-22 13:57:39 -0700797static inline size_t xfrm_spdinfo_msgsize(void)
798{
799 return NLMSG_ALIGN(4)
800 + nla_total_size(sizeof(struct xfrmu_spdinfo))
801 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
802}
803
Alexey Dobriyane0710412010-01-23 13:37:10 +0000804static int build_spdinfo(struct sk_buff *skb, struct net *net,
805 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700806{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700807 struct xfrmk_spdinfo si;
808 struct xfrmu_spdinfo spc;
809 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700810 struct nlmsghdr *nlh;
811 u32 *f;
812
813 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
814 if (nlh == NULL) /* shouldnt really happen ... */
815 return -EMSGSIZE;
816
817 f = nlmsg_data(nlh);
818 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000819 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700820 spc.incnt = si.incnt;
821 spc.outcnt = si.outcnt;
822 spc.fwdcnt = si.fwdcnt;
823 spc.inscnt = si.inscnt;
824 spc.outscnt = si.outscnt;
825 spc.fwdscnt = si.fwdscnt;
826 sph.spdhcnt = si.spdhcnt;
827 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700828
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700829 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
830 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700831
832 return nlmsg_end(skb, nlh);
833
834nla_put_failure:
835 nlmsg_cancel(skb, nlh);
836 return -EMSGSIZE;
837}
838
839static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700840 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700841{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800842 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700843 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700844 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700845 u32 spid = NETLINK_CB(skb).pid;
846 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700847
Thomas Graf7deb2262007-08-22 13:57:39 -0700848 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700849 if (r_skb == NULL)
850 return -ENOMEM;
851
Alexey Dobriyane0710412010-01-23 13:37:10 +0000852 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700853 BUG();
854
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800855 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700856}
857
Thomas Graf7deb2262007-08-22 13:57:39 -0700858static inline size_t xfrm_sadinfo_msgsize(void)
859{
860 return NLMSG_ALIGN(4)
861 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
862 + nla_total_size(4); /* XFRMA_SAD_CNT */
863}
864
Alexey Dobriyane0710412010-01-23 13:37:10 +0000865static int build_sadinfo(struct sk_buff *skb, struct net *net,
866 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700867{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700868 struct xfrmk_sadinfo si;
869 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700870 struct nlmsghdr *nlh;
871 u32 *f;
872
873 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
874 if (nlh == NULL) /* shouldnt really happen ... */
875 return -EMSGSIZE;
876
877 f = nlmsg_data(nlh);
878 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000879 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700880
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700881 sh.sadhmcnt = si.sadhmcnt;
882 sh.sadhcnt = si.sadhcnt;
883
884 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
885 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700886
887 return nlmsg_end(skb, nlh);
888
889nla_put_failure:
890 nlmsg_cancel(skb, nlh);
891 return -EMSGSIZE;
892}
893
894static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700895 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700896{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800897 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700898 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700899 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700900 u32 spid = NETLINK_CB(skb).pid;
901 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700902
Thomas Graf7deb2262007-08-22 13:57:39 -0700903 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700904 if (r_skb == NULL)
905 return -ENOMEM;
906
Alexey Dobriyane0710412010-01-23 13:37:10 +0000907 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700908 BUG();
909
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800910 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700911}
912
Christoph Hellwig22e70052007-01-02 15:22:30 -0800913static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700914 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800916 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700917 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct xfrm_state *x;
919 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700920 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800922 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (x == NULL)
924 goto out_noput;
925
926 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
927 if (IS_ERR(resp_skb)) {
928 err = PTR_ERR(resp_skb);
929 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800930 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 xfrm_state_put(x);
933out_noput:
934 return err;
935}
936
937static int verify_userspi_info(struct xfrm_userspi_info *p)
938{
939 switch (p->info.id.proto) {
940 case IPPROTO_AH:
941 case IPPROTO_ESP:
942 break;
943
944 case IPPROTO_COMP:
945 /* IPCOMP spi is 16-bits. */
946 if (p->max >= 0x10000)
947 return -EINVAL;
948 break;
949
950 default:
951 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 if (p->min > p->max)
955 return -EINVAL;
956
957 return 0;
958}
959
Christoph Hellwig22e70052007-01-02 15:22:30 -0800960static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700961 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800963 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct xfrm_state *x;
965 struct xfrm_userspi_info *p;
966 struct sk_buff *resp_skb;
967 xfrm_address_t *daddr;
968 int family;
969 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000970 u32 mark;
971 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Thomas Graf7b67c852007-08-22 13:53:52 -0700973 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 err = verify_userspi_info(p);
975 if (err)
976 goto out_noput;
977
978 family = p->info.family;
979 daddr = &p->info.id.daddr;
980
981 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000982
983 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000985 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
987 xfrm_state_put(x);
988 x = NULL;
989 }
990 }
991
992 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000993 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 p->info.id.proto, daddr,
995 &p->info.saddr, 1,
996 family);
997 err = -ENOENT;
998 if (x == NULL)
999 goto out_noput;
1000
Herbert Xu658b2192007-10-09 13:29:52 -07001001 err = xfrm_alloc_spi(x, p->min, p->max);
1002 if (err)
1003 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Herbert Xu658b2192007-10-09 13:29:52 -07001005 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (IS_ERR(resp_skb)) {
1007 err = PTR_ERR(resp_skb);
1008 goto out;
1009 }
1010
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001011 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013out:
1014 xfrm_state_put(x);
1015out_noput:
1016 return err;
1017}
1018
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001019static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 switch (dir) {
1022 case XFRM_POLICY_IN:
1023 case XFRM_POLICY_OUT:
1024 case XFRM_POLICY_FWD:
1025 break;
1026
1027 default:
1028 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 return 0;
1032}
1033
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001034static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001035{
1036 switch (type) {
1037 case XFRM_POLICY_TYPE_MAIN:
1038#ifdef CONFIG_XFRM_SUB_POLICY
1039 case XFRM_POLICY_TYPE_SUB:
1040#endif
1041 break;
1042
1043 default:
1044 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001045 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001046
1047 return 0;
1048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1051{
1052 switch (p->share) {
1053 case XFRM_SHARE_ANY:
1054 case XFRM_SHARE_SESSION:
1055 case XFRM_SHARE_USER:
1056 case XFRM_SHARE_UNIQUE:
1057 break;
1058
1059 default:
1060 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 switch (p->action) {
1064 case XFRM_POLICY_ALLOW:
1065 case XFRM_POLICY_BLOCK:
1066 break;
1067
1068 default:
1069 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 switch (p->sel.family) {
1073 case AF_INET:
1074 break;
1075
1076 case AF_INET6:
1077#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1078 break;
1079#else
1080 return -EAFNOSUPPORT;
1081#endif
1082
1083 default:
1084 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 return verify_policy_dir(p->dir);
1088}
1089
Thomas Graf5424f322007-08-22 14:01:33 -07001090static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001091{
Thomas Graf5424f322007-08-22 14:01:33 -07001092 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001093 struct xfrm_user_sec_ctx *uctx;
1094
1095 if (!rt)
1096 return 0;
1097
Thomas Graf5424f322007-08-22 14:01:33 -07001098 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001099 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1103 int nr)
1104{
1105 int i;
1106
1107 xp->xfrm_nr = nr;
1108 for (i = 0; i < nr; i++, ut++) {
1109 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1110
1111 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1112 memcpy(&t->saddr, &ut->saddr,
1113 sizeof(xfrm_address_t));
1114 t->reqid = ut->reqid;
1115 t->mode = ut->mode;
1116 t->share = ut->share;
1117 t->optional = ut->optional;
1118 t->aalgos = ut->aalgos;
1119 t->ealgos = ut->ealgos;
1120 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001121 /* If all masks are ~0, then we allow all algorithms. */
1122 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001123 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125}
1126
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001127static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1128{
1129 int i;
1130
1131 if (nr > XFRM_MAX_DEPTH)
1132 return -EINVAL;
1133
1134 for (i = 0; i < nr; i++) {
1135 /* We never validated the ut->family value, so many
1136 * applications simply leave it at zero. The check was
1137 * never made and ut->family was ignored because all
1138 * templates could be assumed to have the same family as
1139 * the policy itself. Now that we will have ipv4-in-ipv6
1140 * and ipv6-in-ipv4 tunnels, this is no longer true.
1141 */
1142 if (!ut[i].family)
1143 ut[i].family = family;
1144
1145 switch (ut[i].family) {
1146 case AF_INET:
1147 break;
1148#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1149 case AF_INET6:
1150 break;
1151#endif
1152 default:
1153 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001154 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001155 }
1156
1157 return 0;
1158}
1159
Thomas Graf5424f322007-08-22 14:01:33 -07001160static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Thomas Graf5424f322007-08-22 14:01:33 -07001162 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (!rt) {
1165 pol->xfrm_nr = 0;
1166 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001167 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1168 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001169 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001171 err = validate_tmpl(nr, utmpl, pol->family);
1172 if (err)
1173 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Thomas Graf5424f322007-08-22 14:01:33 -07001175 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 return 0;
1178}
1179
Thomas Graf5424f322007-08-22 14:01:33 -07001180static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001181{
Thomas Graf5424f322007-08-22 14:01:33 -07001182 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001183 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001184 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001185 int err;
1186
1187 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001188 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001189 type = upt->type;
1190 }
1191
1192 err = verify_policy_type(type);
1193 if (err)
1194 return err;
1195
1196 *tp = type;
1197 return 0;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1201{
1202 xp->priority = p->priority;
1203 xp->index = p->index;
1204 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1205 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1206 xp->action = p->action;
1207 xp->flags = p->flags;
1208 xp->family = p->sel.family;
1209 /* XXX xp->share = p->share; */
1210}
1211
1212static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1213{
1214 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1215 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1216 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1217 p->priority = xp->priority;
1218 p->index = xp->index;
1219 p->sel.family = xp->family;
1220 p->dir = dir;
1221 p->action = xp->action;
1222 p->flags = xp->flags;
1223 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1224}
1225
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001226static 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 -07001227{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001228 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230
1231 if (!xp) {
1232 *errp = -ENOMEM;
1233 return NULL;
1234 }
1235
1236 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001237
Thomas Graf35a7aa02007-08-22 14:00:40 -07001238 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001239 if (err)
1240 goto error;
1241
Thomas Graf35a7aa02007-08-22 14:00:40 -07001242 if (!(err = copy_from_user_tmpl(xp, attrs)))
1243 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001244 if (err)
1245 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001247 xfrm_mark_get(attrs, &xp->mark);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001250 error:
1251 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001252 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001253 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001254 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
Christoph Hellwig22e70052007-01-02 15:22:30 -08001257static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001258 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001260 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001261 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001263 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int err;
1265 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001266 uid_t loginuid = NETLINK_CB(skb).loginuid;
1267 u32 sessionid = NETLINK_CB(skb).sessionid;
1268 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 err = verify_newpolicy_info(p);
1271 if (err)
1272 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001273 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001274 if (err)
1275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001277 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (!xp)
1279 return err;
1280
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001281 /* shouldnt excl be based on nlh flags??
1282 * Aha! this is anti-netlink really i.e more pfkey derived
1283 * in netlink excl is a flag and you wouldnt need
1284 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1286 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001287 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001290 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 kfree(xp);
1292 return err;
1293 }
1294
Herbert Xuf60f6b82005-06-18 22:44:37 -07001295 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001296 c.seq = nlh->nlmsg_seq;
1297 c.pid = nlh->nlmsg_pid;
1298 km_policy_notify(xp, p->dir, &c);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 xfrm_pol_put(xp);
1301
1302 return 0;
1303}
1304
1305static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1306{
1307 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1308 int i;
1309
1310 if (xp->xfrm_nr == 0)
1311 return 0;
1312
1313 for (i = 0; i < xp->xfrm_nr; i++) {
1314 struct xfrm_user_tmpl *up = &vec[i];
1315 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1316
1317 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001318 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1320 up->reqid = kp->reqid;
1321 up->mode = kp->mode;
1322 up->share = kp->share;
1323 up->optional = kp->optional;
1324 up->aalgos = kp->aalgos;
1325 up->ealgos = kp->ealgos;
1326 up->calgos = kp->calgos;
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Thomas Grafc0144be2007-08-22 13:55:43 -07001329 return nla_put(skb, XFRMA_TMPL,
1330 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001331}
1332
Serge Hallyn0d681622006-07-24 23:30:44 -07001333static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1334{
1335 if (x->security) {
1336 return copy_sec_ctx(x->security, skb);
1337 }
1338 return 0;
1339}
1340
1341static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1342{
1343 if (xp->security) {
1344 return copy_sec_ctx(xp->security, skb);
1345 }
1346 return 0;
1347}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001348static inline size_t userpolicy_type_attrsize(void)
1349{
1350#ifdef CONFIG_XFRM_SUB_POLICY
1351 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1352#else
1353 return 0;
1354#endif
1355}
Serge Hallyn0d681622006-07-24 23:30:44 -07001356
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001357#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001358static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001359{
Thomas Grafc0144be2007-08-22 13:55:43 -07001360 struct xfrm_userpolicy_type upt = {
1361 .type = type,
1362 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001363
Thomas Grafc0144be2007-08-22 13:55:43 -07001364 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001365}
1366
1367#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001368static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001369{
1370 return 0;
1371}
1372#endif
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1375{
1376 struct xfrm_dump_info *sp = ptr;
1377 struct xfrm_userpolicy_info *p;
1378 struct sk_buff *in_skb = sp->in_skb;
1379 struct sk_buff *skb = sp->out_skb;
1380 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001382 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1383 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1384 if (nlh == NULL)
1385 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Thomas Graf7b67c852007-08-22 13:53:52 -07001387 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 copy_to_user_policy(xp, p, dir);
1389 if (copy_to_user_tmpl(xp, skb) < 0)
1390 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001391 if (copy_to_user_sec_ctx(xp, skb))
1392 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001393 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001394 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001395 if (xfrm_mark_put(skb, &xp->mark))
1396 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Thomas Graf98250692007-08-22 12:47:26 -07001398 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
1400
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001401nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001403 nlmsg_cancel(skb, nlh);
1404 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Timo Teras4c563f72008-02-28 21:31:08 -08001407static int xfrm_dump_policy_done(struct netlink_callback *cb)
1408{
1409 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1410
1411 xfrm_policy_walk_done(walk);
1412 return 0;
1413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1416{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001417 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001418 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct xfrm_dump_info info;
1420
Timo Teras4c563f72008-02-28 21:31:08 -08001421 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1422 sizeof(cb->args) - sizeof(cb->args[0]));
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 info.in_skb = cb->skb;
1425 info.out_skb = skb;
1426 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1427 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001428
1429 if (!cb->args[0]) {
1430 cb->args[0] = 1;
1431 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1432 }
1433
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001434 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 return skb->len;
1437}
1438
1439static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1440 struct xfrm_policy *xp,
1441 int dir, u32 seq)
1442{
1443 struct xfrm_dump_info info;
1444 struct sk_buff *skb;
1445
Thomas Graf7deb2262007-08-22 13:57:39 -07001446 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!skb)
1448 return ERR_PTR(-ENOMEM);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 info.in_skb = in_skb;
1451 info.out_skb = skb;
1452 info.nlmsg_seq = seq;
1453 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1456 kfree_skb(skb);
1457 return NULL;
1458 }
1459
1460 return skb;
1461}
1462
Christoph Hellwig22e70052007-01-02 15:22:30 -08001463static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001464 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001466 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 struct xfrm_policy *xp;
1468 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001469 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001471 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int delete;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001473 struct xfrm_mark m;
1474 u32 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Thomas Graf7b67c852007-08-22 13:53:52 -07001476 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1478
Thomas Graf35a7aa02007-08-22 14:00:40 -07001479 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001480 if (err)
1481 return err;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 err = verify_policy_dir(p->dir);
1484 if (err)
1485 return err;
1486
1487 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001488 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001489 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001490 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001491 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001492
Thomas Graf35a7aa02007-08-22 14:00:40 -07001493 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001494 if (err)
1495 return err;
1496
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001497 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001498 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001499 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001500
Paul Moore03e1ad72008-04-12 19:07:52 -07001501 err = security_xfrm_policy_alloc(&ctx, uctx);
1502 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001503 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001504 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001505 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001506 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001507 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (xp == NULL)
1510 return -ENOENT;
1511
1512 if (!delete) {
1513 struct sk_buff *resp_skb;
1514
1515 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1516 if (IS_ERR(resp_skb)) {
1517 err = PTR_ERR(resp_skb);
1518 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001519 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001520 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001522 } else {
Eric Paris25323862008-04-18 10:09:25 -04001523 uid_t loginuid = NETLINK_CB(skb).loginuid;
1524 u32 sessionid = NETLINK_CB(skb).sessionid;
1525 u32 sid = NETLINK_CB(skb).sid;
1526
1527 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1528 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001529
1530 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001531 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001532
Herbert Xue7443892005-06-18 22:44:18 -07001533 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001534 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001535 c.seq = nlh->nlmsg_seq;
1536 c.pid = nlh->nlmsg_pid;
1537 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001540out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001541 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return err;
1543}
1544
Christoph Hellwig22e70052007-01-02 15:22:30 -08001545static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001546 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001548 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001549 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001550 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001551 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001552 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Joy Latten161a09e2006-11-27 13:11:54 -06001554 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001555 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001556 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001557 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001558 if (err) {
1559 if (err == -ESRCH) /* empty table */
1560 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001561 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001562 }
Herbert Xubf088672005-06-18 22:44:00 -07001563 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001564 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001565 c.seq = nlh->nlmsg_seq;
1566 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001567 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001568 km_state_notify(NULL, &c);
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 return 0;
1571}
1572
Thomas Graf7deb2262007-08-22 13:57:39 -07001573static inline size_t xfrm_aevent_msgsize(void)
1574{
1575 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1576 + nla_total_size(sizeof(struct xfrm_replay_state))
1577 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001578 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001579 + nla_total_size(4) /* XFRM_AE_RTHR */
1580 + nla_total_size(4); /* XFRM_AE_ETHR */
1581}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001582
1583static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1584{
1585 struct xfrm_aevent_id *id;
1586 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001587
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001588 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1589 if (nlh == NULL)
1590 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001591
Thomas Graf7b67c852007-08-22 13:53:52 -07001592 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001593 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001594 id->sa_id.spi = x->id.spi;
1595 id->sa_id.family = x->props.family;
1596 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001597 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1598 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001599 id->flags = c->data.aevent;
1600
Thomas Grafc0144be2007-08-22 13:55:43 -07001601 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1602 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001603
Thomas Grafc0144be2007-08-22 13:55:43 -07001604 if (id->flags & XFRM_AE_RTHR)
1605 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001606
Thomas Grafc0144be2007-08-22 13:55:43 -07001607 if (id->flags & XFRM_AE_ETHR)
1608 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1609 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001610
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001611 if (xfrm_mark_put(skb, &x->mark))
1612 goto nla_put_failure;
1613
Thomas Graf98250692007-08-22 12:47:26 -07001614 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001615
Thomas Grafc0144be2007-08-22 13:55:43 -07001616nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001617 nlmsg_cancel(skb, nlh);
1618 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001619}
1620
Christoph Hellwig22e70052007-01-02 15:22:30 -08001621static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001622 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001623{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001624 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001625 struct xfrm_state *x;
1626 struct sk_buff *r_skb;
1627 int err;
1628 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001629 u32 mark;
1630 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001631 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001632 struct xfrm_usersa_id *id = &p->sa_id;
1633
Thomas Graf7deb2262007-08-22 13:57:39 -07001634 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001635 if (r_skb == NULL)
1636 return -ENOMEM;
1637
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001638 mark = xfrm_mark_get(attrs, &m);
1639
1640 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001641 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001642 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001643 return -ESRCH;
1644 }
1645
1646 /*
1647 * XXX: is this lock really needed - none of the other
1648 * gets lock (the concern is things getting updated
1649 * while we are still reading) - jhs
1650 */
1651 spin_lock_bh(&x->lock);
1652 c.data.aevent = p->flags;
1653 c.seq = nlh->nlmsg_seq;
1654 c.pid = nlh->nlmsg_pid;
1655
1656 if (build_aevent(r_skb, x, &c) < 0)
1657 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001658 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001659 spin_unlock_bh(&x->lock);
1660 xfrm_state_put(x);
1661 return err;
1662}
1663
Christoph Hellwig22e70052007-01-02 15:22:30 -08001664static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001665 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001666{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001667 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001668 struct xfrm_state *x;
1669 struct km_event c;
1670 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001671 u32 mark = 0;
1672 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001673 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001674 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1675 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001676
1677 if (!lt && !rp)
1678 return err;
1679
1680 /* pedantic mode - thou shalt sayeth replaceth */
1681 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1682 return err;
1683
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001684 mark = xfrm_mark_get(attrs, &m);
1685
1686 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001687 if (x == NULL)
1688 return -ESRCH;
1689
1690 if (x->km.state != XFRM_STATE_VALID)
1691 goto out;
1692
1693 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001694 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001695 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001696
1697 c.event = nlh->nlmsg_type;
1698 c.seq = nlh->nlmsg_seq;
1699 c.pid = nlh->nlmsg_pid;
1700 c.data.aevent = XFRM_AE_CU;
1701 km_state_notify(x, &c);
1702 err = 0;
1703out:
1704 xfrm_state_put(x);
1705 return err;
1706}
1707
Christoph Hellwig22e70052007-01-02 15:22:30 -08001708static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001709 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001711 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001712 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001713 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001714 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001715 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001716
Thomas Graf35a7aa02007-08-22 14:00:40 -07001717 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001718 if (err)
1719 return err;
1720
Joy Latten161a09e2006-11-27 13:11:54 -06001721 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001722 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001723 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001724 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001725 if (err) {
1726 if (err == -ESRCH) /* empty table */
1727 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001728 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001729 }
1730
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001731 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001732 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001733 c.seq = nlh->nlmsg_seq;
1734 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001735 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001736 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return 0;
1738}
1739
Christoph Hellwig22e70052007-01-02 15:22:30 -08001740static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001741 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001742{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001743 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001744 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001745 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001746 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001747 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001748 int err = -ENOENT;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001749 struct xfrm_mark m;
1750 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001751
Thomas Graf35a7aa02007-08-22 14:00:40 -07001752 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001753 if (err)
1754 return err;
1755
Timo Teräsc8bf4d02010-03-31 00:17:04 +00001756 err = verify_policy_dir(p->dir);
1757 if (err)
1758 return err;
1759
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001760 if (p->index)
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001761 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001762 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001763 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001764 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001765
Thomas Graf35a7aa02007-08-22 14:00:40 -07001766 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001767 if (err)
1768 return err;
1769
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001770 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001771 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001772 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001773
Paul Moore03e1ad72008-04-12 19:07:52 -07001774 err = security_xfrm_policy_alloc(&ctx, uctx);
1775 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001776 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001777 }
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00001778 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001779 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001780 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001781 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001782 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001783 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001784
Timo Teräsea2dea92010-03-31 00:17:05 +00001785 if (unlikely(xp->walk.dead))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001786 goto out;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001787
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001788 err = 0;
1789 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001790 uid_t loginuid = NETLINK_CB(skb).loginuid;
1791 uid_t sessionid = NETLINK_CB(skb).sessionid;
1792 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001793 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001794 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001795
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001796 } else {
1797 // reset the timers here?
stephen hemminger62db5cf2010-05-12 06:37:06 +00001798 WARN(1, "Dont know what to do with soft policy expire\n");
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001799 }
1800 km_policy_expired(xp, p->dir, up->hard, current->pid);
1801
1802out:
1803 xfrm_pol_put(xp);
1804 return err;
1805}
1806
Christoph Hellwig22e70052007-01-02 15:22:30 -08001807static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001808 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001809{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001810 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001811 struct xfrm_state *x;
1812 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001813 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001814 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001815 struct xfrm_mark m;
Nicolas Dichtel928497f2010-08-31 05:54:00 +00001816 u32 mark = xfrm_mark_get(attrs, &m);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001817
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001818 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001819
David S. Miller3a765aa2007-02-26 14:52:21 -08001820 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001821 if (x == NULL)
1822 return err;
1823
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001824 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001825 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001826 if (x->km.state != XFRM_STATE_VALID)
1827 goto out;
1828 km_state_expired(x, ue->hard, current->pid);
1829
Joy Latten161a09e2006-11-27 13:11:54 -06001830 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001831 uid_t loginuid = NETLINK_CB(skb).loginuid;
1832 uid_t sessionid = NETLINK_CB(skb).sessionid;
1833 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001834 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001835 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001836 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001837 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001838out:
1839 spin_unlock_bh(&x->lock);
1840 xfrm_state_put(x);
1841 return err;
1842}
1843
Christoph Hellwig22e70052007-01-02 15:22:30 -08001844static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001845 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001846{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001847 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001848 struct xfrm_policy *xp;
1849 struct xfrm_user_tmpl *ut;
1850 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001851 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001852 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001853
Thomas Graf7b67c852007-08-22 13:53:52 -07001854 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001855 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001856 int err = -ENOMEM;
1857
1858 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001859 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001860
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001861 xfrm_mark_get(attrs, &mark);
1862
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001863 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001864 if (err)
1865 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001866
1867 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001868 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001869 if (!xp)
1870 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001871
1872 memcpy(&x->id, &ua->id, sizeof(ua->id));
1873 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1874 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001875 xp->mark.m = x->mark.m = mark.m;
1876 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001877 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001878 /* extract the templates and for each call km_key */
1879 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1880 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1881 memcpy(&x->id, &t->id, sizeof(x->id));
1882 x->props.mode = t->mode;
1883 x->props.reqid = t->reqid;
1884 x->props.family = ut->family;
1885 t->aalgos = ua->aalgos;
1886 t->ealgos = ua->ealgos;
1887 t->calgos = ua->calgos;
1888 err = km_query(x, t, xp);
1889
1890 }
1891
1892 kfree(x);
1893 kfree(xp);
1894
1895 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001896
1897bad_policy:
stephen hemminger62db5cf2010-05-12 06:37:06 +00001898 WARN(1, "BAD policy passed\n");
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001899free_state:
1900 kfree(x);
1901nomem:
1902 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001903}
1904
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001905#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001906static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001907 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001908 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001909{
Thomas Graf5424f322007-08-22 14:01:33 -07001910 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001911 struct xfrm_user_migrate *um;
1912 int i, num_migrate;
1913
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001914 if (k != NULL) {
1915 struct xfrm_user_kmaddress *uk;
1916
1917 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1918 memcpy(&k->local, &uk->local, sizeof(k->local));
1919 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1920 k->family = uk->family;
1921 k->reserved = uk->reserved;
1922 }
1923
Thomas Graf5424f322007-08-22 14:01:33 -07001924 um = nla_data(rt);
1925 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001926
1927 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1928 return -EINVAL;
1929
1930 for (i = 0; i < num_migrate; i++, um++, ma++) {
1931 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1932 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1933 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1934 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1935
1936 ma->proto = um->proto;
1937 ma->mode = um->mode;
1938 ma->reqid = um->reqid;
1939
1940 ma->old_family = um->old_family;
1941 ma->new_family = um->new_family;
1942 }
1943
1944 *num = i;
1945 return 0;
1946}
1947
1948static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001949 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001950{
Thomas Graf7b67c852007-08-22 13:53:52 -07001951 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001952 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001953 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001954 u8 type;
1955 int err;
1956 int n = 0;
1957
Thomas Graf35a7aa02007-08-22 14:00:40 -07001958 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001959 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001960
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001961 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1962
Thomas Graf5424f322007-08-22 14:01:33 -07001963 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001964 if (err)
1965 return err;
1966
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001967 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001968 if (err)
1969 return err;
1970
1971 if (!n)
1972 return 0;
1973
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001974 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001975
1976 return 0;
1977}
1978#else
1979static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001980 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001981{
1982 return -ENOPROTOOPT;
1983}
1984#endif
1985
1986#ifdef CONFIG_XFRM_MIGRATE
1987static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1988{
1989 struct xfrm_user_migrate um;
1990
1991 memset(&um, 0, sizeof(um));
1992 um.proto = m->proto;
1993 um.mode = m->mode;
1994 um.reqid = m->reqid;
1995 um.old_family = m->old_family;
1996 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1997 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1998 um.new_family = m->new_family;
1999 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
2000 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
2001
Thomas Grafc0144be2007-08-22 13:55:43 -07002002 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002003}
2004
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002005static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
2006{
2007 struct xfrm_user_kmaddress uk;
2008
2009 memset(&uk, 0, sizeof(uk));
2010 uk.family = k->family;
2011 uk.reserved = k->reserved;
2012 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08002013 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002014
2015 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
2016}
2017
2018static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002019{
2020 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002021 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2022 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2023 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002024}
2025
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002026static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002027 int num_migrate, struct xfrm_kmaddress *k,
2028 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002029{
2030 struct xfrm_migrate *mp;
2031 struct xfrm_userpolicy_id *pol_id;
2032 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002033 int i;
2034
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002035 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2036 if (nlh == NULL)
2037 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002038
Thomas Graf7b67c852007-08-22 13:53:52 -07002039 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002040 /* copy data from selector, dir, and type to the pol_id */
2041 memset(pol_id, 0, sizeof(*pol_id));
2042 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2043 pol_id->dir = dir;
2044
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002045 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2046 goto nlmsg_failure;
2047
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002048 if (copy_to_user_policy_type(type, skb) < 0)
2049 goto nlmsg_failure;
2050
2051 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2052 if (copy_to_user_migrate(mp, skb) < 0)
2053 goto nlmsg_failure;
2054 }
2055
Thomas Graf98250692007-08-22 12:47:26 -07002056 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002057nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002058 nlmsg_cancel(skb, nlh);
2059 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002060}
2061
2062static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002063 struct xfrm_migrate *m, int num_migrate,
2064 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002065{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002066 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002067 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002068
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002069 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002070 if (skb == NULL)
2071 return -ENOMEM;
2072
2073 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002074 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002075 BUG();
2076
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002077 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002078}
2079#else
2080static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002081 struct xfrm_migrate *m, int num_migrate,
2082 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002083{
2084 return -ENOPROTOOPT;
2085}
2086#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002087
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002088#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002089
2090static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002091 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002092 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2093 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2094 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2095 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2096 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2097 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002098 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002099 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002100 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002101 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002102 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002103 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002104 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002105 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2106 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002107 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002108 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002109 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2110 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111};
2112
Thomas Graf492b5582005-05-03 14:26:40 -07002113#undef XMSGSIZE
2114
Thomas Grafcf5cb792007-08-22 13:59:04 -07002115static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002116 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2117 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2118 [XFRMA_LASTUSED] = { .type = NLA_U64},
2119 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002120 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002121 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2122 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2123 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2124 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2125 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2126 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2127 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2128 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2129 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2130 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2131 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2132 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2133 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2134 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002135 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002136 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Martin Willi35d28562010-12-08 04:37:49 +00002137 [XFRMA_TFCPAD] = { .type = NLA_U32 },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002138};
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002141 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002143 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002144} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2145 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2146 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2147 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002148 .dump = xfrm_dump_sa,
2149 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002150 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2151 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2152 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002153 .dump = xfrm_dump_policy,
2154 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002155 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002156 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002157 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002158 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2159 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002160 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002161 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2162 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002163 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2164 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002165 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002166 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002167 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002170static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002172 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002173 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002175 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 type -= XFRM_MSG_BASE;
2182 link = &xfrm_dispatch[type];
2183
2184 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002185 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2186 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Thomas Graf492b5582005-05-03 14:26:40 -07002188 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2189 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2190 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002192 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002194 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
Thomas Graf35a7aa02007-08-22 14:00:40 -07002197 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002198 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002199 if (err < 0)
2200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002203 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Thomas Graf5424f322007-08-22 14:01:33 -07002205 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002208static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002210 mutex_lock(&xfrm_cfg_mutex);
2211 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2212 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Thomas Graf7deb2262007-08-22 13:57:39 -07002215static inline size_t xfrm_expire_msgsize(void)
2216{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002217 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2218 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002219}
2220
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002221static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
2223 struct xfrm_user_expire *ue;
2224 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002226 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2227 if (nlh == NULL)
2228 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Thomas Graf7b67c852007-08-22 13:53:52 -07002230 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002232 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002234 if (xfrm_mark_put(skb, &x->mark))
2235 goto nla_put_failure;
2236
Thomas Graf98250692007-08-22 12:47:26 -07002237 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002238
2239nla_put_failure:
2240 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002243static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002245 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 struct sk_buff *skb;
2247
Thomas Graf7deb2262007-08-22 13:57:39 -07002248 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (skb == NULL)
2250 return -ENOMEM;
2251
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002252 if (build_expire(skb, x, c) < 0) {
2253 kfree_skb(skb);
2254 return -EMSGSIZE;
2255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002257 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002260static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2261{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002262 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002263 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002264
Thomas Graf7deb2262007-08-22 13:57:39 -07002265 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002266 if (skb == NULL)
2267 return -ENOMEM;
2268
2269 if (build_aevent(skb, x, c) < 0)
2270 BUG();
2271
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002272 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002273}
2274
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002275static int xfrm_notify_sa_flush(struct km_event *c)
2276{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002277 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002278 struct xfrm_usersa_flush *p;
2279 struct nlmsghdr *nlh;
2280 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002281 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002282
Thomas Graf7deb2262007-08-22 13:57:39 -07002283 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002284 if (skb == NULL)
2285 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002286
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002287 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2288 if (nlh == NULL) {
2289 kfree_skb(skb);
2290 return -EMSGSIZE;
2291 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002292
Thomas Graf7b67c852007-08-22 13:53:52 -07002293 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002294 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295
Thomas Graf98250692007-08-22 12:47:26 -07002296 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002297
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002298 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002299}
2300
Thomas Graf7deb2262007-08-22 13:57:39 -07002301static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002302{
Thomas Graf7deb2262007-08-22 13:57:39 -07002303 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002304 if (x->aead)
2305 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002306 if (x->aalg) {
2307 l += nla_total_size(sizeof(struct xfrm_algo) +
2308 (x->aalg->alg_key_len + 7) / 8);
2309 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2310 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002311 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002312 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002313 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002314 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002316 l += nla_total_size(sizeof(*x->encap));
Martin Willi35d28562010-12-08 04:37:49 +00002317 if (x->tfcpad)
2318 l += nla_total_size(sizeof(x->tfcpad));
Herbert Xu68325d32007-10-09 13:30:57 -07002319 if (x->security)
2320 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2321 x->security->ctx_len);
2322 if (x->coaddr)
2323 l += nla_total_size(sizeof(*x->coaddr));
2324
Herbert Xud26f3982007-11-13 21:47:08 -08002325 /* Must count x->lastused as it may become non-zero behind our back. */
2326 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002327
2328 return l;
2329}
2330
2331static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2332{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002333 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002334 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002335 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002336 struct nlmsghdr *nlh;
2337 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002339 int headlen;
2340
2341 headlen = sizeof(*p);
2342 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002343 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002344 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002345 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002346 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002347 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348
Thomas Graf7deb2262007-08-22 13:57:39 -07002349 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002350 if (skb == NULL)
2351 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002352
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002353 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2354 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002355 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002356
Thomas Graf7b67c852007-08-22 13:53:52 -07002357 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002358 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002359 struct nlattr *attr;
2360
Thomas Graf7b67c852007-08-22 13:53:52 -07002361 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002362 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2363 id->spi = x->id.spi;
2364 id->family = x->props.family;
2365 id->proto = x->id.proto;
2366
Thomas Grafc0144be2007-08-22 13:55:43 -07002367 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2368 if (attr == NULL)
2369 goto nla_put_failure;
2370
2371 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002372 }
2373
Herbert Xu68325d32007-10-09 13:30:57 -07002374 if (copy_to_user_state_extra(x, p, skb))
2375 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002376
Thomas Graf98250692007-08-22 12:47:26 -07002377 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002379 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380
Thomas Grafc0144be2007-08-22 13:55:43 -07002381nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002382 /* Somebody screwed up with xfrm_sa_len! */
2383 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384 kfree_skb(skb);
2385 return -1;
2386}
2387
2388static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2389{
2390
2391 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002392 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002393 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002394 case XFRM_MSG_NEWAE:
2395 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002396 case XFRM_MSG_DELSA:
2397 case XFRM_MSG_UPDSA:
2398 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002400 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401 return xfrm_notify_sa_flush(c);
2402 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002403 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
2404 c->event);
2405 break;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002406 }
2407
2408 return 0;
2409
2410}
2411
Thomas Graf7deb2262007-08-22 13:57:39 -07002412static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2413 struct xfrm_policy *xp)
2414{
2415 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2416 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002417 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002418 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2419 + userpolicy_type_attrsize();
2420}
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2423 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2424 int dir)
2425{
2426 struct xfrm_user_acquire *ua;
2427 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 __u32 seq = xfrm_get_acqseq();
2429
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002430 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2431 if (nlh == NULL)
2432 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Thomas Graf7b67c852007-08-22 13:53:52 -07002434 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 memcpy(&ua->id, &x->id, sizeof(ua->id));
2436 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2437 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2438 copy_to_user_policy(xp, &ua->policy, dir);
2439 ua->aalgos = xt->aalgos;
2440 ua->ealgos = xt->ealgos;
2441 ua->calgos = xt->calgos;
2442 ua->seq = x->km.seq = seq;
2443
2444 if (copy_to_user_tmpl(xp, skb) < 0)
2445 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002446 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002447 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002448 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002449 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002450 if (xfrm_mark_put(skb, &xp->mark))
2451 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Thomas Graf98250692007-08-22 12:47:26 -07002453 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002455nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002457 nlmsg_cancel(skb, nlh);
2458 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
2461static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2462 struct xfrm_policy *xp, int dir)
2463{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002464 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Thomas Graf7deb2262007-08-22 13:57:39 -07002467 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (skb == NULL)
2469 return -ENOMEM;
2470
2471 if (build_acquire(skb, x, xt, xp, dir) < 0)
2472 BUG();
2473
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002474 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477/* User gives us xfrm_user_policy_info followed by an array of 0
2478 * or more templates.
2479 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002480static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 u8 *data, int len, int *dir)
2482{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002483 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2485 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2486 struct xfrm_policy *xp;
2487 int nr;
2488
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002489 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 case AF_INET:
2491 if (opt != IP_XFRM_POLICY) {
2492 *dir = -EOPNOTSUPP;
2493 return NULL;
2494 }
2495 break;
2496#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2497 case AF_INET6:
2498 if (opt != IPV6_XFRM_POLICY) {
2499 *dir = -EOPNOTSUPP;
2500 return NULL;
2501 }
2502 break;
2503#endif
2504 default:
2505 *dir = -EINVAL;
2506 return NULL;
2507 }
2508
2509 *dir = -EINVAL;
2510
2511 if (len < sizeof(*p) ||
2512 verify_newpolicy_info(p))
2513 return NULL;
2514
2515 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002516 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return NULL;
2518
Herbert Xua4f1bac2005-07-26 15:43:17 -07002519 if (p->dir > XFRM_POLICY_OUT)
2520 return NULL;
2521
Herbert Xu2f09a4d2010-08-14 22:38:09 -07002522 xp = xfrm_policy_alloc(net, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (xp == NULL) {
2524 *dir = -ENOBUFS;
2525 return NULL;
2526 }
2527
2528 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002529 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 copy_templates(xp, ut, nr);
2531
2532 *dir = p->dir;
2533
2534 return xp;
2535}
2536
Thomas Graf7deb2262007-08-22 13:57:39 -07002537static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2538{
2539 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2540 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2541 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002542 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002543 + userpolicy_type_attrsize();
2544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002547 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 struct xfrm_user_polexpire *upe;
2550 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002551 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002553 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2554 if (nlh == NULL)
2555 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Thomas Graf7b67c852007-08-22 13:53:52 -07002557 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 copy_to_user_policy(xp, &upe->pol, dir);
2559 if (copy_to_user_tmpl(xp, skb) < 0)
2560 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002561 if (copy_to_user_sec_ctx(xp, skb))
2562 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002563 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002564 goto nlmsg_failure;
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002565 if (xfrm_mark_put(skb, &xp->mark))
2566 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 upe->hard = !!hard;
2568
Thomas Graf98250692007-08-22 12:47:26 -07002569 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002571nla_put_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002573 nlmsg_cancel(skb, nlh);
2574 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002577static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002579 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Thomas Graf7deb2262007-08-22 13:57:39 -07002582 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (skb == NULL)
2584 return -ENOMEM;
2585
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002586 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 BUG();
2588
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002589 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002592static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2593{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002594 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002596 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597 struct nlmsghdr *nlh;
2598 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002599 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002600 int headlen;
2601
2602 headlen = sizeof(*p);
2603 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002604 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002605 headlen = sizeof(*id);
2606 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002607 len += userpolicy_type_attrsize();
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002608 len += nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002609 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002610
Thomas Graf7deb2262007-08-22 13:57:39 -07002611 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612 if (skb == NULL)
2613 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002614
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002615 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2616 if (nlh == NULL)
2617 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002618
Thomas Graf7b67c852007-08-22 13:53:52 -07002619 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002620 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002621 struct nlattr *attr;
2622
Thomas Graf7b67c852007-08-22 13:53:52 -07002623 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002624 memset(id, 0, sizeof(*id));
2625 id->dir = dir;
2626 if (c->data.byid)
2627 id->index = xp->index;
2628 else
2629 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2630
Thomas Grafc0144be2007-08-22 13:55:43 -07002631 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2632 if (attr == NULL)
2633 goto nlmsg_failure;
2634
2635 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002636 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002638 copy_to_user_policy(xp, p, dir);
2639 if (copy_to_user_tmpl(xp, skb) < 0)
2640 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002641 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002642 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002643
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002644 if (xfrm_mark_put(skb, &xp->mark))
2645 goto nla_put_failure;
2646
Thomas Graf98250692007-08-22 12:47:26 -07002647 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002648
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002649 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002650
Jamal Hadi Salim295fae52010-02-22 11:33:00 +00002651nla_put_failure:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002652nlmsg_failure:
2653 kfree_skb(skb);
2654 return -1;
2655}
2656
2657static int xfrm_notify_policy_flush(struct km_event *c)
2658{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002659 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002660 struct nlmsghdr *nlh;
2661 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002662
Thomas Graf7deb2262007-08-22 13:57:39 -07002663 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002664 if (skb == NULL)
2665 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002666
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002667 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2668 if (nlh == NULL)
2669 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002670 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2671 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002672
Thomas Graf98250692007-08-22 12:47:26 -07002673 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002674
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002675 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002676
2677nlmsg_failure:
2678 kfree_skb(skb);
2679 return -1;
2680}
2681
2682static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2683{
2684
2685 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002686 case XFRM_MSG_NEWPOLICY:
2687 case XFRM_MSG_UPDPOLICY:
2688 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002689 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002690 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002691 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002692 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002693 return xfrm_exp_policy_notify(xp, dir, c);
2694 default:
stephen hemminger62db5cf2010-05-12 06:37:06 +00002695 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
2696 c->event);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002697 }
2698
2699 return 0;
2700
2701}
2702
Thomas Graf7deb2262007-08-22 13:57:39 -07002703static inline size_t xfrm_report_msgsize(void)
2704{
2705 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2706}
2707
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002708static int build_report(struct sk_buff *skb, u8 proto,
2709 struct xfrm_selector *sel, xfrm_address_t *addr)
2710{
2711 struct xfrm_user_report *ur;
2712 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002713
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002714 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2715 if (nlh == NULL)
2716 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002717
Thomas Graf7b67c852007-08-22 13:53:52 -07002718 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002719 ur->proto = proto;
2720 memcpy(&ur->sel, sel, sizeof(ur->sel));
2721
2722 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002723 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002724
Thomas Graf98250692007-08-22 12:47:26 -07002725 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002726
Thomas Grafc0144be2007-08-22 13:55:43 -07002727nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002728 nlmsg_cancel(skb, nlh);
2729 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002730}
2731
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002732static int xfrm_send_report(struct net *net, u8 proto,
2733 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002734{
2735 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002736
Thomas Graf7deb2262007-08-22 13:57:39 -07002737 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002738 if (skb == NULL)
2739 return -ENOMEM;
2740
2741 if (build_report(skb, proto, sel, addr) < 0)
2742 BUG();
2743
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002744 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002745}
2746
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002747static inline size_t xfrm_mapping_msgsize(void)
2748{
2749 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2750}
2751
2752static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2753 xfrm_address_t *new_saddr, __be16 new_sport)
2754{
2755 struct xfrm_user_mapping *um;
2756 struct nlmsghdr *nlh;
2757
2758 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2759 if (nlh == NULL)
2760 return -EMSGSIZE;
2761
2762 um = nlmsg_data(nlh);
2763
2764 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2765 um->id.spi = x->id.spi;
2766 um->id.family = x->props.family;
2767 um->id.proto = x->id.proto;
2768 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2769 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2770 um->new_sport = new_sport;
2771 um->old_sport = x->encap->encap_sport;
2772 um->reqid = x->props.reqid;
2773
2774 return nlmsg_end(skb, nlh);
2775}
2776
2777static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2778 __be16 sport)
2779{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002780 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002781 struct sk_buff *skb;
2782
2783 if (x->id.proto != IPPROTO_ESP)
2784 return -EINVAL;
2785
2786 if (!x->encap)
2787 return -EINVAL;
2788
2789 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2790 if (skb == NULL)
2791 return -ENOMEM;
2792
2793 if (build_mapping(skb, x, ipaddr, sport) < 0)
2794 BUG();
2795
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002796 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002797}
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799static struct xfrm_mgr netlink_mgr = {
2800 .id = "netlink",
2801 .notify = xfrm_send_state_notify,
2802 .acquire = xfrm_send_acquire,
2803 .compile_policy = xfrm_compile_policy,
2804 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002805 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002806 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002807 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808};
2809
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002810static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Patrick McHardybe336902006-03-20 22:40:54 -08002812 struct sock *nlsk;
2813
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002814 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002815 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002816 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002818 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002819 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return 0;
2821}
2822
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002823static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002824{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002825 struct net *net;
2826 list_for_each_entry(net, net_exit_list, exit_list)
2827 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2828 synchronize_net();
2829 list_for_each_entry(net, net_exit_list, exit_list)
2830 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002831}
2832
2833static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002834 .init = xfrm_user_net_init,
2835 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002836};
2837
2838static int __init xfrm_user_init(void)
2839{
2840 int rv;
2841
2842 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2843
2844 rv = register_pernet_subsys(&xfrm_user_net_ops);
2845 if (rv < 0)
2846 return rv;
2847 rv = xfrm_register_km(&netlink_mgr);
2848 if (rv < 0)
2849 unregister_pernet_subsys(&xfrm_user_net_ops);
2850 return rv;
2851}
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853static void __exit xfrm_user_exit(void)
2854{
2855 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002856 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857}
2858
2859module_init(xfrm_user_init);
2860module_exit(xfrm_user_exit);
2861MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002862MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002863