blob: 331ae731080ab1bbd23d42606b3a371bbd0cf651 [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
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080034#define DUMMY_MARK 0
35static struct xfrm_mark dummy_mark = {0, 0};
36
Herbert Xu1a6509d2008-01-28 19:37:29 -080037static inline int aead_len(struct xfrm_algo_aead *alg)
38{
39 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
40}
41
Thomas Graf5424f322007-08-22 14:01:33 -070042static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Thomas Graf5424f322007-08-22 14:01:33 -070044 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct xfrm_algo *algp;
46
47 if (!rt)
48 return 0;
49
Thomas Graf5424f322007-08-22 14:01:33 -070050 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080051 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070052 return -EINVAL;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 switch (type) {
55 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 break;
59
60 default:
61 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
65 return 0;
66}
67
Martin Willi4447bb32009-11-25 00:29:52 +000068static int verify_auth_trunc(struct nlattr **attrs)
69{
70 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
71 struct xfrm_algo_auth *algp;
72
73 if (!rt)
74 return 0;
75
76 algp = nla_data(rt);
77 if (nla_len(rt) < xfrm_alg_auth_len(algp))
78 return -EINVAL;
79
80 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
81 return 0;
82}
83
Herbert Xu1a6509d2008-01-28 19:37:29 -080084static int verify_aead(struct nlattr **attrs)
85{
86 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
87 struct xfrm_algo_aead *algp;
88
89 if (!rt)
90 return 0;
91
92 algp = nla_data(rt);
93 if (nla_len(rt) < aead_len(algp))
94 return -EINVAL;
95
96 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
97 return 0;
98}
99
Thomas Graf5424f322007-08-22 14:01:33 -0700100static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700101 xfrm_address_t **addrp)
102{
Thomas Graf5424f322007-08-22 14:01:33 -0700103 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700104
Thomas Grafcf5cb792007-08-22 13:59:04 -0700105 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700106 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700107}
Trent Jaegerdf718372005-12-13 23:12:27 -0800108
Thomas Graf5424f322007-08-22 14:01:33 -0700109static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800110{
Thomas Graf5424f322007-08-22 14:01:33 -0700111 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800112 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800113
114 if (!rt)
115 return 0;
116
Thomas Graf5424f322007-08-22 14:01:33 -0700117 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700118 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800119 return -EINVAL;
120
121 return 0;
122}
123
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700126 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int err;
129
130 err = -EINVAL;
131 switch (p->family) {
132 case AF_INET:
133 break;
134
135 case AF_INET6:
136#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
137 break;
138#else
139 err = -EAFNOSUPPORT;
140 goto out;
141#endif
142
143 default:
144 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 err = -EINVAL;
148 switch (p->id.proto) {
149 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000150 if ((!attrs[XFRMA_ALG_AUTH] &&
151 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800152 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700153 attrs[XFRMA_ALG_CRYPT] ||
154 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 goto out;
156 break;
157
158 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800159 if (attrs[XFRMA_ALG_COMP])
160 goto out;
161 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000162 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800163 !attrs[XFRMA_ALG_CRYPT] &&
164 !attrs[XFRMA_ALG_AEAD])
165 goto out;
166 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000167 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800168 attrs[XFRMA_ALG_CRYPT]) &&
169 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out;
171 break;
172
173 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700174 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800175 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700176 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000177 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700178 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto out;
180 break;
181
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700182#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
183 case IPPROTO_DSTOPTS:
184 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700185 if (attrs[XFRMA_ALG_COMP] ||
186 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000187 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800188 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700189 attrs[XFRMA_ALG_CRYPT] ||
190 attrs[XFRMA_ENCAP] ||
191 attrs[XFRMA_SEC_CTX] ||
192 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700193 goto out;
194 break;
195#endif
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 default:
198 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Herbert Xu1a6509d2008-01-28 19:37:29 -0800201 if ((err = verify_aead(attrs)))
202 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000203 if ((err = verify_auth_trunc(attrs)))
204 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700205 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700207 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700209 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700211 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 err = -EINVAL;
215 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700216 case XFRM_MODE_TRANSPORT:
217 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700218 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700219 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 break;
221
222 default:
223 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 err = 0;
227
228out:
229 return err;
230}
231
232static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
233 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700234 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct xfrm_algo *p, *ualg;
237 struct xfrm_algo_desc *algo;
238
239 if (!rta)
240 return 0;
241
Thomas Graf5424f322007-08-22 14:01:33 -0700242 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 algo = get_byname(ualg->alg_name, 1);
245 if (!algo)
246 return -ENOSYS;
247 *props = algo->desc.sadb_alg_id;
248
Eric Dumazet0f99be02008-01-08 23:39:06 -0800249 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!p)
251 return -ENOMEM;
252
Herbert Xu04ff1262006-08-13 08:50:00 +1000253 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *algpp = p;
255 return 0;
256}
257
Martin Willi4447bb32009-11-25 00:29:52 +0000258static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
259 struct nlattr *rta)
260{
261 struct xfrm_algo *ualg;
262 struct xfrm_algo_auth *p;
263 struct xfrm_algo_desc *algo;
264
265 if (!rta)
266 return 0;
267
268 ualg = nla_data(rta);
269
270 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
271 if (!algo)
272 return -ENOSYS;
273 *props = algo->desc.sadb_alg_id;
274
275 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
276 if (!p)
277 return -ENOMEM;
278
279 strcpy(p->alg_name, algo->name);
280 p->alg_key_len = ualg->alg_key_len;
281 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
282 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
283
284 *algpp = p;
285 return 0;
286}
287
288static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
289 struct nlattr *rta)
290{
291 struct xfrm_algo_auth *p, *ualg;
292 struct xfrm_algo_desc *algo;
293
294 if (!rta)
295 return 0;
296
297 ualg = nla_data(rta);
298
299 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
300 if (!algo)
301 return -ENOSYS;
302 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
303 return -EINVAL;
304 *props = algo->desc.sadb_alg_id;
305
306 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
307 if (!p)
308 return -ENOMEM;
309
310 strcpy(p->alg_name, algo->name);
311 if (!p->alg_trunc_len)
312 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
313
314 *algpp = p;
315 return 0;
316}
317
Herbert Xu1a6509d2008-01-28 19:37:29 -0800318static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
319 struct nlattr *rta)
320{
321 struct xfrm_algo_aead *p, *ualg;
322 struct xfrm_algo_desc *algo;
323
324 if (!rta)
325 return 0;
326
327 ualg = nla_data(rta);
328
329 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
330 if (!algo)
331 return -ENOSYS;
332 *props = algo->desc.sadb_alg_id;
333
334 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
335 if (!p)
336 return -ENOMEM;
337
338 strcpy(p->alg_name, algo->name);
339 *algpp = p;
340 return 0;
341}
342
Joy Latten661697f2007-04-13 16:14:35 -0700343static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800344{
Trent Jaegerdf718372005-12-13 23:12:27 -0800345 int len = 0;
346
347 if (xfrm_ctx) {
348 len += sizeof(struct xfrm_user_sec_ctx);
349 len += xfrm_ctx->ctx_len;
350 }
351 return len;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
355{
356 memcpy(&x->id, &p->id, sizeof(x->id));
357 memcpy(&x->sel, &p->sel, sizeof(x->sel));
358 memcpy(&x->lft, &p->lft, sizeof(x->lft));
359 x->props.mode = p->mode;
360 x->props.replay_window = p->replay_window;
361 x->props.reqid = p->reqid;
362 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700363 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700365
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700366 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700367 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800370/*
371 * someday when pfkey also has support, we could have the code
372 * somehow made shareable and move it to xfrm_state.c - JHS
373 *
374*/
Thomas Graf5424f322007-08-22 14:01:33 -0700375static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800376{
Thomas Graf5424f322007-08-22 14:01:33 -0700377 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
378 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
379 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
380 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800381
382 if (rp) {
383 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700384 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800385 memcpy(&x->replay, replay, sizeof(*replay));
386 memcpy(&x->preplay, replay, sizeof(*replay));
387 }
388
389 if (lt) {
390 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700391 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800392 x->curlft.bytes = ltime->bytes;
393 x->curlft.packets = ltime->packets;
394 x->curlft.add_time = ltime->add_time;
395 x->curlft.use_time = ltime->use_time;
396 }
397
Thomas Grafcf5cb792007-08-22 13:59:04 -0700398 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700399 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800400
Thomas Grafcf5cb792007-08-22 13:59:04 -0700401 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700402 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800403}
404
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800405static struct xfrm_state *xfrm_state_construct(struct net *net,
406 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700407 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int *errp)
409{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800410 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int err = -ENOMEM;
412
413 if (!x)
414 goto error_no_put;
415
416 copy_from_user_state(x, p);
417
Herbert Xu1a6509d2008-01-28 19:37:29 -0800418 if ((err = attach_aead(&x->aead, &x->props.ealgo,
419 attrs[XFRMA_ALG_AEAD])))
420 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000421 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
422 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000424 if (!x->props.aalgo) {
425 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
426 attrs[XFRMA_ALG_AUTH])))
427 goto error;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
430 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700431 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto error;
433 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
434 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700435 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700437
438 if (attrs[XFRMA_ENCAP]) {
439 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
440 sizeof(*x->encap), GFP_KERNEL);
441 if (x->encap == NULL)
442 goto error;
443 }
444
445 if (attrs[XFRMA_COADDR]) {
446 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
447 sizeof(*x->coaddr), GFP_KERNEL);
448 if (x->coaddr == NULL)
449 goto error;
450 }
451
Herbert Xu72cb6962005-06-20 13:18:08 -0700452 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (err)
454 goto error;
455
Thomas Graffd211502007-09-06 03:28:08 -0700456 if (attrs[XFRMA_SEC_CTX] &&
457 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800458 goto error;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800461 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800462 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800463 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800464 x->preplay.bitmap = 0;
465 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
466 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
467
468 /* override default values from above */
469
Thomas Graf5424f322007-08-22 14:01:33 -0700470 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 return x;
473
474error:
475 x->km.state = XFRM_STATE_DEAD;
476 xfrm_state_put(x);
477error_no_put:
478 *errp = err;
479 return NULL;
480}
481
Christoph Hellwig22e70052007-01-02 15:22:30 -0800482static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700483 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800485 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700486 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct xfrm_state *x;
488 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700489 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400490 uid_t loginuid = NETLINK_CB(skb).loginuid;
491 u32 sessionid = NETLINK_CB(skb).sessionid;
492 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Thomas Graf35a7aa02007-08-22 14:00:40 -0700494 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (err)
496 return err;
497
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800498 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (!x)
500 return err;
501
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700502 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
504 err = xfrm_state_add(x);
505 else
506 err = xfrm_state_update(x);
507
Eric Paris25323862008-04-18 10:09:25 -0400508 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (err < 0) {
511 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800512 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700516 c.seq = nlh->nlmsg_seq;
517 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700518 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700519
520 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700521out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700522 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return err;
524}
525
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800526static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
527 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700528 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700529 int *errp)
530{
531 struct xfrm_state *x = NULL;
532 int err;
533
534 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
535 err = -ESRCH;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800536 x = xfrm_state_lookup(net, DUMMY_MARK, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700537 } else {
538 xfrm_address_t *saddr = NULL;
539
Thomas Graf35a7aa02007-08-22 14:00:40 -0700540 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700541 if (!saddr) {
542 err = -EINVAL;
543 goto out;
544 }
545
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800546 err = -ESRCH;
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800547 x = xfrm_state_lookup_byaddr(net, DUMMY_MARK, &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800548 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700549 }
550
551 out:
552 if (!x && errp)
553 *errp = err;
554 return x;
555}
556
Christoph Hellwig22e70052007-01-02 15:22:30 -0800557static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700558 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800560 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700562 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700563 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700564 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400565 uid_t loginuid = NETLINK_CB(skb).loginuid;
566 u32 sessionid = NETLINK_CB(skb).sessionid;
567 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800569 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
David S. Miller6f68dc32006-06-08 23:58:52 -0700573 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700574 goto out;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700577 err = -EPERM;
578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700581 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600582
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700583 if (err < 0)
584 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700585
586 c.seq = nlh->nlmsg_seq;
587 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700588 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700589 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700591out:
Eric Paris25323862008-04-18 10:09:25 -0400592 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700593 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
598{
599 memcpy(&p->id, &x->id, sizeof(p->id));
600 memcpy(&p->sel, &x->sel, sizeof(p->sel));
601 memcpy(&p->lft, &x->lft, sizeof(p->lft));
602 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
603 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700604 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 p->mode = x->props.mode;
606 p->replay_window = x->props.replay_window;
607 p->reqid = x->props.reqid;
608 p->family = x->props.family;
609 p->flags = x->props.flags;
610 p->seq = x->km.seq;
611}
612
613struct xfrm_dump_info {
614 struct sk_buff *in_skb;
615 struct sk_buff *out_skb;
616 u32 nlmsg_seq;
617 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618};
619
Thomas Grafc0144be2007-08-22 13:55:43 -0700620static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
621{
Thomas Grafc0144be2007-08-22 13:55:43 -0700622 struct xfrm_user_sec_ctx *uctx;
623 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700624 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700625
626 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
627 if (attr == NULL)
628 return -EMSGSIZE;
629
630 uctx = nla_data(attr);
631 uctx->exttype = XFRMA_SEC_CTX;
632 uctx->len = ctx_size;
633 uctx->ctx_doi = s->ctx_doi;
634 uctx->ctx_alg = s->ctx_alg;
635 uctx->ctx_len = s->ctx_len;
636 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
637
638 return 0;
639}
640
Martin Willi4447bb32009-11-25 00:29:52 +0000641static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
642{
643 struct xfrm_algo *algo;
644 struct nlattr *nla;
645
646 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
647 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
648 if (!nla)
649 return -EMSGSIZE;
650
651 algo = nla_data(nla);
652 strcpy(algo->alg_name, auth->alg_name);
653 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
654 algo->alg_key_len = auth->alg_key_len;
655
656 return 0;
657}
658
Herbert Xu68325d32007-10-09 13:30:57 -0700659/* Don't change this without updating xfrm_sa_len! */
660static int copy_to_user_state_extra(struct xfrm_state *x,
661 struct xfrm_usersa_info *p,
662 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 copy_to_user_state(x, p);
665
Herbert Xu050f0092007-10-09 13:31:47 -0700666 if (x->coaddr)
667 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
668
669 if (x->lastused)
670 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700671
Herbert Xu1a6509d2008-01-28 19:37:29 -0800672 if (x->aead)
673 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000674 if (x->aalg) {
675 if (copy_to_user_auth(x->aalg, skb))
676 goto nla_put_failure;
677
678 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
679 xfrm_alg_auth_len(x->aalg), x->aalg);
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800682 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700684 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700687 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Thomas Grafc0144be2007-08-22 13:55:43 -0700689 if (x->security && copy_sec_ctx(x->security, skb) < 0)
690 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700691
Herbert Xu68325d32007-10-09 13:30:57 -0700692 return 0;
693
694nla_put_failure:
695 return -EMSGSIZE;
696}
697
698static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
699{
700 struct xfrm_dump_info *sp = ptr;
701 struct sk_buff *in_skb = sp->in_skb;
702 struct sk_buff *skb = sp->out_skb;
703 struct xfrm_usersa_info *p;
704 struct nlmsghdr *nlh;
705 int err;
706
Herbert Xu68325d32007-10-09 13:30:57 -0700707 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
708 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
709 if (nlh == NULL)
710 return -EMSGSIZE;
711
712 p = nlmsg_data(nlh);
713
714 err = copy_to_user_state_extra(x, p, skb);
715 if (err)
716 goto nla_put_failure;
717
Thomas Graf98250692007-08-22 12:47:26 -0700718 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720
Thomas Grafc0144be2007-08-22 13:55:43 -0700721nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700722 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700723 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Timo Teras4c563f72008-02-28 21:31:08 -0800726static int xfrm_dump_sa_done(struct netlink_callback *cb)
727{
728 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
729 xfrm_state_walk_done(walk);
730 return 0;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
734{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800735 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800736 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct xfrm_dump_info info;
738
Timo Teras4c563f72008-02-28 21:31:08 -0800739 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
740 sizeof(cb->args) - sizeof(cb->args[0]));
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 info.in_skb = cb->skb;
743 info.out_skb = skb;
744 info.nlmsg_seq = cb->nlh->nlmsg_seq;
745 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800746
747 if (!cb->args[0]) {
748 cb->args[0] = 1;
749 xfrm_state_walk_init(walk, 0);
750 }
751
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800752 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 return skb->len;
755}
756
757static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
758 struct xfrm_state *x, u32 seq)
759{
760 struct xfrm_dump_info info;
761 struct sk_buff *skb;
762
Thomas Graf7deb2262007-08-22 13:57:39 -0700763 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!skb)
765 return ERR_PTR(-ENOMEM);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 info.in_skb = in_skb;
768 info.out_skb = skb;
769 info.nlmsg_seq = seq;
770 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 if (dump_one_state(x, 0, &info)) {
773 kfree_skb(skb);
774 return NULL;
775 }
776
777 return skb;
778}
779
Thomas Graf7deb2262007-08-22 13:57:39 -0700780static inline size_t xfrm_spdinfo_msgsize(void)
781{
782 return NLMSG_ALIGN(4)
783 + nla_total_size(sizeof(struct xfrmu_spdinfo))
784 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
785}
786
Alexey Dobriyane0710412010-01-23 13:37:10 +0000787static int build_spdinfo(struct sk_buff *skb, struct net *net,
788 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700789{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700790 struct xfrmk_spdinfo si;
791 struct xfrmu_spdinfo spc;
792 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700793 struct nlmsghdr *nlh;
794 u32 *f;
795
796 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
797 if (nlh == NULL) /* shouldnt really happen ... */
798 return -EMSGSIZE;
799
800 f = nlmsg_data(nlh);
801 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000802 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700803 spc.incnt = si.incnt;
804 spc.outcnt = si.outcnt;
805 spc.fwdcnt = si.fwdcnt;
806 spc.inscnt = si.inscnt;
807 spc.outscnt = si.outscnt;
808 spc.fwdscnt = si.fwdscnt;
809 sph.spdhcnt = si.spdhcnt;
810 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700811
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700812 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
813 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700814
815 return nlmsg_end(skb, nlh);
816
817nla_put_failure:
818 nlmsg_cancel(skb, nlh);
819 return -EMSGSIZE;
820}
821
822static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700823 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700824{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800825 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700826 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700827 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700828 u32 spid = NETLINK_CB(skb).pid;
829 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700830
Thomas Graf7deb2262007-08-22 13:57:39 -0700831 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700832 if (r_skb == NULL)
833 return -ENOMEM;
834
Alexey Dobriyane0710412010-01-23 13:37:10 +0000835 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700836 BUG();
837
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800838 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700839}
840
Thomas Graf7deb2262007-08-22 13:57:39 -0700841static inline size_t xfrm_sadinfo_msgsize(void)
842{
843 return NLMSG_ALIGN(4)
844 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
845 + nla_total_size(4); /* XFRMA_SAD_CNT */
846}
847
Alexey Dobriyane0710412010-01-23 13:37:10 +0000848static int build_sadinfo(struct sk_buff *skb, struct net *net,
849 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700850{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700851 struct xfrmk_sadinfo si;
852 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700853 struct nlmsghdr *nlh;
854 u32 *f;
855
856 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
857 if (nlh == NULL) /* shouldnt really happen ... */
858 return -EMSGSIZE;
859
860 f = nlmsg_data(nlh);
861 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000862 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700863
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700864 sh.sadhmcnt = si.sadhmcnt;
865 sh.sadhcnt = si.sadhcnt;
866
867 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
868 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700869
870 return nlmsg_end(skb, nlh);
871
872nla_put_failure:
873 nlmsg_cancel(skb, nlh);
874 return -EMSGSIZE;
875}
876
877static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700878 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700879{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800880 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700881 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700882 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700883 u32 spid = NETLINK_CB(skb).pid;
884 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700885
Thomas Graf7deb2262007-08-22 13:57:39 -0700886 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700887 if (r_skb == NULL)
888 return -ENOMEM;
889
Alexey Dobriyane0710412010-01-23 13:37:10 +0000890 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700891 BUG();
892
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800893 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700894}
895
Christoph Hellwig22e70052007-01-02 15:22:30 -0800896static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700897 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800899 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700900 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct xfrm_state *x;
902 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700903 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800905 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (x == NULL)
907 goto out_noput;
908
909 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
910 if (IS_ERR(resp_skb)) {
911 err = PTR_ERR(resp_skb);
912 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800913 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 xfrm_state_put(x);
916out_noput:
917 return err;
918}
919
920static int verify_userspi_info(struct xfrm_userspi_info *p)
921{
922 switch (p->info.id.proto) {
923 case IPPROTO_AH:
924 case IPPROTO_ESP:
925 break;
926
927 case IPPROTO_COMP:
928 /* IPCOMP spi is 16-bits. */
929 if (p->max >= 0x10000)
930 return -EINVAL;
931 break;
932
933 default:
934 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (p->min > p->max)
938 return -EINVAL;
939
940 return 0;
941}
942
Christoph Hellwig22e70052007-01-02 15:22:30 -0800943static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700944 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800946 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct xfrm_state *x;
948 struct xfrm_userspi_info *p;
949 struct sk_buff *resp_skb;
950 xfrm_address_t *daddr;
951 int family;
952 int err;
953
Thomas Graf7b67c852007-08-22 13:53:52 -0700954 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 err = verify_userspi_info(p);
956 if (err)
957 goto out_noput;
958
959 family = p->info.family;
960 daddr = &p->info.id.daddr;
961
962 x = NULL;
963 if (p->info.seq) {
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800964 x = xfrm_find_acq_byseq(net, DUMMY_MARK, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
966 xfrm_state_put(x);
967 x = NULL;
968 }
969 }
970
971 if (!x)
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800972 x = xfrm_find_acq(net, &dummy_mark, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 p->info.id.proto, daddr,
974 &p->info.saddr, 1,
975 family);
976 err = -ENOENT;
977 if (x == NULL)
978 goto out_noput;
979
Herbert Xu658b2192007-10-09 13:29:52 -0700980 err = xfrm_alloc_spi(x, p->min, p->max);
981 if (err)
982 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Herbert Xu658b2192007-10-09 13:29:52 -0700984 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (IS_ERR(resp_skb)) {
986 err = PTR_ERR(resp_skb);
987 goto out;
988 }
989
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800990 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992out:
993 xfrm_state_put(x);
994out_noput:
995 return err;
996}
997
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800998static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 switch (dir) {
1001 case XFRM_POLICY_IN:
1002 case XFRM_POLICY_OUT:
1003 case XFRM_POLICY_FWD:
1004 break;
1005
1006 default:
1007 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 return 0;
1011}
1012
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001013static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001014{
1015 switch (type) {
1016 case XFRM_POLICY_TYPE_MAIN:
1017#ifdef CONFIG_XFRM_SUB_POLICY
1018 case XFRM_POLICY_TYPE_SUB:
1019#endif
1020 break;
1021
1022 default:
1023 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001024 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001025
1026 return 0;
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1030{
1031 switch (p->share) {
1032 case XFRM_SHARE_ANY:
1033 case XFRM_SHARE_SESSION:
1034 case XFRM_SHARE_USER:
1035 case XFRM_SHARE_UNIQUE:
1036 break;
1037
1038 default:
1039 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 switch (p->action) {
1043 case XFRM_POLICY_ALLOW:
1044 case XFRM_POLICY_BLOCK:
1045 break;
1046
1047 default:
1048 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 switch (p->sel.family) {
1052 case AF_INET:
1053 break;
1054
1055 case AF_INET6:
1056#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1057 break;
1058#else
1059 return -EAFNOSUPPORT;
1060#endif
1061
1062 default:
1063 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 return verify_policy_dir(p->dir);
1067}
1068
Thomas Graf5424f322007-08-22 14:01:33 -07001069static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001070{
Thomas Graf5424f322007-08-22 14:01:33 -07001071 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001072 struct xfrm_user_sec_ctx *uctx;
1073
1074 if (!rt)
1075 return 0;
1076
Thomas Graf5424f322007-08-22 14:01:33 -07001077 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001078 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1082 int nr)
1083{
1084 int i;
1085
1086 xp->xfrm_nr = nr;
1087 for (i = 0; i < nr; i++, ut++) {
1088 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1089
1090 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1091 memcpy(&t->saddr, &ut->saddr,
1092 sizeof(xfrm_address_t));
1093 t->reqid = ut->reqid;
1094 t->mode = ut->mode;
1095 t->share = ut->share;
1096 t->optional = ut->optional;
1097 t->aalgos = ut->aalgos;
1098 t->ealgos = ut->ealgos;
1099 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001100 /* If all masks are ~0, then we allow all algorithms. */
1101 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001102 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104}
1105
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001106static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1107{
1108 int i;
1109
1110 if (nr > XFRM_MAX_DEPTH)
1111 return -EINVAL;
1112
1113 for (i = 0; i < nr; i++) {
1114 /* We never validated the ut->family value, so many
1115 * applications simply leave it at zero. The check was
1116 * never made and ut->family was ignored because all
1117 * templates could be assumed to have the same family as
1118 * the policy itself. Now that we will have ipv4-in-ipv6
1119 * and ipv6-in-ipv4 tunnels, this is no longer true.
1120 */
1121 if (!ut[i].family)
1122 ut[i].family = family;
1123
1124 switch (ut[i].family) {
1125 case AF_INET:
1126 break;
1127#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1128 case AF_INET6:
1129 break;
1130#endif
1131 default:
1132 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001133 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001134 }
1135
1136 return 0;
1137}
1138
Thomas Graf5424f322007-08-22 14:01:33 -07001139static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Thomas Graf5424f322007-08-22 14:01:33 -07001141 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (!rt) {
1144 pol->xfrm_nr = 0;
1145 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001146 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1147 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001148 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001150 err = validate_tmpl(nr, utmpl, pol->family);
1151 if (err)
1152 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Thomas Graf5424f322007-08-22 14:01:33 -07001154 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 return 0;
1157}
1158
Thomas Graf5424f322007-08-22 14:01:33 -07001159static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001160{
Thomas Graf5424f322007-08-22 14:01:33 -07001161 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001162 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001163 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001164 int err;
1165
1166 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001167 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001168 type = upt->type;
1169 }
1170
1171 err = verify_policy_type(type);
1172 if (err)
1173 return err;
1174
1175 *tp = type;
1176 return 0;
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1180{
1181 xp->priority = p->priority;
1182 xp->index = p->index;
1183 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1184 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1185 xp->action = p->action;
1186 xp->flags = p->flags;
1187 xp->family = p->sel.family;
1188 /* XXX xp->share = p->share; */
1189}
1190
1191static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1192{
1193 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1194 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1195 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1196 p->priority = xp->priority;
1197 p->index = xp->index;
1198 p->sel.family = xp->family;
1199 p->dir = dir;
1200 p->action = xp->action;
1201 p->flags = xp->flags;
1202 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1203}
1204
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001205static 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 -07001206{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001207 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 int err;
1209
1210 if (!xp) {
1211 *errp = -ENOMEM;
1212 return NULL;
1213 }
1214
1215 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001216
Thomas Graf35a7aa02007-08-22 14:00:40 -07001217 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001218 if (err)
1219 goto error;
1220
Thomas Graf35a7aa02007-08-22 14:00:40 -07001221 if (!(err = copy_from_user_tmpl(xp, attrs)))
1222 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001223 if (err)
1224 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001227 error:
1228 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001229 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001230 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001231 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Christoph Hellwig22e70052007-01-02 15:22:30 -08001234static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001235 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001237 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001238 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001240 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int err;
1242 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001243 uid_t loginuid = NETLINK_CB(skb).loginuid;
1244 u32 sessionid = NETLINK_CB(skb).sessionid;
1245 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 err = verify_newpolicy_info(p);
1248 if (err)
1249 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001250 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001251 if (err)
1252 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001254 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (!xp)
1256 return err;
1257
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001258 /* shouldnt excl be based on nlh flags??
1259 * Aha! this is anti-netlink really i.e more pfkey derived
1260 * in netlink excl is a flag and you wouldnt need
1261 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1263 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001264 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001267 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 kfree(xp);
1269 return err;
1270 }
1271
Herbert Xuf60f6b82005-06-18 22:44:37 -07001272 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001273 c.seq = nlh->nlmsg_seq;
1274 c.pid = nlh->nlmsg_pid;
1275 km_policy_notify(xp, p->dir, &c);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 xfrm_pol_put(xp);
1278
1279 return 0;
1280}
1281
1282static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1283{
1284 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1285 int i;
1286
1287 if (xp->xfrm_nr == 0)
1288 return 0;
1289
1290 for (i = 0; i < xp->xfrm_nr; i++) {
1291 struct xfrm_user_tmpl *up = &vec[i];
1292 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1293
1294 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001295 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1297 up->reqid = kp->reqid;
1298 up->mode = kp->mode;
1299 up->share = kp->share;
1300 up->optional = kp->optional;
1301 up->aalgos = kp->aalgos;
1302 up->ealgos = kp->ealgos;
1303 up->calgos = kp->calgos;
1304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Thomas Grafc0144be2007-08-22 13:55:43 -07001306 return nla_put(skb, XFRMA_TMPL,
1307 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001308}
1309
Serge Hallyn0d681622006-07-24 23:30:44 -07001310static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1311{
1312 if (x->security) {
1313 return copy_sec_ctx(x->security, skb);
1314 }
1315 return 0;
1316}
1317
1318static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1319{
1320 if (xp->security) {
1321 return copy_sec_ctx(xp->security, skb);
1322 }
1323 return 0;
1324}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001325static inline size_t userpolicy_type_attrsize(void)
1326{
1327#ifdef CONFIG_XFRM_SUB_POLICY
1328 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1329#else
1330 return 0;
1331#endif
1332}
Serge Hallyn0d681622006-07-24 23:30:44 -07001333
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001334#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001335static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001336{
Thomas Grafc0144be2007-08-22 13:55:43 -07001337 struct xfrm_userpolicy_type upt = {
1338 .type = type,
1339 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001340
Thomas Grafc0144be2007-08-22 13:55:43 -07001341 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001342}
1343
1344#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001345static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001346{
1347 return 0;
1348}
1349#endif
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1352{
1353 struct xfrm_dump_info *sp = ptr;
1354 struct xfrm_userpolicy_info *p;
1355 struct sk_buff *in_skb = sp->in_skb;
1356 struct sk_buff *skb = sp->out_skb;
1357 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001359 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1360 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1361 if (nlh == NULL)
1362 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Thomas Graf7b67c852007-08-22 13:53:52 -07001364 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 copy_to_user_policy(xp, p, dir);
1366 if (copy_to_user_tmpl(xp, skb) < 0)
1367 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001368 if (copy_to_user_sec_ctx(xp, skb))
1369 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001370 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001371 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Thomas Graf98250692007-08-22 12:47:26 -07001373 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375
1376nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001377 nlmsg_cancel(skb, nlh);
1378 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Timo Teras4c563f72008-02-28 21:31:08 -08001381static int xfrm_dump_policy_done(struct netlink_callback *cb)
1382{
1383 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1384
1385 xfrm_policy_walk_done(walk);
1386 return 0;
1387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1390{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001391 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001392 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 struct xfrm_dump_info info;
1394
Timo Teras4c563f72008-02-28 21:31:08 -08001395 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1396 sizeof(cb->args) - sizeof(cb->args[0]));
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 info.in_skb = cb->skb;
1399 info.out_skb = skb;
1400 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1401 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001402
1403 if (!cb->args[0]) {
1404 cb->args[0] = 1;
1405 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1406 }
1407
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001408 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 return skb->len;
1411}
1412
1413static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1414 struct xfrm_policy *xp,
1415 int dir, u32 seq)
1416{
1417 struct xfrm_dump_info info;
1418 struct sk_buff *skb;
1419
Thomas Graf7deb2262007-08-22 13:57:39 -07001420 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!skb)
1422 return ERR_PTR(-ENOMEM);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 info.in_skb = in_skb;
1425 info.out_skb = skb;
1426 info.nlmsg_seq = seq;
1427 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1430 kfree_skb(skb);
1431 return NULL;
1432 }
1433
1434 return skb;
1435}
1436
Christoph Hellwig22e70052007-01-02 15:22:30 -08001437static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001438 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001440 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 struct xfrm_policy *xp;
1442 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001443 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001445 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int delete;
1447
Thomas Graf7b67c852007-08-22 13:53:52 -07001448 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1450
Thomas Graf35a7aa02007-08-22 14:00:40 -07001451 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001452 if (err)
1453 return err;
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 err = verify_policy_dir(p->dir);
1456 if (err)
1457 return err;
1458
1459 if (p->index)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001460 xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001461 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001462 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001463 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001464
Thomas Graf35a7aa02007-08-22 14:00:40 -07001465 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001466 if (err)
1467 return err;
1468
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001469 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001470 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001471 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001472
Paul Moore03e1ad72008-04-12 19:07:52 -07001473 err = security_xfrm_policy_alloc(&ctx, uctx);
1474 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001475 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001476 }
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001477 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001478 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001479 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (xp == NULL)
1482 return -ENOENT;
1483
1484 if (!delete) {
1485 struct sk_buff *resp_skb;
1486
1487 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1488 if (IS_ERR(resp_skb)) {
1489 err = PTR_ERR(resp_skb);
1490 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001491 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001492 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001494 } else {
Eric Paris25323862008-04-18 10:09:25 -04001495 uid_t loginuid = NETLINK_CB(skb).loginuid;
1496 u32 sessionid = NETLINK_CB(skb).sessionid;
1497 u32 sid = NETLINK_CB(skb).sid;
1498
1499 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1500 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001501
1502 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001503 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001504
Herbert Xue7443892005-06-18 22:44:18 -07001505 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001506 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001507 c.seq = nlh->nlmsg_seq;
1508 c.pid = nlh->nlmsg_pid;
1509 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001512out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001513 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return err;
1515}
1516
Christoph Hellwig22e70052007-01-02 15:22:30 -08001517static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001518 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001520 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001521 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001522 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001523 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Joy Latten161a09e2006-11-27 13:11:54 -06001526 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001527 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001528 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001529 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001530 if (err) {
1531 if (err == -ESRCH) /* empty table */
1532 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001533 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001534 }
Herbert Xubf088672005-06-18 22:44:00 -07001535 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001536 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001537 c.seq = nlh->nlmsg_seq;
1538 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001539 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001540 km_state_notify(NULL, &c);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 0;
1543}
1544
Thomas Graf7deb2262007-08-22 13:57:39 -07001545static inline size_t xfrm_aevent_msgsize(void)
1546{
1547 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1548 + nla_total_size(sizeof(struct xfrm_replay_state))
1549 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1550 + nla_total_size(4) /* XFRM_AE_RTHR */
1551 + nla_total_size(4); /* XFRM_AE_ETHR */
1552}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001553
1554static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1555{
1556 struct xfrm_aevent_id *id;
1557 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001558
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001559 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1560 if (nlh == NULL)
1561 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001562
Thomas Graf7b67c852007-08-22 13:53:52 -07001563 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001564 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001565 id->sa_id.spi = x->id.spi;
1566 id->sa_id.family = x->props.family;
1567 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001568 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1569 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001570 id->flags = c->data.aevent;
1571
Thomas Grafc0144be2007-08-22 13:55:43 -07001572 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1573 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001574
Thomas Grafc0144be2007-08-22 13:55:43 -07001575 if (id->flags & XFRM_AE_RTHR)
1576 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001577
Thomas Grafc0144be2007-08-22 13:55:43 -07001578 if (id->flags & XFRM_AE_ETHR)
1579 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1580 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001581
Thomas Graf98250692007-08-22 12:47:26 -07001582 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001583
Thomas Grafc0144be2007-08-22 13:55:43 -07001584nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001585 nlmsg_cancel(skb, nlh);
1586 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001587}
1588
Christoph Hellwig22e70052007-01-02 15:22:30 -08001589static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001590 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001591{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001592 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001593 struct xfrm_state *x;
1594 struct sk_buff *r_skb;
1595 int err;
1596 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001597 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001598 struct xfrm_usersa_id *id = &p->sa_id;
1599
Thomas Graf7deb2262007-08-22 13:57:39 -07001600 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001601 if (r_skb == NULL)
1602 return -ENOMEM;
1603
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001604 x = xfrm_state_lookup(net, DUMMY_MARK, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001605 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001606 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001607 return -ESRCH;
1608 }
1609
1610 /*
1611 * XXX: is this lock really needed - none of the other
1612 * gets lock (the concern is things getting updated
1613 * while we are still reading) - jhs
1614 */
1615 spin_lock_bh(&x->lock);
1616 c.data.aevent = p->flags;
1617 c.seq = nlh->nlmsg_seq;
1618 c.pid = nlh->nlmsg_pid;
1619
1620 if (build_aevent(r_skb, x, &c) < 0)
1621 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001622 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001623 spin_unlock_bh(&x->lock);
1624 xfrm_state_put(x);
1625 return err;
1626}
1627
Christoph Hellwig22e70052007-01-02 15:22:30 -08001628static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001629 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001630{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001631 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001632 struct xfrm_state *x;
1633 struct km_event c;
1634 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001635 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001636 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1637 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001638
1639 if (!lt && !rp)
1640 return err;
1641
1642 /* pedantic mode - thou shalt sayeth replaceth */
1643 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1644 return err;
1645
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001646 x = xfrm_state_lookup(net, DUMMY_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 -08001647 if (x == NULL)
1648 return -ESRCH;
1649
1650 if (x->km.state != XFRM_STATE_VALID)
1651 goto out;
1652
1653 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001654 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001655 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001656
1657 c.event = nlh->nlmsg_type;
1658 c.seq = nlh->nlmsg_seq;
1659 c.pid = nlh->nlmsg_pid;
1660 c.data.aevent = XFRM_AE_CU;
1661 km_state_notify(x, &c);
1662 err = 0;
1663out:
1664 xfrm_state_put(x);
1665 return err;
1666}
1667
Christoph Hellwig22e70052007-01-02 15:22:30 -08001668static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001669 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001671 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001672 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001673 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001674 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001675 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001676
Thomas Graf35a7aa02007-08-22 14:00:40 -07001677 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001678 if (err)
1679 return err;
1680
Joy Latten161a09e2006-11-27 13:11:54 -06001681 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001682 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001683 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001684 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001685 if (err) {
1686 if (err == -ESRCH) /* empty table */
1687 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001688 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001689 }
1690
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001691 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001692 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001693 c.seq = nlh->nlmsg_seq;
1694 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001695 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001696 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return 0;
1698}
1699
Christoph Hellwig22e70052007-01-02 15:22:30 -08001700static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001701 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001702{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001703 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001704 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001705 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001706 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001707 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001708 int err = -ENOENT;
1709
Thomas Graf35a7aa02007-08-22 14:00:40 -07001710 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001711 if (err)
1712 return err;
1713
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001714 if (p->index)
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001715 xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001716 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001717 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001718 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001719
Thomas Graf35a7aa02007-08-22 14:00:40 -07001720 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001721 if (err)
1722 return err;
1723
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001724 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001725 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001726 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001727
Paul Moore03e1ad72008-04-12 19:07:52 -07001728 err = security_xfrm_policy_alloc(&ctx, uctx);
1729 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001730 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001731 }
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001732 xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001733 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001734 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001735 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001736 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001737
Eric Parisef41aaa2007-03-07 15:37:58 -08001738 read_lock(&xp->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001739 if (xp->walk.dead) {
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001740 read_unlock(&xp->lock);
1741 goto out;
1742 }
1743
1744 read_unlock(&xp->lock);
1745 err = 0;
1746 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001747 uid_t loginuid = NETLINK_CB(skb).loginuid;
1748 uid_t sessionid = NETLINK_CB(skb).sessionid;
1749 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001750 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001751 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001752
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001753 } else {
1754 // reset the timers here?
1755 printk("Dont know what to do with soft policy expire\n");
1756 }
1757 km_policy_expired(xp, p->dir, up->hard, current->pid);
1758
1759out:
1760 xfrm_pol_put(xp);
1761 return err;
1762}
1763
Christoph Hellwig22e70052007-01-02 15:22:30 -08001764static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001765 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001766{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001767 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001768 struct xfrm_state *x;
1769 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001770 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001771 struct xfrm_usersa_info *p = &ue->state;
1772
Jamal Hadi Salimbd557752010-02-22 16:20:22 -08001773 x = xfrm_state_lookup(net, DUMMY_MARK, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001774
David S. Miller3a765aa2007-02-26 14:52:21 -08001775 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001776 if (x == NULL)
1777 return err;
1778
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001779 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001780 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001781 if (x->km.state != XFRM_STATE_VALID)
1782 goto out;
1783 km_state_expired(x, ue->hard, current->pid);
1784
Joy Latten161a09e2006-11-27 13:11:54 -06001785 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001786 uid_t loginuid = NETLINK_CB(skb).loginuid;
1787 uid_t sessionid = NETLINK_CB(skb).sessionid;
1788 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001789 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001790 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001791 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001792 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001793out:
1794 spin_unlock_bh(&x->lock);
1795 xfrm_state_put(x);
1796 return err;
1797}
1798
Christoph Hellwig22e70052007-01-02 15:22:30 -08001799static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001800 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001801{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001802 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001803 struct xfrm_policy *xp;
1804 struct xfrm_user_tmpl *ut;
1805 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001806 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001807
Thomas Graf7b67c852007-08-22 13:53:52 -07001808 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001809 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001810 int err = -ENOMEM;
1811
1812 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001813 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001814
1815 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001816 if (err)
1817 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001818
1819 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001820 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001821 if (!xp)
1822 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001823
1824 memcpy(&x->id, &ua->id, sizeof(ua->id));
1825 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1826 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1827
Thomas Graf5424f322007-08-22 14:01:33 -07001828 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001829 /* extract the templates and for each call km_key */
1830 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1831 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1832 memcpy(&x->id, &t->id, sizeof(x->id));
1833 x->props.mode = t->mode;
1834 x->props.reqid = t->reqid;
1835 x->props.family = ut->family;
1836 t->aalgos = ua->aalgos;
1837 t->ealgos = ua->ealgos;
1838 t->calgos = ua->calgos;
1839 err = km_query(x, t, xp);
1840
1841 }
1842
1843 kfree(x);
1844 kfree(xp);
1845
1846 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001847
1848bad_policy:
1849 printk("BAD policy passed\n");
1850free_state:
1851 kfree(x);
1852nomem:
1853 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001854}
1855
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001856#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001857static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001858 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001859 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001860{
Thomas Graf5424f322007-08-22 14:01:33 -07001861 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001862 struct xfrm_user_migrate *um;
1863 int i, num_migrate;
1864
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001865 if (k != NULL) {
1866 struct xfrm_user_kmaddress *uk;
1867
1868 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1869 memcpy(&k->local, &uk->local, sizeof(k->local));
1870 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1871 k->family = uk->family;
1872 k->reserved = uk->reserved;
1873 }
1874
Thomas Graf5424f322007-08-22 14:01:33 -07001875 um = nla_data(rt);
1876 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001877
1878 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1879 return -EINVAL;
1880
1881 for (i = 0; i < num_migrate; i++, um++, ma++) {
1882 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1883 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1884 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1885 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1886
1887 ma->proto = um->proto;
1888 ma->mode = um->mode;
1889 ma->reqid = um->reqid;
1890
1891 ma->old_family = um->old_family;
1892 ma->new_family = um->new_family;
1893 }
1894
1895 *num = i;
1896 return 0;
1897}
1898
1899static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001900 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001901{
Thomas Graf7b67c852007-08-22 13:53:52 -07001902 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001903 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001904 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001905 u8 type;
1906 int err;
1907 int n = 0;
1908
Thomas Graf35a7aa02007-08-22 14:00:40 -07001909 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001910 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001911
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001912 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1913
Thomas Graf5424f322007-08-22 14:01:33 -07001914 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001915 if (err)
1916 return err;
1917
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001918 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001919 if (err)
1920 return err;
1921
1922 if (!n)
1923 return 0;
1924
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001925 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001926
1927 return 0;
1928}
1929#else
1930static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001931 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001932{
1933 return -ENOPROTOOPT;
1934}
1935#endif
1936
1937#ifdef CONFIG_XFRM_MIGRATE
1938static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1939{
1940 struct xfrm_user_migrate um;
1941
1942 memset(&um, 0, sizeof(um));
1943 um.proto = m->proto;
1944 um.mode = m->mode;
1945 um.reqid = m->reqid;
1946 um.old_family = m->old_family;
1947 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1948 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1949 um.new_family = m->new_family;
1950 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1951 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1952
Thomas Grafc0144be2007-08-22 13:55:43 -07001953 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001954}
1955
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001956static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1957{
1958 struct xfrm_user_kmaddress uk;
1959
1960 memset(&uk, 0, sizeof(uk));
1961 uk.family = k->family;
1962 uk.reserved = k->reserved;
1963 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08001964 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001965
1966 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1967}
1968
1969static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07001970{
1971 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001972 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
1973 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1974 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07001975}
1976
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001977static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001978 int num_migrate, struct xfrm_kmaddress *k,
1979 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001980{
1981 struct xfrm_migrate *mp;
1982 struct xfrm_userpolicy_id *pol_id;
1983 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001984 int i;
1985
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001986 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1987 if (nlh == NULL)
1988 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001989
Thomas Graf7b67c852007-08-22 13:53:52 -07001990 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001991 /* copy data from selector, dir, and type to the pol_id */
1992 memset(pol_id, 0, sizeof(*pol_id));
1993 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1994 pol_id->dir = dir;
1995
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001996 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
1997 goto nlmsg_failure;
1998
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001999 if (copy_to_user_policy_type(type, skb) < 0)
2000 goto nlmsg_failure;
2001
2002 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2003 if (copy_to_user_migrate(mp, skb) < 0)
2004 goto nlmsg_failure;
2005 }
2006
Thomas Graf98250692007-08-22 12:47:26 -07002007 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002008nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002009 nlmsg_cancel(skb, nlh);
2010 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002011}
2012
2013static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002014 struct xfrm_migrate *m, int num_migrate,
2015 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002016{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002017 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002018 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002019
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002020 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002021 if (skb == NULL)
2022 return -ENOMEM;
2023
2024 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002025 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002026 BUG();
2027
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002028 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002029}
2030#else
2031static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002032 struct xfrm_migrate *m, int num_migrate,
2033 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002034{
2035 return -ENOPROTOOPT;
2036}
2037#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002038
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002039#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002040
2041static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002042 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002043 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2044 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2045 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2046 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2047 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2048 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002049 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002050 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002051 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002052 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002053 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002054 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002055 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002056 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2057 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002058 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002059 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002060 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2061 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062};
2063
Thomas Graf492b5582005-05-03 14:26:40 -07002064#undef XMSGSIZE
2065
Thomas Grafcf5cb792007-08-22 13:59:04 -07002066static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002067 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2068 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2069 [XFRMA_LASTUSED] = { .type = NLA_U64},
2070 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002071 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002072 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2073 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2074 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2075 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2076 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2077 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2078 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2079 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2080 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2081 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2082 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2083 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2084 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2085 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002086 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002087};
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002090 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002092 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002093} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2094 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2095 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2096 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002097 .dump = xfrm_dump_sa,
2098 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002099 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2100 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2101 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002102 .dump = xfrm_dump_policy,
2103 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002104 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002105 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002106 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002107 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2108 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002109 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002110 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2111 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002112 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2113 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002114 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002115 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002116 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117};
2118
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002119static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002121 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002122 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002124 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002128 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 type -= XFRM_MSG_BASE;
2131 link = &xfrm_dispatch[type];
2132
2133 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002134 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2135 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Thomas Graf492b5582005-05-03 14:26:40 -07002137 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2138 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2139 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002143 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Thomas Graf35a7aa02007-08-22 14:00:40 -07002146 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002147 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002148 if (err < 0)
2149 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002152 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Thomas Graf5424f322007-08-22 14:01:33 -07002154 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002157static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002159 mutex_lock(&xfrm_cfg_mutex);
2160 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2161 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Thomas Graf7deb2262007-08-22 13:57:39 -07002164static inline size_t xfrm_expire_msgsize(void)
2165{
2166 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
2167}
2168
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002169static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct xfrm_user_expire *ue;
2172 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002174 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2175 if (nlh == NULL)
2176 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Thomas Graf7b67c852007-08-22 13:53:52 -07002178 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002180 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Thomas Graf98250692007-08-22 12:47:26 -07002182 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002185static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002187 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct sk_buff *skb;
2189
Thomas Graf7deb2262007-08-22 13:57:39 -07002190 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (skb == NULL)
2192 return -ENOMEM;
2193
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002194 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 BUG();
2196
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002197 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002200static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2201{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002202 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002203 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002204
Thomas Graf7deb2262007-08-22 13:57:39 -07002205 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002206 if (skb == NULL)
2207 return -ENOMEM;
2208
2209 if (build_aevent(skb, x, c) < 0)
2210 BUG();
2211
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002212 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002213}
2214
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002215static int xfrm_notify_sa_flush(struct km_event *c)
2216{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002217 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002218 struct xfrm_usersa_flush *p;
2219 struct nlmsghdr *nlh;
2220 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002221 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002222
Thomas Graf7deb2262007-08-22 13:57:39 -07002223 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002224 if (skb == NULL)
2225 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002226
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002227 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2228 if (nlh == NULL) {
2229 kfree_skb(skb);
2230 return -EMSGSIZE;
2231 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002232
Thomas Graf7b67c852007-08-22 13:53:52 -07002233 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002234 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002235
Thomas Graf98250692007-08-22 12:47:26 -07002236 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002237
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002238 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002239}
2240
Thomas Graf7deb2262007-08-22 13:57:39 -07002241static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002242{
Thomas Graf7deb2262007-08-22 13:57:39 -07002243 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002244 if (x->aead)
2245 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002246 if (x->aalg) {
2247 l += nla_total_size(sizeof(struct xfrm_algo) +
2248 (x->aalg->alg_key_len + 7) / 8);
2249 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2250 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002251 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002252 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002253 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002254 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002255 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002256 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002257 if (x->security)
2258 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2259 x->security->ctx_len);
2260 if (x->coaddr)
2261 l += nla_total_size(sizeof(*x->coaddr));
2262
Herbert Xud26f3982007-11-13 21:47:08 -08002263 /* Must count x->lastused as it may become non-zero behind our back. */
2264 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002265
2266 return l;
2267}
2268
2269static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2270{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002271 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002272 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002273 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002274 struct nlmsghdr *nlh;
2275 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002276 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002277 int headlen;
2278
2279 headlen = sizeof(*p);
2280 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002281 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002282 headlen = sizeof(*id);
2283 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002284 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002285
Thomas Graf7deb2262007-08-22 13:57:39 -07002286 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002287 if (skb == NULL)
2288 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002289
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002290 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2291 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002292 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293
Thomas Graf7b67c852007-08-22 13:53:52 -07002294 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002295 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002296 struct nlattr *attr;
2297
Thomas Graf7b67c852007-08-22 13:53:52 -07002298 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002299 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2300 id->spi = x->id.spi;
2301 id->family = x->props.family;
2302 id->proto = x->id.proto;
2303
Thomas Grafc0144be2007-08-22 13:55:43 -07002304 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2305 if (attr == NULL)
2306 goto nla_put_failure;
2307
2308 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002309 }
2310
Herbert Xu68325d32007-10-09 13:30:57 -07002311 if (copy_to_user_state_extra(x, p, skb))
2312 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002313
Thomas Graf98250692007-08-22 12:47:26 -07002314 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002315
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002316 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002317
Thomas Grafc0144be2007-08-22 13:55:43 -07002318nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002319 /* Somebody screwed up with xfrm_sa_len! */
2320 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321 kfree_skb(skb);
2322 return -1;
2323}
2324
2325static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2326{
2327
2328 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002329 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002330 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002331 case XFRM_MSG_NEWAE:
2332 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002333 case XFRM_MSG_DELSA:
2334 case XFRM_MSG_UPDSA:
2335 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002336 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002337 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338 return xfrm_notify_sa_flush(c);
2339 default:
2340 printk("xfrm_user: Unknown SA event %d\n", c->event);
2341 break;
2342 }
2343
2344 return 0;
2345
2346}
2347
Thomas Graf7deb2262007-08-22 13:57:39 -07002348static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2349 struct xfrm_policy *xp)
2350{
2351 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2352 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2353 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2354 + userpolicy_type_attrsize();
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2358 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2359 int dir)
2360{
2361 struct xfrm_user_acquire *ua;
2362 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 __u32 seq = xfrm_get_acqseq();
2364
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002365 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2366 if (nlh == NULL)
2367 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Thomas Graf7b67c852007-08-22 13:53:52 -07002369 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 memcpy(&ua->id, &x->id, sizeof(ua->id));
2371 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2372 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2373 copy_to_user_policy(xp, &ua->policy, dir);
2374 ua->aalgos = xt->aalgos;
2375 ua->ealgos = xt->ealgos;
2376 ua->calgos = xt->calgos;
2377 ua->seq = x->km.seq = seq;
2378
2379 if (copy_to_user_tmpl(xp, skb) < 0)
2380 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002381 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002382 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002383 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002384 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Thomas Graf98250692007-08-22 12:47:26 -07002386 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002389 nlmsg_cancel(skb, nlh);
2390 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2394 struct xfrm_policy *xp, int dir)
2395{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002396 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Thomas Graf7deb2262007-08-22 13:57:39 -07002399 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (skb == NULL)
2401 return -ENOMEM;
2402
2403 if (build_acquire(skb, x, xt, xp, dir) < 0)
2404 BUG();
2405
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002406 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409/* User gives us xfrm_user_policy_info followed by an array of 0
2410 * or more templates.
2411 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002412static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 u8 *data, int len, int *dir)
2414{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002415 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2417 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2418 struct xfrm_policy *xp;
2419 int nr;
2420
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002421 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 case AF_INET:
2423 if (opt != IP_XFRM_POLICY) {
2424 *dir = -EOPNOTSUPP;
2425 return NULL;
2426 }
2427 break;
2428#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2429 case AF_INET6:
2430 if (opt != IPV6_XFRM_POLICY) {
2431 *dir = -EOPNOTSUPP;
2432 return NULL;
2433 }
2434 break;
2435#endif
2436 default:
2437 *dir = -EINVAL;
2438 return NULL;
2439 }
2440
2441 *dir = -EINVAL;
2442
2443 if (len < sizeof(*p) ||
2444 verify_newpolicy_info(p))
2445 return NULL;
2446
2447 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002448 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return NULL;
2450
Herbert Xua4f1bac2005-07-26 15:43:17 -07002451 if (p->dir > XFRM_POLICY_OUT)
2452 return NULL;
2453
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002454 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (xp == NULL) {
2456 *dir = -ENOBUFS;
2457 return NULL;
2458 }
2459
2460 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002461 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 copy_templates(xp, ut, nr);
2463
2464 *dir = p->dir;
2465
2466 return xp;
2467}
2468
Thomas Graf7deb2262007-08-22 13:57:39 -07002469static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2470{
2471 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2472 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2473 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2474 + userpolicy_type_attrsize();
2475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002478 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 struct xfrm_user_polexpire *upe;
2481 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002482 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002484 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2485 if (nlh == NULL)
2486 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Thomas Graf7b67c852007-08-22 13:53:52 -07002488 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 copy_to_user_policy(xp, &upe->pol, dir);
2490 if (copy_to_user_tmpl(xp, skb) < 0)
2491 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002492 if (copy_to_user_sec_ctx(xp, skb))
2493 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002494 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002495 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 upe->hard = !!hard;
2497
Thomas Graf98250692007-08-22 12:47:26 -07002498 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002501 nlmsg_cancel(skb, nlh);
2502 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002505static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002507 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Thomas Graf7deb2262007-08-22 13:57:39 -07002510 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (skb == NULL)
2512 return -ENOMEM;
2513
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002514 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 BUG();
2516
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002517 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002520static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2521{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002522 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002523 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002524 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002525 struct nlmsghdr *nlh;
2526 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002527 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002528 int headlen;
2529
2530 headlen = sizeof(*p);
2531 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002532 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002533 headlen = sizeof(*id);
2534 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002535 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002536 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002537
Thomas Graf7deb2262007-08-22 13:57:39 -07002538 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002539 if (skb == NULL)
2540 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002541
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002542 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2543 if (nlh == NULL)
2544 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002545
Thomas Graf7b67c852007-08-22 13:53:52 -07002546 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002547 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002548 struct nlattr *attr;
2549
Thomas Graf7b67c852007-08-22 13:53:52 -07002550 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002551 memset(id, 0, sizeof(*id));
2552 id->dir = dir;
2553 if (c->data.byid)
2554 id->index = xp->index;
2555 else
2556 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2557
Thomas Grafc0144be2007-08-22 13:55:43 -07002558 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2559 if (attr == NULL)
2560 goto nlmsg_failure;
2561
2562 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002563 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002564
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002565 copy_to_user_policy(xp, p, dir);
2566 if (copy_to_user_tmpl(xp, skb) < 0)
2567 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002568 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002569 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002570
Thomas Graf98250692007-08-22 12:47:26 -07002571 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002572
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002573 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002574
2575nlmsg_failure:
2576 kfree_skb(skb);
2577 return -1;
2578}
2579
2580static int xfrm_notify_policy_flush(struct km_event *c)
2581{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002582 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583 struct nlmsghdr *nlh;
2584 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002585
Thomas Graf7deb2262007-08-22 13:57:39 -07002586 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002587 if (skb == NULL)
2588 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002589
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002590 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2591 if (nlh == NULL)
2592 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002593 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2594 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002595
Thomas Graf98250692007-08-22 12:47:26 -07002596 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002597
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002598 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002599
2600nlmsg_failure:
2601 kfree_skb(skb);
2602 return -1;
2603}
2604
2605static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2606{
2607
2608 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002609 case XFRM_MSG_NEWPOLICY:
2610 case XFRM_MSG_UPDPOLICY:
2611 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002613 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002614 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002615 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002616 return xfrm_exp_policy_notify(xp, dir, c);
2617 default:
2618 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2619 }
2620
2621 return 0;
2622
2623}
2624
Thomas Graf7deb2262007-08-22 13:57:39 -07002625static inline size_t xfrm_report_msgsize(void)
2626{
2627 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2628}
2629
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002630static int build_report(struct sk_buff *skb, u8 proto,
2631 struct xfrm_selector *sel, xfrm_address_t *addr)
2632{
2633 struct xfrm_user_report *ur;
2634 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002635
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002636 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2637 if (nlh == NULL)
2638 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002639
Thomas Graf7b67c852007-08-22 13:53:52 -07002640 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002641 ur->proto = proto;
2642 memcpy(&ur->sel, sel, sizeof(ur->sel));
2643
2644 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002645 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002646
Thomas Graf98250692007-08-22 12:47:26 -07002647 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002648
Thomas Grafc0144be2007-08-22 13:55:43 -07002649nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002650 nlmsg_cancel(skb, nlh);
2651 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002652}
2653
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002654static int xfrm_send_report(struct net *net, u8 proto,
2655 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002656{
2657 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002658
Thomas Graf7deb2262007-08-22 13:57:39 -07002659 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002660 if (skb == NULL)
2661 return -ENOMEM;
2662
2663 if (build_report(skb, proto, sel, addr) < 0)
2664 BUG();
2665
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002666 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002667}
2668
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002669static inline size_t xfrm_mapping_msgsize(void)
2670{
2671 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2672}
2673
2674static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2675 xfrm_address_t *new_saddr, __be16 new_sport)
2676{
2677 struct xfrm_user_mapping *um;
2678 struct nlmsghdr *nlh;
2679
2680 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2681 if (nlh == NULL)
2682 return -EMSGSIZE;
2683
2684 um = nlmsg_data(nlh);
2685
2686 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2687 um->id.spi = x->id.spi;
2688 um->id.family = x->props.family;
2689 um->id.proto = x->id.proto;
2690 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2691 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2692 um->new_sport = new_sport;
2693 um->old_sport = x->encap->encap_sport;
2694 um->reqid = x->props.reqid;
2695
2696 return nlmsg_end(skb, nlh);
2697}
2698
2699static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2700 __be16 sport)
2701{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002702 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002703 struct sk_buff *skb;
2704
2705 if (x->id.proto != IPPROTO_ESP)
2706 return -EINVAL;
2707
2708 if (!x->encap)
2709 return -EINVAL;
2710
2711 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2712 if (skb == NULL)
2713 return -ENOMEM;
2714
2715 if (build_mapping(skb, x, ipaddr, sport) < 0)
2716 BUG();
2717
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002718 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002719}
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721static struct xfrm_mgr netlink_mgr = {
2722 .id = "netlink",
2723 .notify = xfrm_send_state_notify,
2724 .acquire = xfrm_send_acquire,
2725 .compile_policy = xfrm_compile_policy,
2726 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002727 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002728 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002729 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730};
2731
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002732static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Patrick McHardybe336902006-03-20 22:40:54 -08002734 struct sock *nlsk;
2735
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002736 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002737 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002738 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002740 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002741 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return 0;
2743}
2744
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002745static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002746{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002747 struct net *net;
2748 list_for_each_entry(net, net_exit_list, exit_list)
2749 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2750 synchronize_net();
2751 list_for_each_entry(net, net_exit_list, exit_list)
2752 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002753}
2754
2755static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002756 .init = xfrm_user_net_init,
2757 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002758};
2759
2760static int __init xfrm_user_init(void)
2761{
2762 int rv;
2763
2764 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2765
2766 rv = register_pernet_subsys(&xfrm_user_net_ops);
2767 if (rv < 0)
2768 return rv;
2769 rv = xfrm_register_km(&netlink_mgr);
2770 if (rv < 0)
2771 unregister_pernet_subsys(&xfrm_user_net_ops);
2772 return rv;
2773}
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775static void __exit xfrm_user_exit(void)
2776{
2777 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002778 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781module_init(xfrm_user_init);
2782module_exit(xfrm_user_exit);
2783MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002784MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002785