blob: 303092f7088badfda6d3c68d8b5d451e50274813 [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
Jamal Hadi Salimbd557752010-02-22 16:20:22 -080035
Herbert Xu1a6509d2008-01-28 19:37:29 -080036static inline int aead_len(struct xfrm_algo_aead *alg)
37{
38 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
39}
40
Thomas Graf5424f322007-08-22 14:01:33 -070041static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Thomas Graf5424f322007-08-22 14:01:33 -070043 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct xfrm_algo *algp;
45
46 if (!rt)
47 return 0;
48
Thomas Graf5424f322007-08-22 14:01:33 -070049 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080050 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070051 return -EINVAL;
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 switch (type) {
54 case XFRMA_ALG_AUTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 case XFRMA_ALG_CRYPT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 case XFRMA_ALG_COMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 break;
58
59 default:
60 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
64 return 0;
65}
66
Martin Willi4447bb32009-11-25 00:29:52 +000067static int verify_auth_trunc(struct nlattr **attrs)
68{
69 struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
70 struct xfrm_algo_auth *algp;
71
72 if (!rt)
73 return 0;
74
75 algp = nla_data(rt);
76 if (nla_len(rt) < xfrm_alg_auth_len(algp))
77 return -EINVAL;
78
79 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
80 return 0;
81}
82
Herbert Xu1a6509d2008-01-28 19:37:29 -080083static int verify_aead(struct nlattr **attrs)
84{
85 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
86 struct xfrm_algo_aead *algp;
87
88 if (!rt)
89 return 0;
90
91 algp = nla_data(rt);
92 if (nla_len(rt) < aead_len(algp))
93 return -EINVAL;
94
95 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
96 return 0;
97}
98
Thomas Graf5424f322007-08-22 14:01:33 -070099static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700100 xfrm_address_t **addrp)
101{
Thomas Graf5424f322007-08-22 14:01:33 -0700102 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700103
Thomas Grafcf5cb792007-08-22 13:59:04 -0700104 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -0700105 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700106}
Trent Jaegerdf718372005-12-13 23:12:27 -0800107
Thomas Graf5424f322007-08-22 14:01:33 -0700108static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800109{
Thomas Graf5424f322007-08-22 14:01:33 -0700110 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800111 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800112
113 if (!rt)
114 return 0;
115
Thomas Graf5424f322007-08-22 14:01:33 -0700116 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700117 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800118 return -EINVAL;
119
120 return 0;
121}
122
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700125 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 int err;
128
129 err = -EINVAL;
130 switch (p->family) {
131 case AF_INET:
132 break;
133
134 case AF_INET6:
135#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
136 break;
137#else
138 err = -EAFNOSUPPORT;
139 goto out;
140#endif
141
142 default:
143 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 err = -EINVAL;
147 switch (p->id.proto) {
148 case IPPROTO_AH:
Martin Willi4447bb32009-11-25 00:29:52 +0000149 if ((!attrs[XFRMA_ALG_AUTH] &&
150 !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800151 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700152 attrs[XFRMA_ALG_CRYPT] ||
153 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 goto out;
155 break;
156
157 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800158 if (attrs[XFRMA_ALG_COMP])
159 goto out;
160 if (!attrs[XFRMA_ALG_AUTH] &&
Martin Willi4447bb32009-11-25 00:29:52 +0000161 !attrs[XFRMA_ALG_AUTH_TRUNC] &&
Herbert Xu1a6509d2008-01-28 19:37:29 -0800162 !attrs[XFRMA_ALG_CRYPT] &&
163 !attrs[XFRMA_ALG_AEAD])
164 goto out;
165 if ((attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000166 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800167 attrs[XFRMA_ALG_CRYPT]) &&
168 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto out;
170 break;
171
172 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700173 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800174 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700175 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000176 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700177 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto out;
179 break;
180
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700181#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
182 case IPPROTO_DSTOPTS:
183 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700184 if (attrs[XFRMA_ALG_COMP] ||
185 attrs[XFRMA_ALG_AUTH] ||
Martin Willi4447bb32009-11-25 00:29:52 +0000186 attrs[XFRMA_ALG_AUTH_TRUNC] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800187 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700188 attrs[XFRMA_ALG_CRYPT] ||
189 attrs[XFRMA_ENCAP] ||
190 attrs[XFRMA_SEC_CTX] ||
191 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700192 goto out;
193 break;
194#endif
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 default:
197 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Herbert Xu1a6509d2008-01-28 19:37:29 -0800200 if ((err = verify_aead(attrs)))
201 goto out;
Martin Willi4447bb32009-11-25 00:29:52 +0000202 if ((err = verify_auth_trunc(attrs)))
203 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700204 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700206 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700208 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700210 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800211 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 err = -EINVAL;
214 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700215 case XFRM_MODE_TRANSPORT:
216 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700217 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700218 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
220
221 default:
222 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 err = 0;
226
227out:
228 return err;
229}
230
231static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
232 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700233 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct xfrm_algo *p, *ualg;
236 struct xfrm_algo_desc *algo;
237
238 if (!rta)
239 return 0;
240
Thomas Graf5424f322007-08-22 14:01:33 -0700241 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 algo = get_byname(ualg->alg_name, 1);
244 if (!algo)
245 return -ENOSYS;
246 *props = algo->desc.sadb_alg_id;
247
Eric Dumazet0f99be02008-01-08 23:39:06 -0800248 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!p)
250 return -ENOMEM;
251
Herbert Xu04ff1262006-08-13 08:50:00 +1000252 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *algpp = p;
254 return 0;
255}
256
Martin Willi4447bb32009-11-25 00:29:52 +0000257static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
258 struct nlattr *rta)
259{
260 struct xfrm_algo *ualg;
261 struct xfrm_algo_auth *p;
262 struct xfrm_algo_desc *algo;
263
264 if (!rta)
265 return 0;
266
267 ualg = nla_data(rta);
268
269 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
270 if (!algo)
271 return -ENOSYS;
272 *props = algo->desc.sadb_alg_id;
273
274 p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
275 if (!p)
276 return -ENOMEM;
277
278 strcpy(p->alg_name, algo->name);
279 p->alg_key_len = ualg->alg_key_len;
280 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
281 memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
282
283 *algpp = p;
284 return 0;
285}
286
287static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
288 struct nlattr *rta)
289{
290 struct xfrm_algo_auth *p, *ualg;
291 struct xfrm_algo_desc *algo;
292
293 if (!rta)
294 return 0;
295
296 ualg = nla_data(rta);
297
298 algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
299 if (!algo)
300 return -ENOSYS;
301 if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
302 return -EINVAL;
303 *props = algo->desc.sadb_alg_id;
304
305 p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
306 if (!p)
307 return -ENOMEM;
308
309 strcpy(p->alg_name, algo->name);
310 if (!p->alg_trunc_len)
311 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
312
313 *algpp = p;
314 return 0;
315}
316
Herbert Xu1a6509d2008-01-28 19:37:29 -0800317static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
318 struct nlattr *rta)
319{
320 struct xfrm_algo_aead *p, *ualg;
321 struct xfrm_algo_desc *algo;
322
323 if (!rta)
324 return 0;
325
326 ualg = nla_data(rta);
327
328 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
329 if (!algo)
330 return -ENOSYS;
331 *props = algo->desc.sadb_alg_id;
332
333 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
334 if (!p)
335 return -ENOMEM;
336
337 strcpy(p->alg_name, algo->name);
338 *algpp = p;
339 return 0;
340}
341
Joy Latten661697f2007-04-13 16:14:35 -0700342static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800343{
Trent Jaegerdf718372005-12-13 23:12:27 -0800344 int len = 0;
345
346 if (xfrm_ctx) {
347 len += sizeof(struct xfrm_user_sec_ctx);
348 len += xfrm_ctx->ctx_len;
349 }
350 return len;
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
354{
355 memcpy(&x->id, &p->id, sizeof(x->id));
356 memcpy(&x->sel, &p->sel, sizeof(x->sel));
357 memcpy(&x->lft, &p->lft, sizeof(x->lft));
358 x->props.mode = p->mode;
359 x->props.replay_window = p->replay_window;
360 x->props.reqid = p->reqid;
361 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700362 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700364
Steffen Klassertccf9b3b2008-07-10 16:55:37 -0700365 if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
Herbert Xu196b0032007-07-31 02:04:32 -0700366 x->sel.family = p->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800369/*
370 * someday when pfkey also has support, we could have the code
371 * somehow made shareable and move it to xfrm_state.c - JHS
372 *
373*/
Thomas Graf5424f322007-08-22 14:01:33 -0700374static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800375{
Thomas Graf5424f322007-08-22 14:01:33 -0700376 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
377 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
378 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
379 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800380
381 if (rp) {
382 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700383 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800384 memcpy(&x->replay, replay, sizeof(*replay));
385 memcpy(&x->preplay, replay, sizeof(*replay));
386 }
387
388 if (lt) {
389 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700390 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800391 x->curlft.bytes = ltime->bytes;
392 x->curlft.packets = ltime->packets;
393 x->curlft.add_time = ltime->add_time;
394 x->curlft.use_time = ltime->use_time;
395 }
396
Thomas Grafcf5cb792007-08-22 13:59:04 -0700397 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700398 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800399
Thomas Grafcf5cb792007-08-22 13:59:04 -0700400 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700401 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800402}
403
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800404static struct xfrm_state *xfrm_state_construct(struct net *net,
405 struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700406 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int *errp)
408{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800409 struct xfrm_state *x = xfrm_state_alloc(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int err = -ENOMEM;
411
412 if (!x)
413 goto error_no_put;
414
415 copy_from_user_state(x, p);
416
Herbert Xu1a6509d2008-01-28 19:37:29 -0800417 if ((err = attach_aead(&x->aead, &x->props.ealgo,
418 attrs[XFRMA_ALG_AEAD])))
419 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000420 if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
421 attrs[XFRMA_ALG_AUTH_TRUNC])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto error;
Martin Willi4447bb32009-11-25 00:29:52 +0000423 if (!x->props.aalgo) {
424 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
425 attrs[XFRMA_ALG_AUTH])))
426 goto error;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
429 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700430 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto error;
432 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
433 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700434 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700436
437 if (attrs[XFRMA_ENCAP]) {
438 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
439 sizeof(*x->encap), GFP_KERNEL);
440 if (x->encap == NULL)
441 goto error;
442 }
443
444 if (attrs[XFRMA_COADDR]) {
445 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
446 sizeof(*x->coaddr), GFP_KERNEL);
447 if (x->coaddr == NULL)
448 goto error;
449 }
450
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000451 xfrm_mark_get(attrs, &x->mark);
452
Herbert Xu72cb6962005-06-20 13:18:08 -0700453 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (err)
455 goto error;
456
Thomas Graffd211502007-09-06 03:28:08 -0700457 if (attrs[XFRMA_SEC_CTX] &&
458 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800459 goto error;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 x->km.seq = p->seq;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800462 x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800463 /* sysctl_xfrm_aevent_etime is in 100ms units */
Alexey Dobriyanb27aead2008-11-25 18:00:48 -0800464 x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800465 x->preplay.bitmap = 0;
466 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
467 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
468
469 /* override default values from above */
470
Thomas Graf5424f322007-08-22 14:01:33 -0700471 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 return x;
474
475error:
476 x->km.state = XFRM_STATE_DEAD;
477 xfrm_state_put(x);
478error_no_put:
479 *errp = err;
480 return NULL;
481}
482
Christoph Hellwig22e70052007-01-02 15:22:30 -0800483static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700484 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800486 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700487 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct xfrm_state *x;
489 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700490 struct km_event c;
Eric Paris25323862008-04-18 10:09:25 -0400491 uid_t loginuid = NETLINK_CB(skb).loginuid;
492 u32 sessionid = NETLINK_CB(skb).sessionid;
493 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Thomas Graf35a7aa02007-08-22 14:00:40 -0700495 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (err)
497 return err;
498
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800499 x = xfrm_state_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!x)
501 return err;
502
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700503 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
505 err = xfrm_state_add(x);
506 else
507 err = xfrm_state_update(x);
508
Eric Paris25323862008-04-18 10:09:25 -0400509 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (err < 0) {
512 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800513 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700517 c.seq = nlh->nlmsg_seq;
518 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700519 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700520
521 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700522out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700523 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return err;
525}
526
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800527static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
528 struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700529 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700530 int *errp)
531{
532 struct xfrm_state *x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000533 struct xfrm_mark m;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700534 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000535 u32 mark = xfrm_mark_get(attrs, &m);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700536
537 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
538 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000539 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700540 } else {
541 xfrm_address_t *saddr = NULL;
542
Thomas Graf35a7aa02007-08-22 14:00:40 -0700543 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700544 if (!saddr) {
545 err = -EINVAL;
546 goto out;
547 }
548
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800549 err = -ESRCH;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000550 x = xfrm_state_lookup_byaddr(net, mark,
551 &p->daddr, saddr,
Alexey Dobriyan221df1e2008-11-25 17:30:50 -0800552 p->proto, p->family);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700553 }
554
555 out:
556 if (!x && errp)
557 *errp = err;
558 return x;
559}
560
Christoph Hellwig22e70052007-01-02 15:22:30 -0800561static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700562 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800564 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700566 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700567 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700568 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400569 uid_t loginuid = NETLINK_CB(skb).loginuid;
570 u32 sessionid = NETLINK_CB(skb).sessionid;
571 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800573 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700575 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David S. Miller6f68dc32006-06-08 23:58:52 -0700577 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700578 goto out;
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700581 err = -EPERM;
582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700585 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600586
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700587 if (err < 0)
588 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700589
590 c.seq = nlh->nlmsg_seq;
591 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700592 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700593 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700595out:
Eric Paris25323862008-04-18 10:09:25 -0400596 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700597 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
602{
603 memcpy(&p->id, &x->id, sizeof(p->id));
604 memcpy(&p->sel, &x->sel, sizeof(p->sel));
605 memcpy(&p->lft, &x->lft, sizeof(p->lft));
606 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
607 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700608 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 p->mode = x->props.mode;
610 p->replay_window = x->props.replay_window;
611 p->reqid = x->props.reqid;
612 p->family = x->props.family;
613 p->flags = x->props.flags;
614 p->seq = x->km.seq;
615}
616
617struct xfrm_dump_info {
618 struct sk_buff *in_skb;
619 struct sk_buff *out_skb;
620 u32 nlmsg_seq;
621 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622};
623
Thomas Grafc0144be2007-08-22 13:55:43 -0700624static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
625{
Thomas Grafc0144be2007-08-22 13:55:43 -0700626 struct xfrm_user_sec_ctx *uctx;
627 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700628 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700629
630 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
631 if (attr == NULL)
632 return -EMSGSIZE;
633
634 uctx = nla_data(attr);
635 uctx->exttype = XFRMA_SEC_CTX;
636 uctx->len = ctx_size;
637 uctx->ctx_doi = s->ctx_doi;
638 uctx->ctx_alg = s->ctx_alg;
639 uctx->ctx_len = s->ctx_len;
640 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
641
642 return 0;
643}
644
Martin Willi4447bb32009-11-25 00:29:52 +0000645static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
646{
647 struct xfrm_algo *algo;
648 struct nlattr *nla;
649
650 nla = nla_reserve(skb, XFRMA_ALG_AUTH,
651 sizeof(*algo) + (auth->alg_key_len + 7) / 8);
652 if (!nla)
653 return -EMSGSIZE;
654
655 algo = nla_data(nla);
656 strcpy(algo->alg_name, auth->alg_name);
657 memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
658 algo->alg_key_len = auth->alg_key_len;
659
660 return 0;
661}
662
Herbert Xu68325d32007-10-09 13:30:57 -0700663/* Don't change this without updating xfrm_sa_len! */
664static int copy_to_user_state_extra(struct xfrm_state *x,
665 struct xfrm_usersa_info *p,
666 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 copy_to_user_state(x, p);
669
Herbert Xu050f0092007-10-09 13:31:47 -0700670 if (x->coaddr)
671 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
672
673 if (x->lastused)
674 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700675
Herbert Xu1a6509d2008-01-28 19:37:29 -0800676 if (x->aead)
677 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Martin Willi4447bb32009-11-25 00:29:52 +0000678 if (x->aalg) {
679 if (copy_to_user_auth(x->aalg, skb))
680 goto nla_put_failure;
681
682 NLA_PUT(skb, XFRMA_ALG_AUTH_TRUNC,
683 xfrm_alg_auth_len(x->aalg), x->aalg);
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800686 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700688 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700691 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000693 if (xfrm_mark_put(skb, &x->mark))
694 goto nla_put_failure;
695
Thomas Grafc0144be2007-08-22 13:55:43 -0700696 if (x->security && copy_sec_ctx(x->security, skb) < 0)
697 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700698
Herbert Xu68325d32007-10-09 13:30:57 -0700699 return 0;
700
701nla_put_failure:
702 return -EMSGSIZE;
703}
704
705static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
706{
707 struct xfrm_dump_info *sp = ptr;
708 struct sk_buff *in_skb = sp->in_skb;
709 struct sk_buff *skb = sp->out_skb;
710 struct xfrm_usersa_info *p;
711 struct nlmsghdr *nlh;
712 int err;
713
Herbert Xu68325d32007-10-09 13:30:57 -0700714 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
715 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
716 if (nlh == NULL)
717 return -EMSGSIZE;
718
719 p = nlmsg_data(nlh);
720
721 err = copy_to_user_state_extra(x, p, skb);
722 if (err)
723 goto nla_put_failure;
724
Thomas Graf98250692007-08-22 12:47:26 -0700725 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727
Thomas Grafc0144be2007-08-22 13:55:43 -0700728nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700729 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Timo Teras4c563f72008-02-28 21:31:08 -0800733static int xfrm_dump_sa_done(struct netlink_callback *cb)
734{
735 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
736 xfrm_state_walk_done(walk);
737 return 0;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
741{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800742 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -0800743 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct xfrm_dump_info info;
745
Timo Teras4c563f72008-02-28 21:31:08 -0800746 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
747 sizeof(cb->args) - sizeof(cb->args[0]));
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 info.in_skb = cb->skb;
750 info.out_skb = skb;
751 info.nlmsg_seq = cb->nlh->nlmsg_seq;
752 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800753
754 if (!cb->args[0]) {
755 cb->args[0] = 1;
756 xfrm_state_walk_init(walk, 0);
757 }
758
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800759 (void) xfrm_state_walk(net, walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 return skb->len;
762}
763
764static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
765 struct xfrm_state *x, u32 seq)
766{
767 struct xfrm_dump_info info;
768 struct sk_buff *skb;
769
Thomas Graf7deb2262007-08-22 13:57:39 -0700770 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!skb)
772 return ERR_PTR(-ENOMEM);
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 info.in_skb = in_skb;
775 info.out_skb = skb;
776 info.nlmsg_seq = seq;
777 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 if (dump_one_state(x, 0, &info)) {
780 kfree_skb(skb);
781 return NULL;
782 }
783
784 return skb;
785}
786
Thomas Graf7deb2262007-08-22 13:57:39 -0700787static inline size_t xfrm_spdinfo_msgsize(void)
788{
789 return NLMSG_ALIGN(4)
790 + nla_total_size(sizeof(struct xfrmu_spdinfo))
791 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
792}
793
Alexey Dobriyane0710412010-01-23 13:37:10 +0000794static int build_spdinfo(struct sk_buff *skb, struct net *net,
795 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700796{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700797 struct xfrmk_spdinfo si;
798 struct xfrmu_spdinfo spc;
799 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700800 struct nlmsghdr *nlh;
801 u32 *f;
802
803 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
804 if (nlh == NULL) /* shouldnt really happen ... */
805 return -EMSGSIZE;
806
807 f = nlmsg_data(nlh);
808 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000809 xfrm_spd_getinfo(net, &si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700810 spc.incnt = si.incnt;
811 spc.outcnt = si.outcnt;
812 spc.fwdcnt = si.fwdcnt;
813 spc.inscnt = si.inscnt;
814 spc.outscnt = si.outscnt;
815 spc.fwdscnt = si.fwdscnt;
816 sph.spdhcnt = si.spdhcnt;
817 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700818
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700819 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
820 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700821
822 return nlmsg_end(skb, nlh);
823
824nla_put_failure:
825 nlmsg_cancel(skb, nlh);
826 return -EMSGSIZE;
827}
828
829static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700830 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700831{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800832 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700833 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700834 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700835 u32 spid = NETLINK_CB(skb).pid;
836 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700837
Thomas Graf7deb2262007-08-22 13:57:39 -0700838 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700839 if (r_skb == NULL)
840 return -ENOMEM;
841
Alexey Dobriyane0710412010-01-23 13:37:10 +0000842 if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700843 BUG();
844
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800845 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700846}
847
Thomas Graf7deb2262007-08-22 13:57:39 -0700848static inline size_t xfrm_sadinfo_msgsize(void)
849{
850 return NLMSG_ALIGN(4)
851 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
852 + nla_total_size(4); /* XFRMA_SAD_CNT */
853}
854
Alexey Dobriyane0710412010-01-23 13:37:10 +0000855static int build_sadinfo(struct sk_buff *skb, struct net *net,
856 u32 pid, u32 seq, u32 flags)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700857{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700858 struct xfrmk_sadinfo si;
859 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700860 struct nlmsghdr *nlh;
861 u32 *f;
862
863 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
864 if (nlh == NULL) /* shouldnt really happen ... */
865 return -EMSGSIZE;
866
867 f = nlmsg_data(nlh);
868 *f = flags;
Alexey Dobriyane0710412010-01-23 13:37:10 +0000869 xfrm_sad_getinfo(net, &si);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700870
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700871 sh.sadhmcnt = si.sadhmcnt;
872 sh.sadhcnt = si.sadhcnt;
873
874 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
875 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700876
877 return nlmsg_end(skb, nlh);
878
879nla_put_failure:
880 nlmsg_cancel(skb, nlh);
881 return -EMSGSIZE;
882}
883
884static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700885 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700886{
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800887 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700888 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700889 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700890 u32 spid = NETLINK_CB(skb).pid;
891 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700892
Thomas Graf7deb2262007-08-22 13:57:39 -0700893 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700894 if (r_skb == NULL)
895 return -ENOMEM;
896
Alexey Dobriyane0710412010-01-23 13:37:10 +0000897 if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700898 BUG();
899
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800900 return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700901}
902
Christoph Hellwig22e70052007-01-02 15:22:30 -0800903static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700904 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800906 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -0700907 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct xfrm_state *x;
909 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700910 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800912 x = xfrm_user_state_lookup(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (x == NULL)
914 goto out_noput;
915
916 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
917 if (IS_ERR(resp_skb)) {
918 err = PTR_ERR(resp_skb);
919 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -0800920 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 xfrm_state_put(x);
923out_noput:
924 return err;
925}
926
927static int verify_userspi_info(struct xfrm_userspi_info *p)
928{
929 switch (p->info.id.proto) {
930 case IPPROTO_AH:
931 case IPPROTO_ESP:
932 break;
933
934 case IPPROTO_COMP:
935 /* IPCOMP spi is 16-bits. */
936 if (p->max >= 0x10000)
937 return -EINVAL;
938 break;
939
940 default:
941 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (p->min > p->max)
945 return -EINVAL;
946
947 return 0;
948}
949
Christoph Hellwig22e70052007-01-02 15:22:30 -0800950static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700951 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -0800953 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct xfrm_state *x;
955 struct xfrm_userspi_info *p;
956 struct sk_buff *resp_skb;
957 xfrm_address_t *daddr;
958 int family;
959 int err;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000960 u32 mark;
961 struct xfrm_mark m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Thomas Graf7b67c852007-08-22 13:53:52 -0700963 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 err = verify_userspi_info(p);
965 if (err)
966 goto out_noput;
967
968 family = p->info.family;
969 daddr = &p->info.id.daddr;
970
971 x = NULL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000972
973 mark = xfrm_mark_get(attrs, &m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (p->info.seq) {
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000975 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
977 xfrm_state_put(x);
978 x = NULL;
979 }
980 }
981
982 if (!x)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +0000983 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 p->info.id.proto, daddr,
985 &p->info.saddr, 1,
986 family);
987 err = -ENOENT;
988 if (x == NULL)
989 goto out_noput;
990
Herbert Xu658b2192007-10-09 13:29:52 -0700991 err = xfrm_alloc_spi(x, p->min, p->max);
992 if (err)
993 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Herbert Xu658b2192007-10-09 13:29:52 -0700995 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (IS_ERR(resp_skb)) {
997 err = PTR_ERR(resp_skb);
998 goto out;
999 }
1000
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001001 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003out:
1004 xfrm_state_put(x);
1005out_noput:
1006 return err;
1007}
1008
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001009static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 switch (dir) {
1012 case XFRM_POLICY_IN:
1013 case XFRM_POLICY_OUT:
1014 case XFRM_POLICY_FWD:
1015 break;
1016
1017 default:
1018 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 return 0;
1022}
1023
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001024static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001025{
1026 switch (type) {
1027 case XFRM_POLICY_TYPE_MAIN:
1028#ifdef CONFIG_XFRM_SUB_POLICY
1029 case XFRM_POLICY_TYPE_SUB:
1030#endif
1031 break;
1032
1033 default:
1034 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001035 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001036
1037 return 0;
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
1041{
1042 switch (p->share) {
1043 case XFRM_SHARE_ANY:
1044 case XFRM_SHARE_SESSION:
1045 case XFRM_SHARE_USER:
1046 case XFRM_SHARE_UNIQUE:
1047 break;
1048
1049 default:
1050 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 switch (p->action) {
1054 case XFRM_POLICY_ALLOW:
1055 case XFRM_POLICY_BLOCK:
1056 break;
1057
1058 default:
1059 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 switch (p->sel.family) {
1063 case AF_INET:
1064 break;
1065
1066 case AF_INET6:
1067#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1068 break;
1069#else
1070 return -EAFNOSUPPORT;
1071#endif
1072
1073 default:
1074 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 return verify_policy_dir(p->dir);
1078}
1079
Thomas Graf5424f322007-08-22 14:01:33 -07001080static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -08001081{
Thomas Graf5424f322007-08-22 14:01:33 -07001082 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001083 struct xfrm_user_sec_ctx *uctx;
1084
1085 if (!rt)
1086 return 0;
1087
Thomas Graf5424f322007-08-22 14:01:33 -07001088 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -07001089 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
1093 int nr)
1094{
1095 int i;
1096
1097 xp->xfrm_nr = nr;
1098 for (i = 0; i < nr; i++, ut++) {
1099 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1100
1101 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
1102 memcpy(&t->saddr, &ut->saddr,
1103 sizeof(xfrm_address_t));
1104 t->reqid = ut->reqid;
1105 t->mode = ut->mode;
1106 t->share = ut->share;
1107 t->optional = ut->optional;
1108 t->aalgos = ut->aalgos;
1109 t->ealgos = ut->ealgos;
1110 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -07001111 /* If all masks are ~0, then we allow all algorithms. */
1112 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -08001113 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115}
1116
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001117static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
1118{
1119 int i;
1120
1121 if (nr > XFRM_MAX_DEPTH)
1122 return -EINVAL;
1123
1124 for (i = 0; i < nr; i++) {
1125 /* We never validated the ut->family value, so many
1126 * applications simply leave it at zero. The check was
1127 * never made and ut->family was ignored because all
1128 * templates could be assumed to have the same family as
1129 * the policy itself. Now that we will have ipv4-in-ipv6
1130 * and ipv6-in-ipv4 tunnels, this is no longer true.
1131 */
1132 if (!ut[i].family)
1133 ut[i].family = family;
1134
1135 switch (ut[i].family) {
1136 case AF_INET:
1137 break;
1138#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1139 case AF_INET6:
1140 break;
1141#endif
1142 default:
1143 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001144 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001145 }
1146
1147 return 0;
1148}
1149
Thomas Graf5424f322007-08-22 14:01:33 -07001150static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Thomas Graf5424f322007-08-22 14:01:33 -07001152 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 if (!rt) {
1155 pol->xfrm_nr = 0;
1156 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001157 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1158 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001159 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001161 err = validate_tmpl(nr, utmpl, pol->family);
1162 if (err)
1163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Thomas Graf5424f322007-08-22 14:01:33 -07001165 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167 return 0;
1168}
1169
Thomas Graf5424f322007-08-22 14:01:33 -07001170static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001171{
Thomas Graf5424f322007-08-22 14:01:33 -07001172 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001173 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001174 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001175 int err;
1176
1177 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001178 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001179 type = upt->type;
1180 }
1181
1182 err = verify_policy_type(type);
1183 if (err)
1184 return err;
1185
1186 *tp = type;
1187 return 0;
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1191{
1192 xp->priority = p->priority;
1193 xp->index = p->index;
1194 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1195 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1196 xp->action = p->action;
1197 xp->flags = p->flags;
1198 xp->family = p->sel.family;
1199 /* XXX xp->share = p->share; */
1200}
1201
1202static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1203{
1204 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1205 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1206 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1207 p->priority = xp->priority;
1208 p->index = xp->index;
1209 p->sel.family = xp->family;
1210 p->dir = dir;
1211 p->action = xp->action;
1212 p->flags = xp->flags;
1213 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1214}
1215
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001216static 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 -07001217{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001218 struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int err;
1220
1221 if (!xp) {
1222 *errp = -ENOMEM;
1223 return NULL;
1224 }
1225
1226 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001227
Thomas Graf35a7aa02007-08-22 14:00:40 -07001228 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001229 if (err)
1230 goto error;
1231
Thomas Graf35a7aa02007-08-22 14:00:40 -07001232 if (!(err = copy_from_user_tmpl(xp, attrs)))
1233 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001234 if (err)
1235 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001238 error:
1239 *errp = err;
Herbert Xu12a169e2008-10-01 07:03:24 -07001240 xp->walk.dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001241 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001242 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Christoph Hellwig22e70052007-01-02 15:22:30 -08001245static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001246 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001248 struct net *net = sock_net(skb->sk);
Thomas Graf7b67c852007-08-22 13:53:52 -07001249 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001251 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int err;
1253 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001254 uid_t loginuid = NETLINK_CB(skb).loginuid;
1255 u32 sessionid = NETLINK_CB(skb).sessionid;
1256 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 err = verify_newpolicy_info(p);
1259 if (err)
1260 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001261 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001262 if (err)
1263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001265 xp = xfrm_policy_construct(net, p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!xp)
1267 return err;
1268
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001269 /* shouldnt excl be based on nlh flags??
1270 * Aha! this is anti-netlink really i.e more pfkey derived
1271 * in netlink excl is a flag and you wouldnt need
1272 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1274 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001275 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001278 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 kfree(xp);
1280 return err;
1281 }
1282
Herbert Xuf60f6b82005-06-18 22:44:37 -07001283 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001284 c.seq = nlh->nlmsg_seq;
1285 c.pid = nlh->nlmsg_pid;
1286 km_policy_notify(xp, p->dir, &c);
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 xfrm_pol_put(xp);
1289
1290 return 0;
1291}
1292
1293static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1294{
1295 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1296 int i;
1297
1298 if (xp->xfrm_nr == 0)
1299 return 0;
1300
1301 for (i = 0; i < xp->xfrm_nr; i++) {
1302 struct xfrm_user_tmpl *up = &vec[i];
1303 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1304
1305 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001306 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1308 up->reqid = kp->reqid;
1309 up->mode = kp->mode;
1310 up->share = kp->share;
1311 up->optional = kp->optional;
1312 up->aalgos = kp->aalgos;
1313 up->ealgos = kp->ealgos;
1314 up->calgos = kp->calgos;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Thomas Grafc0144be2007-08-22 13:55:43 -07001317 return nla_put(skb, XFRMA_TMPL,
1318 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001319}
1320
Serge Hallyn0d681622006-07-24 23:30:44 -07001321static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1322{
1323 if (x->security) {
1324 return copy_sec_ctx(x->security, skb);
1325 }
1326 return 0;
1327}
1328
1329static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1330{
1331 if (xp->security) {
1332 return copy_sec_ctx(xp->security, skb);
1333 }
1334 return 0;
1335}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001336static inline size_t userpolicy_type_attrsize(void)
1337{
1338#ifdef CONFIG_XFRM_SUB_POLICY
1339 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1340#else
1341 return 0;
1342#endif
1343}
Serge Hallyn0d681622006-07-24 23:30:44 -07001344
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001345#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001346static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001347{
Thomas Grafc0144be2007-08-22 13:55:43 -07001348 struct xfrm_userpolicy_type upt = {
1349 .type = type,
1350 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001351
Thomas Grafc0144be2007-08-22 13:55:43 -07001352 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001353}
1354
1355#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001356static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001357{
1358 return 0;
1359}
1360#endif
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1363{
1364 struct xfrm_dump_info *sp = ptr;
1365 struct xfrm_userpolicy_info *p;
1366 struct sk_buff *in_skb = sp->in_skb;
1367 struct sk_buff *skb = sp->out_skb;
1368 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001370 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1371 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1372 if (nlh == NULL)
1373 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Thomas Graf7b67c852007-08-22 13:53:52 -07001375 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 copy_to_user_policy(xp, p, dir);
1377 if (copy_to_user_tmpl(xp, skb) < 0)
1378 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001379 if (copy_to_user_sec_ctx(xp, skb))
1380 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001381 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001382 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Thomas Graf98250692007-08-22 12:47:26 -07001384 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386
1387nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001388 nlmsg_cancel(skb, nlh);
1389 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Timo Teras4c563f72008-02-28 21:31:08 -08001392static int xfrm_dump_policy_done(struct netlink_callback *cb)
1393{
1394 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1395
1396 xfrm_policy_walk_done(walk);
1397 return 0;
1398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1401{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001402 struct net *net = sock_net(skb->sk);
Timo Teras4c563f72008-02-28 21:31:08 -08001403 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct xfrm_dump_info info;
1405
Timo Teras4c563f72008-02-28 21:31:08 -08001406 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1407 sizeof(cb->args) - sizeof(cb->args[0]));
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 info.in_skb = cb->skb;
1410 info.out_skb = skb;
1411 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1412 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001413
1414 if (!cb->args[0]) {
1415 cb->args[0] = 1;
1416 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1417 }
1418
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001419 (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 return skb->len;
1422}
1423
1424static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1425 struct xfrm_policy *xp,
1426 int dir, u32 seq)
1427{
1428 struct xfrm_dump_info info;
1429 struct sk_buff *skb;
1430
Thomas Graf7deb2262007-08-22 13:57:39 -07001431 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (!skb)
1433 return ERR_PTR(-ENOMEM);
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 info.in_skb = in_skb;
1436 info.out_skb = skb;
1437 info.nlmsg_seq = seq;
1438 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1441 kfree_skb(skb);
1442 return NULL;
1443 }
1444
1445 return skb;
1446}
1447
Christoph Hellwig22e70052007-01-02 15:22:30 -08001448static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001449 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001451 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct xfrm_policy *xp;
1453 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001454 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001456 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int delete;
1458
Thomas Graf7b67c852007-08-22 13:53:52 -07001459 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1461
Thomas Graf35a7aa02007-08-22 14:00:40 -07001462 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001463 if (err)
1464 return err;
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 err = verify_policy_dir(p->dir);
1467 if (err)
1468 return err;
1469
1470 if (p->index)
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00001471 xp = xfrm_policy_byid(net, DUMMY_MARK, type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001472 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001473 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001474 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001475
Thomas Graf35a7aa02007-08-22 14:00:40 -07001476 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001477 if (err)
1478 return err;
1479
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001480 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001481 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001482 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001483
Paul Moore03e1ad72008-04-12 19:07:52 -07001484 err = security_xfrm_policy_alloc(&ctx, uctx);
1485 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001486 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001487 }
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001488 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, type, p->dir, &p->sel,
1489 ctx, delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001490 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (xp == NULL)
1493 return -ENOENT;
1494
1495 if (!delete) {
1496 struct sk_buff *resp_skb;
1497
1498 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1499 if (IS_ERR(resp_skb)) {
1500 err = PTR_ERR(resp_skb);
1501 } else {
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001502 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
Thomas Graf082a1ad2007-08-22 13:54:36 -07001503 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001505 } else {
Eric Paris25323862008-04-18 10:09:25 -04001506 uid_t loginuid = NETLINK_CB(skb).loginuid;
1507 u32 sessionid = NETLINK_CB(skb).sessionid;
1508 u32 sid = NETLINK_CB(skb).sid;
1509
1510 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1511 sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001512
1513 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001514 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001515
Herbert Xue7443892005-06-18 22:44:18 -07001516 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001517 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001518 c.seq = nlh->nlmsg_seq;
1519 c.pid = nlh->nlmsg_pid;
1520 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001523out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001524 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return err;
1526}
1527
Christoph Hellwig22e70052007-01-02 15:22:30 -08001528static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001529 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001531 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001532 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001533 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001534 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001535 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Joy Latten161a09e2006-11-27 13:11:54 -06001537 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001538 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001539 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001540 err = xfrm_state_flush(net, p->proto, &audit_info);
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001541 if (err) {
1542 if (err == -ESRCH) /* empty table */
1543 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001544 return err;
Jamal Hadi Salim9e64cc92010-02-19 02:00:41 +00001545 }
Herbert Xubf088672005-06-18 22:44:00 -07001546 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001547 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001548 c.seq = nlh->nlmsg_seq;
1549 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001550 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001551 km_state_notify(NULL, &c);
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return 0;
1554}
1555
Thomas Graf7deb2262007-08-22 13:57:39 -07001556static inline size_t xfrm_aevent_msgsize(void)
1557{
1558 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1559 + nla_total_size(sizeof(struct xfrm_replay_state))
1560 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001561 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07001562 + nla_total_size(4) /* XFRM_AE_RTHR */
1563 + nla_total_size(4); /* XFRM_AE_ETHR */
1564}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001565
1566static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1567{
1568 struct xfrm_aevent_id *id;
1569 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001570
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001571 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1572 if (nlh == NULL)
1573 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001574
Thomas Graf7b67c852007-08-22 13:53:52 -07001575 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001576 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001577 id->sa_id.spi = x->id.spi;
1578 id->sa_id.family = x->props.family;
1579 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001580 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1581 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001582 id->flags = c->data.aevent;
1583
Thomas Grafc0144be2007-08-22 13:55:43 -07001584 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1585 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001586
Thomas Grafc0144be2007-08-22 13:55:43 -07001587 if (id->flags & XFRM_AE_RTHR)
1588 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001589
Thomas Grafc0144be2007-08-22 13:55:43 -07001590 if (id->flags & XFRM_AE_ETHR)
1591 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1592 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001593
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001594 if (xfrm_mark_put(skb, &x->mark))
1595 goto nla_put_failure;
1596
Thomas Graf98250692007-08-22 12:47:26 -07001597 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001598
Thomas Grafc0144be2007-08-22 13:55:43 -07001599nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001600 nlmsg_cancel(skb, nlh);
1601 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001602}
1603
Christoph Hellwig22e70052007-01-02 15:22:30 -08001604static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001605 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001606{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001607 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001608 struct xfrm_state *x;
1609 struct sk_buff *r_skb;
1610 int err;
1611 struct km_event c;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001612 u32 mark;
1613 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001614 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001615 struct xfrm_usersa_id *id = &p->sa_id;
1616
Thomas Graf7deb2262007-08-22 13:57:39 -07001617 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001618 if (r_skb == NULL)
1619 return -ENOMEM;
1620
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001621 mark = xfrm_mark_get(attrs, &m);
1622
1623 x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001624 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001625 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001626 return -ESRCH;
1627 }
1628
1629 /*
1630 * XXX: is this lock really needed - none of the other
1631 * gets lock (the concern is things getting updated
1632 * while we are still reading) - jhs
1633 */
1634 spin_lock_bh(&x->lock);
1635 c.data.aevent = p->flags;
1636 c.seq = nlh->nlmsg_seq;
1637 c.pid = nlh->nlmsg_pid;
1638
1639 if (build_aevent(r_skb, x, &c) < 0)
1640 BUG();
Alexey Dobriyana6483b72008-11-25 17:38:20 -08001641 err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001642 spin_unlock_bh(&x->lock);
1643 xfrm_state_put(x);
1644 return err;
1645}
1646
Christoph Hellwig22e70052007-01-02 15:22:30 -08001647static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001648 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001649{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001650 struct net *net = sock_net(skb->sk);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001651 struct xfrm_state *x;
1652 struct km_event c;
1653 int err = - EINVAL;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001654 u32 mark = 0;
1655 struct xfrm_mark m;
Thomas Graf7b67c852007-08-22 13:53:52 -07001656 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001657 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1658 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001659
1660 if (!lt && !rp)
1661 return err;
1662
1663 /* pedantic mode - thou shalt sayeth replaceth */
1664 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1665 return err;
1666
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001667 mark = xfrm_mark_get(attrs, &m);
1668
1669 x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001670 if (x == NULL)
1671 return -ESRCH;
1672
1673 if (x->km.state != XFRM_STATE_VALID)
1674 goto out;
1675
1676 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001677 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001678 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001679
1680 c.event = nlh->nlmsg_type;
1681 c.seq = nlh->nlmsg_seq;
1682 c.pid = nlh->nlmsg_pid;
1683 c.data.aevent = XFRM_AE_CU;
1684 km_state_notify(x, &c);
1685 err = 0;
1686out:
1687 xfrm_state_put(x);
1688 return err;
1689}
1690
Christoph Hellwig22e70052007-01-02 15:22:30 -08001691static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001692 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001694 struct net *net = sock_net(skb->sk);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001695 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001696 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001697 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001698 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001699
Thomas Graf35a7aa02007-08-22 14:00:40 -07001700 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001701 if (err)
1702 return err;
1703
Joy Latten161a09e2006-11-27 13:11:54 -06001704 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001705 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001706 audit_info.secid = NETLINK_CB(skb).sid;
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001707 err = xfrm_policy_flush(net, type, &audit_info);
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001708 if (err) {
1709 if (err == -ESRCH) /* empty table */
1710 return 0;
David S. Miller069c4742010-02-17 13:41:40 -08001711 return err;
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001712 }
1713
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001714 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001715 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001716 c.seq = nlh->nlmsg_seq;
1717 c.pid = nlh->nlmsg_pid;
Alexey Dobriyan70678022008-11-25 17:50:36 -08001718 c.net = net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001719 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return 0;
1721}
1722
Christoph Hellwig22e70052007-01-02 15:22:30 -08001723static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001724 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001725{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001726 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001727 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001728 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001729 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001730 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001731 int err = -ENOENT;
1732
Thomas Graf35a7aa02007-08-22 14:00:40 -07001733 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001734 if (err)
1735 return err;
1736
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001737 if (p->index)
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +00001738 xp = xfrm_policy_byid(net, DUMMY_MARK, type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001739 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001740 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001741 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001742
Thomas Graf35a7aa02007-08-22 14:00:40 -07001743 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001744 if (err)
1745 return err;
1746
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001747 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001748 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001749 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001750
Paul Moore03e1ad72008-04-12 19:07:52 -07001751 err = security_xfrm_policy_alloc(&ctx, uctx);
1752 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001753 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001754 }
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001755 xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, type, p->dir,
1756 &p->sel, ctx, 0, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001757 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001758 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001759 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001760 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001761
Eric Parisef41aaa2007-03-07 15:37:58 -08001762 read_lock(&xp->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001763 if (xp->walk.dead) {
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001764 read_unlock(&xp->lock);
1765 goto out;
1766 }
1767
1768 read_unlock(&xp->lock);
1769 err = 0;
1770 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001771 uid_t loginuid = NETLINK_CB(skb).loginuid;
1772 uid_t sessionid = NETLINK_CB(skb).sessionid;
1773 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001774 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001775 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001776
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001777 } else {
1778 // reset the timers here?
1779 printk("Dont know what to do with soft policy expire\n");
1780 }
1781 km_policy_expired(xp, p->dir, up->hard, current->pid);
1782
1783out:
1784 xfrm_pol_put(xp);
1785 return err;
1786}
1787
Christoph Hellwig22e70052007-01-02 15:22:30 -08001788static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001789 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001790{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001791 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001792 struct xfrm_state *x;
1793 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001794 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001795 struct xfrm_usersa_info *p = &ue->state;
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001796 struct xfrm_mark m;
1797 u32 mark = xfrm_mark_get(attrs, &m);;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001798
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001799 x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001800
David S. Miller3a765aa2007-02-26 14:52:21 -08001801 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001802 if (x == NULL)
1803 return err;
1804
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001805 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001806 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001807 if (x->km.state != XFRM_STATE_VALID)
1808 goto out;
1809 km_state_expired(x, ue->hard, current->pid);
1810
Joy Latten161a09e2006-11-27 13:11:54 -06001811 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001812 uid_t loginuid = NETLINK_CB(skb).loginuid;
1813 uid_t sessionid = NETLINK_CB(skb).sessionid;
1814 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001815 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001816 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001817 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001818 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001819out:
1820 spin_unlock_bh(&x->lock);
1821 xfrm_state_put(x);
1822 return err;
1823}
1824
Christoph Hellwig22e70052007-01-02 15:22:30 -08001825static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001826 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001827{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001828 struct net *net = sock_net(skb->sk);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001829 struct xfrm_policy *xp;
1830 struct xfrm_user_tmpl *ut;
1831 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001832 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001833 struct xfrm_mark mark;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001834
Thomas Graf7b67c852007-08-22 13:53:52 -07001835 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001836 struct xfrm_state *x = xfrm_state_alloc(net);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001837 int err = -ENOMEM;
1838
1839 if (!x)
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001840 goto nomem;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001841
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001842 xfrm_mark_get(attrs, &mark);
1843
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001844 err = verify_newpolicy_info(&ua->policy);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001845 if (err)
1846 goto bad_policy;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001847
1848 /* build an XP */
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08001849 xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001850 if (!xp)
1851 goto free_state;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001852
1853 memcpy(&x->id, &ua->id, sizeof(ua->id));
1854 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1855 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00001856 xp->mark.m = x->mark.m = mark.m;
1857 xp->mark.v = x->mark.v = mark.v;
Thomas Graf5424f322007-08-22 14:01:33 -07001858 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001859 /* extract the templates and for each call km_key */
1860 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1861 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1862 memcpy(&x->id, &t->id, sizeof(x->id));
1863 x->props.mode = t->mode;
1864 x->props.reqid = t->reqid;
1865 x->props.family = ut->family;
1866 t->aalgos = ua->aalgos;
1867 t->ealgos = ua->ealgos;
1868 t->calgos = ua->calgos;
1869 err = km_query(x, t, xp);
1870
1871 }
1872
1873 kfree(x);
1874 kfree(xp);
1875
1876 return 0;
Ilpo Järvinend8eb9302008-12-14 23:16:22 -08001877
1878bad_policy:
1879 printk("BAD policy passed\n");
1880free_state:
1881 kfree(x);
1882nomem:
1883 return err;
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001884}
1885
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001886#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001887static int copy_from_user_migrate(struct xfrm_migrate *ma,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001888 struct xfrm_kmaddress *k,
Thomas Graf5424f322007-08-22 14:01:33 -07001889 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001890{
Thomas Graf5424f322007-08-22 14:01:33 -07001891 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001892 struct xfrm_user_migrate *um;
1893 int i, num_migrate;
1894
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001895 if (k != NULL) {
1896 struct xfrm_user_kmaddress *uk;
1897
1898 uk = nla_data(attrs[XFRMA_KMADDRESS]);
1899 memcpy(&k->local, &uk->local, sizeof(k->local));
1900 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
1901 k->family = uk->family;
1902 k->reserved = uk->reserved;
1903 }
1904
Thomas Graf5424f322007-08-22 14:01:33 -07001905 um = nla_data(rt);
1906 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001907
1908 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1909 return -EINVAL;
1910
1911 for (i = 0; i < num_migrate; i++, um++, ma++) {
1912 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1913 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1914 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1915 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1916
1917 ma->proto = um->proto;
1918 ma->mode = um->mode;
1919 ma->reqid = um->reqid;
1920
1921 ma->old_family = um->old_family;
1922 ma->new_family = um->new_family;
1923 }
1924
1925 *num = i;
1926 return 0;
1927}
1928
1929static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001930 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001931{
Thomas Graf7b67c852007-08-22 13:53:52 -07001932 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001933 struct xfrm_migrate m[XFRM_MAX_DEPTH];
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001934 struct xfrm_kmaddress km, *kmp;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001935 u8 type;
1936 int err;
1937 int n = 0;
1938
Thomas Graf35a7aa02007-08-22 14:00:40 -07001939 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001940 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001941
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001942 kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
1943
Thomas Graf5424f322007-08-22 14:01:33 -07001944 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001945 if (err)
1946 return err;
1947
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001948 err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001949 if (err)
1950 return err;
1951
1952 if (!n)
1953 return 0;
1954
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001955 xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001956
1957 return 0;
1958}
1959#else
1960static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001961 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001962{
1963 return -ENOPROTOOPT;
1964}
1965#endif
1966
1967#ifdef CONFIG_XFRM_MIGRATE
1968static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1969{
1970 struct xfrm_user_migrate um;
1971
1972 memset(&um, 0, sizeof(um));
1973 um.proto = m->proto;
1974 um.mode = m->mode;
1975 um.reqid = m->reqid;
1976 um.old_family = m->old_family;
1977 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1978 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1979 um.new_family = m->new_family;
1980 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1981 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1982
Thomas Grafc0144be2007-08-22 13:55:43 -07001983 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001984}
1985
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001986static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
1987{
1988 struct xfrm_user_kmaddress uk;
1989
1990 memset(&uk, 0, sizeof(uk));
1991 uk.family = k->family;
1992 uk.reserved = k->reserved;
1993 memcpy(&uk.local, &k->local, sizeof(uk.local));
Arnaud Ebalarda1caa322008-11-03 01:30:23 -08001994 memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07001995
1996 return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
1997}
1998
1999static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
Thomas Graf7deb2262007-08-22 13:57:39 -07002000{
2001 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002002 + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
2003 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
2004 + userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002005}
2006
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002007static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002008 int num_migrate, struct xfrm_kmaddress *k,
2009 struct xfrm_selector *sel, u8 dir, u8 type)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002010{
2011 struct xfrm_migrate *mp;
2012 struct xfrm_userpolicy_id *pol_id;
2013 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002014 int i;
2015
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002016 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
2017 if (nlh == NULL)
2018 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002019
Thomas Graf7b67c852007-08-22 13:53:52 -07002020 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002021 /* copy data from selector, dir, and type to the pol_id */
2022 memset(pol_id, 0, sizeof(*pol_id));
2023 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
2024 pol_id->dir = dir;
2025
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002026 if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
2027 goto nlmsg_failure;
2028
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002029 if (copy_to_user_policy_type(type, skb) < 0)
2030 goto nlmsg_failure;
2031
2032 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
2033 if (copy_to_user_migrate(mp, skb) < 0)
2034 goto nlmsg_failure;
2035 }
2036
Thomas Graf98250692007-08-22 12:47:26 -07002037 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002038nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002039 nlmsg_cancel(skb, nlh);
2040 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002041}
2042
2043static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002044 struct xfrm_migrate *m, int num_migrate,
2045 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002046{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002047 struct net *net = &init_net;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002048 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002049
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002050 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002051 if (skb == NULL)
2052 return -ENOMEM;
2053
2054 /* build migrate */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002055 if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002056 BUG();
2057
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002058 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002059}
2060#else
2061static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002062 struct xfrm_migrate *m, int num_migrate,
2063 struct xfrm_kmaddress *k)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002064{
2065 return -ENOPROTOOPT;
2066}
2067#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002068
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002069#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07002070
2071static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
David S. Miller66f9a252009-01-20 09:49:51 -08002072 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Thomas Graf492b5582005-05-03 14:26:40 -07002073 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2074 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
2075 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
2076 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2077 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
2078 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002079 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002080 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07002081 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
David S. Miller66f9a252009-01-20 09:49:51 -08002082 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002083 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07002084 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002085 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002086 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
2087 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002088 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002089 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002090 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
2091 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092};
2093
Thomas Graf492b5582005-05-03 14:26:40 -07002094#undef XMSGSIZE
2095
Thomas Grafcf5cb792007-08-22 13:59:04 -07002096static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
jamalc28e9302010-02-09 03:59:38 +00002097 [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
2098 [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
2099 [XFRMA_LASTUSED] = { .type = NLA_U64},
2100 [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
Herbert Xu1a6509d2008-01-28 19:37:29 -08002101 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002102 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
2103 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
2104 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
2105 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
2106 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
2107 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
2108 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
2109 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
2110 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
2111 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
2112 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
2113 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
2114 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
2115 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002116 [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002117 [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07002118};
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07002121 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08002123 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07002124} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
2125 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
2126 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
2127 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08002128 .dump = xfrm_dump_sa,
2129 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002130 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2131 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
2132 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08002133 .dump = xfrm_dump_policy,
2134 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07002135 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08002136 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08002137 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07002138 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
2139 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08002140 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07002141 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
2142 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002143 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
2144 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002145 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07002146 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07002147 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148};
2149
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002150static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002152 struct net *net = sock_net(skb->sk);
Thomas Graf35a7aa02007-08-22 14:00:40 -07002153 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002155 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 type -= XFRM_MSG_BASE;
2162 link = &xfrm_dispatch[type];
2163
2164 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002165 if (security_netlink_recv(skb, CAP_NET_ADMIN))
2166 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Thomas Graf492b5582005-05-03 14:26:40 -07002168 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
2169 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
2170 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002172 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002174 return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176
Thomas Graf35a7aa02007-08-22 14:00:40 -07002177 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07002178 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07002179 if (err < 0)
2180 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002183 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Thomas Graf5424f322007-08-22 14:01:33 -07002185 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002188static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002190 mutex_lock(&xfrm_cfg_mutex);
2191 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
2192 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Thomas Graf7deb2262007-08-22 13:57:39 -07002195static inline size_t xfrm_expire_msgsize(void)
2196{
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002197 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
2198 + nla_total_size(sizeof(struct xfrm_mark));
Thomas Graf7deb2262007-08-22 13:57:39 -07002199}
2200
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002201static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct xfrm_user_expire *ue;
2204 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002206 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2207 if (nlh == NULL)
2208 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Thomas Graf7b67c852007-08-22 13:53:52 -07002210 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002212 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002214 if (xfrm_mark_put(skb, &x->mark))
2215 goto nla_put_failure;
2216
Thomas Graf98250692007-08-22 12:47:26 -07002217 return nlmsg_end(skb, nlh);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002218
2219nla_put_failure:
2220 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002223static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002225 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 struct sk_buff *skb;
2227
Thomas Graf7deb2262007-08-22 13:57:39 -07002228 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (skb == NULL)
2230 return -ENOMEM;
2231
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002232 if (build_expire(skb, x, c) < 0) {
2233 kfree_skb(skb);
2234 return -EMSGSIZE;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002237 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002240static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2241{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002242 struct net *net = xs_net(x);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002243 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002244
Thomas Graf7deb2262007-08-22 13:57:39 -07002245 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002246 if (skb == NULL)
2247 return -ENOMEM;
2248
2249 if (build_aevent(skb, x, c) < 0)
2250 BUG();
2251
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002252 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002253}
2254
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002255static int xfrm_notify_sa_flush(struct km_event *c)
2256{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002257 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002258 struct xfrm_usersa_flush *p;
2259 struct nlmsghdr *nlh;
2260 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002261 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002262
Thomas Graf7deb2262007-08-22 13:57:39 -07002263 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002264 if (skb == NULL)
2265 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002266
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002267 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2268 if (nlh == NULL) {
2269 kfree_skb(skb);
2270 return -EMSGSIZE;
2271 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002272
Thomas Graf7b67c852007-08-22 13:53:52 -07002273 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002274 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002275
Thomas Graf98250692007-08-22 12:47:26 -07002276 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002277
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002278 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002279}
2280
Thomas Graf7deb2262007-08-22 13:57:39 -07002281static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002282{
Thomas Graf7deb2262007-08-22 13:57:39 -07002283 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002284 if (x->aead)
2285 l += nla_total_size(aead_len(x->aead));
Martin Willi4447bb32009-11-25 00:29:52 +00002286 if (x->aalg) {
2287 l += nla_total_size(sizeof(struct xfrm_algo) +
2288 (x->aalg->alg_key_len + 7) / 8);
2289 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
2290 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002291 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002292 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002293 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002294 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002295 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002296 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002297 if (x->security)
2298 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2299 x->security->ctx_len);
2300 if (x->coaddr)
2301 l += nla_total_size(sizeof(*x->coaddr));
2302
Herbert Xud26f3982007-11-13 21:47:08 -08002303 /* Must count x->lastused as it may become non-zero behind our back. */
2304 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305
2306 return l;
2307}
2308
2309static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2310{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002311 struct net *net = xs_net(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002312 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002313 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002314 struct nlmsghdr *nlh;
2315 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002316 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002317 int headlen;
2318
2319 headlen = sizeof(*p);
2320 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002321 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002322 headlen = sizeof(*id);
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002323 len += nla_total_size(sizeof(struct xfrm_mark));
Herbert Xu0603eac2005-06-18 22:54:36 -07002324 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002325 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002326
Thomas Graf7deb2262007-08-22 13:57:39 -07002327 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002328 if (skb == NULL)
2329 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002330
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002331 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2332 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002333 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002334
Thomas Graf7b67c852007-08-22 13:53:52 -07002335 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002336 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002337 struct nlattr *attr;
2338
Thomas Graf7b67c852007-08-22 13:53:52 -07002339 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002340 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2341 id->spi = x->id.spi;
2342 id->family = x->props.family;
2343 id->proto = x->id.proto;
2344
Thomas Grafc0144be2007-08-22 13:55:43 -07002345 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2346 if (attr == NULL)
2347 goto nla_put_failure;
2348
2349 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002350 }
2351
Herbert Xu68325d32007-10-09 13:30:57 -07002352 if (copy_to_user_state_extra(x, p, skb))
2353 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002354
Thomas Graf98250692007-08-22 12:47:26 -07002355 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002356
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002357 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358
Thomas Grafc0144be2007-08-22 13:55:43 -07002359nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002360 /* Somebody screwed up with xfrm_sa_len! */
2361 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002362 kfree_skb(skb);
2363 return -1;
2364}
2365
2366static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2367{
2368
2369 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002370 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002371 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002372 case XFRM_MSG_NEWAE:
2373 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002374 case XFRM_MSG_DELSA:
2375 case XFRM_MSG_UPDSA:
2376 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002377 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002378 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002379 return xfrm_notify_sa_flush(c);
2380 default:
2381 printk("xfrm_user: Unknown SA event %d\n", c->event);
2382 break;
2383 }
2384
2385 return 0;
2386
2387}
2388
Thomas Graf7deb2262007-08-22 13:57:39 -07002389static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2390 struct xfrm_policy *xp)
2391{
2392 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2393 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
Jamal Hadi Salim6f26b612010-02-22 11:32:59 +00002394 + nla_total_size(sizeof(struct xfrm_mark))
Thomas Graf7deb2262007-08-22 13:57:39 -07002395 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2396 + userpolicy_type_attrsize();
2397}
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2400 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2401 int dir)
2402{
2403 struct xfrm_user_acquire *ua;
2404 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 __u32 seq = xfrm_get_acqseq();
2406
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002407 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2408 if (nlh == NULL)
2409 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Thomas Graf7b67c852007-08-22 13:53:52 -07002411 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 memcpy(&ua->id, &x->id, sizeof(ua->id));
2413 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2414 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2415 copy_to_user_policy(xp, &ua->policy, dir);
2416 ua->aalgos = xt->aalgos;
2417 ua->ealgos = xt->ealgos;
2418 ua->calgos = xt->calgos;
2419 ua->seq = x->km.seq = seq;
2420
2421 if (copy_to_user_tmpl(xp, skb) < 0)
2422 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002423 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002424 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002425 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002426 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Thomas Graf98250692007-08-22 12:47:26 -07002428 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
2430nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002431 nlmsg_cancel(skb, nlh);
2432 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
2435static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2436 struct xfrm_policy *xp, int dir)
2437{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002438 struct net *net = xs_net(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Thomas Graf7deb2262007-08-22 13:57:39 -07002441 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (skb == NULL)
2443 return -ENOMEM;
2444
2445 if (build_acquire(skb, x, xt, xp, dir) < 0)
2446 BUG();
2447
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002448 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
2451/* User gives us xfrm_user_policy_info followed by an array of 0
2452 * or more templates.
2453 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002454static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 u8 *data, int len, int *dir)
2456{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002457 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2459 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2460 struct xfrm_policy *xp;
2461 int nr;
2462
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002463 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 case AF_INET:
2465 if (opt != IP_XFRM_POLICY) {
2466 *dir = -EOPNOTSUPP;
2467 return NULL;
2468 }
2469 break;
2470#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2471 case AF_INET6:
2472 if (opt != IPV6_XFRM_POLICY) {
2473 *dir = -EOPNOTSUPP;
2474 return NULL;
2475 }
2476 break;
2477#endif
2478 default:
2479 *dir = -EINVAL;
2480 return NULL;
2481 }
2482
2483 *dir = -EINVAL;
2484
2485 if (len < sizeof(*p) ||
2486 verify_newpolicy_info(p))
2487 return NULL;
2488
2489 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002490 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return NULL;
2492
Herbert Xua4f1bac2005-07-26 15:43:17 -07002493 if (p->dir > XFRM_POLICY_OUT)
2494 return NULL;
2495
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002496 xp = xfrm_policy_alloc(net, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (xp == NULL) {
2498 *dir = -ENOBUFS;
2499 return NULL;
2500 }
2501
2502 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002503 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 copy_templates(xp, ut, nr);
2505
2506 *dir = p->dir;
2507
2508 return xp;
2509}
2510
Thomas Graf7deb2262007-08-22 13:57:39 -07002511static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2512{
2513 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2514 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2515 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2516 + userpolicy_type_attrsize();
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002520 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 struct xfrm_user_polexpire *upe;
2523 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002524 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002526 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2527 if (nlh == NULL)
2528 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Thomas Graf7b67c852007-08-22 13:53:52 -07002530 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 copy_to_user_policy(xp, &upe->pol, dir);
2532 if (copy_to_user_tmpl(xp, skb) < 0)
2533 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002534 if (copy_to_user_sec_ctx(xp, skb))
2535 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002536 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002537 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 upe->hard = !!hard;
2539
Thomas Graf98250692007-08-22 12:47:26 -07002540 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002543 nlmsg_cancel(skb, nlh);
2544 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002547static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002549 struct net *net = xp_net(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Thomas Graf7deb2262007-08-22 13:57:39 -07002552 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (skb == NULL)
2554 return -ENOMEM;
2555
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002556 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 BUG();
2558
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002559 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002562static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2563{
Alexey Dobriyanfc34acd2008-11-25 17:50:08 -08002564 struct net *net = xp_net(xp);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002565 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002566 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002567 struct nlmsghdr *nlh;
2568 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002569 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002570 int headlen;
2571
2572 headlen = sizeof(*p);
2573 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002574 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002575 headlen = sizeof(*id);
2576 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002577 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002578 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002579
Thomas Graf7deb2262007-08-22 13:57:39 -07002580 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002581 if (skb == NULL)
2582 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002583
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002584 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2585 if (nlh == NULL)
2586 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002587
Thomas Graf7b67c852007-08-22 13:53:52 -07002588 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002589 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002590 struct nlattr *attr;
2591
Thomas Graf7b67c852007-08-22 13:53:52 -07002592 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002593 memset(id, 0, sizeof(*id));
2594 id->dir = dir;
2595 if (c->data.byid)
2596 id->index = xp->index;
2597 else
2598 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2599
Thomas Grafc0144be2007-08-22 13:55:43 -07002600 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2601 if (attr == NULL)
2602 goto nlmsg_failure;
2603
2604 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002605 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002606
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002607 copy_to_user_policy(xp, p, dir);
2608 if (copy_to_user_tmpl(xp, skb) < 0)
2609 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002610 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002611 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002612
Thomas Graf98250692007-08-22 12:47:26 -07002613 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002614
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002615 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002616
2617nlmsg_failure:
2618 kfree_skb(skb);
2619 return -1;
2620}
2621
2622static int xfrm_notify_policy_flush(struct km_event *c)
2623{
Alexey Dobriyan70678022008-11-25 17:50:36 -08002624 struct net *net = c->net;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002625 struct nlmsghdr *nlh;
2626 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002627
Thomas Graf7deb2262007-08-22 13:57:39 -07002628 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002629 if (skb == NULL)
2630 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002631
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002632 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2633 if (nlh == NULL)
2634 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002635 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2636 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002637
Thomas Graf98250692007-08-22 12:47:26 -07002638 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002639
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002640 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002641
2642nlmsg_failure:
2643 kfree_skb(skb);
2644 return -1;
2645}
2646
2647static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2648{
2649
2650 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002651 case XFRM_MSG_NEWPOLICY:
2652 case XFRM_MSG_UPDPOLICY:
2653 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002654 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002655 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002656 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002657 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002658 return xfrm_exp_policy_notify(xp, dir, c);
2659 default:
2660 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2661 }
2662
2663 return 0;
2664
2665}
2666
Thomas Graf7deb2262007-08-22 13:57:39 -07002667static inline size_t xfrm_report_msgsize(void)
2668{
2669 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2670}
2671
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002672static int build_report(struct sk_buff *skb, u8 proto,
2673 struct xfrm_selector *sel, xfrm_address_t *addr)
2674{
2675 struct xfrm_user_report *ur;
2676 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002677
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002678 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2679 if (nlh == NULL)
2680 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002681
Thomas Graf7b67c852007-08-22 13:53:52 -07002682 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002683 ur->proto = proto;
2684 memcpy(&ur->sel, sel, sizeof(ur->sel));
2685
2686 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002687 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002688
Thomas Graf98250692007-08-22 12:47:26 -07002689 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002690
Thomas Grafc0144be2007-08-22 13:55:43 -07002691nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002692 nlmsg_cancel(skb, nlh);
2693 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002694}
2695
Alexey Dobriyandb983c12008-11-25 17:51:01 -08002696static int xfrm_send_report(struct net *net, u8 proto,
2697 struct xfrm_selector *sel, xfrm_address_t *addr)
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002698{
2699 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002700
Thomas Graf7deb2262007-08-22 13:57:39 -07002701 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002702 if (skb == NULL)
2703 return -ENOMEM;
2704
2705 if (build_report(skb, proto, sel, addr) < 0)
2706 BUG();
2707
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002708 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002709}
2710
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002711static inline size_t xfrm_mapping_msgsize(void)
2712{
2713 return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
2714}
2715
2716static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
2717 xfrm_address_t *new_saddr, __be16 new_sport)
2718{
2719 struct xfrm_user_mapping *um;
2720 struct nlmsghdr *nlh;
2721
2722 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
2723 if (nlh == NULL)
2724 return -EMSGSIZE;
2725
2726 um = nlmsg_data(nlh);
2727
2728 memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
2729 um->id.spi = x->id.spi;
2730 um->id.family = x->props.family;
2731 um->id.proto = x->id.proto;
2732 memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
2733 memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
2734 um->new_sport = new_sport;
2735 um->old_sport = x->encap->encap_sport;
2736 um->reqid = x->props.reqid;
2737
2738 return nlmsg_end(skb, nlh);
2739}
2740
2741static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
2742 __be16 sport)
2743{
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002744 struct net *net = xs_net(x);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002745 struct sk_buff *skb;
2746
2747 if (x->id.proto != IPPROTO_ESP)
2748 return -EINVAL;
2749
2750 if (!x->encap)
2751 return -EINVAL;
2752
2753 skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
2754 if (skb == NULL)
2755 return -ENOMEM;
2756
2757 if (build_mapping(skb, x, ipaddr, sport) < 0)
2758 BUG();
2759
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002760 return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002761}
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763static struct xfrm_mgr netlink_mgr = {
2764 .id = "netlink",
2765 .notify = xfrm_send_state_notify,
2766 .acquire = xfrm_send_acquire,
2767 .compile_policy = xfrm_compile_policy,
2768 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002769 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002770 .migrate = xfrm_send_migrate,
Martin Willi3a2dfbe2008-10-28 16:01:07 -07002771 .new_mapping = xfrm_send_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772};
2773
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002774static int __net_init xfrm_user_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Patrick McHardybe336902006-03-20 22:40:54 -08002776 struct sock *nlsk;
2777
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002778 nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002779 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002780 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return -ENOMEM;
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002782 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002783 rcu_assign_pointer(net->xfrm.nlsk, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785}
2786
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002787static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002788{
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002789 struct net *net;
2790 list_for_each_entry(net, net_exit_list, exit_list)
2791 rcu_assign_pointer(net->xfrm.nlsk, NULL);
2792 synchronize_net();
2793 list_for_each_entry(net, net_exit_list, exit_list)
2794 netlink_kernel_release(net->xfrm.nlsk_stash);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002795}
2796
2797static struct pernet_operations xfrm_user_net_ops = {
Eric W. Biedermand79d7922009-12-03 02:29:05 +00002798 .init = xfrm_user_net_init,
2799 .exit_batch = xfrm_user_net_exit,
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002800};
2801
2802static int __init xfrm_user_init(void)
2803{
2804 int rv;
2805
2806 printk(KERN_INFO "Initializing XFRM netlink socket\n");
2807
2808 rv = register_pernet_subsys(&xfrm_user_net_ops);
2809 if (rv < 0)
2810 return rv;
2811 rv = xfrm_register_km(&netlink_mgr);
2812 if (rv < 0)
2813 unregister_pernet_subsys(&xfrm_user_net_ops);
2814 return rv;
2815}
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817static void __exit xfrm_user_exit(void)
2818{
2819 xfrm_unregister_km(&netlink_mgr);
Alexey Dobriyana6483b72008-11-25 17:38:20 -08002820 unregister_pernet_subsys(&xfrm_user_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823module_init(xfrm_user_init);
2824module_exit(xfrm_user_exit);
2825MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002826MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002827