blob: a1b0fbe3ea351f6e7e953078ffd90d29dd29e196 [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;
Eric Paris25323862008-04-18 10:09:25 -0400410 uid_t loginuid = NETLINK_CB(skb).loginuid;
411 u32 sessionid = NETLINK_CB(skb).sessionid;
412 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Thomas Graf35a7aa02007-08-22 14:00:40 -0700414 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (err)
416 return err;
417
Thomas Graf35a7aa02007-08-22 14:00:40 -0700418 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!x)
420 return err;
421
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700422 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
424 err = xfrm_state_add(x);
425 else
426 err = xfrm_state_update(x);
427
Eric Paris25323862008-04-18 10:09:25 -0400428 xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (err < 0) {
431 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800432 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700436 c.seq = nlh->nlmsg_seq;
437 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700438 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700439
440 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700441out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700442 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return err;
444}
445
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700446static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700447 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700448 int *errp)
449{
450 struct xfrm_state *x = NULL;
451 int err;
452
453 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
454 err = -ESRCH;
455 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
456 } else {
457 xfrm_address_t *saddr = NULL;
458
Thomas Graf35a7aa02007-08-22 14:00:40 -0700459 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700460 if (!saddr) {
461 err = -EINVAL;
462 goto out;
463 }
464
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800465 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700466 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
467 p->family);
468 }
469
470 out:
471 if (!x && errp)
472 *errp = err;
473 return x;
474}
475
Christoph Hellwig22e70052007-01-02 15:22:30 -0800476static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700477 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700480 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700481 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700482 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Eric Paris25323862008-04-18 10:09:25 -0400483 uid_t loginuid = NETLINK_CB(skb).loginuid;
484 u32 sessionid = NETLINK_CB(skb).sessionid;
485 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Thomas Graf35a7aa02007-08-22 14:00:40 -0700487 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David S. Miller6f68dc32006-06-08 23:58:52 -0700491 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700492 goto out;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700495 err = -EPERM;
496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700499 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600500
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700501 if (err < 0)
502 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700503
504 c.seq = nlh->nlmsg_seq;
505 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700506 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700507 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700509out:
Eric Paris25323862008-04-18 10:09:25 -0400510 xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700511 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700512 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
516{
517 memcpy(&p->id, &x->id, sizeof(p->id));
518 memcpy(&p->sel, &x->sel, sizeof(p->sel));
519 memcpy(&p->lft, &x->lft, sizeof(p->lft));
520 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
521 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700522 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 p->mode = x->props.mode;
524 p->replay_window = x->props.replay_window;
525 p->reqid = x->props.reqid;
526 p->family = x->props.family;
527 p->flags = x->props.flags;
528 p->seq = x->km.seq;
529}
530
531struct xfrm_dump_info {
532 struct sk_buff *in_skb;
533 struct sk_buff *out_skb;
534 u32 nlmsg_seq;
535 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536};
537
Thomas Grafc0144be2007-08-22 13:55:43 -0700538static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
539{
Thomas Grafc0144be2007-08-22 13:55:43 -0700540 struct xfrm_user_sec_ctx *uctx;
541 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700542 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700543
544 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
545 if (attr == NULL)
546 return -EMSGSIZE;
547
548 uctx = nla_data(attr);
549 uctx->exttype = XFRMA_SEC_CTX;
550 uctx->len = ctx_size;
551 uctx->ctx_doi = s->ctx_doi;
552 uctx->ctx_alg = s->ctx_alg;
553 uctx->ctx_len = s->ctx_len;
554 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
555
556 return 0;
557}
558
Herbert Xu68325d32007-10-09 13:30:57 -0700559/* Don't change this without updating xfrm_sa_len! */
560static int copy_to_user_state_extra(struct xfrm_state *x,
561 struct xfrm_usersa_info *p,
562 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 copy_to_user_state(x, p);
565
Herbert Xu050f0092007-10-09 13:31:47 -0700566 if (x->coaddr)
567 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
568
569 if (x->lastused)
570 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700571
Herbert Xu1a6509d2008-01-28 19:37:29 -0800572 if (x->aead)
573 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800575 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800577 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700579 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700582 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Thomas Grafc0144be2007-08-22 13:55:43 -0700584 if (x->security && copy_sec_ctx(x->security, skb) < 0)
585 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700586
Herbert Xu68325d32007-10-09 13:30:57 -0700587 return 0;
588
589nla_put_failure:
590 return -EMSGSIZE;
591}
592
593static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
594{
595 struct xfrm_dump_info *sp = ptr;
596 struct sk_buff *in_skb = sp->in_skb;
597 struct sk_buff *skb = sp->out_skb;
598 struct xfrm_usersa_info *p;
599 struct nlmsghdr *nlh;
600 int err;
601
Herbert Xu68325d32007-10-09 13:30:57 -0700602 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
603 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
604 if (nlh == NULL)
605 return -EMSGSIZE;
606
607 p = nlmsg_data(nlh);
608
609 err = copy_to_user_state_extra(x, p, skb);
610 if (err)
611 goto nla_put_failure;
612
Thomas Graf98250692007-08-22 12:47:26 -0700613 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
615
Thomas Grafc0144be2007-08-22 13:55:43 -0700616nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700617 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Timo Teras4c563f72008-02-28 21:31:08 -0800621static int xfrm_dump_sa_done(struct netlink_callback *cb)
622{
623 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
624 xfrm_state_walk_done(walk);
625 return 0;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
629{
Timo Teras4c563f72008-02-28 21:31:08 -0800630 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct xfrm_dump_info info;
632
Timo Teras4c563f72008-02-28 21:31:08 -0800633 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
634 sizeof(cb->args) - sizeof(cb->args[0]));
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 info.in_skb = cb->skb;
637 info.out_skb = skb;
638 info.nlmsg_seq = cb->nlh->nlmsg_seq;
639 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800640
641 if (!cb->args[0]) {
642 cb->args[0] = 1;
643 xfrm_state_walk_init(walk, 0);
644 }
645
646 (void) xfrm_state_walk(walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 return skb->len;
649}
650
651static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
652 struct xfrm_state *x, u32 seq)
653{
654 struct xfrm_dump_info info;
655 struct sk_buff *skb;
656
Thomas Graf7deb2262007-08-22 13:57:39 -0700657 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!skb)
659 return ERR_PTR(-ENOMEM);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 info.in_skb = in_skb;
662 info.out_skb = skb;
663 info.nlmsg_seq = seq;
664 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (dump_one_state(x, 0, &info)) {
667 kfree_skb(skb);
668 return NULL;
669 }
670
671 return skb;
672}
673
Thomas Graf7deb2262007-08-22 13:57:39 -0700674static inline size_t xfrm_spdinfo_msgsize(void)
675{
676 return NLMSG_ALIGN(4)
677 + nla_total_size(sizeof(struct xfrmu_spdinfo))
678 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
679}
680
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700681static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
682{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700683 struct xfrmk_spdinfo si;
684 struct xfrmu_spdinfo spc;
685 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700686 struct nlmsghdr *nlh;
687 u32 *f;
688
689 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
690 if (nlh == NULL) /* shouldnt really happen ... */
691 return -EMSGSIZE;
692
693 f = nlmsg_data(nlh);
694 *f = flags;
695 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700696 spc.incnt = si.incnt;
697 spc.outcnt = si.outcnt;
698 spc.fwdcnt = si.fwdcnt;
699 spc.inscnt = si.inscnt;
700 spc.outscnt = si.outscnt;
701 spc.fwdscnt = si.fwdscnt;
702 sph.spdhcnt = si.spdhcnt;
703 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700704
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700705 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
706 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700707
708 return nlmsg_end(skb, nlh);
709
710nla_put_failure:
711 nlmsg_cancel(skb, nlh);
712 return -EMSGSIZE;
713}
714
715static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700716 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700717{
718 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700719 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700720 u32 spid = NETLINK_CB(skb).pid;
721 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700722
Thomas Graf7deb2262007-08-22 13:57:39 -0700723 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700724 if (r_skb == NULL)
725 return -ENOMEM;
726
727 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
728 BUG();
729
730 return nlmsg_unicast(xfrm_nl, r_skb, spid);
731}
732
Thomas Graf7deb2262007-08-22 13:57:39 -0700733static inline size_t xfrm_sadinfo_msgsize(void)
734{
735 return NLMSG_ALIGN(4)
736 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
737 + nla_total_size(4); /* XFRMA_SAD_CNT */
738}
739
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700740static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
741{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700742 struct xfrmk_sadinfo si;
743 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700744 struct nlmsghdr *nlh;
745 u32 *f;
746
747 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
748 if (nlh == NULL) /* shouldnt really happen ... */
749 return -EMSGSIZE;
750
751 f = nlmsg_data(nlh);
752 *f = flags;
753 xfrm_sad_getinfo(&si);
754
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700755 sh.sadhmcnt = si.sadhmcnt;
756 sh.sadhcnt = si.sadhcnt;
757
758 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
759 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700760
761 return nlmsg_end(skb, nlh);
762
763nla_put_failure:
764 nlmsg_cancel(skb, nlh);
765 return -EMSGSIZE;
766}
767
768static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700769 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700770{
771 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700772 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700773 u32 spid = NETLINK_CB(skb).pid;
774 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700775
Thomas Graf7deb2262007-08-22 13:57:39 -0700776 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700777 if (r_skb == NULL)
778 return -ENOMEM;
779
780 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
781 BUG();
782
783 return nlmsg_unicast(xfrm_nl, r_skb, spid);
784}
785
Christoph Hellwig22e70052007-01-02 15:22:30 -0800786static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700787 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Thomas Graf7b67c852007-08-22 13:53:52 -0700789 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct xfrm_state *x;
791 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700792 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Thomas Graf35a7aa02007-08-22 14:00:40 -0700794 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (x == NULL)
796 goto out_noput;
797
798 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
799 if (IS_ERR(resp_skb)) {
800 err = PTR_ERR(resp_skb);
801 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700802 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 xfrm_state_put(x);
805out_noput:
806 return err;
807}
808
809static int verify_userspi_info(struct xfrm_userspi_info *p)
810{
811 switch (p->info.id.proto) {
812 case IPPROTO_AH:
813 case IPPROTO_ESP:
814 break;
815
816 case IPPROTO_COMP:
817 /* IPCOMP spi is 16-bits. */
818 if (p->max >= 0x10000)
819 return -EINVAL;
820 break;
821
822 default:
823 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (p->min > p->max)
827 return -EINVAL;
828
829 return 0;
830}
831
Christoph Hellwig22e70052007-01-02 15:22:30 -0800832static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700833 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct xfrm_state *x;
836 struct xfrm_userspi_info *p;
837 struct sk_buff *resp_skb;
838 xfrm_address_t *daddr;
839 int family;
840 int err;
841
Thomas Graf7b67c852007-08-22 13:53:52 -0700842 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 err = verify_userspi_info(p);
844 if (err)
845 goto out_noput;
846
847 family = p->info.family;
848 daddr = &p->info.id.daddr;
849
850 x = NULL;
851 if (p->info.seq) {
852 x = xfrm_find_acq_byseq(p->info.seq);
853 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
854 xfrm_state_put(x);
855 x = NULL;
856 }
857 }
858
859 if (!x)
860 x = xfrm_find_acq(p->info.mode, p->info.reqid,
861 p->info.id.proto, daddr,
862 &p->info.saddr, 1,
863 family);
864 err = -ENOENT;
865 if (x == NULL)
866 goto out_noput;
867
Herbert Xu658b2192007-10-09 13:29:52 -0700868 err = xfrm_alloc_spi(x, p->min, p->max);
869 if (err)
870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Herbert Xu658b2192007-10-09 13:29:52 -0700872 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (IS_ERR(resp_skb)) {
874 err = PTR_ERR(resp_skb);
875 goto out;
876 }
877
Thomas Graf082a1ad2007-08-22 13:54:36 -0700878 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880out:
881 xfrm_state_put(x);
882out_noput:
883 return err;
884}
885
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800886static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 switch (dir) {
889 case XFRM_POLICY_IN:
890 case XFRM_POLICY_OUT:
891 case XFRM_POLICY_FWD:
892 break;
893
894 default:
895 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 return 0;
899}
900
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800901static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700902{
903 switch (type) {
904 case XFRM_POLICY_TYPE_MAIN:
905#ifdef CONFIG_XFRM_SUB_POLICY
906 case XFRM_POLICY_TYPE_SUB:
907#endif
908 break;
909
910 default:
911 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700912 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700913
914 return 0;
915}
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
918{
919 switch (p->share) {
920 case XFRM_SHARE_ANY:
921 case XFRM_SHARE_SESSION:
922 case XFRM_SHARE_USER:
923 case XFRM_SHARE_UNIQUE:
924 break;
925
926 default:
927 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 switch (p->action) {
931 case XFRM_POLICY_ALLOW:
932 case XFRM_POLICY_BLOCK:
933 break;
934
935 default:
936 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 switch (p->sel.family) {
940 case AF_INET:
941 break;
942
943 case AF_INET6:
944#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
945 break;
946#else
947 return -EAFNOSUPPORT;
948#endif
949
950 default:
951 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 return verify_policy_dir(p->dir);
955}
956
Thomas Graf5424f322007-08-22 14:01:33 -0700957static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800958{
Thomas Graf5424f322007-08-22 14:01:33 -0700959 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800960 struct xfrm_user_sec_ctx *uctx;
961
962 if (!rt)
963 return 0;
964
Thomas Graf5424f322007-08-22 14:01:33 -0700965 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -0700966 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -0800967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
970 int nr)
971{
972 int i;
973
974 xp->xfrm_nr = nr;
975 for (i = 0; i < nr; i++, ut++) {
976 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
977
978 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
979 memcpy(&t->saddr, &ut->saddr,
980 sizeof(xfrm_address_t));
981 t->reqid = ut->reqid;
982 t->mode = ut->mode;
983 t->share = ut->share;
984 t->optional = ut->optional;
985 t->aalgos = ut->aalgos;
986 t->ealgos = ut->ealgos;
987 t->calgos = ut->calgos;
Herbert Xuc5d18e92008-04-22 00:46:42 -0700988 /* If all masks are ~0, then we allow all algorithms. */
989 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
Miika Komu8511d012006-11-30 16:40:51 -0800990 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992}
993
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800994static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
995{
996 int i;
997
998 if (nr > XFRM_MAX_DEPTH)
999 return -EINVAL;
1000
1001 for (i = 0; i < nr; i++) {
1002 /* We never validated the ut->family value, so many
1003 * applications simply leave it at zero. The check was
1004 * never made and ut->family was ignored because all
1005 * templates could be assumed to have the same family as
1006 * the policy itself. Now that we will have ipv4-in-ipv6
1007 * and ipv6-in-ipv4 tunnels, this is no longer true.
1008 */
1009 if (!ut[i].family)
1010 ut[i].family = family;
1011
1012 switch (ut[i].family) {
1013 case AF_INET:
1014 break;
1015#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1016 case AF_INET6:
1017 break;
1018#endif
1019 default:
1020 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001021 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001022 }
1023
1024 return 0;
1025}
1026
Thomas Graf5424f322007-08-22 14:01:33 -07001027static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Thomas Graf5424f322007-08-22 14:01:33 -07001029 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (!rt) {
1032 pol->xfrm_nr = 0;
1033 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001034 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1035 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001036 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001038 err = validate_tmpl(nr, utmpl, pol->family);
1039 if (err)
1040 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Thomas Graf5424f322007-08-22 14:01:33 -07001042 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 return 0;
1045}
1046
Thomas Graf5424f322007-08-22 14:01:33 -07001047static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001048{
Thomas Graf5424f322007-08-22 14:01:33 -07001049 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001050 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001051 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001052 int err;
1053
1054 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001055 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001056 type = upt->type;
1057 }
1058
1059 err = verify_policy_type(type);
1060 if (err)
1061 return err;
1062
1063 *tp = type;
1064 return 0;
1065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1068{
1069 xp->priority = p->priority;
1070 xp->index = p->index;
1071 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1072 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1073 xp->action = p->action;
1074 xp->flags = p->flags;
1075 xp->family = p->sel.family;
1076 /* XXX xp->share = p->share; */
1077}
1078
1079static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1080{
1081 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1082 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1083 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1084 p->priority = xp->priority;
1085 p->index = xp->index;
1086 p->sel.family = xp->family;
1087 p->dir = dir;
1088 p->action = xp->action;
1089 p->flags = xp->flags;
1090 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1091}
1092
Thomas Graf5424f322007-08-22 14:01:33 -07001093static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1096 int err;
1097
1098 if (!xp) {
1099 *errp = -ENOMEM;
1100 return NULL;
1101 }
1102
1103 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001104
Thomas Graf35a7aa02007-08-22 14:00:40 -07001105 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001106 if (err)
1107 goto error;
1108
Thomas Graf35a7aa02007-08-22 14:00:40 -07001109 if (!(err = copy_from_user_tmpl(xp, attrs)))
1110 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001111 if (err)
1112 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001115 error:
1116 *errp = err;
YOSHIFUJI Hideaki073a3712008-02-14 14:52:38 -08001117 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001118 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001119 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Christoph Hellwig22e70052007-01-02 15:22:30 -08001122static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001123 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Thomas Graf7b67c852007-08-22 13:53:52 -07001125 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001127 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int err;
1129 int excl;
Eric Paris25323862008-04-18 10:09:25 -04001130 uid_t loginuid = NETLINK_CB(skb).loginuid;
1131 u32 sessionid = NETLINK_CB(skb).sessionid;
1132 u32 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 err = verify_newpolicy_info(p);
1135 if (err)
1136 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001137 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001138 if (err)
1139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Thomas Graf35a7aa02007-08-22 14:00:40 -07001141 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (!xp)
1143 return err;
1144
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001145 /* shouldnt excl be based on nlh flags??
1146 * Aha! this is anti-netlink really i.e more pfkey derived
1147 * in netlink excl is a flag and you wouldnt need
1148 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1150 err = xfrm_policy_insert(p->dir, xp, excl);
Eric Paris25323862008-04-18 10:09:25 -04001151 xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001154 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 kfree(xp);
1156 return err;
1157 }
1158
Herbert Xuf60f6b82005-06-18 22:44:37 -07001159 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001160 c.seq = nlh->nlmsg_seq;
1161 c.pid = nlh->nlmsg_pid;
1162 km_policy_notify(xp, p->dir, &c);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 xfrm_pol_put(xp);
1165
1166 return 0;
1167}
1168
1169static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1170{
1171 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1172 int i;
1173
1174 if (xp->xfrm_nr == 0)
1175 return 0;
1176
1177 for (i = 0; i < xp->xfrm_nr; i++) {
1178 struct xfrm_user_tmpl *up = &vec[i];
1179 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1180
1181 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001182 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1184 up->reqid = kp->reqid;
1185 up->mode = kp->mode;
1186 up->share = kp->share;
1187 up->optional = kp->optional;
1188 up->aalgos = kp->aalgos;
1189 up->ealgos = kp->ealgos;
1190 up->calgos = kp->calgos;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Thomas Grafc0144be2007-08-22 13:55:43 -07001193 return nla_put(skb, XFRMA_TMPL,
1194 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001195}
1196
Serge Hallyn0d681622006-07-24 23:30:44 -07001197static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1198{
1199 if (x->security) {
1200 return copy_sec_ctx(x->security, skb);
1201 }
1202 return 0;
1203}
1204
1205static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1206{
1207 if (xp->security) {
1208 return copy_sec_ctx(xp->security, skb);
1209 }
1210 return 0;
1211}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001212static inline size_t userpolicy_type_attrsize(void)
1213{
1214#ifdef CONFIG_XFRM_SUB_POLICY
1215 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1216#else
1217 return 0;
1218#endif
1219}
Serge Hallyn0d681622006-07-24 23:30:44 -07001220
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001221#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001222static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001223{
Thomas Grafc0144be2007-08-22 13:55:43 -07001224 struct xfrm_userpolicy_type upt = {
1225 .type = type,
1226 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001227
Thomas Grafc0144be2007-08-22 13:55:43 -07001228 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001229}
1230
1231#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001232static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001233{
1234 return 0;
1235}
1236#endif
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1239{
1240 struct xfrm_dump_info *sp = ptr;
1241 struct xfrm_userpolicy_info *p;
1242 struct sk_buff *in_skb = sp->in_skb;
1243 struct sk_buff *skb = sp->out_skb;
1244 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001246 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1247 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1248 if (nlh == NULL)
1249 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Thomas Graf7b67c852007-08-22 13:53:52 -07001251 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 copy_to_user_policy(xp, p, dir);
1253 if (copy_to_user_tmpl(xp, skb) < 0)
1254 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001255 if (copy_to_user_sec_ctx(xp, skb))
1256 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001257 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001258 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Thomas Graf98250692007-08-22 12:47:26 -07001260 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262
1263nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001264 nlmsg_cancel(skb, nlh);
1265 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Timo Teras4c563f72008-02-28 21:31:08 -08001268static int xfrm_dump_policy_done(struct netlink_callback *cb)
1269{
1270 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1271
1272 xfrm_policy_walk_done(walk);
1273 return 0;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1277{
Timo Teras4c563f72008-02-28 21:31:08 -08001278 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 struct xfrm_dump_info info;
1280
Timo Teras4c563f72008-02-28 21:31:08 -08001281 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1282 sizeof(cb->args) - sizeof(cb->args[0]));
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 info.in_skb = cb->skb;
1285 info.out_skb = skb;
1286 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1287 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001288
1289 if (!cb->args[0]) {
1290 cb->args[0] = 1;
1291 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1292 }
1293
1294 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 return skb->len;
1297}
1298
1299static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1300 struct xfrm_policy *xp,
1301 int dir, u32 seq)
1302{
1303 struct xfrm_dump_info info;
1304 struct sk_buff *skb;
1305
Thomas Graf7deb2262007-08-22 13:57:39 -07001306 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!skb)
1308 return ERR_PTR(-ENOMEM);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 info.in_skb = in_skb;
1311 info.out_skb = skb;
1312 info.nlmsg_seq = seq;
1313 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1316 kfree_skb(skb);
1317 return NULL;
1318 }
1319
1320 return skb;
1321}
1322
Christoph Hellwig22e70052007-01-02 15:22:30 -08001323static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001324 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 struct xfrm_policy *xp;
1327 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001328 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001330 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int delete;
1332
Thomas Graf7b67c852007-08-22 13:53:52 -07001333 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1335
Thomas Graf35a7aa02007-08-22 14:00:40 -07001336 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001337 if (err)
1338 return err;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 err = verify_policy_dir(p->dir);
1341 if (err)
1342 return err;
1343
1344 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001345 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001346 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001347 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001348 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001349
Thomas Graf35a7aa02007-08-22 14:00:40 -07001350 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001351 if (err)
1352 return err;
1353
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001354 ctx = NULL;
Trent Jaegerdf718372005-12-13 23:12:27 -08001355 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001356 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001357
Paul Moore03e1ad72008-04-12 19:07:52 -07001358 err = security_xfrm_policy_alloc(&ctx, uctx);
1359 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001360 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001361 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001362 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001363 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001364 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (xp == NULL)
1367 return -ENOENT;
1368
1369 if (!delete) {
1370 struct sk_buff *resp_skb;
1371
1372 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1373 if (IS_ERR(resp_skb)) {
1374 err = PTR_ERR(resp_skb);
1375 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001376 err = nlmsg_unicast(xfrm_nl, resp_skb,
1377 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001379 } else {
Eric Paris25323862008-04-18 10:09:25 -04001380 uid_t loginuid = NETLINK_CB(skb).loginuid;
1381 u32 sessionid = NETLINK_CB(skb).sessionid;
1382 u32 sid = NETLINK_CB(skb).sid;
1383
1384 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
1385 sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001386
1387 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001388 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001389
Herbert Xue7443892005-06-18 22:44:18 -07001390 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001391 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001392 c.seq = nlh->nlmsg_seq;
1393 c.pid = nlh->nlmsg_pid;
1394 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001397out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001398 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return err;
1400}
1401
Christoph Hellwig22e70052007-01-02 15:22:30 -08001402static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001403 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001405 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001406 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001407 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001408 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Joy Latten161a09e2006-11-27 13:11:54 -06001410 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001411 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001412 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001413 err = xfrm_state_flush(p->proto, &audit_info);
1414 if (err)
1415 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001416 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001417 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001418 c.seq = nlh->nlmsg_seq;
1419 c.pid = nlh->nlmsg_pid;
1420 km_state_notify(NULL, &c);
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return 0;
1423}
1424
Thomas Graf7deb2262007-08-22 13:57:39 -07001425static inline size_t xfrm_aevent_msgsize(void)
1426{
1427 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1428 + nla_total_size(sizeof(struct xfrm_replay_state))
1429 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1430 + nla_total_size(4) /* XFRM_AE_RTHR */
1431 + nla_total_size(4); /* XFRM_AE_ETHR */
1432}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001433
1434static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1435{
1436 struct xfrm_aevent_id *id;
1437 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001439 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1440 if (nlh == NULL)
1441 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001442
Thomas Graf7b67c852007-08-22 13:53:52 -07001443 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001444 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001445 id->sa_id.spi = x->id.spi;
1446 id->sa_id.family = x->props.family;
1447 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001448 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1449 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001450 id->flags = c->data.aevent;
1451
Thomas Grafc0144be2007-08-22 13:55:43 -07001452 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1453 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001454
Thomas Grafc0144be2007-08-22 13:55:43 -07001455 if (id->flags & XFRM_AE_RTHR)
1456 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001457
Thomas Grafc0144be2007-08-22 13:55:43 -07001458 if (id->flags & XFRM_AE_ETHR)
1459 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1460 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001461
Thomas Graf98250692007-08-22 12:47:26 -07001462 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001463
Thomas Grafc0144be2007-08-22 13:55:43 -07001464nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001465 nlmsg_cancel(skb, nlh);
1466 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001467}
1468
Christoph Hellwig22e70052007-01-02 15:22:30 -08001469static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001470 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001471{
1472 struct xfrm_state *x;
1473 struct sk_buff *r_skb;
1474 int err;
1475 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001476 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001477 struct xfrm_usersa_id *id = &p->sa_id;
1478
Thomas Graf7deb2262007-08-22 13:57:39 -07001479 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001480 if (r_skb == NULL)
1481 return -ENOMEM;
1482
1483 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1484 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001485 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001486 return -ESRCH;
1487 }
1488
1489 /*
1490 * XXX: is this lock really needed - none of the other
1491 * gets lock (the concern is things getting updated
1492 * while we are still reading) - jhs
1493 */
1494 spin_lock_bh(&x->lock);
1495 c.data.aevent = p->flags;
1496 c.seq = nlh->nlmsg_seq;
1497 c.pid = nlh->nlmsg_pid;
1498
1499 if (build_aevent(r_skb, x, &c) < 0)
1500 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001501 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001502 spin_unlock_bh(&x->lock);
1503 xfrm_state_put(x);
1504 return err;
1505}
1506
Christoph Hellwig22e70052007-01-02 15:22:30 -08001507static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001508 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001509{
1510 struct xfrm_state *x;
1511 struct km_event c;
1512 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001513 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001514 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1515 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001516
1517 if (!lt && !rp)
1518 return err;
1519
1520 /* pedantic mode - thou shalt sayeth replaceth */
1521 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1522 return err;
1523
1524 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1525 if (x == NULL)
1526 return -ESRCH;
1527
1528 if (x->km.state != XFRM_STATE_VALID)
1529 goto out;
1530
1531 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001532 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001533 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001534
1535 c.event = nlh->nlmsg_type;
1536 c.seq = nlh->nlmsg_seq;
1537 c.pid = nlh->nlmsg_pid;
1538 c.data.aevent = XFRM_AE_CU;
1539 km_state_notify(x, &c);
1540 err = 0;
1541out:
1542 xfrm_state_put(x);
1543 return err;
1544}
1545
Christoph Hellwig22e70052007-01-02 15:22:30 -08001546static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001547 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001549 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001550 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001551 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001552 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001553
Thomas Graf35a7aa02007-08-22 14:00:40 -07001554 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001555 if (err)
1556 return err;
1557
Joy Latten161a09e2006-11-27 13:11:54 -06001558 audit_info.loginuid = NETLINK_CB(skb).loginuid;
Eric Paris25323862008-04-18 10:09:25 -04001559 audit_info.sessionid = NETLINK_CB(skb).sessionid;
Joy Latten161a09e2006-11-27 13:11:54 -06001560 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001561 err = xfrm_policy_flush(type, &audit_info);
1562 if (err)
1563 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001564 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001565 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001566 c.seq = nlh->nlmsg_seq;
1567 c.pid = nlh->nlmsg_pid;
1568 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570}
1571
Christoph Hellwig22e70052007-01-02 15:22:30 -08001572static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001573 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001574{
1575 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001576 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001577 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001578 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001579 int err = -ENOENT;
1580
Thomas Graf35a7aa02007-08-22 14:00:40 -07001581 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001582 if (err)
1583 return err;
1584
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001585 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001586 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001587 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001588 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001589 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001590
Thomas Graf35a7aa02007-08-22 14:00:40 -07001591 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001592 if (err)
1593 return err;
1594
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001595 ctx = NULL;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001596 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001597 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001598
Paul Moore03e1ad72008-04-12 19:07:52 -07001599 err = security_xfrm_policy_alloc(&ctx, uctx);
1600 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001601 return err;
Denis V. Lunev2c8dd112008-04-14 14:47:48 -07001602 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001603 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
1604 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001605 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001606 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001607 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001608
Eric Parisef41aaa2007-03-07 15:37:58 -08001609 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001610 if (xp->dead) {
1611 read_unlock(&xp->lock);
1612 goto out;
1613 }
1614
1615 read_unlock(&xp->lock);
1616 err = 0;
1617 if (up->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001618 uid_t loginuid = NETLINK_CB(skb).loginuid;
1619 uid_t sessionid = NETLINK_CB(skb).sessionid;
1620 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001621 xfrm_policy_delete(xp, p->dir);
Eric Paris25323862008-04-18 10:09:25 -04001622 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001623
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001624 } else {
1625 // reset the timers here?
1626 printk("Dont know what to do with soft policy expire\n");
1627 }
1628 km_policy_expired(xp, p->dir, up->hard, current->pid);
1629
1630out:
1631 xfrm_pol_put(xp);
1632 return err;
1633}
1634
Christoph Hellwig22e70052007-01-02 15:22:30 -08001635static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001636 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001637{
1638 struct xfrm_state *x;
1639 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001640 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001641 struct xfrm_usersa_info *p = &ue->state;
1642
1643 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001644
David S. Miller3a765aa2007-02-26 14:52:21 -08001645 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001646 if (x == NULL)
1647 return err;
1648
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001649 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001650 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001651 if (x->km.state != XFRM_STATE_VALID)
1652 goto out;
1653 km_state_expired(x, ue->hard, current->pid);
1654
Joy Latten161a09e2006-11-27 13:11:54 -06001655 if (ue->hard) {
Eric Paris25323862008-04-18 10:09:25 -04001656 uid_t loginuid = NETLINK_CB(skb).loginuid;
1657 uid_t sessionid = NETLINK_CB(skb).sessionid;
1658 u32 sid = NETLINK_CB(skb).sid;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001659 __xfrm_state_delete(x);
Eric Paris25323862008-04-18 10:09:25 -04001660 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001661 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001662 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001663out:
1664 spin_unlock_bh(&x->lock);
1665 xfrm_state_put(x);
1666 return err;
1667}
1668
Christoph Hellwig22e70052007-01-02 15:22:30 -08001669static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001670 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001671{
1672 struct xfrm_policy *xp;
1673 struct xfrm_user_tmpl *ut;
1674 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001675 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001676
Thomas Graf7b67c852007-08-22 13:53:52 -07001677 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001678 struct xfrm_state *x = xfrm_state_alloc();
1679 int err = -ENOMEM;
1680
1681 if (!x)
1682 return err;
1683
1684 err = verify_newpolicy_info(&ua->policy);
1685 if (err) {
1686 printk("BAD policy passed\n");
1687 kfree(x);
1688 return err;
1689 }
1690
1691 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001692 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001693 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001694 kfree(x);
1695 return err;
1696 }
1697
1698 memcpy(&x->id, &ua->id, sizeof(ua->id));
1699 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1700 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1701
Thomas Graf5424f322007-08-22 14:01:33 -07001702 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001703 /* extract the templates and for each call km_key */
1704 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1705 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1706 memcpy(&x->id, &t->id, sizeof(x->id));
1707 x->props.mode = t->mode;
1708 x->props.reqid = t->reqid;
1709 x->props.family = ut->family;
1710 t->aalgos = ua->aalgos;
1711 t->ealgos = ua->ealgos;
1712 t->calgos = ua->calgos;
1713 err = km_query(x, t, xp);
1714
1715 }
1716
1717 kfree(x);
1718 kfree(xp);
1719
1720 return 0;
1721}
1722
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001723#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001724static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001725 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001726{
Thomas Graf5424f322007-08-22 14:01:33 -07001727 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001728 struct xfrm_user_migrate *um;
1729 int i, num_migrate;
1730
Thomas Graf5424f322007-08-22 14:01:33 -07001731 um = nla_data(rt);
1732 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001733
1734 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1735 return -EINVAL;
1736
1737 for (i = 0; i < num_migrate; i++, um++, ma++) {
1738 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1739 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1740 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1741 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1742
1743 ma->proto = um->proto;
1744 ma->mode = um->mode;
1745 ma->reqid = um->reqid;
1746
1747 ma->old_family = um->old_family;
1748 ma->new_family = um->new_family;
1749 }
1750
1751 *num = i;
1752 return 0;
1753}
1754
1755static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001756 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001757{
Thomas Graf7b67c852007-08-22 13:53:52 -07001758 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001759 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1760 u8 type;
1761 int err;
1762 int n = 0;
1763
Thomas Graf35a7aa02007-08-22 14:00:40 -07001764 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001765 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001766
Thomas Graf5424f322007-08-22 14:01:33 -07001767 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001768 if (err)
1769 return err;
1770
1771 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001772 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001773 if (err)
1774 return err;
1775
1776 if (!n)
1777 return 0;
1778
1779 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1780
1781 return 0;
1782}
1783#else
1784static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001785 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001786{
1787 return -ENOPROTOOPT;
1788}
1789#endif
1790
1791#ifdef CONFIG_XFRM_MIGRATE
1792static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1793{
1794 struct xfrm_user_migrate um;
1795
1796 memset(&um, 0, sizeof(um));
1797 um.proto = m->proto;
1798 um.mode = m->mode;
1799 um.reqid = m->reqid;
1800 um.old_family = m->old_family;
1801 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1802 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1803 um.new_family = m->new_family;
1804 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1805 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1806
Thomas Grafc0144be2007-08-22 13:55:43 -07001807 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001808}
1809
Thomas Graf7deb2262007-08-22 13:57:39 -07001810static inline size_t xfrm_migrate_msgsize(int num_migrate)
1811{
1812 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1813 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1814 + userpolicy_type_attrsize();
1815}
1816
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001817static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1818 int num_migrate, struct xfrm_selector *sel,
1819 u8 dir, u8 type)
1820{
1821 struct xfrm_migrate *mp;
1822 struct xfrm_userpolicy_id *pol_id;
1823 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001824 int i;
1825
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001826 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1827 if (nlh == NULL)
1828 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001829
Thomas Graf7b67c852007-08-22 13:53:52 -07001830 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001831 /* copy data from selector, dir, and type to the pol_id */
1832 memset(pol_id, 0, sizeof(*pol_id));
1833 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1834 pol_id->dir = dir;
1835
1836 if (copy_to_user_policy_type(type, skb) < 0)
1837 goto nlmsg_failure;
1838
1839 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1840 if (copy_to_user_migrate(mp, skb) < 0)
1841 goto nlmsg_failure;
1842 }
1843
Thomas Graf98250692007-08-22 12:47:26 -07001844 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001845nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001846 nlmsg_cancel(skb, nlh);
1847 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001848}
1849
1850static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1851 struct xfrm_migrate *m, int num_migrate)
1852{
1853 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001854
Thomas Graf7deb2262007-08-22 13:57:39 -07001855 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001856 if (skb == NULL)
1857 return -ENOMEM;
1858
1859 /* build migrate */
1860 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1861 BUG();
1862
Thomas Graf082a1ad2007-08-22 13:54:36 -07001863 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001864}
1865#else
1866static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1867 struct xfrm_migrate *m, int num_migrate)
1868{
1869 return -ENOPROTOOPT;
1870}
1871#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001872
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001873#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001874
1875static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1876 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1877 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1878 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1879 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1880 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1881 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1882 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001883 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001884 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001885 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1886 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001887 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001888 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001889 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001890 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1891 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001892 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001893 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001894 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1895 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896};
1897
Thomas Graf492b5582005-05-03 14:26:40 -07001898#undef XMSGSIZE
1899
Thomas Grafcf5cb792007-08-22 13:59:04 -07001900static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001901 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001902 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1903 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1904 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1905 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1906 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1907 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1908 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1909 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1910 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1911 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1912 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1913 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1914 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1915 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1916};
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001919 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001921 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001922} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1923 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1924 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1925 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001926 .dump = xfrm_dump_sa,
1927 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001928 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1929 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1930 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001931 .dump = xfrm_dump_policy,
1932 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001933 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001934 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001935 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001936 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1937 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001938 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001939 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1940 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001941 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1942 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001943 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001944 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001945 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946};
1947
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001948static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001950 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001952 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001956 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 type -= XFRM_MSG_BASE;
1959 link = &xfrm_dispatch[type];
1960
1961 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001962 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1963 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Thomas Graf492b5582005-05-03 14:26:40 -07001965 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1966 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1967 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001969 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Timo Teras4c563f72008-02-28 21:31:08 -08001971 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973
Thomas Graf35a7aa02007-08-22 14:00:40 -07001974 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001975 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001976 if (err < 0)
1977 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001980 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Thomas Graf5424f322007-08-22 14:01:33 -07001982 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001985static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001987 mutex_lock(&xfrm_cfg_mutex);
1988 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1989 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990}
1991
Thomas Graf7deb2262007-08-22 13:57:39 -07001992static inline size_t xfrm_expire_msgsize(void)
1993{
1994 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1995}
1996
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001997static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 struct xfrm_user_expire *ue;
2000 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002002 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
2003 if (nlh == NULL)
2004 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Thomas Graf7b67c852007-08-22 13:53:52 -07002006 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002008 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Thomas Graf98250692007-08-22 12:47:26 -07002010 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002013static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct sk_buff *skb;
2016
Thomas Graf7deb2262007-08-22 13:57:39 -07002017 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (skb == NULL)
2019 return -ENOMEM;
2020
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002021 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 BUG();
2023
Thomas Graf082a1ad2007-08-22 13:54:36 -07002024 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002027static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2028{
2029 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002030
Thomas Graf7deb2262007-08-22 13:57:39 -07002031 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002032 if (skb == NULL)
2033 return -ENOMEM;
2034
2035 if (build_aevent(skb, x, c) < 0)
2036 BUG();
2037
Thomas Graf082a1ad2007-08-22 13:54:36 -07002038 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002039}
2040
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002041static int xfrm_notify_sa_flush(struct km_event *c)
2042{
2043 struct xfrm_usersa_flush *p;
2044 struct nlmsghdr *nlh;
2045 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002046 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002047
Thomas Graf7deb2262007-08-22 13:57:39 -07002048 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002049 if (skb == NULL)
2050 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002051
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002052 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2053 if (nlh == NULL) {
2054 kfree_skb(skb);
2055 return -EMSGSIZE;
2056 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002057
Thomas Graf7b67c852007-08-22 13:53:52 -07002058 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002059 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060
Thomas Graf98250692007-08-22 12:47:26 -07002061 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002062
Thomas Graf082a1ad2007-08-22 13:54:36 -07002063 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002064}
2065
Thomas Graf7deb2262007-08-22 13:57:39 -07002066static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002067{
Thomas Graf7deb2262007-08-22 13:57:39 -07002068 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002069 if (x->aead)
2070 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002071 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002072 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002073 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002074 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002075 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002076 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002077 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002078 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002079 if (x->security)
2080 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2081 x->security->ctx_len);
2082 if (x->coaddr)
2083 l += nla_total_size(sizeof(*x->coaddr));
2084
Herbert Xud26f3982007-11-13 21:47:08 -08002085 /* Must count x->lastused as it may become non-zero behind our back. */
2086 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002087
2088 return l;
2089}
2090
2091static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2092{
2093 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002094 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002095 struct nlmsghdr *nlh;
2096 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002097 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002098 int headlen;
2099
2100 headlen = sizeof(*p);
2101 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002102 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002103 headlen = sizeof(*id);
2104 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002105 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002106
Thomas Graf7deb2262007-08-22 13:57:39 -07002107 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002108 if (skb == NULL)
2109 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002110
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002111 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2112 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002113 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002114
Thomas Graf7b67c852007-08-22 13:53:52 -07002115 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002116 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002117 struct nlattr *attr;
2118
Thomas Graf7b67c852007-08-22 13:53:52 -07002119 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002120 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2121 id->spi = x->id.spi;
2122 id->family = x->props.family;
2123 id->proto = x->id.proto;
2124
Thomas Grafc0144be2007-08-22 13:55:43 -07002125 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2126 if (attr == NULL)
2127 goto nla_put_failure;
2128
2129 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002130 }
2131
Herbert Xu68325d32007-10-09 13:30:57 -07002132 if (copy_to_user_state_extra(x, p, skb))
2133 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002134
Thomas Graf98250692007-08-22 12:47:26 -07002135 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002136
Thomas Graf082a1ad2007-08-22 13:54:36 -07002137 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002138
Thomas Grafc0144be2007-08-22 13:55:43 -07002139nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002140 /* Somebody screwed up with xfrm_sa_len! */
2141 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002142 kfree_skb(skb);
2143 return -1;
2144}
2145
2146static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2147{
2148
2149 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002150 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002151 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002152 case XFRM_MSG_NEWAE:
2153 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002154 case XFRM_MSG_DELSA:
2155 case XFRM_MSG_UPDSA:
2156 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002157 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002158 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002159 return xfrm_notify_sa_flush(c);
2160 default:
2161 printk("xfrm_user: Unknown SA event %d\n", c->event);
2162 break;
2163 }
2164
2165 return 0;
2166
2167}
2168
Thomas Graf7deb2262007-08-22 13:57:39 -07002169static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2170 struct xfrm_policy *xp)
2171{
2172 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2173 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2174 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2175 + userpolicy_type_attrsize();
2176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2179 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2180 int dir)
2181{
2182 struct xfrm_user_acquire *ua;
2183 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 __u32 seq = xfrm_get_acqseq();
2185
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002186 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2187 if (nlh == NULL)
2188 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Thomas Graf7b67c852007-08-22 13:53:52 -07002190 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 memcpy(&ua->id, &x->id, sizeof(ua->id));
2192 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2193 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2194 copy_to_user_policy(xp, &ua->policy, dir);
2195 ua->aalgos = xt->aalgos;
2196 ua->ealgos = xt->ealgos;
2197 ua->calgos = xt->calgos;
2198 ua->seq = x->km.seq = seq;
2199
2200 if (copy_to_user_tmpl(xp, skb) < 0)
2201 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002202 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002203 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002204 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002205 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Thomas Graf98250692007-08-22 12:47:26 -07002207 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002210 nlmsg_cancel(skb, nlh);
2211 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2215 struct xfrm_policy *xp, int dir)
2216{
2217 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Thomas Graf7deb2262007-08-22 13:57:39 -07002219 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (skb == NULL)
2221 return -ENOMEM;
2222
2223 if (build_acquire(skb, x, xt, xp, dir) < 0)
2224 BUG();
2225
Thomas Graf082a1ad2007-08-22 13:54:36 -07002226 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229/* User gives us xfrm_user_policy_info followed by an array of 0
2230 * or more templates.
2231 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002232static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 u8 *data, int len, int *dir)
2234{
2235 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2236 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2237 struct xfrm_policy *xp;
2238 int nr;
2239
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002240 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 case AF_INET:
2242 if (opt != IP_XFRM_POLICY) {
2243 *dir = -EOPNOTSUPP;
2244 return NULL;
2245 }
2246 break;
2247#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2248 case AF_INET6:
2249 if (opt != IPV6_XFRM_POLICY) {
2250 *dir = -EOPNOTSUPP;
2251 return NULL;
2252 }
2253 break;
2254#endif
2255 default:
2256 *dir = -EINVAL;
2257 return NULL;
2258 }
2259
2260 *dir = -EINVAL;
2261
2262 if (len < sizeof(*p) ||
2263 verify_newpolicy_info(p))
2264 return NULL;
2265
2266 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002267 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return NULL;
2269
Herbert Xua4f1bac2005-07-26 15:43:17 -07002270 if (p->dir > XFRM_POLICY_OUT)
2271 return NULL;
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 xp = xfrm_policy_alloc(GFP_KERNEL);
2274 if (xp == NULL) {
2275 *dir = -ENOBUFS;
2276 return NULL;
2277 }
2278
2279 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002280 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 copy_templates(xp, ut, nr);
2282
2283 *dir = p->dir;
2284
2285 return xp;
2286}
2287
Thomas Graf7deb2262007-08-22 13:57:39 -07002288static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2289{
2290 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2291 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2292 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2293 + userpolicy_type_attrsize();
2294}
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002297 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 struct xfrm_user_polexpire *upe;
2300 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002301 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002303 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2304 if (nlh == NULL)
2305 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Thomas Graf7b67c852007-08-22 13:53:52 -07002307 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 copy_to_user_policy(xp, &upe->pol, dir);
2309 if (copy_to_user_tmpl(xp, skb) < 0)
2310 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002311 if (copy_to_user_sec_ctx(xp, skb))
2312 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002313 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002314 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 upe->hard = !!hard;
2316
Thomas Graf98250692007-08-22 12:47:26 -07002317 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002320 nlmsg_cancel(skb, nlh);
2321 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002324static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Thomas Graf7deb2262007-08-22 13:57:39 -07002328 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (skb == NULL)
2330 return -ENOMEM;
2331
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002332 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 BUG();
2334
Thomas Graf082a1ad2007-08-22 13:54:36 -07002335 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002338static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2339{
2340 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002341 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002342 struct nlmsghdr *nlh;
2343 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002344 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002345 int headlen;
2346
2347 headlen = sizeof(*p);
2348 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002349 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002350 headlen = sizeof(*id);
2351 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002352 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002353 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002354
Thomas Graf7deb2262007-08-22 13:57:39 -07002355 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002356 if (skb == NULL)
2357 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002358
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002359 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2360 if (nlh == NULL)
2361 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002362
Thomas Graf7b67c852007-08-22 13:53:52 -07002363 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002364 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002365 struct nlattr *attr;
2366
Thomas Graf7b67c852007-08-22 13:53:52 -07002367 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002368 memset(id, 0, sizeof(*id));
2369 id->dir = dir;
2370 if (c->data.byid)
2371 id->index = xp->index;
2372 else
2373 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2374
Thomas Grafc0144be2007-08-22 13:55:43 -07002375 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2376 if (attr == NULL)
2377 goto nlmsg_failure;
2378
2379 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002380 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002381
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002382 copy_to_user_policy(xp, p, dir);
2383 if (copy_to_user_tmpl(xp, skb) < 0)
2384 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002385 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002386 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002387
Thomas Graf98250692007-08-22 12:47:26 -07002388 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002389
Thomas Graf082a1ad2007-08-22 13:54:36 -07002390 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002391
2392nlmsg_failure:
2393 kfree_skb(skb);
2394 return -1;
2395}
2396
2397static int xfrm_notify_policy_flush(struct km_event *c)
2398{
2399 struct nlmsghdr *nlh;
2400 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002401
Thomas Graf7deb2262007-08-22 13:57:39 -07002402 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002403 if (skb == NULL)
2404 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002405
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002406 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2407 if (nlh == NULL)
2408 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002409 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2410 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411
Thomas Graf98250692007-08-22 12:47:26 -07002412 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413
Thomas Graf082a1ad2007-08-22 13:54:36 -07002414 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415
2416nlmsg_failure:
2417 kfree_skb(skb);
2418 return -1;
2419}
2420
2421static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2422{
2423
2424 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002425 case XFRM_MSG_NEWPOLICY:
2426 case XFRM_MSG_UPDPOLICY:
2427 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002428 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002429 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002430 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002431 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002432 return xfrm_exp_policy_notify(xp, dir, c);
2433 default:
2434 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2435 }
2436
2437 return 0;
2438
2439}
2440
Thomas Graf7deb2262007-08-22 13:57:39 -07002441static inline size_t xfrm_report_msgsize(void)
2442{
2443 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2444}
2445
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002446static int build_report(struct sk_buff *skb, u8 proto,
2447 struct xfrm_selector *sel, xfrm_address_t *addr)
2448{
2449 struct xfrm_user_report *ur;
2450 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002451
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002452 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2453 if (nlh == NULL)
2454 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002455
Thomas Graf7b67c852007-08-22 13:53:52 -07002456 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002457 ur->proto = proto;
2458 memcpy(&ur->sel, sel, sizeof(ur->sel));
2459
2460 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002461 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002462
Thomas Graf98250692007-08-22 12:47:26 -07002463 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002464
Thomas Grafc0144be2007-08-22 13:55:43 -07002465nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002466 nlmsg_cancel(skb, nlh);
2467 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002468}
2469
2470static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2471 xfrm_address_t *addr)
2472{
2473 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002474
Thomas Graf7deb2262007-08-22 13:57:39 -07002475 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002476 if (skb == NULL)
2477 return -ENOMEM;
2478
2479 if (build_report(skb, proto, sel, addr) < 0)
2480 BUG();
2481
Thomas Graf082a1ad2007-08-22 13:54:36 -07002482 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002483}
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485static struct xfrm_mgr netlink_mgr = {
2486 .id = "netlink",
2487 .notify = xfrm_send_state_notify,
2488 .acquire = xfrm_send_acquire,
2489 .compile_policy = xfrm_compile_policy,
2490 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002491 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002492 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493};
2494
2495static int __init xfrm_user_init(void)
2496{
Patrick McHardybe336902006-03-20 22:40:54 -08002497 struct sock *nlsk;
2498
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002499 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002501 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002502 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002503 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002505 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 xfrm_register_km(&netlink_mgr);
2508
2509 return 0;
2510}
2511
2512static void __exit xfrm_user_exit(void)
2513{
Patrick McHardybe336902006-03-20 22:40:54 -08002514 struct sock *nlsk = xfrm_nl;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002517 rcu_assign_pointer(xfrm_nl, NULL);
2518 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002519 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522module_init(xfrm_user_init);
2523module_exit(xfrm_user_exit);
2524MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002525MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002526