blob: ecf9d67daef5b98238a5e41fab80198f42ced1ce [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
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700291 if (x->props.mode == XFRM_MODE_TRANSPORT)
Herbert Xu196b0032007-07-31 02:04:32 -0700292 x->sel.family = p->family;
Kazunori MIYAZAWAdf9dcb42008-03-24 14:51:51 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800296/*
297 * someday when pfkey also has support, we could have the code
298 * somehow made shareable and move it to xfrm_state.c - JHS
299 *
300*/
Thomas Graf5424f322007-08-22 14:01:33 -0700301static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800302{
Thomas Graf5424f322007-08-22 14:01:33 -0700303 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
304 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
305 struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
306 struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800307
308 if (rp) {
309 struct xfrm_replay_state *replay;
Thomas Graf5424f322007-08-22 14:01:33 -0700310 replay = nla_data(rp);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800311 memcpy(&x->replay, replay, sizeof(*replay));
312 memcpy(&x->preplay, replay, sizeof(*replay));
313 }
314
315 if (lt) {
316 struct xfrm_lifetime_cur *ltime;
Thomas Graf5424f322007-08-22 14:01:33 -0700317 ltime = nla_data(lt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800318 x->curlft.bytes = ltime->bytes;
319 x->curlft.packets = ltime->packets;
320 x->curlft.add_time = ltime->add_time;
321 x->curlft.use_time = ltime->use_time;
322 }
323
Thomas Grafcf5cb792007-08-22 13:59:04 -0700324 if (et)
Thomas Graf5424f322007-08-22 14:01:33 -0700325 x->replay_maxage = nla_get_u32(et);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800326
Thomas Grafcf5cb792007-08-22 13:59:04 -0700327 if (rt)
Thomas Graf5424f322007-08-22 14:01:33 -0700328 x->replay_maxdiff = nla_get_u32(rt);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700332 struct nlattr **attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int *errp)
334{
335 struct xfrm_state *x = xfrm_state_alloc();
336 int err = -ENOMEM;
337
338 if (!x)
339 goto error_no_put;
340
341 copy_from_user_state(x, p);
342
Herbert Xu1a6509d2008-01-28 19:37:29 -0800343 if ((err = attach_aead(&x->aead, &x->props.ealgo,
344 attrs[XFRMA_ALG_AEAD])))
345 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
347 xfrm_aalg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700348 attrs[XFRMA_ALG_AUTH])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 goto error;
350 if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
351 xfrm_ealg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700352 attrs[XFRMA_ALG_CRYPT])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto error;
354 if ((err = attach_one_algo(&x->calg, &x->props.calgo,
355 xfrm_calg_get_byname,
Thomas Graf35a7aa02007-08-22 14:00:40 -0700356 attrs[XFRMA_ALG_COMP])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto error;
Thomas Graffd211502007-09-06 03:28:08 -0700358
359 if (attrs[XFRMA_ENCAP]) {
360 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
361 sizeof(*x->encap), GFP_KERNEL);
362 if (x->encap == NULL)
363 goto error;
364 }
365
366 if (attrs[XFRMA_COADDR]) {
367 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
368 sizeof(*x->coaddr), GFP_KERNEL);
369 if (x->coaddr == NULL)
370 goto error;
371 }
372
Herbert Xu72cb6962005-06-20 13:18:08 -0700373 err = xfrm_init_state(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (err)
375 goto error;
376
Thomas Graffd211502007-09-06 03:28:08 -0700377 if (attrs[XFRMA_SEC_CTX] &&
378 security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
Trent Jaegerdf718372005-12-13 23:12:27 -0800379 goto error;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 x->km.seq = p->seq;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -0800382 x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
383 /* sysctl_xfrm_aevent_etime is in 100ms units */
384 x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
385 x->preplay.bitmap = 0;
386 x->preplay.seq = x->replay.seq+x->replay_maxdiff;
387 x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
388
389 /* override default values from above */
390
Thomas Graf5424f322007-08-22 14:01:33 -0700391 xfrm_update_ae_params(x, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return x;
394
395error:
396 x->km.state = XFRM_STATE_DEAD;
397 xfrm_state_put(x);
398error_no_put:
399 *errp = err;
400 return NULL;
401}
402
Christoph Hellwig22e70052007-01-02 15:22:30 -0800403static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700404 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Thomas Graf7b67c852007-08-22 13:53:52 -0700406 struct xfrm_usersa_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct xfrm_state *x;
408 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700409 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Thomas Graf35a7aa02007-08-22 14:00:40 -0700411 err = verify_newsa_info(p, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (err)
413 return err;
414
Thomas Graf35a7aa02007-08-22 14:00:40 -0700415 x = xfrm_state_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!x)
417 return err;
418
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700419 xfrm_state_hold(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
421 err = xfrm_state_add(x);
422 else
423 err = xfrm_state_update(x);
424
Joy Lattenab5f5e82007-09-17 11:51:22 -0700425 xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
426 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -0600427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (err < 0) {
429 x->km.state = XFRM_STATE_DEAD;
Herbert Xu21380b82006-02-22 14:47:13 -0800430 __xfrm_state_put(x);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700434 c.seq = nlh->nlmsg_seq;
435 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700436 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700437
438 km_state_notify(x, &c);
Patrick McHardy7d6dfe12005-06-18 22:45:31 -0700439out:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700440 xfrm_state_put(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return err;
442}
443
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700444static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
Thomas Graf5424f322007-08-22 14:01:33 -0700445 struct nlattr **attrs,
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700446 int *errp)
447{
448 struct xfrm_state *x = NULL;
449 int err;
450
451 if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
452 err = -ESRCH;
453 x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
454 } else {
455 xfrm_address_t *saddr = NULL;
456
Thomas Graf35a7aa02007-08-22 14:00:40 -0700457 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700458 if (!saddr) {
459 err = -EINVAL;
460 goto out;
461 }
462
Masahide NAKAMURA9abbffe2006-11-24 20:34:51 -0800463 err = -ESRCH;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700464 x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
465 p->family);
466 }
467
468 out:
469 if (!x && errp)
470 *errp = err;
471 return x;
472}
473
Christoph Hellwig22e70052007-01-02 15:22:30 -0800474static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700475 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct xfrm_state *x;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700478 int err = -ESRCH;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700479 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -0700480 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Thomas Graf35a7aa02007-08-22 14:00:40 -0700482 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (x == NULL)
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David S. Miller6f68dc32006-06-08 23:58:52 -0700486 if ((err = security_xfrm_state_delete(x)) != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700487 goto out;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (xfrm_state_kern(x)) {
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700490 err = -EPERM;
491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700494 err = xfrm_state_delete(x);
Joy Latten161a09e2006-11-27 13:11:54 -0600495
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700496 if (err < 0)
497 goto out;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700498
499 c.seq = nlh->nlmsg_seq;
500 c.pid = nlh->nlmsg_pid;
Herbert Xuf60f6b82005-06-18 22:44:37 -0700501 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700502 km_state_notify(x, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700504out:
Joy Lattenab5f5e82007-09-17 11:51:22 -0700505 xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid,
506 NETLINK_CB(skb).sid);
Catherine Zhangc8c05a82006-06-08 23:39:49 -0700507 xfrm_state_put(x);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -0700508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
512{
513 memcpy(&p->id, &x->id, sizeof(p->id));
514 memcpy(&p->sel, &x->sel, sizeof(p->sel));
515 memcpy(&p->lft, &x->lft, sizeof(p->lft));
516 memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
517 memcpy(&p->stats, &x->stats, sizeof(p->stats));
David S. Miller54489c142006-10-27 15:29:47 -0700518 memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 p->mode = x->props.mode;
520 p->replay_window = x->props.replay_window;
521 p->reqid = x->props.reqid;
522 p->family = x->props.family;
523 p->flags = x->props.flags;
524 p->seq = x->km.seq;
525}
526
527struct xfrm_dump_info {
528 struct sk_buff *in_skb;
529 struct sk_buff *out_skb;
530 u32 nlmsg_seq;
531 u16 nlmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532};
533
Thomas Grafc0144be2007-08-22 13:55:43 -0700534static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
535{
Thomas Grafc0144be2007-08-22 13:55:43 -0700536 struct xfrm_user_sec_ctx *uctx;
537 struct nlattr *attr;
Herbert Xu68325d32007-10-09 13:30:57 -0700538 int ctx_size = sizeof(*uctx) + s->ctx_len;
Thomas Grafc0144be2007-08-22 13:55:43 -0700539
540 attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
541 if (attr == NULL)
542 return -EMSGSIZE;
543
544 uctx = nla_data(attr);
545 uctx->exttype = XFRMA_SEC_CTX;
546 uctx->len = ctx_size;
547 uctx->ctx_doi = s->ctx_doi;
548 uctx->ctx_alg = s->ctx_alg;
549 uctx->ctx_len = s->ctx_len;
550 memcpy(uctx + 1, s->ctx_str, s->ctx_len);
551
552 return 0;
553}
554
Herbert Xu68325d32007-10-09 13:30:57 -0700555/* Don't change this without updating xfrm_sa_len! */
556static int copy_to_user_state_extra(struct xfrm_state *x,
557 struct xfrm_usersa_info *p,
558 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 copy_to_user_state(x, p);
561
Herbert Xu050f0092007-10-09 13:31:47 -0700562 if (x->coaddr)
563 NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
564
565 if (x->lastused)
566 NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
Herbert Xu050f0092007-10-09 13:31:47 -0700567
Herbert Xu1a6509d2008-01-28 19:37:29 -0800568 if (x->aead)
569 NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800571 NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -0800573 NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (x->calg)
Thomas Grafc0144be2007-08-22 13:55:43 -0700575 NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (x->encap)
Thomas Grafc0144be2007-08-22 13:55:43 -0700578 NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Thomas Grafc0144be2007-08-22 13:55:43 -0700580 if (x->security && copy_sec_ctx(x->security, skb) < 0)
581 goto nla_put_failure;
Noriaki TAKAMIYA060f02a2006-08-23 18:18:55 -0700582
Herbert Xu68325d32007-10-09 13:30:57 -0700583 return 0;
584
585nla_put_failure:
586 return -EMSGSIZE;
587}
588
589static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
590{
591 struct xfrm_dump_info *sp = ptr;
592 struct sk_buff *in_skb = sp->in_skb;
593 struct sk_buff *skb = sp->out_skb;
594 struct xfrm_usersa_info *p;
595 struct nlmsghdr *nlh;
596 int err;
597
Herbert Xu68325d32007-10-09 13:30:57 -0700598 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
599 XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
600 if (nlh == NULL)
601 return -EMSGSIZE;
602
603 p = nlmsg_data(nlh);
604
605 err = copy_to_user_state_extra(x, p, skb);
606 if (err)
607 goto nla_put_failure;
608
Thomas Graf98250692007-08-22 12:47:26 -0700609 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 0;
611
Thomas Grafc0144be2007-08-22 13:55:43 -0700612nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -0700613 nlmsg_cancel(skb, nlh);
Herbert Xu68325d32007-10-09 13:30:57 -0700614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Timo Teras4c563f72008-02-28 21:31:08 -0800617static int xfrm_dump_sa_done(struct netlink_callback *cb)
618{
619 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
620 xfrm_state_walk_done(walk);
621 return 0;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
625{
Timo Teras4c563f72008-02-28 21:31:08 -0800626 struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct xfrm_dump_info info;
628
Timo Teras4c563f72008-02-28 21:31:08 -0800629 BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
630 sizeof(cb->args) - sizeof(cb->args[0]));
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 info.in_skb = cb->skb;
633 info.out_skb = skb;
634 info.nlmsg_seq = cb->nlh->nlmsg_seq;
635 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -0800636
637 if (!cb->args[0]) {
638 cb->args[0] = 1;
639 xfrm_state_walk_init(walk, 0);
640 }
641
642 (void) xfrm_state_walk(walk, dump_one_state, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 return skb->len;
645}
646
647static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
648 struct xfrm_state *x, u32 seq)
649{
650 struct xfrm_dump_info info;
651 struct sk_buff *skb;
652
Thomas Graf7deb2262007-08-22 13:57:39 -0700653 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!skb)
655 return ERR_PTR(-ENOMEM);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 info.in_skb = in_skb;
658 info.out_skb = skb;
659 info.nlmsg_seq = seq;
660 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (dump_one_state(x, 0, &info)) {
663 kfree_skb(skb);
664 return NULL;
665 }
666
667 return skb;
668}
669
Thomas Graf7deb2262007-08-22 13:57:39 -0700670static inline size_t xfrm_spdinfo_msgsize(void)
671{
672 return NLMSG_ALIGN(4)
673 + nla_total_size(sizeof(struct xfrmu_spdinfo))
674 + nla_total_size(sizeof(struct xfrmu_spdhinfo));
675}
676
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700677static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
678{
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700679 struct xfrmk_spdinfo si;
680 struct xfrmu_spdinfo spc;
681 struct xfrmu_spdhinfo sph;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700682 struct nlmsghdr *nlh;
683 u32 *f;
684
685 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
686 if (nlh == NULL) /* shouldnt really happen ... */
687 return -EMSGSIZE;
688
689 f = nlmsg_data(nlh);
690 *f = flags;
691 xfrm_spd_getinfo(&si);
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700692 spc.incnt = si.incnt;
693 spc.outcnt = si.outcnt;
694 spc.fwdcnt = si.fwdcnt;
695 spc.inscnt = si.inscnt;
696 spc.outscnt = si.outscnt;
697 spc.fwdscnt = si.fwdscnt;
698 sph.spdhcnt = si.spdhcnt;
699 sph.spdhmcnt = si.spdhmcnt;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700700
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700701 NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
702 NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700703
704 return nlmsg_end(skb, nlh);
705
706nla_put_failure:
707 nlmsg_cancel(skb, nlh);
708 return -EMSGSIZE;
709}
710
711static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700712 struct nlattr **attrs)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700713{
714 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700715 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700716 u32 spid = NETLINK_CB(skb).pid;
717 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700718
Thomas Graf7deb2262007-08-22 13:57:39 -0700719 r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700720 if (r_skb == NULL)
721 return -ENOMEM;
722
723 if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
724 BUG();
725
726 return nlmsg_unicast(xfrm_nl, r_skb, spid);
727}
728
Thomas Graf7deb2262007-08-22 13:57:39 -0700729static inline size_t xfrm_sadinfo_msgsize(void)
730{
731 return NLMSG_ALIGN(4)
732 + nla_total_size(sizeof(struct xfrmu_sadhinfo))
733 + nla_total_size(4); /* XFRMA_SAD_CNT */
734}
735
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700736static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
737{
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700738 struct xfrmk_sadinfo si;
739 struct xfrmu_sadhinfo sh;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700740 struct nlmsghdr *nlh;
741 u32 *f;
742
743 nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
744 if (nlh == NULL) /* shouldnt really happen ... */
745 return -EMSGSIZE;
746
747 f = nlmsg_data(nlh);
748 *f = flags;
749 xfrm_sad_getinfo(&si);
750
Jamal Hadi Salimaf11e312007-05-04 12:55:13 -0700751 sh.sadhmcnt = si.sadhmcnt;
752 sh.sadhcnt = si.sadhcnt;
753
754 NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
755 NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700756
757 return nlmsg_end(skb, nlh);
758
759nla_put_failure:
760 nlmsg_cancel(skb, nlh);
761 return -EMSGSIZE;
762}
763
764static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700765 struct nlattr **attrs)
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700766{
767 struct sk_buff *r_skb;
Thomas Graf7b67c852007-08-22 13:53:52 -0700768 u32 *flags = nlmsg_data(nlh);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700769 u32 spid = NETLINK_CB(skb).pid;
770 u32 seq = nlh->nlmsg_seq;
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700771
Thomas Graf7deb2262007-08-22 13:57:39 -0700772 r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
Jamal Hadi Salim28d89092007-04-26 00:10:29 -0700773 if (r_skb == NULL)
774 return -ENOMEM;
775
776 if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
777 BUG();
778
779 return nlmsg_unicast(xfrm_nl, r_skb, spid);
780}
781
Christoph Hellwig22e70052007-01-02 15:22:30 -0800782static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700783 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Thomas Graf7b67c852007-08-22 13:53:52 -0700785 struct xfrm_usersa_id *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct xfrm_state *x;
787 struct sk_buff *resp_skb;
Masahide NAKAMURAeb2971b2006-08-23 17:56:04 -0700788 int err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Thomas Graf35a7aa02007-08-22 14:00:40 -0700790 x = xfrm_user_state_lookup(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (x == NULL)
792 goto out_noput;
793
794 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
795 if (IS_ERR(resp_skb)) {
796 err = PTR_ERR(resp_skb);
797 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -0700798 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 xfrm_state_put(x);
801out_noput:
802 return err;
803}
804
805static int verify_userspi_info(struct xfrm_userspi_info *p)
806{
807 switch (p->info.id.proto) {
808 case IPPROTO_AH:
809 case IPPROTO_ESP:
810 break;
811
812 case IPPROTO_COMP:
813 /* IPCOMP spi is 16-bits. */
814 if (p->max >= 0x10000)
815 return -EINVAL;
816 break;
817
818 default:
819 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (p->min > p->max)
823 return -EINVAL;
824
825 return 0;
826}
827
Christoph Hellwig22e70052007-01-02 15:22:30 -0800828static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -0700829 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct xfrm_state *x;
832 struct xfrm_userspi_info *p;
833 struct sk_buff *resp_skb;
834 xfrm_address_t *daddr;
835 int family;
836 int err;
837
Thomas Graf7b67c852007-08-22 13:53:52 -0700838 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 err = verify_userspi_info(p);
840 if (err)
841 goto out_noput;
842
843 family = p->info.family;
844 daddr = &p->info.id.daddr;
845
846 x = NULL;
847 if (p->info.seq) {
848 x = xfrm_find_acq_byseq(p->info.seq);
849 if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
850 xfrm_state_put(x);
851 x = NULL;
852 }
853 }
854
855 if (!x)
856 x = xfrm_find_acq(p->info.mode, p->info.reqid,
857 p->info.id.proto, daddr,
858 &p->info.saddr, 1,
859 family);
860 err = -ENOENT;
861 if (x == NULL)
862 goto out_noput;
863
Herbert Xu658b2192007-10-09 13:29:52 -0700864 err = xfrm_alloc_spi(x, p->min, p->max);
865 if (err)
866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Herbert Xu658b2192007-10-09 13:29:52 -0700868 resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (IS_ERR(resp_skb)) {
870 err = PTR_ERR(resp_skb);
871 goto out;
872 }
873
Thomas Graf082a1ad2007-08-22 13:54:36 -0700874 err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876out:
877 xfrm_state_put(x);
878out_noput:
879 return err;
880}
881
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800882static int verify_policy_dir(u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 switch (dir) {
885 case XFRM_POLICY_IN:
886 case XFRM_POLICY_OUT:
887 case XFRM_POLICY_FWD:
888 break;
889
890 default:
891 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 return 0;
895}
896
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -0800897static int verify_policy_type(u8 type)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700898{
899 switch (type) {
900 case XFRM_POLICY_TYPE_MAIN:
901#ifdef CONFIG_XFRM_SUB_POLICY
902 case XFRM_POLICY_TYPE_SUB:
903#endif
904 break;
905
906 default:
907 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700908 }
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -0700909
910 return 0;
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
914{
915 switch (p->share) {
916 case XFRM_SHARE_ANY:
917 case XFRM_SHARE_SESSION:
918 case XFRM_SHARE_USER:
919 case XFRM_SHARE_UNIQUE:
920 break;
921
922 default:
923 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 switch (p->action) {
927 case XFRM_POLICY_ALLOW:
928 case XFRM_POLICY_BLOCK:
929 break;
930
931 default:
932 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 switch (p->sel.family) {
936 case AF_INET:
937 break;
938
939 case AF_INET6:
940#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
941 break;
942#else
943 return -EAFNOSUPPORT;
944#endif
945
946 default:
947 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 return verify_policy_dir(p->dir);
951}
952
Thomas Graf5424f322007-08-22 14:01:33 -0700953static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
Trent Jaegerdf718372005-12-13 23:12:27 -0800954{
Thomas Graf5424f322007-08-22 14:01:33 -0700955 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Trent Jaegerdf718372005-12-13 23:12:27 -0800956 struct xfrm_user_sec_ctx *uctx;
957
958 if (!rt)
959 return 0;
960
Thomas Graf5424f322007-08-22 14:01:33 -0700961 uctx = nla_data(rt);
Paul Moore03e1ad72008-04-12 19:07:52 -0700962 return security_xfrm_policy_alloc(&pol->security, uctx);
Trent Jaegerdf718372005-12-13 23:12:27 -0800963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
966 int nr)
967{
968 int i;
969
970 xp->xfrm_nr = nr;
971 for (i = 0; i < nr; i++, ut++) {
972 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
973
974 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
975 memcpy(&t->saddr, &ut->saddr,
976 sizeof(xfrm_address_t));
977 t->reqid = ut->reqid;
978 t->mode = ut->mode;
979 t->share = ut->share;
980 t->optional = ut->optional;
981 t->aalgos = ut->aalgos;
982 t->ealgos = ut->ealgos;
983 t->calgos = ut->calgos;
Miika Komu8511d012006-11-30 16:40:51 -0800984 t->encap_family = ut->family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986}
987
David S. Millerb4ad86bf2006-12-03 19:19:26 -0800988static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
989{
990 int i;
991
992 if (nr > XFRM_MAX_DEPTH)
993 return -EINVAL;
994
995 for (i = 0; i < nr; i++) {
996 /* We never validated the ut->family value, so many
997 * applications simply leave it at zero. The check was
998 * never made and ut->family was ignored because all
999 * templates could be assumed to have the same family as
1000 * the policy itself. Now that we will have ipv4-in-ipv6
1001 * and ipv6-in-ipv4 tunnels, this is no longer true.
1002 */
1003 if (!ut[i].family)
1004 ut[i].family = family;
1005
1006 switch (ut[i].family) {
1007 case AF_INET:
1008 break;
1009#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1010 case AF_INET6:
1011 break;
1012#endif
1013 default:
1014 return -EINVAL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001015 }
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001016 }
1017
1018 return 0;
1019}
1020
Thomas Graf5424f322007-08-22 14:01:33 -07001021static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Thomas Graf5424f322007-08-22 14:01:33 -07001023 struct nlattr *rt = attrs[XFRMA_TMPL];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (!rt) {
1026 pol->xfrm_nr = 0;
1027 } else {
Thomas Graf5424f322007-08-22 14:01:33 -07001028 struct xfrm_user_tmpl *utmpl = nla_data(rt);
1029 int nr = nla_len(rt) / sizeof(*utmpl);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001030 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001032 err = validate_tmpl(nr, utmpl, pol->family);
1033 if (err)
1034 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Thomas Graf5424f322007-08-22 14:01:33 -07001036 copy_templates(pol, utmpl, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038 return 0;
1039}
1040
Thomas Graf5424f322007-08-22 14:01:33 -07001041static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001042{
Thomas Graf5424f322007-08-22 14:01:33 -07001043 struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001044 struct xfrm_userpolicy_type *upt;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001045 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001046 int err;
1047
1048 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001049 upt = nla_data(rt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001050 type = upt->type;
1051 }
1052
1053 err = verify_policy_type(type);
1054 if (err)
1055 return err;
1056
1057 *tp = type;
1058 return 0;
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
1062{
1063 xp->priority = p->priority;
1064 xp->index = p->index;
1065 memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
1066 memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
1067 xp->action = p->action;
1068 xp->flags = p->flags;
1069 xp->family = p->sel.family;
1070 /* XXX xp->share = p->share; */
1071}
1072
1073static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
1074{
1075 memcpy(&p->sel, &xp->selector, sizeof(p->sel));
1076 memcpy(&p->lft, &xp->lft, sizeof(p->lft));
1077 memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
1078 p->priority = xp->priority;
1079 p->index = xp->index;
1080 p->sel.family = xp->family;
1081 p->dir = dir;
1082 p->action = xp->action;
1083 p->flags = xp->flags;
1084 p->share = XFRM_SHARE_ANY; /* XXX xp->share */
1085}
1086
Thomas Graf5424f322007-08-22 14:01:33 -07001087static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
1090 int err;
1091
1092 if (!xp) {
1093 *errp = -ENOMEM;
1094 return NULL;
1095 }
1096
1097 copy_from_user_policy(xp, p);
Trent Jaegerdf718372005-12-13 23:12:27 -08001098
Thomas Graf35a7aa02007-08-22 14:00:40 -07001099 err = copy_from_user_policy_type(&xp->type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001100 if (err)
1101 goto error;
1102
Thomas Graf35a7aa02007-08-22 14:00:40 -07001103 if (!(err = copy_from_user_tmpl(xp, attrs)))
1104 err = copy_from_user_sec_ctx(xp, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001105 if (err)
1106 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 return xp;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001109 error:
1110 *errp = err;
YOSHIFUJI Hideaki073a3712008-02-14 14:52:38 -08001111 xp->dead = 1;
WANG Cong64c31b32008-01-07 22:34:29 -08001112 xfrm_policy_destroy(xp);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001113 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Christoph Hellwig22e70052007-01-02 15:22:30 -08001116static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001117 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Thomas Graf7b67c852007-08-22 13:53:52 -07001119 struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct xfrm_policy *xp;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001121 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int err;
1123 int excl;
1124
1125 err = verify_newpolicy_info(p);
1126 if (err)
1127 return err;
Thomas Graf35a7aa02007-08-22 14:00:40 -07001128 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001129 if (err)
1130 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Thomas Graf35a7aa02007-08-22 14:00:40 -07001132 xp = xfrm_policy_construct(p, attrs, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!xp)
1134 return err;
1135
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001136 /* shouldnt excl be based on nlh flags??
1137 * Aha! this is anti-netlink really i.e more pfkey derived
1138 * in netlink excl is a flag and you wouldnt need
1139 * a type XFRM_MSG_UPDPOLICY - JHS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
1141 err = xfrm_policy_insert(p->dir, xp, excl);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001142 xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid,
1143 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (err) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001146 security_xfrm_policy_free(xp->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 kfree(xp);
1148 return err;
1149 }
1150
Herbert Xuf60f6b82005-06-18 22:44:37 -07001151 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001152 c.seq = nlh->nlmsg_seq;
1153 c.pid = nlh->nlmsg_pid;
1154 km_policy_notify(xp, p->dir, &c);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 xfrm_pol_put(xp);
1157
1158 return 0;
1159}
1160
1161static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
1162{
1163 struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
1164 int i;
1165
1166 if (xp->xfrm_nr == 0)
1167 return 0;
1168
1169 for (i = 0; i < xp->xfrm_nr; i++) {
1170 struct xfrm_user_tmpl *up = &vec[i];
1171 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
1172
1173 memcpy(&up->id, &kp->id, sizeof(up->id));
Miika Komu8511d012006-11-30 16:40:51 -08001174 up->family = kp->encap_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
1176 up->reqid = kp->reqid;
1177 up->mode = kp->mode;
1178 up->share = kp->share;
1179 up->optional = kp->optional;
1180 up->aalgos = kp->aalgos;
1181 up->ealgos = kp->ealgos;
1182 up->calgos = kp->calgos;
1183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Thomas Grafc0144be2007-08-22 13:55:43 -07001185 return nla_put(skb, XFRMA_TMPL,
1186 sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
Trent Jaegerdf718372005-12-13 23:12:27 -08001187}
1188
Serge Hallyn0d681622006-07-24 23:30:44 -07001189static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
1190{
1191 if (x->security) {
1192 return copy_sec_ctx(x->security, skb);
1193 }
1194 return 0;
1195}
1196
1197static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
1198{
1199 if (xp->security) {
1200 return copy_sec_ctx(xp->security, skb);
1201 }
1202 return 0;
1203}
Thomas Grafcfbfd452007-08-22 13:57:04 -07001204static inline size_t userpolicy_type_attrsize(void)
1205{
1206#ifdef CONFIG_XFRM_SUB_POLICY
1207 return nla_total_size(sizeof(struct xfrm_userpolicy_type));
1208#else
1209 return 0;
1210#endif
1211}
Serge Hallyn0d681622006-07-24 23:30:44 -07001212
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001213#ifdef CONFIG_XFRM_SUB_POLICY
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001214static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001215{
Thomas Grafc0144be2007-08-22 13:55:43 -07001216 struct xfrm_userpolicy_type upt = {
1217 .type = type,
1218 };
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001219
Thomas Grafc0144be2007-08-22 13:55:43 -07001220 return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001221}
1222
1223#else
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001224static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001225{
1226 return 0;
1227}
1228#endif
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
1231{
1232 struct xfrm_dump_info *sp = ptr;
1233 struct xfrm_userpolicy_info *p;
1234 struct sk_buff *in_skb = sp->in_skb;
1235 struct sk_buff *skb = sp->out_skb;
1236 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001238 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
1239 XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
1240 if (nlh == NULL)
1241 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Thomas Graf7b67c852007-08-22 13:53:52 -07001243 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 copy_to_user_policy(xp, p, dir);
1245 if (copy_to_user_tmpl(xp, skb) < 0)
1246 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08001247 if (copy_to_user_sec_ctx(xp, skb))
1248 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08001249 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001250 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Thomas Graf98250692007-08-22 12:47:26 -07001252 nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
1254
1255nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001256 nlmsg_cancel(skb, nlh);
1257 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Timo Teras4c563f72008-02-28 21:31:08 -08001260static int xfrm_dump_policy_done(struct netlink_callback *cb)
1261{
1262 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
1263
1264 xfrm_policy_walk_done(walk);
1265 return 0;
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
1269{
Timo Teras4c563f72008-02-28 21:31:08 -08001270 struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct xfrm_dump_info info;
1272
Timo Teras4c563f72008-02-28 21:31:08 -08001273 BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
1274 sizeof(cb->args) - sizeof(cb->args[0]));
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 info.in_skb = cb->skb;
1277 info.out_skb = skb;
1278 info.nlmsg_seq = cb->nlh->nlmsg_seq;
1279 info.nlmsg_flags = NLM_F_MULTI;
Timo Teras4c563f72008-02-28 21:31:08 -08001280
1281 if (!cb->args[0]) {
1282 cb->args[0] = 1;
1283 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
1284 }
1285
1286 (void) xfrm_policy_walk(walk, dump_one_policy, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 return skb->len;
1289}
1290
1291static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
1292 struct xfrm_policy *xp,
1293 int dir, u32 seq)
1294{
1295 struct xfrm_dump_info info;
1296 struct sk_buff *skb;
1297
Thomas Graf7deb2262007-08-22 13:57:39 -07001298 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (!skb)
1300 return ERR_PTR(-ENOMEM);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 info.in_skb = in_skb;
1303 info.out_skb = skb;
1304 info.nlmsg_seq = seq;
1305 info.nlmsg_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 if (dump_one_policy(xp, dir, 0, &info) < 0) {
1308 kfree_skb(skb);
1309 return NULL;
1310 }
1311
1312 return skb;
1313}
1314
Christoph Hellwig22e70052007-01-02 15:22:30 -08001315static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001316 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct xfrm_policy *xp;
1319 struct xfrm_userpolicy_id *p;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001320 u8 type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int err;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001322 struct km_event c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int delete;
1324
Thomas Graf7b67c852007-08-22 13:53:52 -07001325 p = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
1327
Thomas Graf35a7aa02007-08-22 14:00:40 -07001328 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001329 if (err)
1330 return err;
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 err = verify_policy_dir(p->dir);
1333 if (err)
1334 return err;
1335
1336 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001337 xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
Trent Jaegerdf718372005-12-13 23:12:27 -08001338 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001339 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001340 struct xfrm_sec_ctx *ctx;
Trent Jaegerdf718372005-12-13 23:12:27 -08001341
Thomas Graf35a7aa02007-08-22 14:00:40 -07001342 err = verify_sec_ctx_len(attrs);
Trent Jaegerdf718372005-12-13 23:12:27 -08001343 if (err)
1344 return err;
1345
Trent Jaegerdf718372005-12-13 23:12:27 -08001346 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001347 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Trent Jaegerdf718372005-12-13 23:12:27 -08001348
Paul Moore03e1ad72008-04-12 19:07:52 -07001349 err = security_xfrm_policy_alloc(&ctx, uctx);
1350 if (err)
Trent Jaegerdf718372005-12-13 23:12:27 -08001351 return err;
Paul Moore03e1ad72008-04-12 19:07:52 -07001352 } else
1353 ctx = NULL;
1354 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
Eric Parisef41aaa2007-03-07 15:37:58 -08001355 delete, &err);
Paul Moore03e1ad72008-04-12 19:07:52 -07001356 security_xfrm_policy_free(ctx);
Trent Jaegerdf718372005-12-13 23:12:27 -08001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (xp == NULL)
1359 return -ENOENT;
1360
1361 if (!delete) {
1362 struct sk_buff *resp_skb;
1363
1364 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
1365 if (IS_ERR(resp_skb)) {
1366 err = PTR_ERR(resp_skb);
1367 } else {
Thomas Graf082a1ad2007-08-22 13:54:36 -07001368 err = nlmsg_unicast(xfrm_nl, resp_skb,
1369 NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001371 } else {
Joy Lattenab5f5e82007-09-17 11:51:22 -07001372 xfrm_audit_policy_delete(xp, err ? 0 : 1,
1373 NETLINK_CB(skb).loginuid,
1374 NETLINK_CB(skb).sid);
David S. Miller13fcfbb02007-02-12 13:53:54 -08001375
1376 if (err != 0)
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001377 goto out;
David S. Miller13fcfbb02007-02-12 13:53:54 -08001378
Herbert Xue7443892005-06-18 22:44:18 -07001379 c.data.byid = p->index;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001380 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001381 c.seq = nlh->nlmsg_seq;
1382 c.pid = nlh->nlmsg_pid;
1383 km_policy_notify(xp, p->dir, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
Catherine Zhangc8c05a82006-06-08 23:39:49 -07001386out:
Eric Parisef41aaa2007-03-07 15:37:58 -08001387 xfrm_pol_put(xp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return err;
1389}
1390
Christoph Hellwig22e70052007-01-02 15:22:30 -08001391static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001392 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001394 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001395 struct xfrm_usersa_flush *p = nlmsg_data(nlh);
Joy Latten161a09e2006-11-27 13:11:54 -06001396 struct xfrm_audit audit_info;
Joy Latten4aa2e622007-06-04 19:05:57 -04001397 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Joy Latten161a09e2006-11-27 13:11:54 -06001399 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1400 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001401 err = xfrm_state_flush(p->proto, &audit_info);
1402 if (err)
1403 return err;
Herbert Xubf088672005-06-18 22:44:00 -07001404 c.data.proto = p->proto;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001405 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001406 c.seq = nlh->nlmsg_seq;
1407 c.pid = nlh->nlmsg_pid;
1408 km_state_notify(NULL, &c);
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return 0;
1411}
1412
Thomas Graf7deb2262007-08-22 13:57:39 -07001413static inline size_t xfrm_aevent_msgsize(void)
1414{
1415 return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
1416 + nla_total_size(sizeof(struct xfrm_replay_state))
1417 + nla_total_size(sizeof(struct xfrm_lifetime_cur))
1418 + nla_total_size(4) /* XFRM_AE_RTHR */
1419 + nla_total_size(4); /* XFRM_AE_ETHR */
1420}
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001421
1422static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
1423{
1424 struct xfrm_aevent_id *id;
1425 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001426
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001427 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
1428 if (nlh == NULL)
1429 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001430
Thomas Graf7b67c852007-08-22 13:53:52 -07001431 id = nlmsg_data(nlh);
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001432 memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001433 id->sa_id.spi = x->id.spi;
1434 id->sa_id.family = x->props.family;
1435 id->sa_id.proto = x->id.proto;
Jamal Hadi Salim2b5f6dc2006-12-02 22:22:25 -08001436 memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
1437 id->reqid = x->props.reqid;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001438 id->flags = c->data.aevent;
1439
Thomas Grafc0144be2007-08-22 13:55:43 -07001440 NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
1441 NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001442
Thomas Grafc0144be2007-08-22 13:55:43 -07001443 if (id->flags & XFRM_AE_RTHR)
1444 NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001445
Thomas Grafc0144be2007-08-22 13:55:43 -07001446 if (id->flags & XFRM_AE_ETHR)
1447 NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
1448 x->replay_maxage * 10 / HZ);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001449
Thomas Graf98250692007-08-22 12:47:26 -07001450 return nlmsg_end(skb, nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001451
Thomas Grafc0144be2007-08-22 13:55:43 -07001452nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001453 nlmsg_cancel(skb, nlh);
1454 return -EMSGSIZE;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001455}
1456
Christoph Hellwig22e70052007-01-02 15:22:30 -08001457static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001458 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001459{
1460 struct xfrm_state *x;
1461 struct sk_buff *r_skb;
1462 int err;
1463 struct km_event c;
Thomas Graf7b67c852007-08-22 13:53:52 -07001464 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001465 struct xfrm_usersa_id *id = &p->sa_id;
1466
Thomas Graf7deb2262007-08-22 13:57:39 -07001467 r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001468 if (r_skb == NULL)
1469 return -ENOMEM;
1470
1471 x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
1472 if (x == NULL) {
Patrick McHardyb08d5842007-02-27 09:57:37 -08001473 kfree_skb(r_skb);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001474 return -ESRCH;
1475 }
1476
1477 /*
1478 * XXX: is this lock really needed - none of the other
1479 * gets lock (the concern is things getting updated
1480 * while we are still reading) - jhs
1481 */
1482 spin_lock_bh(&x->lock);
1483 c.data.aevent = p->flags;
1484 c.seq = nlh->nlmsg_seq;
1485 c.pid = nlh->nlmsg_pid;
1486
1487 if (build_aevent(r_skb, x, &c) < 0)
1488 BUG();
Thomas Graf082a1ad2007-08-22 13:54:36 -07001489 err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001490 spin_unlock_bh(&x->lock);
1491 xfrm_state_put(x);
1492 return err;
1493}
1494
Christoph Hellwig22e70052007-01-02 15:22:30 -08001495static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001496 struct nlattr **attrs)
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001497{
1498 struct xfrm_state *x;
1499 struct km_event c;
1500 int err = - EINVAL;
Thomas Graf7b67c852007-08-22 13:53:52 -07001501 struct xfrm_aevent_id *p = nlmsg_data(nlh);
Thomas Graf5424f322007-08-22 14:01:33 -07001502 struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
1503 struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001504
1505 if (!lt && !rp)
1506 return err;
1507
1508 /* pedantic mode - thou shalt sayeth replaceth */
1509 if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
1510 return err;
1511
1512 x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
1513 if (x == NULL)
1514 return -ESRCH;
1515
1516 if (x->km.state != XFRM_STATE_VALID)
1517 goto out;
1518
1519 spin_lock_bh(&x->lock);
Thomas Graf35a7aa02007-08-22 14:00:40 -07001520 xfrm_update_ae_params(x, attrs);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001521 spin_unlock_bh(&x->lock);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001522
1523 c.event = nlh->nlmsg_type;
1524 c.seq = nlh->nlmsg_seq;
1525 c.pid = nlh->nlmsg_pid;
1526 c.data.aevent = XFRM_AE_CU;
1527 km_state_notify(x, &c);
1528 err = 0;
1529out:
1530 xfrm_state_put(x);
1531 return err;
1532}
1533
Christoph Hellwig22e70052007-01-02 15:22:30 -08001534static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001535 struct nlattr **attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001537 struct km_event c;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001538 u8 type = XFRM_POLICY_TYPE_MAIN;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001539 int err;
Joy Latten161a09e2006-11-27 13:11:54 -06001540 struct xfrm_audit audit_info;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001541
Thomas Graf35a7aa02007-08-22 14:00:40 -07001542 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001543 if (err)
1544 return err;
1545
Joy Latten161a09e2006-11-27 13:11:54 -06001546 audit_info.loginuid = NETLINK_CB(skb).loginuid;
1547 audit_info.secid = NETLINK_CB(skb).sid;
Joy Latten4aa2e622007-06-04 19:05:57 -04001548 err = xfrm_policy_flush(type, &audit_info);
1549 if (err)
1550 return err;
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001551 c.data.type = type;
Herbert Xuf60f6b82005-06-18 22:44:37 -07001552 c.event = nlh->nlmsg_type;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001553 c.seq = nlh->nlmsg_seq;
1554 c.pid = nlh->nlmsg_pid;
1555 km_policy_notify(NULL, 0, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557}
1558
Christoph Hellwig22e70052007-01-02 15:22:30 -08001559static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001560 struct nlattr **attrs)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001561{
1562 struct xfrm_policy *xp;
Thomas Graf7b67c852007-08-22 13:53:52 -07001563 struct xfrm_user_polexpire *up = nlmsg_data(nlh);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001564 struct xfrm_userpolicy_info *p = &up->pol;
Jamal Hadi Salimb798a9e2006-11-27 12:59:30 -08001565 u8 type = XFRM_POLICY_TYPE_MAIN;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001566 int err = -ENOENT;
1567
Thomas Graf35a7aa02007-08-22 14:00:40 -07001568 err = copy_from_user_policy_type(&type, attrs);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07001569 if (err)
1570 return err;
1571
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001572 if (p->index)
Eric Parisef41aaa2007-03-07 15:37:58 -08001573 xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001574 else {
Thomas Graf5424f322007-08-22 14:01:33 -07001575 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
Paul Moore03e1ad72008-04-12 19:07:52 -07001576 struct xfrm_sec_ctx *ctx;
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001577
Thomas Graf35a7aa02007-08-22 14:00:40 -07001578 err = verify_sec_ctx_len(attrs);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001579 if (err)
1580 return err;
1581
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001582 if (rt) {
Thomas Graf5424f322007-08-22 14:01:33 -07001583 struct xfrm_user_sec_ctx *uctx = nla_data(rt);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001584
Paul Moore03e1ad72008-04-12 19:07:52 -07001585 err = security_xfrm_policy_alloc(&ctx, uctx);
1586 if (err)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001587 return err;
Paul Moore03e1ad72008-04-12 19:07:52 -07001588 } else
1589 ctx = NULL;
1590 xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
1591 security_xfrm_policy_free(ctx);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001592 }
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001593 if (xp == NULL)
Eric Parisef41aaa2007-03-07 15:37:58 -08001594 return -ENOENT;
Paul Moore03e1ad72008-04-12 19:07:52 -07001595
Eric Parisef41aaa2007-03-07 15:37:58 -08001596 read_lock(&xp->lock);
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001597 if (xp->dead) {
1598 read_unlock(&xp->lock);
1599 goto out;
1600 }
1601
1602 read_unlock(&xp->lock);
1603 err = 0;
1604 if (up->hard) {
1605 xfrm_policy_delete(xp, p->dir);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001606 xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid,
1607 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001608
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001609 } else {
1610 // reset the timers here?
1611 printk("Dont know what to do with soft policy expire\n");
1612 }
1613 km_policy_expired(xp, p->dir, up->hard, current->pid);
1614
1615out:
1616 xfrm_pol_put(xp);
1617 return err;
1618}
1619
Christoph Hellwig22e70052007-01-02 15:22:30 -08001620static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001621 struct nlattr **attrs)
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001622{
1623 struct xfrm_state *x;
1624 int err;
Thomas Graf7b67c852007-08-22 13:53:52 -07001625 struct xfrm_user_expire *ue = nlmsg_data(nlh);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001626 struct xfrm_usersa_info *p = &ue->state;
1627
1628 x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001629
David S. Miller3a765aa2007-02-26 14:52:21 -08001630 err = -ENOENT;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001631 if (x == NULL)
1632 return err;
1633
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001634 spin_lock_bh(&x->lock);
David S. Miller3a765aa2007-02-26 14:52:21 -08001635 err = -EINVAL;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001636 if (x->km.state != XFRM_STATE_VALID)
1637 goto out;
1638 km_state_expired(x, ue->hard, current->pid);
1639
Joy Latten161a09e2006-11-27 13:11:54 -06001640 if (ue->hard) {
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001641 __xfrm_state_delete(x);
Joy Lattenab5f5e82007-09-17 11:51:22 -07001642 xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid,
1643 NETLINK_CB(skb).sid);
Joy Latten161a09e2006-11-27 13:11:54 -06001644 }
David S. Miller3a765aa2007-02-26 14:52:21 -08001645 err = 0;
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001646out:
1647 spin_unlock_bh(&x->lock);
1648 xfrm_state_put(x);
1649 return err;
1650}
1651
Christoph Hellwig22e70052007-01-02 15:22:30 -08001652static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001653 struct nlattr **attrs)
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001654{
1655 struct xfrm_policy *xp;
1656 struct xfrm_user_tmpl *ut;
1657 int i;
Thomas Graf5424f322007-08-22 14:01:33 -07001658 struct nlattr *rt = attrs[XFRMA_TMPL];
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001659
Thomas Graf7b67c852007-08-22 13:53:52 -07001660 struct xfrm_user_acquire *ua = nlmsg_data(nlh);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001661 struct xfrm_state *x = xfrm_state_alloc();
1662 int err = -ENOMEM;
1663
1664 if (!x)
1665 return err;
1666
1667 err = verify_newpolicy_info(&ua->policy);
1668 if (err) {
1669 printk("BAD policy passed\n");
1670 kfree(x);
1671 return err;
1672 }
1673
1674 /* build an XP */
Thomas Graf5424f322007-08-22 14:01:33 -07001675 xp = xfrm_policy_construct(&ua->policy, attrs, &err);
David S. Millerb4ad86bf2006-12-03 19:19:26 -08001676 if (!xp) {
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001677 kfree(x);
1678 return err;
1679 }
1680
1681 memcpy(&x->id, &ua->id, sizeof(ua->id));
1682 memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
1683 memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
1684
Thomas Graf5424f322007-08-22 14:01:33 -07001685 ut = nla_data(rt);
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001686 /* extract the templates and for each call km_key */
1687 for (i = 0; i < xp->xfrm_nr; i++, ut++) {
1688 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
1689 memcpy(&x->id, &t->id, sizeof(x->id));
1690 x->props.mode = t->mode;
1691 x->props.reqid = t->reqid;
1692 x->props.family = ut->family;
1693 t->aalgos = ua->aalgos;
1694 t->ealgos = ua->ealgos;
1695 t->calgos = ua->calgos;
1696 err = km_query(x, t, xp);
1697
1698 }
1699
1700 kfree(x);
1701 kfree(xp);
1702
1703 return 0;
1704}
1705
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001706#ifdef CONFIG_XFRM_MIGRATE
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001707static int copy_from_user_migrate(struct xfrm_migrate *ma,
Thomas Graf5424f322007-08-22 14:01:33 -07001708 struct nlattr **attrs, int *num)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001709{
Thomas Graf5424f322007-08-22 14:01:33 -07001710 struct nlattr *rt = attrs[XFRMA_MIGRATE];
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001711 struct xfrm_user_migrate *um;
1712 int i, num_migrate;
1713
Thomas Graf5424f322007-08-22 14:01:33 -07001714 um = nla_data(rt);
1715 num_migrate = nla_len(rt) / sizeof(*um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001716
1717 if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
1718 return -EINVAL;
1719
1720 for (i = 0; i < num_migrate; i++, um++, ma++) {
1721 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
1722 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
1723 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
1724 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
1725
1726 ma->proto = um->proto;
1727 ma->mode = um->mode;
1728 ma->reqid = um->reqid;
1729
1730 ma->old_family = um->old_family;
1731 ma->new_family = um->new_family;
1732 }
1733
1734 *num = i;
1735 return 0;
1736}
1737
1738static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001739 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001740{
Thomas Graf7b67c852007-08-22 13:53:52 -07001741 struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001742 struct xfrm_migrate m[XFRM_MAX_DEPTH];
1743 u8 type;
1744 int err;
1745 int n = 0;
1746
Thomas Graf35a7aa02007-08-22 14:00:40 -07001747 if (attrs[XFRMA_MIGRATE] == NULL)
Thomas Grafcf5cb792007-08-22 13:59:04 -07001748 return -EINVAL;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001749
Thomas Graf5424f322007-08-22 14:01:33 -07001750 err = copy_from_user_policy_type(&type, attrs);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001751 if (err)
1752 return err;
1753
1754 err = copy_from_user_migrate((struct xfrm_migrate *)m,
Thomas Graf5424f322007-08-22 14:01:33 -07001755 attrs, &n);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001756 if (err)
1757 return err;
1758
1759 if (!n)
1760 return 0;
1761
1762 xfrm_migrate(&pi->sel, pi->dir, type, m, n);
1763
1764 return 0;
1765}
1766#else
1767static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
Thomas Graf5424f322007-08-22 14:01:33 -07001768 struct nlattr **attrs)
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001769{
1770 return -ENOPROTOOPT;
1771}
1772#endif
1773
1774#ifdef CONFIG_XFRM_MIGRATE
1775static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
1776{
1777 struct xfrm_user_migrate um;
1778
1779 memset(&um, 0, sizeof(um));
1780 um.proto = m->proto;
1781 um.mode = m->mode;
1782 um.reqid = m->reqid;
1783 um.old_family = m->old_family;
1784 memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
1785 memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
1786 um.new_family = m->new_family;
1787 memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
1788 memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
1789
Thomas Grafc0144be2007-08-22 13:55:43 -07001790 return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001791}
1792
Thomas Graf7deb2262007-08-22 13:57:39 -07001793static inline size_t xfrm_migrate_msgsize(int num_migrate)
1794{
1795 return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
1796 + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
1797 + userpolicy_type_attrsize();
1798}
1799
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001800static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
1801 int num_migrate, struct xfrm_selector *sel,
1802 u8 dir, u8 type)
1803{
1804 struct xfrm_migrate *mp;
1805 struct xfrm_userpolicy_id *pol_id;
1806 struct nlmsghdr *nlh;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001807 int i;
1808
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001809 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
1810 if (nlh == NULL)
1811 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001812
Thomas Graf7b67c852007-08-22 13:53:52 -07001813 pol_id = nlmsg_data(nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001814 /* copy data from selector, dir, and type to the pol_id */
1815 memset(pol_id, 0, sizeof(*pol_id));
1816 memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
1817 pol_id->dir = dir;
1818
1819 if (copy_to_user_policy_type(type, skb) < 0)
1820 goto nlmsg_failure;
1821
1822 for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
1823 if (copy_to_user_migrate(mp, skb) < 0)
1824 goto nlmsg_failure;
1825 }
1826
Thomas Graf98250692007-08-22 12:47:26 -07001827 return nlmsg_end(skb, nlh);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001828nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07001829 nlmsg_cancel(skb, nlh);
1830 return -EMSGSIZE;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001831}
1832
1833static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1834 struct xfrm_migrate *m, int num_migrate)
1835{
1836 struct sk_buff *skb;
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001837
Thomas Graf7deb2262007-08-22 13:57:39 -07001838 skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001839 if (skb == NULL)
1840 return -ENOMEM;
1841
1842 /* build migrate */
1843 if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
1844 BUG();
1845
Thomas Graf082a1ad2007-08-22 13:54:36 -07001846 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001847}
1848#else
1849static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
1850 struct xfrm_migrate *m, int num_migrate)
1851{
1852 return -ENOPROTOOPT;
1853}
1854#endif
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001855
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001856#define XMSGSIZE(type) sizeof(struct type)
Thomas Graf492b5582005-05-03 14:26:40 -07001857
1858static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
1859 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
1860 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1861 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
1862 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1863 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1864 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
1865 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001866 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001867 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
Thomas Graf492b5582005-05-03 14:26:40 -07001868 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
1869 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001870 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
Thomas Graf492b5582005-05-03 14:26:40 -07001871 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001872 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001873 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
1874 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07001875 [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001876 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001877 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
1878 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879};
1880
Thomas Graf492b5582005-05-03 14:26:40 -07001881#undef XMSGSIZE
1882
Thomas Grafcf5cb792007-08-22 13:59:04 -07001883static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
Herbert Xu1a6509d2008-01-28 19:37:29 -08001884 [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
Thomas Grafcf5cb792007-08-22 13:59:04 -07001885 [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
1886 [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
1887 [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
1888 [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
1889 [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
1890 [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
1891 [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
1892 [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
1893 [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
1894 [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
1895 [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
1896 [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
1897 [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
1898 [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
1899};
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901static struct xfrm_link {
Thomas Graf5424f322007-08-22 14:01:33 -07001902 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int (*dump)(struct sk_buff *, struct netlink_callback *);
Timo Teras4c563f72008-02-28 21:31:08 -08001904 int (*done)(struct netlink_callback *);
Thomas Graf492b5582005-05-03 14:26:40 -07001905} xfrm_dispatch[XFRM_NR_MSGTYPES] = {
1906 [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
1907 [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
1908 [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
Timo Teras4c563f72008-02-28 21:31:08 -08001909 .dump = xfrm_dump_sa,
1910 .done = xfrm_dump_sa_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001911 [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1912 [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
1913 [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
Timo Teras4c563f72008-02-28 21:31:08 -08001914 .dump = xfrm_dump_policy,
1915 .done = xfrm_dump_policy_done },
Thomas Graf492b5582005-05-03 14:26:40 -07001916 [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
Jamal Hadi Salim980ebd22006-03-20 19:16:40 -08001917 [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
Jamal Hadi Salim53bc6b42006-03-20 19:17:03 -08001918 [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
Thomas Graf492b5582005-05-03 14:26:40 -07001919 [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
1920 [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -08001921 [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
Thomas Graf492b5582005-05-03 14:26:40 -07001922 [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
1923 [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001924 [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
1925 [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08001926 [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
Jamal Hadi Salim566ec032007-04-26 14:12:15 -07001927 [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -07001928 [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929};
1930
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001931static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Thomas Graf35a7aa02007-08-22 14:00:40 -07001933 struct nlattr *attrs[XFRMA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct xfrm_link *link;
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001935 int type, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 type = nlh->nlmsg_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (type > XFRM_MSG_MAX)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 type -= XFRM_MSG_BASE;
1942 link = &xfrm_dispatch[type];
1943
1944 /* All operations require privileges, even GET */
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001945 if (security_netlink_recv(skb, CAP_NET_ADMIN))
1946 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Thomas Graf492b5582005-05-03 14:26:40 -07001948 if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
1949 type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
1950 (nlh->nlmsg_flags & NLM_F_DUMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (link->dump == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001952 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Timo Teras4c563f72008-02-28 21:31:08 -08001954 return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
Thomas Graf35a7aa02007-08-22 14:00:40 -07001957 err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
Thomas Grafcf5cb792007-08-22 13:59:04 -07001958 xfrma_policy);
Thomas Grafa7bd9a42007-08-22 13:58:18 -07001959 if (err < 0)
1960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 if (link->doit == NULL)
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001963 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Thomas Graf5424f322007-08-22 14:01:33 -07001965 return link->doit(skb, nlh, attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001968static void xfrm_netlink_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001970 mutex_lock(&xfrm_cfg_mutex);
1971 netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
1972 mutex_unlock(&xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Thomas Graf7deb2262007-08-22 13:57:39 -07001975static inline size_t xfrm_expire_msgsize(void)
1976{
1977 return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
1978}
1979
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001980static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct xfrm_user_expire *ue;
1983 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Thomas Graf79b8b7f2007-08-22 12:46:53 -07001985 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
1986 if (nlh == NULL)
1987 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Thomas Graf7b67c852007-08-22 13:53:52 -07001989 ue = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 copy_to_user_state(x, &ue->state);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08001991 ue->hard = (c->data.hard != 0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Thomas Graf98250692007-08-22 12:47:26 -07001993 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07001996static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct sk_buff *skb;
1999
Thomas Graf7deb2262007-08-22 13:57:39 -07002000 skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (skb == NULL)
2002 return -ENOMEM;
2003
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002004 if (build_expire(skb, x, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 BUG();
2006
Thomas Graf082a1ad2007-08-22 13:54:36 -07002007 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002010static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
2011{
2012 struct sk_buff *skb;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002013
Thomas Graf7deb2262007-08-22 13:57:39 -07002014 skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002015 if (skb == NULL)
2016 return -ENOMEM;
2017
2018 if (build_aevent(skb, x, c) < 0)
2019 BUG();
2020
Thomas Graf082a1ad2007-08-22 13:54:36 -07002021 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002022}
2023
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002024static int xfrm_notify_sa_flush(struct km_event *c)
2025{
2026 struct xfrm_usersa_flush *p;
2027 struct nlmsghdr *nlh;
2028 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002029 int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002030
Thomas Graf7deb2262007-08-22 13:57:39 -07002031 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002032 if (skb == NULL)
2033 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002034
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002035 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
2036 if (nlh == NULL) {
2037 kfree_skb(skb);
2038 return -EMSGSIZE;
2039 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002040
Thomas Graf7b67c852007-08-22 13:53:52 -07002041 p = nlmsg_data(nlh);
Herbert Xubf088672005-06-18 22:44:00 -07002042 p->proto = c->data.proto;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002043
Thomas Graf98250692007-08-22 12:47:26 -07002044 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002045
Thomas Graf082a1ad2007-08-22 13:54:36 -07002046 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002047}
2048
Thomas Graf7deb2262007-08-22 13:57:39 -07002049static inline size_t xfrm_sa_len(struct xfrm_state *x)
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002050{
Thomas Graf7deb2262007-08-22 13:57:39 -07002051 size_t l = 0;
Herbert Xu1a6509d2008-01-28 19:37:29 -08002052 if (x->aead)
2053 l += nla_total_size(aead_len(x->aead));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002054 if (x->aalg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002055 l += nla_total_size(xfrm_alg_len(x->aalg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002056 if (x->ealg)
Eric Dumazet0f99be02008-01-08 23:39:06 -08002057 l += nla_total_size(xfrm_alg_len(x->ealg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002058 if (x->calg)
Thomas Graf7deb2262007-08-22 13:57:39 -07002059 l += nla_total_size(sizeof(*x->calg));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002060 if (x->encap)
Thomas Graf7deb2262007-08-22 13:57:39 -07002061 l += nla_total_size(sizeof(*x->encap));
Herbert Xu68325d32007-10-09 13:30:57 -07002062 if (x->security)
2063 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
2064 x->security->ctx_len);
2065 if (x->coaddr)
2066 l += nla_total_size(sizeof(*x->coaddr));
2067
Herbert Xud26f3982007-11-13 21:47:08 -08002068 /* Must count x->lastused as it may become non-zero behind our back. */
2069 l += nla_total_size(sizeof(u64));
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002070
2071 return l;
2072}
2073
2074static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
2075{
2076 struct xfrm_usersa_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002077 struct xfrm_usersa_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002078 struct nlmsghdr *nlh;
2079 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002080 int len = xfrm_sa_len(x);
Herbert Xu0603eac2005-06-18 22:54:36 -07002081 int headlen;
2082
2083 headlen = sizeof(*p);
2084 if (c->event == XFRM_MSG_DELSA) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002085 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002086 headlen = sizeof(*id);
2087 }
Thomas Graf7deb2262007-08-22 13:57:39 -07002088 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002089
Thomas Graf7deb2262007-08-22 13:57:39 -07002090 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002091 if (skb == NULL)
2092 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002093
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002094 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2095 if (nlh == NULL)
Thomas Grafc0144be2007-08-22 13:55:43 -07002096 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002097
Thomas Graf7b67c852007-08-22 13:53:52 -07002098 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002099 if (c->event == XFRM_MSG_DELSA) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002100 struct nlattr *attr;
2101
Thomas Graf7b67c852007-08-22 13:53:52 -07002102 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002103 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
2104 id->spi = x->id.spi;
2105 id->family = x->props.family;
2106 id->proto = x->id.proto;
2107
Thomas Grafc0144be2007-08-22 13:55:43 -07002108 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
2109 if (attr == NULL)
2110 goto nla_put_failure;
2111
2112 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002113 }
2114
Herbert Xu68325d32007-10-09 13:30:57 -07002115 if (copy_to_user_state_extra(x, p, skb))
2116 goto nla_put_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002117
Thomas Graf98250692007-08-22 12:47:26 -07002118 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002119
Thomas Graf082a1ad2007-08-22 13:54:36 -07002120 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002121
Thomas Grafc0144be2007-08-22 13:55:43 -07002122nla_put_failure:
Herbert Xu68325d32007-10-09 13:30:57 -07002123 /* Somebody screwed up with xfrm_sa_len! */
2124 WARN_ON(1);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002125 kfree_skb(skb);
2126 return -1;
2127}
2128
2129static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
2130{
2131
2132 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002133 case XFRM_MSG_EXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002134 return xfrm_exp_state_notify(x, c);
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002135 case XFRM_MSG_NEWAE:
2136 return xfrm_aevent_state_notify(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002137 case XFRM_MSG_DELSA:
2138 case XFRM_MSG_UPDSA:
2139 case XFRM_MSG_NEWSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002140 return xfrm_notify_sa(x, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002141 case XFRM_MSG_FLUSHSA:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002142 return xfrm_notify_sa_flush(c);
2143 default:
2144 printk("xfrm_user: Unknown SA event %d\n", c->event);
2145 break;
2146 }
2147
2148 return 0;
2149
2150}
2151
Thomas Graf7deb2262007-08-22 13:57:39 -07002152static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
2153 struct xfrm_policy *xp)
2154{
2155 return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
2156 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2157 + nla_total_size(xfrm_user_sec_ctx_size(x->security))
2158 + userpolicy_type_attrsize();
2159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
2162 struct xfrm_tmpl *xt, struct xfrm_policy *xp,
2163 int dir)
2164{
2165 struct xfrm_user_acquire *ua;
2166 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 __u32 seq = xfrm_get_acqseq();
2168
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002169 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
2170 if (nlh == NULL)
2171 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Thomas Graf7b67c852007-08-22 13:53:52 -07002173 ua = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 memcpy(&ua->id, &x->id, sizeof(ua->id));
2175 memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
2176 memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
2177 copy_to_user_policy(xp, &ua->policy, dir);
2178 ua->aalgos = xt->aalgos;
2179 ua->ealgos = xt->ealgos;
2180 ua->calgos = xt->calgos;
2181 ua->seq = x->km.seq = seq;
2182
2183 if (copy_to_user_tmpl(xp, skb) < 0)
2184 goto nlmsg_failure;
Serge Hallyn0d681622006-07-24 23:30:44 -07002185 if (copy_to_user_state_sec_ctx(x, skb))
Trent Jaegerdf718372005-12-13 23:12:27 -08002186 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002187 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002188 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Thomas Graf98250692007-08-22 12:47:26 -07002190 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002193 nlmsg_cancel(skb, nlh);
2194 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
2198 struct xfrm_policy *xp, int dir)
2199{
2200 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Thomas Graf7deb2262007-08-22 13:57:39 -07002202 skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (skb == NULL)
2204 return -ENOMEM;
2205
2206 if (build_acquire(skb, x, xt, xp, dir) < 0)
2207 BUG();
2208
Thomas Graf082a1ad2007-08-22 13:54:36 -07002209 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/* User gives us xfrm_user_policy_info followed by an array of 0
2213 * or more templates.
2214 */
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002215static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 u8 *data, int len, int *dir)
2217{
2218 struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
2219 struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
2220 struct xfrm_policy *xp;
2221 int nr;
2222
Venkat Yekkiralacb969f02006-07-24 23:32:20 -07002223 switch (sk->sk_family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 case AF_INET:
2225 if (opt != IP_XFRM_POLICY) {
2226 *dir = -EOPNOTSUPP;
2227 return NULL;
2228 }
2229 break;
2230#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2231 case AF_INET6:
2232 if (opt != IPV6_XFRM_POLICY) {
2233 *dir = -EOPNOTSUPP;
2234 return NULL;
2235 }
2236 break;
2237#endif
2238 default:
2239 *dir = -EINVAL;
2240 return NULL;
2241 }
2242
2243 *dir = -EINVAL;
2244
2245 if (len < sizeof(*p) ||
2246 verify_newpolicy_info(p))
2247 return NULL;
2248
2249 nr = ((len - sizeof(*p)) / sizeof(*ut));
David S. Millerb4ad86bf2006-12-03 19:19:26 -08002250 if (validate_tmpl(nr, ut, p->sel.family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return NULL;
2252
Herbert Xua4f1bac2005-07-26 15:43:17 -07002253 if (p->dir > XFRM_POLICY_OUT)
2254 return NULL;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 xp = xfrm_policy_alloc(GFP_KERNEL);
2257 if (xp == NULL) {
2258 *dir = -ENOBUFS;
2259 return NULL;
2260 }
2261
2262 copy_from_user_policy(xp, p);
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002263 xp->type = XFRM_POLICY_TYPE_MAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 copy_templates(xp, ut, nr);
2265
2266 *dir = p->dir;
2267
2268 return xp;
2269}
2270
Thomas Graf7deb2262007-08-22 13:57:39 -07002271static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
2272{
2273 return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
2274 + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
2275 + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
2276 + userpolicy_type_attrsize();
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002280 int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 struct xfrm_user_polexpire *upe;
2283 struct nlmsghdr *nlh;
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002284 int hard = c->data.hard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002286 nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
2287 if (nlh == NULL)
2288 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Thomas Graf7b67c852007-08-22 13:53:52 -07002290 upe = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 copy_to_user_policy(xp, &upe->pol, dir);
2292 if (copy_to_user_tmpl(xp, skb) < 0)
2293 goto nlmsg_failure;
Trent Jaegerdf718372005-12-13 23:12:27 -08002294 if (copy_to_user_sec_ctx(xp, skb))
2295 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002296 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002297 goto nlmsg_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 upe->hard = !!hard;
2299
Thomas Graf98250692007-08-22 12:47:26 -07002300 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302nlmsg_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002303 nlmsg_cancel(skb, nlh);
2304 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002307static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Thomas Graf7deb2262007-08-22 13:57:39 -07002311 skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (skb == NULL)
2313 return -ENOMEM;
2314
Jamal Hadi Salimd51d0812006-03-20 19:16:12 -08002315 if (build_polexpire(skb, xp, dir, c) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 BUG();
2317
Thomas Graf082a1ad2007-08-22 13:54:36 -07002318 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002321static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
2322{
2323 struct xfrm_userpolicy_info *p;
Herbert Xu0603eac2005-06-18 22:54:36 -07002324 struct xfrm_userpolicy_id *id;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002325 struct nlmsghdr *nlh;
2326 struct sk_buff *skb;
Thomas Graf7deb2262007-08-22 13:57:39 -07002327 int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002328 int headlen;
2329
2330 headlen = sizeof(*p);
2331 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Graf7deb2262007-08-22 13:57:39 -07002332 len += nla_total_size(headlen);
Herbert Xu0603eac2005-06-18 22:54:36 -07002333 headlen = sizeof(*id);
2334 }
Thomas Grafcfbfd452007-08-22 13:57:04 -07002335 len += userpolicy_type_attrsize();
Thomas Graf7deb2262007-08-22 13:57:39 -07002336 len += NLMSG_ALIGN(headlen);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002337
Thomas Graf7deb2262007-08-22 13:57:39 -07002338 skb = nlmsg_new(len, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002339 if (skb == NULL)
2340 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002341
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002342 nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
2343 if (nlh == NULL)
2344 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002345
Thomas Graf7b67c852007-08-22 13:53:52 -07002346 p = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002347 if (c->event == XFRM_MSG_DELPOLICY) {
Thomas Grafc0144be2007-08-22 13:55:43 -07002348 struct nlattr *attr;
2349
Thomas Graf7b67c852007-08-22 13:53:52 -07002350 id = nlmsg_data(nlh);
Herbert Xu0603eac2005-06-18 22:54:36 -07002351 memset(id, 0, sizeof(*id));
2352 id->dir = dir;
2353 if (c->data.byid)
2354 id->index = xp->index;
2355 else
2356 memcpy(&id->sel, &xp->selector, sizeof(id->sel));
2357
Thomas Grafc0144be2007-08-22 13:55:43 -07002358 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
2359 if (attr == NULL)
2360 goto nlmsg_failure;
2361
2362 p = nla_data(attr);
Herbert Xu0603eac2005-06-18 22:54:36 -07002363 }
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002364
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002365 copy_to_user_policy(xp, p, dir);
2366 if (copy_to_user_tmpl(xp, skb) < 0)
2367 goto nlmsg_failure;
Jamal Hadi Salim1459bb32006-11-20 16:51:22 -08002368 if (copy_to_user_policy_type(xp->type, skb) < 0)
Masahide NAKAMURAf7b69832006-08-23 22:49:28 -07002369 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002370
Thomas Graf98250692007-08-22 12:47:26 -07002371 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002372
Thomas Graf082a1ad2007-08-22 13:54:36 -07002373 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002374
2375nlmsg_failure:
2376 kfree_skb(skb);
2377 return -1;
2378}
2379
2380static int xfrm_notify_policy_flush(struct km_event *c)
2381{
2382 struct nlmsghdr *nlh;
2383 struct sk_buff *skb;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002384
Thomas Graf7deb2262007-08-22 13:57:39 -07002385 skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002386 if (skb == NULL)
2387 return -ENOMEM;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002388
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002389 nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
2390 if (nlh == NULL)
2391 goto nlmsg_failure;
Jamal Hadi Salim0c51f532006-11-27 12:58:20 -08002392 if (copy_to_user_policy_type(c->data.type, skb) < 0)
2393 goto nlmsg_failure;
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002394
Thomas Graf98250692007-08-22 12:47:26 -07002395 nlmsg_end(skb, nlh);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002396
Thomas Graf082a1ad2007-08-22 13:54:36 -07002397 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002398
2399nlmsg_failure:
2400 kfree_skb(skb);
2401 return -1;
2402}
2403
2404static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
2405{
2406
2407 switch (c->event) {
Herbert Xuf60f6b82005-06-18 22:44:37 -07002408 case XFRM_MSG_NEWPOLICY:
2409 case XFRM_MSG_UPDPOLICY:
2410 case XFRM_MSG_DELPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002411 return xfrm_notify_policy(xp, dir, c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002412 case XFRM_MSG_FLUSHPOLICY:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002413 return xfrm_notify_policy_flush(c);
Herbert Xuf60f6b82005-06-18 22:44:37 -07002414 case XFRM_MSG_POLEXPIRE:
Jamal Hadi Salim26b15da2005-06-18 22:42:13 -07002415 return xfrm_exp_policy_notify(xp, dir, c);
2416 default:
2417 printk("xfrm_user: Unknown Policy event %d\n", c->event);
2418 }
2419
2420 return 0;
2421
2422}
2423
Thomas Graf7deb2262007-08-22 13:57:39 -07002424static inline size_t xfrm_report_msgsize(void)
2425{
2426 return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
2427}
2428
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002429static int build_report(struct sk_buff *skb, u8 proto,
2430 struct xfrm_selector *sel, xfrm_address_t *addr)
2431{
2432 struct xfrm_user_report *ur;
2433 struct nlmsghdr *nlh;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002434
Thomas Graf79b8b7f2007-08-22 12:46:53 -07002435 nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
2436 if (nlh == NULL)
2437 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002438
Thomas Graf7b67c852007-08-22 13:53:52 -07002439 ur = nlmsg_data(nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002440 ur->proto = proto;
2441 memcpy(&ur->sel, sel, sizeof(ur->sel));
2442
2443 if (addr)
Thomas Grafc0144be2007-08-22 13:55:43 -07002444 NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002445
Thomas Graf98250692007-08-22 12:47:26 -07002446 return nlmsg_end(skb, nlh);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002447
Thomas Grafc0144be2007-08-22 13:55:43 -07002448nla_put_failure:
Thomas Graf98250692007-08-22 12:47:26 -07002449 nlmsg_cancel(skb, nlh);
2450 return -EMSGSIZE;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002451}
2452
2453static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
2454 xfrm_address_t *addr)
2455{
2456 struct sk_buff *skb;
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002457
Thomas Graf7deb2262007-08-22 13:57:39 -07002458 skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002459 if (skb == NULL)
2460 return -ENOMEM;
2461
2462 if (build_report(skb, proto, sel, addr) < 0)
2463 BUG();
2464
Thomas Graf082a1ad2007-08-22 13:54:36 -07002465 return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002466}
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468static struct xfrm_mgr netlink_mgr = {
2469 .id = "netlink",
2470 .notify = xfrm_send_state_notify,
2471 .acquire = xfrm_send_acquire,
2472 .compile_policy = xfrm_compile_policy,
2473 .notify_policy = xfrm_send_policy_notify,
Masahide NAKAMURA97a64b42006-08-23 20:44:06 -07002474 .report = xfrm_send_report,
Shinta Sugimoto5c79de62007-02-08 13:12:32 -08002475 .migrate = xfrm_send_migrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476};
2477
2478static int __init xfrm_user_init(void)
2479{
Patrick McHardybe336902006-03-20 22:40:54 -08002480 struct sock *nlsk;
2481
Masahide NAKAMURA654b32c2006-08-23 19:12:56 -07002482 printk(KERN_INFO "Initializing XFRM netlink socket\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002484 nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002485 xfrm_netlink_rcv, NULL, THIS_MODULE);
Patrick McHardybe336902006-03-20 22:40:54 -08002486 if (nlsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 return -ENOMEM;
Patrick McHardybe336902006-03-20 22:40:54 -08002488 rcu_assign_pointer(xfrm_nl, nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 xfrm_register_km(&netlink_mgr);
2491
2492 return 0;
2493}
2494
2495static void __exit xfrm_user_exit(void)
2496{
Patrick McHardybe336902006-03-20 22:40:54 -08002497 struct sock *nlsk = xfrm_nl;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 xfrm_unregister_km(&netlink_mgr);
Patrick McHardybe336902006-03-20 22:40:54 -08002500 rcu_assign_pointer(xfrm_nl, NULL);
2501 synchronize_rcu();
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002502 netlink_kernel_release(nlsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505module_init(xfrm_user_init);
2506module_exit(xfrm_user_exit);
2507MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002508MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
Jamal Hadi Salimf8cd5482006-03-20 19:15:11 -08002509