blob: 019d21de19b3222b8983fa65537e909715eb63e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* xfrm_user.c: User interface to configure xfrm engine.
2 *
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 *
5 * Changes:
6 * Mitsuru KANDA @USAGI
7 * Kazunori MIYAZAWA @USAGI
8 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
9 * IPv6 support
Trent Jaegerdf718372005-12-13 23:12:27 -080010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Herbert Xu9409f382006-08-06 19:49:12 +100013#include <linux/crypto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/socket.h>
19#include <linux/string.h>
20#include <linux/net.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pfkeyv2.h>
23#include <linux/ipsec.h>
24#include <linux/init.h>
25#include <linux/security.h>
26#include <net/sock.h>
27#include <net/xfrm.h>
Thomas Graf88fc2c82005-11-10 02:25:54 +010028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -070030#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
31#include <linux/in6.h>
32#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Herbert Xu1a6509d2008-01-28 19:37:29 -080034static inline int aead_len(struct xfrm_algo_aead *alg)
35{
36 return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
37}
38
Thomas Graf5424f322007-08-22 14:01:33 -070039static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Thomas Graf5424f322007-08-22 14:01:33 -070041 struct nlattr *rt = attrs[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct xfrm_algo *algp;
43
44 if (!rt)
45 return 0;
46
Thomas Graf5424f322007-08-22 14:01:33 -070047 algp = nla_data(rt);
Eric Dumazet0f99be02008-01-08 23:39:06 -080048 if (nla_len(rt) < xfrm_alg_len(algp))
Herbert Xu31c26852005-05-19 12:39:49 -070049 return -EINVAL;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (type) {
52 case XFRMA_ALG_AUTH:
53 if (!algp->alg_key_len &&
54 strcmp(algp->alg_name, "digest_null") != 0)
55 return -EINVAL;
56 break;
57
58 case XFRMA_ALG_CRYPT:
59 if (!algp->alg_key_len &&
60 strcmp(algp->alg_name, "cipher_null") != 0)
61 return -EINVAL;
62 break;
63
64 case XFRMA_ALG_COMP:
65 /* Zero length keys are legal. */
66 break;
67
68 default:
69 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
73 return 0;
74}
75
Herbert Xu1a6509d2008-01-28 19:37:29 -080076static int verify_aead(struct nlattr **attrs)
77{
78 struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
79 struct xfrm_algo_aead *algp;
80
81 if (!rt)
82 return 0;
83
84 algp = nla_data(rt);
85 if (nla_len(rt) < aead_len(algp))
86 return -EINVAL;
87
88 algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
89 return 0;
90}
91
Thomas Graf5424f322007-08-22 14:01:33 -070092static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070093 xfrm_address_t **addrp)
94{
Thomas Graf5424f322007-08-22 14:01:33 -070095 struct nlattr *rt = attrs[type];
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070096
Thomas Grafcf5cb792007-08-22 13:59:04 -070097 if (rt && addrp)
Thomas Graf5424f322007-08-22 14:01:33 -070098 *addrp = nla_data(rt);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -070099}
Trent Jaegerdf718372005-12-13 23:12:27 -0800100
Thomas Graf5424f322007-08-22 14:01:33 -0700101static inline int verify_sec_ctx_len(struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800102{
Thomas Graf5424f322007-08-22 14:01:33 -0700103 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800104 struct xfrm_user_sec_ctx *uctx;
Trent Jaegerdf718372005-12-13 23:12:27 -0800105
106 if (!rt)
107 return 0;
108
Thomas Graf5424f322007-08-22 14:01:33 -0700109 uctx = nla_data(rt);
Thomas Grafcf5cb792007-08-22 13:59:04 -0700110 if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
Trent Jaegerdf718372005-12-13 23:12:27 -0800111 return -EINVAL;
112
113 return 0;
114}
115
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int verify_newsa_info(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700118 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int err;
121
122 err = -EINVAL;
123 switch (p->family) {
124 case AF_INET:
125 break;
126
127 case AF_INET6:
128#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
129 break;
130#else
131 err = -EAFNOSUPPORT;
132 goto out;
133#endif
134
135 default:
136 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 err = -EINVAL;
140 switch (p->id.proto) {
141 case IPPROTO_AH:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700142 if (!attrs[XFRMA_ALG_AUTH] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800143 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700144 attrs[XFRMA_ALG_CRYPT] ||
145 attrs[XFRMA_ALG_COMP])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto out;
147 break;
148
149 case IPPROTO_ESP:
Herbert Xu1a6509d2008-01-28 19:37:29 -0800150 if (attrs[XFRMA_ALG_COMP])
151 goto out;
152 if (!attrs[XFRMA_ALG_AUTH] &&
153 !attrs[XFRMA_ALG_CRYPT] &&
154 !attrs[XFRMA_ALG_AEAD])
155 goto out;
156 if ((attrs[XFRMA_ALG_AUTH] ||
157 attrs[XFRMA_ALG_CRYPT]) &&
158 attrs[XFRMA_ALG_AEAD])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
160 break;
161
162 case IPPROTO_COMP:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700163 if (!attrs[XFRMA_ALG_COMP] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800164 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700165 attrs[XFRMA_ALG_AUTH] ||
166 attrs[XFRMA_ALG_CRYPT])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto out;
168 break;
169
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700170#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171 case IPPROTO_DSTOPTS:
172 case IPPROTO_ROUTING:
Thomas Graf35a7aa02007-08-22 14:00:40 -0700173 if (attrs[XFRMA_ALG_COMP] ||
174 attrs[XFRMA_ALG_AUTH] ||
Herbert Xu1a6509d2008-01-28 19:37:29 -0800175 attrs[XFRMA_ALG_AEAD] ||
Thomas Graf35a7aa02007-08-22 14:00:40 -0700176 attrs[XFRMA_ALG_CRYPT] ||
177 attrs[XFRMA_ENCAP] ||
178 attrs[XFRMA_SEC_CTX] ||
179 !attrs[XFRMA_COADDR])
Masahide NAKAMURAe23c7192006-08-23 20:33:28 -0700180 goto out;
181 break;
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 default:
185 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Herbert Xu1a6509d2008-01-28 19:37:29 -0800188 if ((err = verify_aead(attrs)))
189 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700190 if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700192 if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700194 if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
Thomas Graf35a7aa02007-08-22 14:00:40 -0700196 if ((err = verify_sec_ctx_len(attrs)))
Trent Jaegerdf718372005-12-13 23:12:27 -0800197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 err = -EINVAL;
200 switch (p->mode) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700201 case XFRM_MODE_TRANSPORT:
202 case XFRM_MODE_TUNNEL:
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700203 case XFRM_MODE_ROUTEOPTIMIZATION:
Diego Beltrami0a694522006-10-03 23:47:05 -0700204 case XFRM_MODE_BEET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206
207 default:
208 goto out;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 err = 0;
212
213out:
214 return err;
215}
216
217static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
218 struct xfrm_algo_desc *(*get_byname)(char *, int),
Thomas Graf5424f322007-08-22 14:01:33 -0700219 struct nlattr *rta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct xfrm_algo *p, *ualg;
222 struct xfrm_algo_desc *algo;
223
224 if (!rta)
225 return 0;
226
Thomas Graf5424f322007-08-22 14:01:33 -0700227 ualg = nla_data(rta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 algo = get_byname(ualg->alg_name, 1);
230 if (!algo)
231 return -ENOSYS;
232 *props = algo->desc.sadb_alg_id;
233
Eric Dumazet0f99be02008-01-08 23:39:06 -0800234 p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!p)
236 return -ENOMEM;
237
Herbert Xu04ff1262006-08-13 08:50:00 +1000238 strcpy(p->alg_name, algo->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *algpp = p;
240 return 0;
241}
242
Herbert Xu1a6509d2008-01-28 19:37:29 -0800243static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
244 struct nlattr *rta)
245{
246 struct xfrm_algo_aead *p, *ualg;
247 struct xfrm_algo_desc *algo;
248
249 if (!rta)
250 return 0;
251
252 ualg = nla_data(rta);
253
254 algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
255 if (!algo)
256 return -ENOSYS;
257 *props = algo->desc.sadb_alg_id;
258
259 p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
260 if (!p)
261 return -ENOMEM;
262
263 strcpy(p->alg_name, algo->name);
264 *algpp = p;
265 return 0;
266}
267
Joy Latten661697f2007-04-13 16:14:35 -0700268static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
Trent Jaegerdf718372005-12-13 23:12:27 -0800269{
Trent Jaegerdf718372005-12-13 23:12:27 -0800270 int len = 0;
271
272 if (xfrm_ctx) {
273 len += sizeof(struct xfrm_user_sec_ctx);
274 len += xfrm_ctx->ctx_len;
275 }
276 return len;
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
280{
281 memcpy(&x->id, &p->id, sizeof(x->id));
282 memcpy(&x->sel, &p->sel, sizeof(x->sel));
283 memcpy(&x->lft, &p->lft, sizeof(x->lft));
284 x->props.mode = p->mode;
285 x->props.replay_window = p->replay_window;
286 x->props.reqid = p->reqid;
287 x->props.family = p->family;
David S. Miller54489c142006-10-27 15:29:47 -0700288 memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 x->props.flags = p->flags;
Herbert Xu196b0032007-07-31 02:04:32 -0700290
Patrick McHardybcf0dda2008-04-09 15:08:24 -0700291 if (!x->sel.family)
Herbert Xu196b0032007-07-31 02:04:32 -0700292 x->sel.family = p->family;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800296/*
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
299 *
300*/
Thomas Graf5424f322007-08-22 14:01:33 -0700301static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800302{
Thomas Graf5424f322007-08-22 14:01:33 -0700303 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
304 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
305 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
306 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800307
308 if (rp) {
309 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700310 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800311 memcpy(&x->replay, replay, sizeof(*replay));
312 memcpy(&x->preplay, replay, sizeof(*replay));
313 }
314
315 if (lt) {
316 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700317 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800318 x->curlft.bytes = ltime->bytes;
319 x->curlft.packets = ltime->packets;
320 x->curlft.add_time = ltime->add_time;
321 x->curlft.use_time = ltime->use_time;
322 }
323
Thomas Grafcf5cb792007-08-22 13:59:04 -0700324 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700325 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800326
Thomas Grafcf5cb792007-08-22 13:59:04 -0700327 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700328 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700332 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int *errp)
334{
335 struct xfrm_state *x = xfrm_state_alloc();
336 int err = -ENOMEM;
337
338 if (!x)
339 goto error_no_put;
340
341 copy_from_user_state(x, p);
342
Herbert Xu1a6509d2008-01-28 19:37:29 -0800343 if ((err = attach_aead(&x->aead, &x->props.ealgo,
344 attrs[XFRMA_ALG_AEAD])))
345 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
347 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700348 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto error;
350 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
351 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700352 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto error;
354 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
355 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700356 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700358
359 if (attrs[XFRMA_ENCAP]) {
360 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
361 sizeof(*x->encap), GFP_KERNEL);
362 if (x->encap == NULL)
363 goto error;
364 }
365
366 if (attrs[XFRMA_COADDR]) {
367 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
368 sizeof(*x->coaddr), GFP_KERNEL);
369 if (x->coaddr == NULL)
370 goto error;
371 }
372
Herbert Xu72cb6962005-06-20 13:18:08 -0700373 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (err)
375 goto error;
376
Thomas Graffd211502007-09-06 03:28:08 -0700377 if (attrs[XFRMA_SEC_CTX] &&
378 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800379 goto error;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800382 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
383 /* sysctl_xfrm_aevent_etime is in 100ms units */
384 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
385 x->preplay.bitmap = 0;
386 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
387 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
388
389 /* override default values from above */
390
Thomas Graf5424f322007-08-22 14:01:33 -0700391 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return x;
394
395error:
396 x->km.state = XFRM_STATE_DEAD;
397 xfrm_state_put(x);
398error_no_put:
399 *errp = err;
400 return NULL;
401}
402
Christoph Hellwig22e70052007-01-02 15:22:30 -0800403static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700404 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Thomas Graf7b67c852007-08-22 13:53:52 -0700406 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct xfrm_state *x;
408 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700409 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Thomas Graf35a7aa02007-08-22 14:00:40 -0700411 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (err)
413 return err;
414
Thomas Graf35a7aa02007-08-22 14:00:40 -0700415 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!x)
417 return err;
418
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700419 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
421 err = xfrm_state_add(x);
422 else
423 err = xfrm_state_update(x);
424
Joy Lattenab5f5e82007-09-17 11:51:22 -0700425 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
426 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (err < 0) {
429 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800430 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700434 c.seq = nlh->nlmsg_seq;
435 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700436 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700437
438 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700439out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700440 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return err;
442}
443
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700444static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700445 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700446 int *errp)
447{
448 struct xfrm_state *x = NULL;
449 int err;
450
451 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
452 err = -ESRCH;
453 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
454 } else {
455 xfrm_address_t *saddr = NULL;
456
Thomas Graf35a7aa02007-08-22 14:00:40 -0700457 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700458 if (!saddr) {
459 err = -EINVAL;
460 goto out;
461 }
462
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800463 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700464 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
465 p->family);
466 }
467
468 out:
469 if (!x && errp)
470 *errp = err;
471 return x;
472}
473
Christoph Hellwig22e70052007-01-02 15:22:30 -0800474static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700475 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700478 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700479 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700480 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Thomas Graf35a7aa02007-08-22 14:00:40 -0700482 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David S. Miller6f68dc32006-06-08 23:58:52 -0700486 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700487 goto out;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700490 err = -EPERM;
491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700494 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600495
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700496 if (err < 0)
497 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700498
499 c.seq = nlh->nlmsg_seq;
500 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700501 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700502 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700504out:
Joy Lattenab5f5e82007-09-17 11:51:22 -0700505 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
506 NETLINK_CB(skb).sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700507 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
512{
513 memcpy(&p->id, &x->id, sizeof(p->id));
514 memcpy(&p->sel, &x->sel, sizeof(p->sel));
515 memcpy(&p->lft, &x->lft, sizeof(p->lft));
516 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
517 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700518 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 p->mode = x->props.mode;
520 p->replay_window = x->props.replay_window;
521 p->reqid = x->props.reqid;
522 p->family = x->props.family;
523 p->flags = x->props.flags;
524 p->seq = x->km.seq;
525}
526
527struct xfrm_dump_info {
528 struct sk_buff *in_skb;
529 struct sk_buff *out_skb;
530 u32 nlmsg_seq;
531 u16 nlmsg_flags;
532 int start_idx;
533 int this_idx;
534};
535
Thomas Grafc0144be2007-08-22 13:55:43 -0700536static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
537{
Thomas Grafc0144be2007-08-22 13:55:43 -0700538 struct xfrm_user_sec_ctx *uctx;
539 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700540 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700541
542 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
543 if (attr == NULL)
544 return -EMSGSIZE;
545
546 uctx = nla_data(attr);
547 uctx->exttype = XFRMA_SEC_CTX;
548 uctx->len = ctx_size;
549 uctx->ctx_doi = s->ctx_doi;
550 uctx->ctx_alg = s->ctx_alg;
551 uctx->ctx_len = s->ctx_len;
552 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
553
554 return 0;
555}
556
Herbert Xu68325d32007-10-09 13:30:57 -0700557/* Don't change this without updating xfrm_sa_len! */
558static int copy_to_user_state_extra(struct xfrm_state *x,
559 struct xfrm_usersa_info *p,
560 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 copy_to_user_state(x, p);
563
Herbert Xu050f0092007-10-09 13:31:47 -0700564 if (x->coaddr)
565 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
566
567 if (x->lastused)
568 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700569
Herbert Xu1a6509d2008-01-28 19:37:29 -0800570 if (x->aead)
571 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800573 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800575 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700577 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700580 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Thomas Grafc0144be2007-08-22 13:55:43 -0700582 if (x->security && copy_sec_ctx(x->security, skb) < 0)
583 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700584
Herbert Xu68325d32007-10-09 13:30:57 -0700585 return 0;
586
587nla_put_failure:
588 return -EMSGSIZE;
589}
590
591static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
592{
593 struct xfrm_dump_info *sp = ptr;
594 struct sk_buff *in_skb = sp->in_skb;
595 struct sk_buff *skb = sp->out_skb;
596 struct xfrm_usersa_info *p;
597 struct nlmsghdr *nlh;
598 int err;
599
600 if (sp->this_idx < sp->start_idx)
601 goto out;
602
603 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
604 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
605 if (nlh == NULL)
606 return -EMSGSIZE;
607
608 p = nlmsg_data(nlh);
609
610 err = copy_to_user_state_extra(x, p, skb);
611 if (err)
612 goto nla_put_failure;
613
Thomas Graf98250692007-08-22 12:47:26 -0700614 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615out:
616 sp->this_idx++;
617 return 0;
618
Thomas Grafc0144be2007-08-22 13:55:43 -0700619nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700620 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
625{
626 struct xfrm_dump_info info;
627
628 info.in_skb = cb->skb;
629 info.out_skb = skb;
630 info.nlmsg_seq = cb->nlh->nlmsg_seq;
631 info.nlmsg_flags = NLM_F_MULTI;
632 info.this_idx = 0;
633 info.start_idx = cb->args[0];
Masahide NAKAMURAdc00a522006-08-23 17:49:52 -0700634 (void) xfrm_state_walk(0, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 cb->args[0] = info.this_idx;
636
637 return skb->len;
638}
639
640static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
641 struct xfrm_state *x, u32 seq)
642{
643 struct xfrm_dump_info info;
644 struct sk_buff *skb;
645
Thomas Graf7deb2262007-08-22 13:57:39 -0700646 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!skb)
648 return ERR_PTR(-ENOMEM);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 info.in_skb = in_skb;
651 info.out_skb = skb;
652 info.nlmsg_seq = seq;
653 info.nlmsg_flags = 0;
654 info.this_idx = info.start_idx = 0;
655
656 if (dump_one_state(x, 0, &info)) {
657 kfree_skb(skb);
658 return NULL;
659 }
660
661 return skb;
662}
663
Thomas Graf7deb2262007-08-22 13:57:39 -0700664static inline size_t xfrm_spdinfo_msgsize(void)
665{
666 return NLMSG_ALIGN(4)
667 + nla_total_size(sizeof(struct xfrmu_spdinfo))
668 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
669}
670
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700671static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
672{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700673 struct xfrmk_spdinfo si;
674 struct xfrmu_spdinfo spc;
675 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700676 struct nlmsghdr *nlh;
677 u32 *f;
678
679 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
680 if (nlh == NULL) /* shouldnt really happen ... */
681 return -EMSGSIZE;
682
683 f = nlmsg_data(nlh);
684 *f = flags;
685 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700686 spc.incnt = si.incnt;
687 spc.outcnt = si.outcnt;
688 spc.fwdcnt = si.fwdcnt;
689 spc.inscnt = si.inscnt;
690 spc.outscnt = si.outscnt;
691 spc.fwdscnt = si.fwdscnt;
692 sph.spdhcnt = si.spdhcnt;
693 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700694
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700695 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
696 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700697
698 return nlmsg_end(skb, nlh);
699
700nla_put_failure:
701 nlmsg_cancel(skb, nlh);
702 return -EMSGSIZE;
703}
704
705static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700706 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700707{
708 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700709 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700710 u32 spid = NETLINK_CB(skb).pid;
711 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700712
Thomas Graf7deb2262007-08-22 13:57:39 -0700713 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700714 if (r_skb == NULL)
715 return -ENOMEM;
716
717 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
718 BUG();
719
720 return nlmsg_unicast(xfrm_nl, r_skb, spid);
721}
722
Thomas Graf7deb2262007-08-22 13:57:39 -0700723static inline size_t xfrm_sadinfo_msgsize(void)
724{
725 return NLMSG_ALIGN(4)
726 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
727 + nla_total_size(4); /* XFRMA_SAD_CNT */
728}
729
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700730static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
731{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700732 struct xfrmk_sadinfo si;
733 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700734 struct nlmsghdr *nlh;
735 u32 *f;
736
737 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
738 if (nlh == NULL) /* shouldnt really happen ... */
739 return -EMSGSIZE;
740
741 f = nlmsg_data(nlh);
742 *f = flags;
743 xfrm_sad_getinfo(&si);
744
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700745 sh.sadhmcnt = si.sadhmcnt;
746 sh.sadhcnt = si.sadhcnt;
747
748 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
749 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700750
751 return nlmsg_end(skb, nlh);
752
753nla_put_failure:
754 nlmsg_cancel(skb, nlh);
755 return -EMSGSIZE;
756}
757
758static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700759 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700760{
761 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700762 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700763 u32 spid = NETLINK_CB(skb).pid;
764 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700765
Thomas Graf7deb2262007-08-22 13:57:39 -0700766 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700767 if (r_skb == NULL)
768 return -ENOMEM;
769
770 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
771 BUG();
772
773 return nlmsg_unicast(xfrm_nl, r_skb, spid);
774}
775
Christoph Hellwig22e70052007-01-02 15:22:30 -0800776static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700777 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Thomas Graf7b67c852007-08-22 13:53:52 -0700779 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct xfrm_state *x;
781 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700782 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Thomas Graf35a7aa02007-08-22 14:00:40 -0700784 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (x == NULL)
786 goto out_noput;
787
788 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
789 if (IS_ERR(resp_skb)) {
790 err = PTR_ERR(resp_skb);
791 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700792 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 xfrm_state_put(x);
795out_noput:
796 return err;
797}
798
799static int verify_userspi_info(struct xfrm_userspi_info *p)
800{
801 switch (p->info.id.proto) {
802 case IPPROTO_AH:
803 case IPPROTO_ESP:
804 break;
805
806 case IPPROTO_COMP:
807 /* IPCOMP spi is 16-bits. */
808 if (p->max >= 0x10000)
809 return -EINVAL;
810 break;
811
812 default:
813 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (p->min > p->max)
817 return -EINVAL;
818
819 return 0;
820}
821
Christoph Hellwig22e70052007-01-02 15:22:30 -0800822static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700823 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct xfrm_state *x;
826 struct xfrm_userspi_info *p;
827 struct sk_buff *resp_skb;
828 xfrm_address_t *daddr;
829 int family;
830 int err;
831
Thomas Graf7b67c852007-08-22 13:53:52 -0700832 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 err = verify_userspi_info(p);
834 if (err)
835 goto out_noput;
836
837 family = p->info.family;
838 daddr = &p->info.id.daddr;
839
840 x = NULL;
841 if (p->info.seq) {
842 x = xfrm_find_acq_byseq(p->info.seq);
843 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
844 xfrm_state_put(x);
845 x = NULL;
846 }
847 }
848
849 if (!x)
850 x = xfrm_find_acq(p->info.mode, p->info.reqid,
851 p->info.id.proto, daddr,
852 &p->info.saddr, 1,
853 family);
854 err = -ENOENT;
855 if (x == NULL)
856 goto out_noput;
857
Herbert Xu658b2192007-10-09 13:29:52 -0700858 err = xfrm_alloc_spi(x, p->min, p->max);
859 if (err)
860 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Herbert Xu658b2192007-10-09 13:29:52 -0700862 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (IS_ERR(resp_skb)) {
864 err = PTR_ERR(resp_skb);
865 goto out;
866 }
867
Thomas Graf082a1ad2007-08-22 13:54:36 -0700868 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870out:
871 xfrm_state_put(x);
872out_noput:
873 return err;
874}
875
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800876static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 switch (dir) {
879 case XFRM_POLICY_IN:
880 case XFRM_POLICY_OUT:
881 case XFRM_POLICY_FWD:
882 break;
883
884 default:
885 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 return 0;
889}
890
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800891static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700892{
893 switch (type) {
894 case XFRM_POLICY_TYPE_MAIN:
895#ifdef CONFIG_XFRM_SUB_POLICY
896 case XFRM_POLICY_TYPE_SUB:
897#endif
898 break;
899
900 default:
901 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700902 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700903
904 return 0;
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
908{
909 switch (p->share) {
910 case XFRM_SHARE_ANY:
911 case XFRM_SHARE_SESSION:
912 case XFRM_SHARE_USER:
913 case XFRM_SHARE_UNIQUE:
914 break;
915
916 default:
917 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 switch (p->action) {
921 case XFRM_POLICY_ALLOW:
922 case XFRM_POLICY_BLOCK:
923 break;
924
925 default:
926 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 switch (p->sel.family) {
930 case AF_INET:
931 break;
932
933 case AF_INET6:
934#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
935 break;
936#else
937 return -EAFNOSUPPORT;
938#endif
939
940 default:
941 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return verify_policy_dir(p->dir);
945}
946
Thomas Graf5424f322007-08-22 14:01:33 -0700947static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800948{
Thomas Graf5424f322007-08-22 14:01:33 -0700949 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800950 struct xfrm_user_sec_ctx *uctx;
951
952 if (!rt)
953 return 0;
954
Thomas Graf5424f322007-08-22 14:01:33 -0700955 uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -0800956 return security_xfrm_policy_alloc(pol, uctx);
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
960 int nr)
961{
962 int i;
963
964 xp->xfrm_nr = nr;
965 for (i = 0; i < nr; i++, ut++) {
966 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
967
968 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
969 memcpy(&t->saddr, &ut->saddr,
970 sizeof(xfrm_address_t));
971 t->reqid = ut->reqid;
972 t->mode = ut->mode;
973 t->share = ut->share;
974 t->optional = ut->optional;
975 t->aalgos = ut->aalgos;
976 t->ealgos = ut->ealgos;
977 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800978 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800982static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
983{
984 int i;
985
986 if (nr > XFRM_MAX_DEPTH)
987 return -EINVAL;
988
989 for (i = 0; i < nr; i++) {
990 /* We never validated the ut->family value, so many
991 * applications simply leave it at zero. The check was
992 * never made and ut->family was ignored because all
993 * templates could be assumed to have the same family as
994 * the policy itself. Now that we will have ipv4-in-ipv6
995 * and ipv6-in-ipv4 tunnels, this is no longer true.
996 */
997 if (!ut[i].family)
998 ut[i].family = family;
999
1000 switch (ut[i].family) {
1001 case AF_INET:
1002 break;
1003#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1004 case AF_INET6:
1005 break;
1006#endif
1007 default:
1008 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001009 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001010 }
1011
1012 return 0;
1013}
1014
Thomas Graf5424f322007-08-22 14:01:33 -07001015static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Thomas Graf5424f322007-08-22 14:01:33 -07001017 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (!rt) {
1020 pol->xfrm_nr = 0;
1021 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001022 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1023 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001024 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001026 err = validate_tmpl(nr, utmpl, pol->family);
1027 if (err)
1028 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Thomas Graf5424f322007-08-22 14:01:33 -07001030 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 return 0;
1033}
1034
Thomas Graf5424f322007-08-22 14:01:33 -07001035static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001036{
Thomas Graf5424f322007-08-22 14:01:33 -07001037 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001038 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001039 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001040 int err;
1041
1042 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001043 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001044 type = upt->type;
1045 }
1046
1047 err = verify_policy_type(type);
1048 if (err)
1049 return err;
1050
1051 *tp = type;
1052 return 0;
1053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1056{
1057 xp->priority = p->priority;
1058 xp->index = p->index;
1059 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1060 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1061 xp->action = p->action;
1062 xp->flags = p->flags;
1063 xp->family = p->sel.family;
1064 /* XXX xp->share = p->share; */
1065}
1066
1067static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1068{
1069 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1070 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1071 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1072 p->priority = xp->priority;
1073 p->index = xp->index;
1074 p->sel.family = xp->family;
1075 p->dir = dir;
1076 p->action = xp->action;
1077 p->flags = xp->flags;
1078 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1079}
1080
Thomas Graf5424f322007-08-22 14:01:33 -07001081static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1084 int err;
1085
1086 if (!xp) {
1087 *errp = -ENOMEM;
1088 return NULL;
1089 }
1090
1091 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001092
Thomas Graf35a7aa02007-08-22 14:00:40 -07001093 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001094 if (err)
1095 goto error;
1096
Thomas Graf35a7aa02007-08-22 14:00:40 -07001097 if (!(err = copy_from_user_tmpl(xp, attrs)))
1098 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001099 if (err)
1100 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001103 error:
1104 *errp = err;
YOSHIFUJI Hideaki073a3712008-02-14 14:52:38 -08001105 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001106 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Christoph Hellwig22e70052007-01-02 15:22:30 -08001110static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001111 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Thomas Graf7b67c852007-08-22 13:53:52 -07001113 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001115 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 int err;
1117 int excl;
1118
1119 err = verify_newpolicy_info(p);
1120 if (err)
1121 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001122 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001123 if (err)
1124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Thomas Graf35a7aa02007-08-22 14:00:40 -07001126 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!xp)
1128 return err;
1129
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001130 /* shouldnt excl be based on nlh flags??
1131 * Aha! this is anti-netlink really i.e more pfkey derived
1132 * in netlink excl is a flag and you wouldnt need
1133 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1135 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001136 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1137 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (err) {
Trent Jaeger5f8ac642006-01-06 13:22:39 -08001140 security_xfrm_policy_free(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 kfree(xp);
1142 return err;
1143 }
1144
Herbert Xuf60f6b82005-06-18 22:44:37 -07001145 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001146 c.seq = nlh->nlmsg_seq;
1147 c.pid = nlh->nlmsg_pid;
1148 km_policy_notify(xp, p->dir, &c);
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 xfrm_pol_put(xp);
1151
1152 return 0;
1153}
1154
1155static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1156{
1157 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1158 int i;
1159
1160 if (xp->xfrm_nr == 0)
1161 return 0;
1162
1163 for (i = 0; i < xp->xfrm_nr; i++) {
1164 struct xfrm_user_tmpl *up = &vec[i];
1165 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1166
1167 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001168 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1170 up->reqid = kp->reqid;
1171 up->mode = kp->mode;
1172 up->share = kp->share;
1173 up->optional = kp->optional;
1174 up->aalgos = kp->aalgos;
1175 up->ealgos = kp->ealgos;
1176 up->calgos = kp->calgos;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Grafc0144be2007-08-22 13:55:43 -07001179 return nla_put(skb, XFRMA_TMPL,
1180 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001181}
1182
Serge Hallyn0d681622006-07-24 23:30:44 -07001183static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1184{
1185 if (x->security) {
1186 return copy_sec_ctx(x->security, skb);
1187 }
1188 return 0;
1189}
1190
1191static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1192{
1193 if (xp->security) {
1194 return copy_sec_ctx(xp->security, skb);
1195 }
1196 return 0;
1197}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001198static inline size_t userpolicy_type_attrsize(void)
1199{
1200#ifdef CONFIG_XFRM_SUB_POLICY
1201 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1202#else
1203 return 0;
1204#endif
1205}
Serge Hallyn0d681622006-07-24 23:30:44 -07001206
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001207#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001208static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001209{
Thomas Grafc0144be2007-08-22 13:55:43 -07001210 struct xfrm_userpolicy_type upt = {
1211 .type = type,
1212 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001213
Thomas Grafc0144be2007-08-22 13:55:43 -07001214 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001215}
1216
1217#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001218static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001219{
1220 return 0;
1221}
1222#endif
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1225{
1226 struct xfrm_dump_info *sp = ptr;
1227 struct xfrm_userpolicy_info *p;
1228 struct sk_buff *in_skb = sp->in_skb;
1229 struct sk_buff *skb = sp->out_skb;
1230 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (sp->this_idx < sp->start_idx)
1233 goto out;
1234
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001235 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1236 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1237 if (nlh == NULL)
1238 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Thomas Graf7b67c852007-08-22 13:53:52 -07001240 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 copy_to_user_policy(xp, p, dir);
1242 if (copy_to_user_tmpl(xp, skb) < 0)
1243 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001244 if (copy_to_user_sec_ctx(xp, skb))
1245 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001246 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001247 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Thomas Graf98250692007-08-22 12:47:26 -07001249 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250out:
1251 sp->this_idx++;
1252 return 0;
1253
1254nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001255 nlmsg_cancel(skb, nlh);
1256 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1260{
1261 struct xfrm_dump_info info;
1262
1263 info.in_skb = cb->skb;
1264 info.out_skb = skb;
1265 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1266 info.nlmsg_flags = NLM_F_MULTI;
1267 info.this_idx = 0;
1268 info.start_idx = cb->args[0];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001269 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
1270#ifdef CONFIG_XFRM_SUB_POLICY
1271 (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
1272#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 cb->args[0] = info.this_idx;
1274
1275 return skb->len;
1276}
1277
1278static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1279 struct xfrm_policy *xp,
1280 int dir, u32 seq)
1281{
1282 struct xfrm_dump_info info;
1283 struct sk_buff *skb;
1284
Thomas Graf7deb2262007-08-22 13:57:39 -07001285 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (!skb)
1287 return ERR_PTR(-ENOMEM);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 info.in_skb = in_skb;
1290 info.out_skb = skb;
1291 info.nlmsg_seq = seq;
1292 info.nlmsg_flags = 0;
1293 info.this_idx = info.start_idx = 0;
1294
1295 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1296 kfree_skb(skb);
1297 return NULL;
1298 }
1299
1300 return skb;
1301}
1302
Christoph Hellwig22e70052007-01-02 15:22:30 -08001303static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001304 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct xfrm_policy *xp;
1307 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001308 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001310 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int delete;
1312
Thomas Graf7b67c852007-08-22 13:53:52 -07001313 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1315
Thomas Graf35a7aa02007-08-22 14:00:40 -07001316 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001317 if (err)
1318 return err;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 err = verify_policy_dir(p->dir);
1321 if (err)
1322 return err;
1323
1324 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001325 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001326 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001327 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -08001328 struct xfrm_policy tmp;
1329
Thomas Graf35a7aa02007-08-22 14:00:40 -07001330 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001331 if (err)
1332 return err;
1333
1334 memset(&tmp, 0, sizeof(struct xfrm_policy));
1335 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001336 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001337
1338 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1339 return err;
1340 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001341 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1342 delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001343 security_xfrm_policy_free(&tmp);
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (xp == NULL)
1346 return -ENOENT;
1347
1348 if (!delete) {
1349 struct sk_buff *resp_skb;
1350
1351 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1352 if (IS_ERR(resp_skb)) {
1353 err = PTR_ERR(resp_skb);
1354 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001355 err = nlmsg_unicast(xfrm_nl, resp_skb,
1356 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001358 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001359 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1360 NETLINK_CB(skb).loginuid,
1361 NETLINK_CB(skb).sid);
David S. Miller13fcfbb2007-02-12 13:53:54 -08001362
1363 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001364 goto out;
David S. Miller13fcfbb2007-02-12 13:53:54 -08001365
Herbert Xue7443892005-06-18 22:44:18 -07001366 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001367 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001368 c.seq = nlh->nlmsg_seq;
1369 c.pid = nlh->nlmsg_pid;
1370 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001373out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001374 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return err;
1376}
1377
Christoph Hellwig22e70052007-01-02 15:22:30 -08001378static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001379 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001381 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001382 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001383 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001384 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Joy Latten161a09e2006-11-27 13:11:54 -06001386 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1387 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001388 err = xfrm_state_flush(p->proto, &audit_info);
1389 if (err)
1390 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001391 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001392 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001393 c.seq = nlh->nlmsg_seq;
1394 c.pid = nlh->nlmsg_pid;
1395 km_state_notify(NULL, &c);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return 0;
1398}
1399
Thomas Graf7deb2262007-08-22 13:57:39 -07001400static inline size_t xfrm_aevent_msgsize(void)
1401{
1402 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1403 + nla_total_size(sizeof(struct xfrm_replay_state))
1404 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1405 + nla_total_size(4) /* XFRM_AE_RTHR */
1406 + nla_total_size(4); /* XFRM_AE_ETHR */
1407}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001408
1409static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1410{
1411 struct xfrm_aevent_id *id;
1412 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001413
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001414 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1415 if (nlh == NULL)
1416 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001417
Thomas Graf7b67c852007-08-22 13:53:52 -07001418 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001419 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001420 id->sa_id.spi = x->id.spi;
1421 id->sa_id.family = x->props.family;
1422 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001423 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1424 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001425 id->flags = c->data.aevent;
1426
Thomas Grafc0144be2007-08-22 13:55:43 -07001427 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1428 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001429
Thomas Grafc0144be2007-08-22 13:55:43 -07001430 if (id->flags & XFRM_AE_RTHR)
1431 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001432
Thomas Grafc0144be2007-08-22 13:55:43 -07001433 if (id->flags & XFRM_AE_ETHR)
1434 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1435 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001436
Thomas Graf98250692007-08-22 12:47:26 -07001437 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438
Thomas Grafc0144be2007-08-22 13:55:43 -07001439nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001440 nlmsg_cancel(skb, nlh);
1441 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001442}
1443
Christoph Hellwig22e70052007-01-02 15:22:30 -08001444static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001445 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001446{
1447 struct xfrm_state *x;
1448 struct sk_buff *r_skb;
1449 int err;
1450 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001451 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001452 struct xfrm_usersa_id *id = &p->sa_id;
1453
Thomas Graf7deb2262007-08-22 13:57:39 -07001454 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001455 if (r_skb == NULL)
1456 return -ENOMEM;
1457
1458 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1459 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001460 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001461 return -ESRCH;
1462 }
1463
1464 /*
1465 * XXX: is this lock really needed - none of the other
1466 * gets lock (the concern is things getting updated
1467 * while we are still reading) - jhs
1468 */
1469 spin_lock_bh(&x->lock);
1470 c.data.aevent = p->flags;
1471 c.seq = nlh->nlmsg_seq;
1472 c.pid = nlh->nlmsg_pid;
1473
1474 if (build_aevent(r_skb, x, &c) < 0)
1475 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001476 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001477 spin_unlock_bh(&x->lock);
1478 xfrm_state_put(x);
1479 return err;
1480}
1481
Christoph Hellwig22e70052007-01-02 15:22:30 -08001482static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001483 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001484{
1485 struct xfrm_state *x;
1486 struct km_event c;
1487 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001488 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001489 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1490 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001491
1492 if (!lt && !rp)
1493 return err;
1494
1495 /* pedantic mode - thou shalt sayeth replaceth */
1496 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1497 return err;
1498
1499 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1500 if (x == NULL)
1501 return -ESRCH;
1502
1503 if (x->km.state != XFRM_STATE_VALID)
1504 goto out;
1505
1506 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001507 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001508 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001509
1510 c.event = nlh->nlmsg_type;
1511 c.seq = nlh->nlmsg_seq;
1512 c.pid = nlh->nlmsg_pid;
1513 c.data.aevent = XFRM_AE_CU;
1514 km_state_notify(x, &c);
1515 err = 0;
1516out:
1517 xfrm_state_put(x);
1518 return err;
1519}
1520
Christoph Hellwig22e70052007-01-02 15:22:30 -08001521static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001522 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001524 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001525 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001526 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001527 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001528
Thomas Graf35a7aa02007-08-22 14:00:40 -07001529 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001530 if (err)
1531 return err;
1532
Joy Latten161a09e2006-11-27 13:11:54 -06001533 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1534 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001535 err = xfrm_policy_flush(type, &audit_info);
1536 if (err)
1537 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001538 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001539 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001540 c.seq = nlh->nlmsg_seq;
1541 c.pid = nlh->nlmsg_pid;
1542 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544}
1545
Christoph Hellwig22e70052007-01-02 15:22:30 -08001546static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001547 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001548{
1549 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001550 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001551 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001552 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001553 int err = -ENOENT;
1554
Thomas Graf35a7aa02007-08-22 14:00:40 -07001555 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001556 if (err)
1557 return err;
1558
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001559 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001560 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001561 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001562 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001563 struct xfrm_policy tmp;
1564
Thomas Graf35a7aa02007-08-22 14:00:40 -07001565 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001566 if (err)
1567 return err;
1568
1569 memset(&tmp, 0, sizeof(struct xfrm_policy));
1570 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001571 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001572
1573 if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
1574 return err;
1575 }
Eric Parisef41aaa2007-03-07 15:37:58 -08001576 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
1577 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001578 security_xfrm_policy_free(&tmp);
1579 }
1580
1581 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001582 return -ENOENT;
1583 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001584 if (xp->dead) {
1585 read_unlock(&xp->lock);
1586 goto out;
1587 }
1588
1589 read_unlock(&xp->lock);
1590 err = 0;
1591 if (up->hard) {
1592 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001593 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1594 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001595
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001596 } else {
1597 // reset the timers here?
1598 printk("Dont know what to do with soft policy expire\n");
1599 }
1600 km_policy_expired(xp, p->dir, up->hard, current->pid);
1601
1602out:
1603 xfrm_pol_put(xp);
1604 return err;
1605}
1606
Christoph Hellwig22e70052007-01-02 15:22:30 -08001607static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001608 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001609{
1610 struct xfrm_state *x;
1611 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001612 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001613 struct xfrm_usersa_info *p = &ue->state;
1614
1615 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001616
David S. Miller3a765aa2007-02-26 14:52:21 -08001617 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001618 if (x == NULL)
1619 return err;
1620
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001621 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001622 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001623 if (x->km.state != XFRM_STATE_VALID)
1624 goto out;
1625 km_state_expired(x, ue->hard, current->pid);
1626
Joy Latten161a09e2006-11-27 13:11:54 -06001627 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001628 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001629 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1630 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001631 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001632 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001633out:
1634 spin_unlock_bh(&x->lock);
1635 xfrm_state_put(x);
1636 return err;
1637}
1638
Christoph Hellwig22e70052007-01-02 15:22:30 -08001639static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001640 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001641{
1642 struct xfrm_policy *xp;
1643 struct xfrm_user_tmpl *ut;
1644 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001645 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001646
Thomas Graf7b67c852007-08-22 13:53:52 -07001647 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001648 struct xfrm_state *x = xfrm_state_alloc();
1649 int err = -ENOMEM;
1650
1651 if (!x)
1652 return err;
1653
1654 err = verify_newpolicy_info(&ua->policy);
1655 if (err) {
1656 printk("BAD policy passed\n");
1657 kfree(x);
1658 return err;
1659 }
1660
1661 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001662 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001663 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001664 kfree(x);
1665 return err;
1666 }
1667
1668 memcpy(&x->id, &ua->id, sizeof(ua->id));
1669 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1670 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1671
Thomas Graf5424f322007-08-22 14:01:33 -07001672 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001673 /* extract the templates and for each call km_key */
1674 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1675 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1676 memcpy(&x->id, &t->id, sizeof(x->id));
1677 x->props.mode = t->mode;
1678 x->props.reqid = t->reqid;
1679 x->props.family = ut->family;
1680 t->aalgos = ua->aalgos;
1681 t->ealgos = ua->ealgos;
1682 t->calgos = ua->calgos;
1683 err = km_query(x, t, xp);
1684
1685 }
1686
1687 kfree(x);
1688 kfree(xp);
1689
1690 return 0;
1691}
1692
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001693#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001694static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001695 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001696{
Thomas Graf5424f322007-08-22 14:01:33 -07001697 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001698 struct xfrm_user_migrate *um;
1699 int i, num_migrate;
1700
Thomas Graf5424f322007-08-22 14:01:33 -07001701 um = nla_data(rt);
1702 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001703
1704 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1705 return -EINVAL;
1706
1707 for (i = 0; i < num_migrate; i++, um++, ma++) {
1708 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1709 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1710 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1711 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1712
1713 ma->proto = um->proto;
1714 ma->mode = um->mode;
1715 ma->reqid = um->reqid;
1716
1717 ma->old_family = um->old_family;
1718 ma->new_family = um->new_family;
1719 }
1720
1721 *num = i;
1722 return 0;
1723}
1724
1725static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001726 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001727{
Thomas Graf7b67c852007-08-22 13:53:52 -07001728 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001729 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1730 u8 type;
1731 int err;
1732 int n = 0;
1733
Thomas Graf35a7aa02007-08-22 14:00:40 -07001734 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001735 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001736
Thomas Graf5424f322007-08-22 14:01:33 -07001737 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001738 if (err)
1739 return err;
1740
1741 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001742 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001743 if (err)
1744 return err;
1745
1746 if (!n)
1747 return 0;
1748
1749 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1750
1751 return 0;
1752}
1753#else
1754static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001755 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001756{
1757 return -ENOPROTOOPT;
1758}
1759#endif
1760
1761#ifdef CONFIG_XFRM_MIGRATE
1762static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1763{
1764 struct xfrm_user_migrate um;
1765
1766 memset(&um, 0, sizeof(um));
1767 um.proto = m->proto;
1768 um.mode = m->mode;
1769 um.reqid = m->reqid;
1770 um.old_family = m->old_family;
1771 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1772 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1773 um.new_family = m->new_family;
1774 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1775 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1776
Thomas Grafc0144be2007-08-22 13:55:43 -07001777 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001778}
1779
Thomas Graf7deb2262007-08-22 13:57:39 -07001780static inline size_t xfrm_migrate_msgsize(int num_migrate)
1781{
1782 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1783 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1784 + userpolicy_type_attrsize();
1785}
1786
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001787static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1788 int num_migrate, struct xfrm_selector *sel,
1789 u8 dir, u8 type)
1790{
1791 struct xfrm_migrate *mp;
1792 struct xfrm_userpolicy_id *pol_id;
1793 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001794 int i;
1795
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001796 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1797 if (nlh == NULL)
1798 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001799
Thomas Graf7b67c852007-08-22 13:53:52 -07001800 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001801 /* copy data from selector, dir, and type to the pol_id */
1802 memset(pol_id, 0, sizeof(*pol_id));
1803 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1804 pol_id->dir = dir;
1805
1806 if (copy_to_user_policy_type(type, skb) < 0)
1807 goto nlmsg_failure;
1808
1809 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1810 if (copy_to_user_migrate(mp, skb) < 0)
1811 goto nlmsg_failure;
1812 }
1813
Thomas Graf98250692007-08-22 12:47:26 -07001814 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001815nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001816 nlmsg_cancel(skb, nlh);
1817 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001818}
1819
1820static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1821 struct xfrm_migrate *m, int num_migrate)
1822{
1823 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001824
Thomas Graf7deb2262007-08-22 13:57:39 -07001825 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001826 if (skb == NULL)
1827 return -ENOMEM;
1828
1829 /* build migrate */
1830 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1831 BUG();
1832
Thomas Graf082a1ad2007-08-22 13:54:36 -07001833 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001834}
1835#else
1836static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1837 struct xfrm_migrate *m, int num_migrate)
1838{
1839 return -ENOPROTOOPT;
1840}
1841#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001842
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001843#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001844
1845static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1846 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1847 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1848 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1849 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1850 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1851 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1852 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001853 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001854 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001855 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1856 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001857 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001858 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001859 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001860 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1861 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001862 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001863 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001864 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1865 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866};
1867
Thomas Graf492b5582005-05-03 14:26:40 -07001868#undef XMSGSIZE
1869
Thomas Grafcf5cb792007-08-22 13:59:04 -07001870static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001871 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001872 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1873 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1874 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1875 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1876 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1877 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1878 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1879 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1880 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1881 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1882 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1883 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1884 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1885 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1886};
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001889 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int (*dump)(struct sk_buff *, struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001891} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1892 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1893 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1894 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
1895 .dump = xfrm_dump_sa },
1896 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1897 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1898 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
1899 .dump = xfrm_dump_policy },
1900 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001901 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001902 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001903 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1904 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001905 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001906 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1907 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001908 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1909 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001910 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001911 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001912 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913};
1914
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001915static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001917 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001919 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001923 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 type -= XFRM_MSG_BASE;
1926 link = &xfrm_dispatch[type];
1927
1928 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001929 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1930 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Thomas Graf492b5582005-05-03 14:26:40 -07001932 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1933 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1934 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001936 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Thomas Grafc702e802007-03-22 23:30:55 -07001938 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940
Thomas Graf35a7aa02007-08-22 14:00:40 -07001941 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001942 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001943 if (err < 0)
1944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001947 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Thomas Graf5424f322007-08-22 14:01:33 -07001949 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001952static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001954 mutex_lock(&xfrm_cfg_mutex);
1955 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1956 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
Thomas Graf7deb2262007-08-22 13:57:39 -07001959static inline size_t xfrm_expire_msgsize(void)
1960{
1961 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1962}
1963
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001964static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct xfrm_user_expire *ue;
1967 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001969 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1970 if (nlh == NULL)
1971 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Thomas Graf7b67c852007-08-22 13:53:52 -07001973 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001975 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Thomas Graf98250692007-08-22 12:47:26 -07001977 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001980static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct sk_buff *skb;
1983
Thomas Graf7deb2262007-08-22 13:57:39 -07001984 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (skb == NULL)
1986 return -ENOMEM;
1987
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001988 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 BUG();
1990
Thomas Graf082a1ad2007-08-22 13:54:36 -07001991 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001994static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
1995{
1996 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001997
Thomas Graf7deb2262007-08-22 13:57:39 -07001998 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001999 if (skb == NULL)
2000 return -ENOMEM;
2001
2002 if (build_aevent(skb, x, c) < 0)
2003 BUG();
2004
Thomas Graf082a1ad2007-08-22 13:54:36 -07002005 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002006}
2007
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002008static int xfrm_notify_sa_flush(struct km_event *c)
2009{
2010 struct xfrm_usersa_flush *p;
2011 struct nlmsghdr *nlh;
2012 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002013 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002014
Thomas Graf7deb2262007-08-22 13:57:39 -07002015 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002016 if (skb == NULL)
2017 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002018
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002019 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2020 if (nlh == NULL) {
2021 kfree_skb(skb);
2022 return -EMSGSIZE;
2023 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002024
Thomas Graf7b67c852007-08-22 13:53:52 -07002025 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002026 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002027
Thomas Graf98250692007-08-22 12:47:26 -07002028 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002029
Thomas Graf082a1ad2007-08-22 13:54:36 -07002030 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002031}
2032
Thomas Graf7deb2262007-08-22 13:57:39 -07002033static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002034{
Thomas Graf7deb2262007-08-22 13:57:39 -07002035 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002036 if (x->aead)
2037 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002038 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002039 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002041 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002042 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002043 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002044 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002045 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002046 if (x->security)
2047 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2048 x->security->ctx_len);
2049 if (x->coaddr)
2050 l += nla_total_size(sizeof(*x->coaddr));
2051
Herbert Xud26f3982007-11-13 21:47:08 -08002052 /* Must count x->lastused as it may become non-zero behind our back. */
2053 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002054
2055 return l;
2056}
2057
2058static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2059{
2060 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002061 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062 struct nlmsghdr *nlh;
2063 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002064 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002065 int headlen;
2066
2067 headlen = sizeof(*p);
2068 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002069 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002070 headlen = sizeof(*id);
2071 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002072 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002073
Thomas Graf7deb2262007-08-22 13:57:39 -07002074 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075 if (skb == NULL)
2076 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002077
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002078 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2079 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002080 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002081
Thomas Graf7b67c852007-08-22 13:53:52 -07002082 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002083 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002084 struct nlattr *attr;
2085
Thomas Graf7b67c852007-08-22 13:53:52 -07002086 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002087 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2088 id->spi = x->id.spi;
2089 id->family = x->props.family;
2090 id->proto = x->id.proto;
2091
Thomas Grafc0144be2007-08-22 13:55:43 -07002092 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2093 if (attr == NULL)
2094 goto nla_put_failure;
2095
2096 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002097 }
2098
Herbert Xu68325d32007-10-09 13:30:57 -07002099 if (copy_to_user_state_extra(x, p, skb))
2100 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002101
Thomas Graf98250692007-08-22 12:47:26 -07002102 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002103
Thomas Graf082a1ad2007-08-22 13:54:36 -07002104 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002105
Thomas Grafc0144be2007-08-22 13:55:43 -07002106nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002107 /* Somebody screwed up with xfrm_sa_len! */
2108 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002109 kfree_skb(skb);
2110 return -1;
2111}
2112
2113static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2114{
2115
2116 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002117 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002118 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002119 case XFRM_MSG_NEWAE:
2120 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002121 case XFRM_MSG_DELSA:
2122 case XFRM_MSG_UPDSA:
2123 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002124 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002125 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002126 return xfrm_notify_sa_flush(c);
2127 default:
2128 printk("xfrm_user: Unknown SA event %d\n", c->event);
2129 break;
2130 }
2131
2132 return 0;
2133
2134}
2135
Thomas Graf7deb2262007-08-22 13:57:39 -07002136static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2137 struct xfrm_policy *xp)
2138{
2139 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2140 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2141 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2142 + userpolicy_type_attrsize();
2143}
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2146 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2147 int dir)
2148{
2149 struct xfrm_user_acquire *ua;
2150 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 __u32 seq = xfrm_get_acqseq();
2152
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002153 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2154 if (nlh == NULL)
2155 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Thomas Graf7b67c852007-08-22 13:53:52 -07002157 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 memcpy(&ua->id, &x->id, sizeof(ua->id));
2159 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2160 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2161 copy_to_user_policy(xp, &ua->policy, dir);
2162 ua->aalgos = xt->aalgos;
2163 ua->ealgos = xt->ealgos;
2164 ua->calgos = xt->calgos;
2165 ua->seq = x->km.seq = seq;
2166
2167 if (copy_to_user_tmpl(xp, skb) < 0)
2168 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002169 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002170 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002171 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002172 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Thomas Graf98250692007-08-22 12:47:26 -07002174 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002177 nlmsg_cancel(skb, nlh);
2178 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2182 struct xfrm_policy *xp, int dir)
2183{
2184 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Thomas Graf7deb2262007-08-22 13:57:39 -07002186 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 if (skb == NULL)
2188 return -ENOMEM;
2189
2190 if (build_acquire(skb, x, xt, xp, dir) < 0)
2191 BUG();
2192
Thomas Graf082a1ad2007-08-22 13:54:36 -07002193 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196/* User gives us xfrm_user_policy_info followed by an array of 0
2197 * or more templates.
2198 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002199static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 u8 *data, int len, int *dir)
2201{
2202 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2203 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2204 struct xfrm_policy *xp;
2205 int nr;
2206
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002207 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 case AF_INET:
2209 if (opt != IP_XFRM_POLICY) {
2210 *dir = -EOPNOTSUPP;
2211 return NULL;
2212 }
2213 break;
2214#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2215 case AF_INET6:
2216 if (opt != IPV6_XFRM_POLICY) {
2217 *dir = -EOPNOTSUPP;
2218 return NULL;
2219 }
2220 break;
2221#endif
2222 default:
2223 *dir = -EINVAL;
2224 return NULL;
2225 }
2226
2227 *dir = -EINVAL;
2228
2229 if (len < sizeof(*p) ||
2230 verify_newpolicy_info(p))
2231 return NULL;
2232
2233 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002234 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return NULL;
2236
Herbert Xua4f1bac2005-07-26 15:43:17 -07002237 if (p->dir > XFRM_POLICY_OUT)
2238 return NULL;
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 xp = xfrm_policy_alloc(GFP_KERNEL);
2241 if (xp == NULL) {
2242 *dir = -ENOBUFS;
2243 return NULL;
2244 }
2245
2246 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002247 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 copy_templates(xp, ut, nr);
2249
2250 *dir = p->dir;
2251
2252 return xp;
2253}
2254
Thomas Graf7deb2262007-08-22 13:57:39 -07002255static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2256{
2257 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2258 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2259 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2260 + userpolicy_type_attrsize();
2261}
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002264 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 struct xfrm_user_polexpire *upe;
2267 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002268 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002270 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2271 if (nlh == NULL)
2272 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Thomas Graf7b67c852007-08-22 13:53:52 -07002274 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 copy_to_user_policy(xp, &upe->pol, dir);
2276 if (copy_to_user_tmpl(xp, skb) < 0)
2277 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002278 if (copy_to_user_sec_ctx(xp, skb))
2279 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002280 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002281 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 upe->hard = !!hard;
2283
Thomas Graf98250692007-08-22 12:47:26 -07002284 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002287 nlmsg_cancel(skb, nlh);
2288 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002291static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Thomas Graf7deb2262007-08-22 13:57:39 -07002295 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (skb == NULL)
2297 return -ENOMEM;
2298
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002299 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 BUG();
2301
Thomas Graf082a1ad2007-08-22 13:54:36 -07002302 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002305static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2306{
2307 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002308 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002309 struct nlmsghdr *nlh;
2310 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002311 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002312 int headlen;
2313
2314 headlen = sizeof(*p);
2315 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002316 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002317 headlen = sizeof(*id);
2318 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002319 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002320 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321
Thomas Graf7deb2262007-08-22 13:57:39 -07002322 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002323 if (skb == NULL)
2324 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002326 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2327 if (nlh == NULL)
2328 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002329
Thomas Graf7b67c852007-08-22 13:53:52 -07002330 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002331 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002332 struct nlattr *attr;
2333
Thomas Graf7b67c852007-08-22 13:53:52 -07002334 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002335 memset(id, 0, sizeof(*id));
2336 id->dir = dir;
2337 if (c->data.byid)
2338 id->index = xp->index;
2339 else
2340 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2341
Thomas Grafc0144be2007-08-22 13:55:43 -07002342 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2343 if (attr == NULL)
2344 goto nlmsg_failure;
2345
2346 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002347 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002348
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002349 copy_to_user_policy(xp, p, dir);
2350 if (copy_to_user_tmpl(xp, skb) < 0)
2351 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002352 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002353 goto nlmsg_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
Thomas Graf082a1ad2007-08-22 13:54:36 -07002357 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358
2359nlmsg_failure:
2360 kfree_skb(skb);
2361 return -1;
2362}
2363
2364static int xfrm_notify_policy_flush(struct km_event *c)
2365{
2366 struct nlmsghdr *nlh;
2367 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002368
Thomas Graf7deb2262007-08-22 13:57:39 -07002369 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370 if (skb == NULL)
2371 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002373 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2374 if (nlh == NULL)
2375 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002376 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2377 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002378
Thomas Graf98250692007-08-22 12:47:26 -07002379 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002380
Thomas Graf082a1ad2007-08-22 13:54:36 -07002381 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002382
2383nlmsg_failure:
2384 kfree_skb(skb);
2385 return -1;
2386}
2387
2388static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2389{
2390
2391 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002392 case XFRM_MSG_NEWPOLICY:
2393 case XFRM_MSG_UPDPOLICY:
2394 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002395 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002396 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002397 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002398 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002399 return xfrm_exp_policy_notify(xp, dir, c);
2400 default:
2401 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2402 }
2403
2404 return 0;
2405
2406}
2407
Thomas Graf7deb2262007-08-22 13:57:39 -07002408static inline size_t xfrm_report_msgsize(void)
2409{
2410 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2411}
2412
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002413static int build_report(struct sk_buff *skb, u8 proto,
2414 struct xfrm_selector *sel, xfrm_address_t *addr)
2415{
2416 struct xfrm_user_report *ur;
2417 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002418
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002419 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2420 if (nlh == NULL)
2421 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002422
Thomas Graf7b67c852007-08-22 13:53:52 -07002423 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002424 ur->proto = proto;
2425 memcpy(&ur->sel, sel, sizeof(ur->sel));
2426
2427 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002428 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002429
Thomas Graf98250692007-08-22 12:47:26 -07002430 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002431
Thomas Grafc0144be2007-08-22 13:55:43 -07002432nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002433 nlmsg_cancel(skb, nlh);
2434 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002435}
2436
2437static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2438 xfrm_address_t *addr)
2439{
2440 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002441
Thomas Graf7deb2262007-08-22 13:57:39 -07002442 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002443 if (skb == NULL)
2444 return -ENOMEM;
2445
2446 if (build_report(skb, proto, sel, addr) < 0)
2447 BUG();
2448
Thomas Graf082a1ad2007-08-22 13:54:36 -07002449 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452static struct xfrm_mgr netlink_mgr = {
2453 .id = "netlink",
2454 .notify = xfrm_send_state_notify,
2455 .acquire = xfrm_send_acquire,
2456 .compile_policy = xfrm_compile_policy,
2457 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002458 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002459 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460};
2461
2462static int __init xfrm_user_init(void)
2463{
Patrick McHardybe336902006-03-20 22:40:54 -08002464 struct sock *nlsk;
2465
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002466 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002468 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002469 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002470 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002472 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 xfrm_register_km(&netlink_mgr);
2475
2476 return 0;
2477}
2478
2479static void __exit xfrm_user_exit(void)
2480{
Patrick McHardybe336902006-03-20 22:40:54 -08002481 struct sock *nlsk = xfrm_nl;
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002484 rcu_assign_pointer(xfrm_nl, NULL);
2485 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002486 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
2489module_init(xfrm_user_init);
2490module_exit(xfrm_user_exit);
2491MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002492MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002493