]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* 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 | |
df71837d | 10 | * |
1da177e4 LT |
11 | */ |
12 | ||
9409f38a | 13 | #include <linux/crypto.h> |
1da177e4 LT |
14 | #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> | |
1da177e4 LT |
22 | #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> | |
88fc2c84 | 28 | #include <net/netlink.h> |
1da177e4 | 29 | #include <asm/uaccess.h> |
e23c7194 MN |
30 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
31 | #include <linux/in6.h> | |
32 | #endif | |
1da177e4 | 33 | |
c26445ac TG |
34 | static inline int alg_len(struct xfrm_algo *alg) |
35 | { | |
36 | return sizeof(*alg) + ((alg->alg_key_len + 7) / 8); | |
37 | } | |
38 | ||
5424f32e | 39 | static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type) |
1da177e4 | 40 | { |
5424f32e | 41 | struct nlattr *rt = attrs[type]; |
1da177e4 LT |
42 | struct xfrm_algo *algp; |
43 | ||
44 | if (!rt) | |
45 | return 0; | |
46 | ||
5424f32e TG |
47 | algp = nla_data(rt); |
48 | if (nla_len(rt) < alg_len(algp)) | |
31c26852 HX |
49 | return -EINVAL; |
50 | ||
1da177e4 LT |
51 | 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; | |
3ff50b79 | 70 | } |
1da177e4 LT |
71 | |
72 | algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0'; | |
73 | return 0; | |
74 | } | |
75 | ||
5424f32e | 76 | static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type, |
eb2971b6 MN |
77 | xfrm_address_t **addrp) |
78 | { | |
5424f32e | 79 | struct nlattr *rt = attrs[type]; |
eb2971b6 | 80 | |
cf5cb79f | 81 | if (rt && addrp) |
5424f32e | 82 | *addrp = nla_data(rt); |
eb2971b6 | 83 | } |
df71837d | 84 | |
5424f32e | 85 | static inline int verify_sec_ctx_len(struct nlattr **attrs) |
df71837d | 86 | { |
5424f32e | 87 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
df71837d | 88 | struct xfrm_user_sec_ctx *uctx; |
df71837d TJ |
89 | |
90 | if (!rt) | |
91 | return 0; | |
92 | ||
5424f32e | 93 | uctx = nla_data(rt); |
cf5cb79f | 94 | if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) |
df71837d TJ |
95 | return -EINVAL; |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | ||
1da177e4 | 101 | static int verify_newsa_info(struct xfrm_usersa_info *p, |
5424f32e | 102 | struct nlattr **attrs) |
1da177e4 LT |
103 | { |
104 | int err; | |
105 | ||
106 | err = -EINVAL; | |
107 | switch (p->family) { | |
108 | case AF_INET: | |
109 | break; | |
110 | ||
111 | case AF_INET6: | |
112 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
113 | break; | |
114 | #else | |
115 | err = -EAFNOSUPPORT; | |
116 | goto out; | |
117 | #endif | |
118 | ||
119 | default: | |
120 | goto out; | |
3ff50b79 | 121 | } |
1da177e4 LT |
122 | |
123 | err = -EINVAL; | |
124 | switch (p->id.proto) { | |
125 | case IPPROTO_AH: | |
35a7aa08 TG |
126 | if (!attrs[XFRMA_ALG_AUTH] || |
127 | attrs[XFRMA_ALG_CRYPT] || | |
128 | attrs[XFRMA_ALG_COMP]) | |
1da177e4 LT |
129 | goto out; |
130 | break; | |
131 | ||
132 | case IPPROTO_ESP: | |
35a7aa08 TG |
133 | if ((!attrs[XFRMA_ALG_AUTH] && |
134 | !attrs[XFRMA_ALG_CRYPT]) || | |
135 | attrs[XFRMA_ALG_COMP]) | |
1da177e4 LT |
136 | goto out; |
137 | break; | |
138 | ||
139 | case IPPROTO_COMP: | |
35a7aa08 TG |
140 | if (!attrs[XFRMA_ALG_COMP] || |
141 | attrs[XFRMA_ALG_AUTH] || | |
142 | attrs[XFRMA_ALG_CRYPT]) | |
1da177e4 LT |
143 | goto out; |
144 | break; | |
145 | ||
e23c7194 MN |
146 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
147 | case IPPROTO_DSTOPTS: | |
148 | case IPPROTO_ROUTING: | |
35a7aa08 TG |
149 | if (attrs[XFRMA_ALG_COMP] || |
150 | attrs[XFRMA_ALG_AUTH] || | |
151 | attrs[XFRMA_ALG_CRYPT] || | |
152 | attrs[XFRMA_ENCAP] || | |
153 | attrs[XFRMA_SEC_CTX] || | |
154 | !attrs[XFRMA_COADDR]) | |
e23c7194 MN |
155 | goto out; |
156 | break; | |
157 | #endif | |
158 | ||
1da177e4 LT |
159 | default: |
160 | goto out; | |
3ff50b79 | 161 | } |
1da177e4 | 162 | |
35a7aa08 | 163 | if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH))) |
1da177e4 | 164 | goto out; |
35a7aa08 | 165 | if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT))) |
1da177e4 | 166 | goto out; |
35a7aa08 | 167 | if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP))) |
1da177e4 | 168 | goto out; |
35a7aa08 | 169 | if ((err = verify_sec_ctx_len(attrs))) |
df71837d | 170 | goto out; |
1da177e4 LT |
171 | |
172 | err = -EINVAL; | |
173 | switch (p->mode) { | |
7e49e6de MN |
174 | case XFRM_MODE_TRANSPORT: |
175 | case XFRM_MODE_TUNNEL: | |
060f02a3 | 176 | case XFRM_MODE_ROUTEOPTIMIZATION: |
0a69452c | 177 | case XFRM_MODE_BEET: |
1da177e4 LT |
178 | break; |
179 | ||
180 | default: | |
181 | goto out; | |
3ff50b79 | 182 | } |
1da177e4 LT |
183 | |
184 | err = 0; | |
185 | ||
186 | out: | |
187 | return err; | |
188 | } | |
189 | ||
190 | static int attach_one_algo(struct xfrm_algo **algpp, u8 *props, | |
191 | struct xfrm_algo_desc *(*get_byname)(char *, int), | |
5424f32e | 192 | struct nlattr *rta) |
1da177e4 | 193 | { |
1da177e4 LT |
194 | struct xfrm_algo *p, *ualg; |
195 | struct xfrm_algo_desc *algo; | |
196 | ||
197 | if (!rta) | |
198 | return 0; | |
199 | ||
5424f32e | 200 | ualg = nla_data(rta); |
1da177e4 LT |
201 | |
202 | algo = get_byname(ualg->alg_name, 1); | |
203 | if (!algo) | |
204 | return -ENOSYS; | |
205 | *props = algo->desc.sadb_alg_id; | |
206 | ||
c26445ac | 207 | p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL); |
1da177e4 LT |
208 | if (!p) |
209 | return -ENOMEM; | |
210 | ||
04ff1260 | 211 | strcpy(p->alg_name, algo->name); |
1da177e4 LT |
212 | *algpp = p; |
213 | return 0; | |
214 | } | |
215 | ||
661697f7 | 216 | static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx) |
df71837d | 217 | { |
df71837d TJ |
218 | int len = 0; |
219 | ||
220 | if (xfrm_ctx) { | |
221 | len += sizeof(struct xfrm_user_sec_ctx); | |
222 | len += xfrm_ctx->ctx_len; | |
223 | } | |
224 | return len; | |
225 | } | |
226 | ||
1da177e4 LT |
227 | static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) |
228 | { | |
229 | memcpy(&x->id, &p->id, sizeof(x->id)); | |
230 | memcpy(&x->sel, &p->sel, sizeof(x->sel)); | |
231 | memcpy(&x->lft, &p->lft, sizeof(x->lft)); | |
232 | x->props.mode = p->mode; | |
233 | x->props.replay_window = p->replay_window; | |
234 | x->props.reqid = p->reqid; | |
235 | x->props.family = p->family; | |
54489c14 | 236 | memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr)); |
1da177e4 | 237 | x->props.flags = p->flags; |
196b0036 HX |
238 | |
239 | /* | |
240 | * Set inner address family if the KM left it as zero. | |
241 | * See comment in validate_tmpl. | |
242 | */ | |
243 | if (!x->sel.family) | |
244 | x->sel.family = p->family; | |
1da177e4 LT |
245 | } |
246 | ||
d51d081d JHS |
247 | /* |
248 | * someday when pfkey also has support, we could have the code | |
249 | * somehow made shareable and move it to xfrm_state.c - JHS | |
250 | * | |
251 | */ | |
5424f32e | 252 | static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs) |
d51d081d | 253 | { |
5424f32e TG |
254 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; |
255 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; | |
256 | struct nlattr *et = attrs[XFRMA_ETIMER_THRESH]; | |
257 | struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH]; | |
d51d081d JHS |
258 | |
259 | if (rp) { | |
260 | struct xfrm_replay_state *replay; | |
5424f32e | 261 | replay = nla_data(rp); |
d51d081d JHS |
262 | memcpy(&x->replay, replay, sizeof(*replay)); |
263 | memcpy(&x->preplay, replay, sizeof(*replay)); | |
264 | } | |
265 | ||
266 | if (lt) { | |
267 | struct xfrm_lifetime_cur *ltime; | |
5424f32e | 268 | ltime = nla_data(lt); |
d51d081d JHS |
269 | x->curlft.bytes = ltime->bytes; |
270 | x->curlft.packets = ltime->packets; | |
271 | x->curlft.add_time = ltime->add_time; | |
272 | x->curlft.use_time = ltime->use_time; | |
273 | } | |
274 | ||
cf5cb79f | 275 | if (et) |
5424f32e | 276 | x->replay_maxage = nla_get_u32(et); |
d51d081d | 277 | |
cf5cb79f | 278 | if (rt) |
5424f32e | 279 | x->replay_maxdiff = nla_get_u32(rt); |
d51d081d JHS |
280 | } |
281 | ||
1da177e4 | 282 | static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p, |
5424f32e | 283 | struct nlattr **attrs, |
1da177e4 LT |
284 | int *errp) |
285 | { | |
286 | struct xfrm_state *x = xfrm_state_alloc(); | |
287 | int err = -ENOMEM; | |
288 | ||
289 | if (!x) | |
290 | goto error_no_put; | |
291 | ||
292 | copy_from_user_state(x, p); | |
293 | ||
294 | if ((err = attach_one_algo(&x->aalg, &x->props.aalgo, | |
295 | xfrm_aalg_get_byname, | |
35a7aa08 | 296 | attrs[XFRMA_ALG_AUTH]))) |
1da177e4 LT |
297 | goto error; |
298 | if ((err = attach_one_algo(&x->ealg, &x->props.ealgo, | |
299 | xfrm_ealg_get_byname, | |
35a7aa08 | 300 | attrs[XFRMA_ALG_CRYPT]))) |
1da177e4 LT |
301 | goto error; |
302 | if ((err = attach_one_algo(&x->calg, &x->props.calgo, | |
303 | xfrm_calg_get_byname, | |
35a7aa08 | 304 | attrs[XFRMA_ALG_COMP]))) |
1da177e4 | 305 | goto error; |
fd21150a TG |
306 | |
307 | if (attrs[XFRMA_ENCAP]) { | |
308 | x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]), | |
309 | sizeof(*x->encap), GFP_KERNEL); | |
310 | if (x->encap == NULL) | |
311 | goto error; | |
312 | } | |
313 | ||
314 | if (attrs[XFRMA_COADDR]) { | |
315 | x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]), | |
316 | sizeof(*x->coaddr), GFP_KERNEL); | |
317 | if (x->coaddr == NULL) | |
318 | goto error; | |
319 | } | |
320 | ||
72cb6962 | 321 | err = xfrm_init_state(x); |
1da177e4 LT |
322 | if (err) |
323 | goto error; | |
324 | ||
fd21150a TG |
325 | if (attrs[XFRMA_SEC_CTX] && |
326 | security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX]))) | |
df71837d TJ |
327 | goto error; |
328 | ||
1da177e4 | 329 | x->km.seq = p->seq; |
d51d081d JHS |
330 | x->replay_maxdiff = sysctl_xfrm_aevent_rseqth; |
331 | /* sysctl_xfrm_aevent_etime is in 100ms units */ | |
332 | x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M; | |
333 | x->preplay.bitmap = 0; | |
334 | x->preplay.seq = x->replay.seq+x->replay_maxdiff; | |
335 | x->preplay.oseq = x->replay.oseq +x->replay_maxdiff; | |
336 | ||
337 | /* override default values from above */ | |
338 | ||
5424f32e | 339 | xfrm_update_ae_params(x, attrs); |
1da177e4 LT |
340 | |
341 | return x; | |
342 | ||
343 | error: | |
344 | x->km.state = XFRM_STATE_DEAD; | |
345 | xfrm_state_put(x); | |
346 | error_no_put: | |
347 | *errp = err; | |
348 | return NULL; | |
349 | } | |
350 | ||
22e70050 | 351 | static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 352 | struct nlattr **attrs) |
1da177e4 | 353 | { |
7b67c857 | 354 | struct xfrm_usersa_info *p = nlmsg_data(nlh); |
1da177e4 LT |
355 | struct xfrm_state *x; |
356 | int err; | |
26b15dad | 357 | struct km_event c; |
1da177e4 | 358 | |
35a7aa08 | 359 | err = verify_newsa_info(p, attrs); |
1da177e4 LT |
360 | if (err) |
361 | return err; | |
362 | ||
35a7aa08 | 363 | x = xfrm_state_construct(p, attrs, &err); |
1da177e4 LT |
364 | if (!x) |
365 | return err; | |
366 | ||
26b15dad | 367 | xfrm_state_hold(x); |
1da177e4 LT |
368 | if (nlh->nlmsg_type == XFRM_MSG_NEWSA) |
369 | err = xfrm_state_add(x); | |
370 | else | |
371 | err = xfrm_state_update(x); | |
372 | ||
ab5f5e8b JL |
373 | xfrm_audit_state_add(x, err ? 0 : 1, NETLINK_CB(skb).loginuid, |
374 | NETLINK_CB(skb).sid); | |
161a09e7 | 375 | |
1da177e4 LT |
376 | if (err < 0) { |
377 | x->km.state = XFRM_STATE_DEAD; | |
21380b81 | 378 | __xfrm_state_put(x); |
7d6dfe1f | 379 | goto out; |
1da177e4 LT |
380 | } |
381 | ||
26b15dad JHS |
382 | c.seq = nlh->nlmsg_seq; |
383 | c.pid = nlh->nlmsg_pid; | |
f60f6b8f | 384 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
385 | |
386 | km_state_notify(x, &c); | |
7d6dfe1f | 387 | out: |
26b15dad | 388 | xfrm_state_put(x); |
1da177e4 LT |
389 | return err; |
390 | } | |
391 | ||
eb2971b6 | 392 | static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p, |
5424f32e | 393 | struct nlattr **attrs, |
eb2971b6 MN |
394 | int *errp) |
395 | { | |
396 | struct xfrm_state *x = NULL; | |
397 | int err; | |
398 | ||
399 | if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) { | |
400 | err = -ESRCH; | |
401 | x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family); | |
402 | } else { | |
403 | xfrm_address_t *saddr = NULL; | |
404 | ||
35a7aa08 | 405 | verify_one_addr(attrs, XFRMA_SRCADDR, &saddr); |
eb2971b6 MN |
406 | if (!saddr) { |
407 | err = -EINVAL; | |
408 | goto out; | |
409 | } | |
410 | ||
9abbffee | 411 | err = -ESRCH; |
eb2971b6 MN |
412 | x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto, |
413 | p->family); | |
414 | } | |
415 | ||
416 | out: | |
417 | if (!x && errp) | |
418 | *errp = err; | |
419 | return x; | |
420 | } | |
421 | ||
22e70050 | 422 | static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 423 | struct nlattr **attrs) |
1da177e4 LT |
424 | { |
425 | struct xfrm_state *x; | |
eb2971b6 | 426 | int err = -ESRCH; |
26b15dad | 427 | struct km_event c; |
7b67c857 | 428 | struct xfrm_usersa_id *p = nlmsg_data(nlh); |
1da177e4 | 429 | |
35a7aa08 | 430 | x = xfrm_user_state_lookup(p, attrs, &err); |
1da177e4 | 431 | if (x == NULL) |
eb2971b6 | 432 | return err; |
1da177e4 | 433 | |
6f68dc37 | 434 | if ((err = security_xfrm_state_delete(x)) != 0) |
c8c05a8e CZ |
435 | goto out; |
436 | ||
1da177e4 | 437 | if (xfrm_state_kern(x)) { |
c8c05a8e CZ |
438 | err = -EPERM; |
439 | goto out; | |
1da177e4 LT |
440 | } |
441 | ||
26b15dad | 442 | err = xfrm_state_delete(x); |
161a09e7 | 443 | |
c8c05a8e CZ |
444 | if (err < 0) |
445 | goto out; | |
26b15dad JHS |
446 | |
447 | c.seq = nlh->nlmsg_seq; | |
448 | c.pid = nlh->nlmsg_pid; | |
f60f6b8f | 449 | c.event = nlh->nlmsg_type; |
26b15dad | 450 | km_state_notify(x, &c); |
1da177e4 | 451 | |
c8c05a8e | 452 | out: |
ab5f5e8b JL |
453 | xfrm_audit_state_delete(x, err ? 0 : 1, NETLINK_CB(skb).loginuid, |
454 | NETLINK_CB(skb).sid); | |
c8c05a8e | 455 | xfrm_state_put(x); |
26b15dad | 456 | return err; |
1da177e4 LT |
457 | } |
458 | ||
459 | static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p) | |
460 | { | |
461 | memcpy(&p->id, &x->id, sizeof(p->id)); | |
462 | memcpy(&p->sel, &x->sel, sizeof(p->sel)); | |
463 | memcpy(&p->lft, &x->lft, sizeof(p->lft)); | |
464 | memcpy(&p->curlft, &x->curlft, sizeof(p->curlft)); | |
465 | memcpy(&p->stats, &x->stats, sizeof(p->stats)); | |
54489c14 | 466 | memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr)); |
1da177e4 LT |
467 | p->mode = x->props.mode; |
468 | p->replay_window = x->props.replay_window; | |
469 | p->reqid = x->props.reqid; | |
470 | p->family = x->props.family; | |
471 | p->flags = x->props.flags; | |
472 | p->seq = x->km.seq; | |
473 | } | |
474 | ||
475 | struct xfrm_dump_info { | |
476 | struct sk_buff *in_skb; | |
477 | struct sk_buff *out_skb; | |
478 | u32 nlmsg_seq; | |
479 | u16 nlmsg_flags; | |
480 | int start_idx; | |
481 | int this_idx; | |
482 | }; | |
483 | ||
c0144bea TG |
484 | static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb) |
485 | { | |
c0144bea TG |
486 | struct xfrm_user_sec_ctx *uctx; |
487 | struct nlattr *attr; | |
68325d3b | 488 | int ctx_size = sizeof(*uctx) + s->ctx_len; |
c0144bea TG |
489 | |
490 | attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size); | |
491 | if (attr == NULL) | |
492 | return -EMSGSIZE; | |
493 | ||
494 | uctx = nla_data(attr); | |
495 | uctx->exttype = XFRMA_SEC_CTX; | |
496 | uctx->len = ctx_size; | |
497 | uctx->ctx_doi = s->ctx_doi; | |
498 | uctx->ctx_alg = s->ctx_alg; | |
499 | uctx->ctx_len = s->ctx_len; | |
500 | memcpy(uctx + 1, s->ctx_str, s->ctx_len); | |
501 | ||
502 | return 0; | |
503 | } | |
504 | ||
68325d3b HX |
505 | /* Don't change this without updating xfrm_sa_len! */ |
506 | static int copy_to_user_state_extra(struct xfrm_state *x, | |
507 | struct xfrm_usersa_info *p, | |
508 | struct sk_buff *skb) | |
1da177e4 | 509 | { |
1da177e4 LT |
510 | copy_to_user_state(x, p); |
511 | ||
050f009e HX |
512 | if (x->coaddr) |
513 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr); | |
514 | ||
515 | if (x->lastused) | |
516 | NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused); | |
050f009e | 517 | |
1da177e4 | 518 | if (x->aalg) |
c26445ac | 519 | NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg); |
1da177e4 | 520 | if (x->ealg) |
c26445ac | 521 | NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg); |
1da177e4 | 522 | if (x->calg) |
c0144bea | 523 | NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg); |
1da177e4 LT |
524 | |
525 | if (x->encap) | |
c0144bea | 526 | NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap); |
1da177e4 | 527 | |
c0144bea TG |
528 | if (x->security && copy_sec_ctx(x->security, skb) < 0) |
529 | goto nla_put_failure; | |
060f02a3 | 530 | |
68325d3b HX |
531 | return 0; |
532 | ||
533 | nla_put_failure: | |
534 | return -EMSGSIZE; | |
535 | } | |
536 | ||
537 | static int dump_one_state(struct xfrm_state *x, int count, void *ptr) | |
538 | { | |
539 | struct xfrm_dump_info *sp = ptr; | |
540 | struct sk_buff *in_skb = sp->in_skb; | |
541 | struct sk_buff *skb = sp->out_skb; | |
542 | struct xfrm_usersa_info *p; | |
543 | struct nlmsghdr *nlh; | |
544 | int err; | |
545 | ||
546 | if (sp->this_idx < sp->start_idx) | |
547 | goto out; | |
548 | ||
549 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, | |
550 | XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags); | |
551 | if (nlh == NULL) | |
552 | return -EMSGSIZE; | |
553 | ||
554 | p = nlmsg_data(nlh); | |
555 | ||
556 | err = copy_to_user_state_extra(x, p, skb); | |
557 | if (err) | |
558 | goto nla_put_failure; | |
559 | ||
9825069d | 560 | nlmsg_end(skb, nlh); |
1da177e4 LT |
561 | out: |
562 | sp->this_idx++; | |
563 | return 0; | |
564 | ||
c0144bea | 565 | nla_put_failure: |
9825069d | 566 | nlmsg_cancel(skb, nlh); |
68325d3b | 567 | return err; |
1da177e4 LT |
568 | } |
569 | ||
570 | static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb) | |
571 | { | |
572 | struct xfrm_dump_info info; | |
573 | ||
574 | info.in_skb = cb->skb; | |
575 | info.out_skb = skb; | |
576 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | |
577 | info.nlmsg_flags = NLM_F_MULTI; | |
578 | info.this_idx = 0; | |
579 | info.start_idx = cb->args[0]; | |
dc00a525 | 580 | (void) xfrm_state_walk(0, dump_one_state, &info); |
1da177e4 LT |
581 | cb->args[0] = info.this_idx; |
582 | ||
583 | return skb->len; | |
584 | } | |
585 | ||
586 | static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb, | |
587 | struct xfrm_state *x, u32 seq) | |
588 | { | |
589 | struct xfrm_dump_info info; | |
590 | struct sk_buff *skb; | |
591 | ||
7deb2264 | 592 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
1da177e4 LT |
593 | if (!skb) |
594 | return ERR_PTR(-ENOMEM); | |
595 | ||
1da177e4 LT |
596 | info.in_skb = in_skb; |
597 | info.out_skb = skb; | |
598 | info.nlmsg_seq = seq; | |
599 | info.nlmsg_flags = 0; | |
600 | info.this_idx = info.start_idx = 0; | |
601 | ||
602 | if (dump_one_state(x, 0, &info)) { | |
603 | kfree_skb(skb); | |
604 | return NULL; | |
605 | } | |
606 | ||
607 | return skb; | |
608 | } | |
609 | ||
7deb2264 TG |
610 | static inline size_t xfrm_spdinfo_msgsize(void) |
611 | { | |
612 | return NLMSG_ALIGN(4) | |
613 | + nla_total_size(sizeof(struct xfrmu_spdinfo)) | |
614 | + nla_total_size(sizeof(struct xfrmu_spdhinfo)); | |
615 | } | |
616 | ||
ecfd6b18 JHS |
617 | static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags) |
618 | { | |
5a6d3416 JHS |
619 | struct xfrmk_spdinfo si; |
620 | struct xfrmu_spdinfo spc; | |
621 | struct xfrmu_spdhinfo sph; | |
ecfd6b18 JHS |
622 | struct nlmsghdr *nlh; |
623 | u32 *f; | |
624 | ||
625 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0); | |
626 | if (nlh == NULL) /* shouldnt really happen ... */ | |
627 | return -EMSGSIZE; | |
628 | ||
629 | f = nlmsg_data(nlh); | |
630 | *f = flags; | |
631 | xfrm_spd_getinfo(&si); | |
5a6d3416 JHS |
632 | spc.incnt = si.incnt; |
633 | spc.outcnt = si.outcnt; | |
634 | spc.fwdcnt = si.fwdcnt; | |
635 | spc.inscnt = si.inscnt; | |
636 | spc.outscnt = si.outscnt; | |
637 | spc.fwdscnt = si.fwdscnt; | |
638 | sph.spdhcnt = si.spdhcnt; | |
639 | sph.spdhmcnt = si.spdhmcnt; | |
640 | ||
641 | NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc); | |
642 | NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph); | |
ecfd6b18 JHS |
643 | |
644 | return nlmsg_end(skb, nlh); | |
645 | ||
646 | nla_put_failure: | |
647 | nlmsg_cancel(skb, nlh); | |
648 | return -EMSGSIZE; | |
649 | } | |
650 | ||
651 | static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 652 | struct nlattr **attrs) |
ecfd6b18 JHS |
653 | { |
654 | struct sk_buff *r_skb; | |
7b67c857 | 655 | u32 *flags = nlmsg_data(nlh); |
ecfd6b18 JHS |
656 | u32 spid = NETLINK_CB(skb).pid; |
657 | u32 seq = nlh->nlmsg_seq; | |
ecfd6b18 | 658 | |
7deb2264 | 659 | r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC); |
ecfd6b18 JHS |
660 | if (r_skb == NULL) |
661 | return -ENOMEM; | |
662 | ||
663 | if (build_spdinfo(r_skb, spid, seq, *flags) < 0) | |
664 | BUG(); | |
665 | ||
666 | return nlmsg_unicast(xfrm_nl, r_skb, spid); | |
667 | } | |
668 | ||
7deb2264 TG |
669 | static inline size_t xfrm_sadinfo_msgsize(void) |
670 | { | |
671 | return NLMSG_ALIGN(4) | |
672 | + nla_total_size(sizeof(struct xfrmu_sadhinfo)) | |
673 | + nla_total_size(4); /* XFRMA_SAD_CNT */ | |
674 | } | |
675 | ||
28d8909b JHS |
676 | static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags) |
677 | { | |
af11e316 JHS |
678 | struct xfrmk_sadinfo si; |
679 | struct xfrmu_sadhinfo sh; | |
28d8909b JHS |
680 | struct nlmsghdr *nlh; |
681 | u32 *f; | |
682 | ||
683 | nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0); | |
684 | if (nlh == NULL) /* shouldnt really happen ... */ | |
685 | return -EMSGSIZE; | |
686 | ||
687 | f = nlmsg_data(nlh); | |
688 | *f = flags; | |
689 | xfrm_sad_getinfo(&si); | |
690 | ||
af11e316 JHS |
691 | sh.sadhmcnt = si.sadhmcnt; |
692 | sh.sadhcnt = si.sadhcnt; | |
693 | ||
694 | NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt); | |
695 | NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh); | |
28d8909b JHS |
696 | |
697 | return nlmsg_end(skb, nlh); | |
698 | ||
699 | nla_put_failure: | |
700 | nlmsg_cancel(skb, nlh); | |
701 | return -EMSGSIZE; | |
702 | } | |
703 | ||
704 | static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 705 | struct nlattr **attrs) |
28d8909b JHS |
706 | { |
707 | struct sk_buff *r_skb; | |
7b67c857 | 708 | u32 *flags = nlmsg_data(nlh); |
28d8909b JHS |
709 | u32 spid = NETLINK_CB(skb).pid; |
710 | u32 seq = nlh->nlmsg_seq; | |
28d8909b | 711 | |
7deb2264 | 712 | r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC); |
28d8909b JHS |
713 | if (r_skb == NULL) |
714 | return -ENOMEM; | |
715 | ||
716 | if (build_sadinfo(r_skb, spid, seq, *flags) < 0) | |
717 | BUG(); | |
718 | ||
719 | return nlmsg_unicast(xfrm_nl, r_skb, spid); | |
720 | } | |
721 | ||
22e70050 | 722 | static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 723 | struct nlattr **attrs) |
1da177e4 | 724 | { |
7b67c857 | 725 | struct xfrm_usersa_id *p = nlmsg_data(nlh); |
1da177e4 LT |
726 | struct xfrm_state *x; |
727 | struct sk_buff *resp_skb; | |
eb2971b6 | 728 | int err = -ESRCH; |
1da177e4 | 729 | |
35a7aa08 | 730 | x = xfrm_user_state_lookup(p, attrs, &err); |
1da177e4 LT |
731 | if (x == NULL) |
732 | goto out_noput; | |
733 | ||
734 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); | |
735 | if (IS_ERR(resp_skb)) { | |
736 | err = PTR_ERR(resp_skb); | |
737 | } else { | |
082a1ad5 | 738 | err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid); |
1da177e4 LT |
739 | } |
740 | xfrm_state_put(x); | |
741 | out_noput: | |
742 | return err; | |
743 | } | |
744 | ||
745 | static int verify_userspi_info(struct xfrm_userspi_info *p) | |
746 | { | |
747 | switch (p->info.id.proto) { | |
748 | case IPPROTO_AH: | |
749 | case IPPROTO_ESP: | |
750 | break; | |
751 | ||
752 | case IPPROTO_COMP: | |
753 | /* IPCOMP spi is 16-bits. */ | |
754 | if (p->max >= 0x10000) | |
755 | return -EINVAL; | |
756 | break; | |
757 | ||
758 | default: | |
759 | return -EINVAL; | |
3ff50b79 | 760 | } |
1da177e4 LT |
761 | |
762 | if (p->min > p->max) | |
763 | return -EINVAL; | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
22e70050 | 768 | static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 769 | struct nlattr **attrs) |
1da177e4 LT |
770 | { |
771 | struct xfrm_state *x; | |
772 | struct xfrm_userspi_info *p; | |
773 | struct sk_buff *resp_skb; | |
774 | xfrm_address_t *daddr; | |
775 | int family; | |
776 | int err; | |
777 | ||
7b67c857 | 778 | p = nlmsg_data(nlh); |
1da177e4 LT |
779 | err = verify_userspi_info(p); |
780 | if (err) | |
781 | goto out_noput; | |
782 | ||
783 | family = p->info.family; | |
784 | daddr = &p->info.id.daddr; | |
785 | ||
786 | x = NULL; | |
787 | if (p->info.seq) { | |
788 | x = xfrm_find_acq_byseq(p->info.seq); | |
789 | if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) { | |
790 | xfrm_state_put(x); | |
791 | x = NULL; | |
792 | } | |
793 | } | |
794 | ||
795 | if (!x) | |
796 | x = xfrm_find_acq(p->info.mode, p->info.reqid, | |
797 | p->info.id.proto, daddr, | |
798 | &p->info.saddr, 1, | |
799 | family); | |
800 | err = -ENOENT; | |
801 | if (x == NULL) | |
802 | goto out_noput; | |
803 | ||
658b219e HX |
804 | err = xfrm_alloc_spi(x, p->min, p->max); |
805 | if (err) | |
806 | goto out; | |
1da177e4 | 807 | |
658b219e | 808 | resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq); |
1da177e4 LT |
809 | if (IS_ERR(resp_skb)) { |
810 | err = PTR_ERR(resp_skb); | |
811 | goto out; | |
812 | } | |
813 | ||
082a1ad5 | 814 | err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid); |
1da177e4 LT |
815 | |
816 | out: | |
817 | xfrm_state_put(x); | |
818 | out_noput: | |
819 | return err; | |
820 | } | |
821 | ||
b798a9ed | 822 | static int verify_policy_dir(u8 dir) |
1da177e4 LT |
823 | { |
824 | switch (dir) { | |
825 | case XFRM_POLICY_IN: | |
826 | case XFRM_POLICY_OUT: | |
827 | case XFRM_POLICY_FWD: | |
828 | break; | |
829 | ||
830 | default: | |
831 | return -EINVAL; | |
3ff50b79 | 832 | } |
1da177e4 LT |
833 | |
834 | return 0; | |
835 | } | |
836 | ||
b798a9ed | 837 | static int verify_policy_type(u8 type) |
f7b6983f MN |
838 | { |
839 | switch (type) { | |
840 | case XFRM_POLICY_TYPE_MAIN: | |
841 | #ifdef CONFIG_XFRM_SUB_POLICY | |
842 | case XFRM_POLICY_TYPE_SUB: | |
843 | #endif | |
844 | break; | |
845 | ||
846 | default: | |
847 | return -EINVAL; | |
3ff50b79 | 848 | } |
f7b6983f MN |
849 | |
850 | return 0; | |
851 | } | |
852 | ||
1da177e4 LT |
853 | static int verify_newpolicy_info(struct xfrm_userpolicy_info *p) |
854 | { | |
855 | switch (p->share) { | |
856 | case XFRM_SHARE_ANY: | |
857 | case XFRM_SHARE_SESSION: | |
858 | case XFRM_SHARE_USER: | |
859 | case XFRM_SHARE_UNIQUE: | |
860 | break; | |
861 | ||
862 | default: | |
863 | return -EINVAL; | |
3ff50b79 | 864 | } |
1da177e4 LT |
865 | |
866 | switch (p->action) { | |
867 | case XFRM_POLICY_ALLOW: | |
868 | case XFRM_POLICY_BLOCK: | |
869 | break; | |
870 | ||
871 | default: | |
872 | return -EINVAL; | |
3ff50b79 | 873 | } |
1da177e4 LT |
874 | |
875 | switch (p->sel.family) { | |
876 | case AF_INET: | |
877 | break; | |
878 | ||
879 | case AF_INET6: | |
880 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
881 | break; | |
882 | #else | |
883 | return -EAFNOSUPPORT; | |
884 | #endif | |
885 | ||
886 | default: | |
887 | return -EINVAL; | |
3ff50b79 | 888 | } |
1da177e4 LT |
889 | |
890 | return verify_policy_dir(p->dir); | |
891 | } | |
892 | ||
5424f32e | 893 | static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs) |
df71837d | 894 | { |
5424f32e | 895 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
df71837d TJ |
896 | struct xfrm_user_sec_ctx *uctx; |
897 | ||
898 | if (!rt) | |
899 | return 0; | |
900 | ||
5424f32e | 901 | uctx = nla_data(rt); |
df71837d TJ |
902 | return security_xfrm_policy_alloc(pol, uctx); |
903 | } | |
904 | ||
1da177e4 LT |
905 | static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut, |
906 | int nr) | |
907 | { | |
908 | int i; | |
909 | ||
910 | xp->xfrm_nr = nr; | |
911 | for (i = 0; i < nr; i++, ut++) { | |
912 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | |
913 | ||
914 | memcpy(&t->id, &ut->id, sizeof(struct xfrm_id)); | |
915 | memcpy(&t->saddr, &ut->saddr, | |
916 | sizeof(xfrm_address_t)); | |
917 | t->reqid = ut->reqid; | |
918 | t->mode = ut->mode; | |
919 | t->share = ut->share; | |
920 | t->optional = ut->optional; | |
921 | t->aalgos = ut->aalgos; | |
922 | t->ealgos = ut->ealgos; | |
923 | t->calgos = ut->calgos; | |
8511d01d | 924 | t->encap_family = ut->family; |
1da177e4 LT |
925 | } |
926 | } | |
927 | ||
b4ad86bf DM |
928 | static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family) |
929 | { | |
930 | int i; | |
931 | ||
932 | if (nr > XFRM_MAX_DEPTH) | |
933 | return -EINVAL; | |
934 | ||
935 | for (i = 0; i < nr; i++) { | |
936 | /* We never validated the ut->family value, so many | |
937 | * applications simply leave it at zero. The check was | |
938 | * never made and ut->family was ignored because all | |
939 | * templates could be assumed to have the same family as | |
940 | * the policy itself. Now that we will have ipv4-in-ipv6 | |
941 | * and ipv6-in-ipv4 tunnels, this is no longer true. | |
942 | */ | |
943 | if (!ut[i].family) | |
944 | ut[i].family = family; | |
945 | ||
946 | switch (ut[i].family) { | |
947 | case AF_INET: | |
948 | break; | |
949 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
950 | case AF_INET6: | |
951 | break; | |
952 | #endif | |
953 | default: | |
954 | return -EINVAL; | |
3ff50b79 | 955 | } |
b4ad86bf DM |
956 | } |
957 | ||
958 | return 0; | |
959 | } | |
960 | ||
5424f32e | 961 | static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs) |
1da177e4 | 962 | { |
5424f32e | 963 | struct nlattr *rt = attrs[XFRMA_TMPL]; |
1da177e4 LT |
964 | |
965 | if (!rt) { | |
966 | pol->xfrm_nr = 0; | |
967 | } else { | |
5424f32e TG |
968 | struct xfrm_user_tmpl *utmpl = nla_data(rt); |
969 | int nr = nla_len(rt) / sizeof(*utmpl); | |
b4ad86bf | 970 | int err; |
1da177e4 | 971 | |
b4ad86bf DM |
972 | err = validate_tmpl(nr, utmpl, pol->family); |
973 | if (err) | |
974 | return err; | |
1da177e4 | 975 | |
5424f32e | 976 | copy_templates(pol, utmpl, nr); |
1da177e4 LT |
977 | } |
978 | return 0; | |
979 | } | |
980 | ||
5424f32e | 981 | static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs) |
f7b6983f | 982 | { |
5424f32e | 983 | struct nlattr *rt = attrs[XFRMA_POLICY_TYPE]; |
f7b6983f | 984 | struct xfrm_userpolicy_type *upt; |
b798a9ed | 985 | u8 type = XFRM_POLICY_TYPE_MAIN; |
f7b6983f MN |
986 | int err; |
987 | ||
988 | if (rt) { | |
5424f32e | 989 | upt = nla_data(rt); |
f7b6983f MN |
990 | type = upt->type; |
991 | } | |
992 | ||
993 | err = verify_policy_type(type); | |
994 | if (err) | |
995 | return err; | |
996 | ||
997 | *tp = type; | |
998 | return 0; | |
999 | } | |
1000 | ||
1da177e4 LT |
1001 | static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p) |
1002 | { | |
1003 | xp->priority = p->priority; | |
1004 | xp->index = p->index; | |
1005 | memcpy(&xp->selector, &p->sel, sizeof(xp->selector)); | |
1006 | memcpy(&xp->lft, &p->lft, sizeof(xp->lft)); | |
1007 | xp->action = p->action; | |
1008 | xp->flags = p->flags; | |
1009 | xp->family = p->sel.family; | |
1010 | /* XXX xp->share = p->share; */ | |
1011 | } | |
1012 | ||
1013 | static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir) | |
1014 | { | |
1015 | memcpy(&p->sel, &xp->selector, sizeof(p->sel)); | |
1016 | memcpy(&p->lft, &xp->lft, sizeof(p->lft)); | |
1017 | memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft)); | |
1018 | p->priority = xp->priority; | |
1019 | p->index = xp->index; | |
1020 | p->sel.family = xp->family; | |
1021 | p->dir = dir; | |
1022 | p->action = xp->action; | |
1023 | p->flags = xp->flags; | |
1024 | p->share = XFRM_SHARE_ANY; /* XXX xp->share */ | |
1025 | } | |
1026 | ||
5424f32e | 1027 | static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp) |
1da177e4 LT |
1028 | { |
1029 | struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL); | |
1030 | int err; | |
1031 | ||
1032 | if (!xp) { | |
1033 | *errp = -ENOMEM; | |
1034 | return NULL; | |
1035 | } | |
1036 | ||
1037 | copy_from_user_policy(xp, p); | |
df71837d | 1038 | |
35a7aa08 | 1039 | err = copy_from_user_policy_type(&xp->type, attrs); |
f7b6983f MN |
1040 | if (err) |
1041 | goto error; | |
1042 | ||
35a7aa08 TG |
1043 | if (!(err = copy_from_user_tmpl(xp, attrs))) |
1044 | err = copy_from_user_sec_ctx(xp, attrs); | |
f7b6983f MN |
1045 | if (err) |
1046 | goto error; | |
1da177e4 LT |
1047 | |
1048 | return xp; | |
f7b6983f MN |
1049 | error: |
1050 | *errp = err; | |
1051 | kfree(xp); | |
1052 | return NULL; | |
1da177e4 LT |
1053 | } |
1054 | ||
22e70050 | 1055 | static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1056 | struct nlattr **attrs) |
1da177e4 | 1057 | { |
7b67c857 | 1058 | struct xfrm_userpolicy_info *p = nlmsg_data(nlh); |
1da177e4 | 1059 | struct xfrm_policy *xp; |
26b15dad | 1060 | struct km_event c; |
1da177e4 LT |
1061 | int err; |
1062 | int excl; | |
1063 | ||
1064 | err = verify_newpolicy_info(p); | |
df71837d TJ |
1065 | if (err) |
1066 | return err; | |
35a7aa08 | 1067 | err = verify_sec_ctx_len(attrs); |
1da177e4 LT |
1068 | if (err) |
1069 | return err; | |
1070 | ||
35a7aa08 | 1071 | xp = xfrm_policy_construct(p, attrs, &err); |
1da177e4 LT |
1072 | if (!xp) |
1073 | return err; | |
1074 | ||
26b15dad JHS |
1075 | /* shouldnt excl be based on nlh flags?? |
1076 | * Aha! this is anti-netlink really i.e more pfkey derived | |
1077 | * in netlink excl is a flag and you wouldnt need | |
1078 | * a type XFRM_MSG_UPDPOLICY - JHS */ | |
1da177e4 LT |
1079 | excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY; |
1080 | err = xfrm_policy_insert(p->dir, xp, excl); | |
ab5f5e8b JL |
1081 | xfrm_audit_policy_add(xp, err ? 0 : 1, NETLINK_CB(skb).loginuid, |
1082 | NETLINK_CB(skb).sid); | |
161a09e7 | 1083 | |
1da177e4 | 1084 | if (err) { |
5f8ac64b | 1085 | security_xfrm_policy_free(xp); |
1da177e4 LT |
1086 | kfree(xp); |
1087 | return err; | |
1088 | } | |
1089 | ||
f60f6b8f | 1090 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1091 | c.seq = nlh->nlmsg_seq; |
1092 | c.pid = nlh->nlmsg_pid; | |
1093 | km_policy_notify(xp, p->dir, &c); | |
1094 | ||
1da177e4 LT |
1095 | xfrm_pol_put(xp); |
1096 | ||
1097 | return 0; | |
1098 | } | |
1099 | ||
1100 | static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb) | |
1101 | { | |
1102 | struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH]; | |
1103 | int i; | |
1104 | ||
1105 | if (xp->xfrm_nr == 0) | |
1106 | return 0; | |
1107 | ||
1108 | for (i = 0; i < xp->xfrm_nr; i++) { | |
1109 | struct xfrm_user_tmpl *up = &vec[i]; | |
1110 | struct xfrm_tmpl *kp = &xp->xfrm_vec[i]; | |
1111 | ||
1112 | memcpy(&up->id, &kp->id, sizeof(up->id)); | |
8511d01d | 1113 | up->family = kp->encap_family; |
1da177e4 LT |
1114 | memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr)); |
1115 | up->reqid = kp->reqid; | |
1116 | up->mode = kp->mode; | |
1117 | up->share = kp->share; | |
1118 | up->optional = kp->optional; | |
1119 | up->aalgos = kp->aalgos; | |
1120 | up->ealgos = kp->ealgos; | |
1121 | up->calgos = kp->calgos; | |
1122 | } | |
df71837d | 1123 | |
c0144bea TG |
1124 | return nla_put(skb, XFRMA_TMPL, |
1125 | sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec); | |
0d681623 SH |
1126 | } |
1127 | ||
1128 | static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb) | |
1129 | { | |
1130 | if (x->security) { | |
1131 | return copy_sec_ctx(x->security, skb); | |
df71837d TJ |
1132 | } |
1133 | return 0; | |
0d681623 | 1134 | } |
df71837d | 1135 | |
0d681623 SH |
1136 | static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb) |
1137 | { | |
1138 | if (xp->security) { | |
1139 | return copy_sec_ctx(xp->security, skb); | |
1140 | } | |
1141 | return 0; | |
df71837d | 1142 | } |
cfbfd45a TG |
1143 | static inline size_t userpolicy_type_attrsize(void) |
1144 | { | |
1145 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1146 | return nla_total_size(sizeof(struct xfrm_userpolicy_type)); | |
1147 | #else | |
1148 | return 0; | |
1149 | #endif | |
1150 | } | |
df71837d | 1151 | |
f7b6983f | 1152 | #ifdef CONFIG_XFRM_SUB_POLICY |
b798a9ed | 1153 | static int copy_to_user_policy_type(u8 type, struct sk_buff *skb) |
f7b6983f | 1154 | { |
c0144bea TG |
1155 | struct xfrm_userpolicy_type upt = { |
1156 | .type = type, | |
1157 | }; | |
f7b6983f | 1158 | |
c0144bea | 1159 | return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt); |
f7b6983f MN |
1160 | } |
1161 | ||
1162 | #else | |
b798a9ed | 1163 | static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb) |
f7b6983f MN |
1164 | { |
1165 | return 0; | |
1166 | } | |
1167 | #endif | |
1168 | ||
1da177e4 LT |
1169 | static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr) |
1170 | { | |
1171 | struct xfrm_dump_info *sp = ptr; | |
1172 | struct xfrm_userpolicy_info *p; | |
1173 | struct sk_buff *in_skb = sp->in_skb; | |
1174 | struct sk_buff *skb = sp->out_skb; | |
1175 | struct nlmsghdr *nlh; | |
1da177e4 LT |
1176 | |
1177 | if (sp->this_idx < sp->start_idx) | |
1178 | goto out; | |
1179 | ||
79b8b7f4 TG |
1180 | nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq, |
1181 | XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags); | |
1182 | if (nlh == NULL) | |
1183 | return -EMSGSIZE; | |
1da177e4 | 1184 | |
7b67c857 | 1185 | p = nlmsg_data(nlh); |
1da177e4 LT |
1186 | copy_to_user_policy(xp, p, dir); |
1187 | if (copy_to_user_tmpl(xp, skb) < 0) | |
1188 | goto nlmsg_failure; | |
df71837d TJ |
1189 | if (copy_to_user_sec_ctx(xp, skb)) |
1190 | goto nlmsg_failure; | |
1459bb36 | 1191 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 1192 | goto nlmsg_failure; |
1da177e4 | 1193 | |
9825069d | 1194 | nlmsg_end(skb, nlh); |
1da177e4 LT |
1195 | out: |
1196 | sp->this_idx++; | |
1197 | return 0; | |
1198 | ||
1199 | nlmsg_failure: | |
9825069d TG |
1200 | nlmsg_cancel(skb, nlh); |
1201 | return -EMSGSIZE; | |
1da177e4 LT |
1202 | } |
1203 | ||
1204 | static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb) | |
1205 | { | |
1206 | struct xfrm_dump_info info; | |
1207 | ||
1208 | info.in_skb = cb->skb; | |
1209 | info.out_skb = skb; | |
1210 | info.nlmsg_seq = cb->nlh->nlmsg_seq; | |
1211 | info.nlmsg_flags = NLM_F_MULTI; | |
1212 | info.this_idx = 0; | |
1213 | info.start_idx = cb->args[0]; | |
f7b6983f MN |
1214 | (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info); |
1215 | #ifdef CONFIG_XFRM_SUB_POLICY | |
1216 | (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info); | |
1217 | #endif | |
1da177e4 LT |
1218 | cb->args[0] = info.this_idx; |
1219 | ||
1220 | return skb->len; | |
1221 | } | |
1222 | ||
1223 | static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb, | |
1224 | struct xfrm_policy *xp, | |
1225 | int dir, u32 seq) | |
1226 | { | |
1227 | struct xfrm_dump_info info; | |
1228 | struct sk_buff *skb; | |
1229 | ||
7deb2264 | 1230 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1da177e4 LT |
1231 | if (!skb) |
1232 | return ERR_PTR(-ENOMEM); | |
1233 | ||
1da177e4 LT |
1234 | info.in_skb = in_skb; |
1235 | info.out_skb = skb; | |
1236 | info.nlmsg_seq = seq; | |
1237 | info.nlmsg_flags = 0; | |
1238 | info.this_idx = info.start_idx = 0; | |
1239 | ||
1240 | if (dump_one_policy(xp, dir, 0, &info) < 0) { | |
1241 | kfree_skb(skb); | |
1242 | return NULL; | |
1243 | } | |
1244 | ||
1245 | return skb; | |
1246 | } | |
1247 | ||
22e70050 | 1248 | static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1249 | struct nlattr **attrs) |
1da177e4 LT |
1250 | { |
1251 | struct xfrm_policy *xp; | |
1252 | struct xfrm_userpolicy_id *p; | |
b798a9ed | 1253 | u8 type = XFRM_POLICY_TYPE_MAIN; |
1da177e4 | 1254 | int err; |
26b15dad | 1255 | struct km_event c; |
1da177e4 LT |
1256 | int delete; |
1257 | ||
7b67c857 | 1258 | p = nlmsg_data(nlh); |
1da177e4 LT |
1259 | delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY; |
1260 | ||
35a7aa08 | 1261 | err = copy_from_user_policy_type(&type, attrs); |
f7b6983f MN |
1262 | if (err) |
1263 | return err; | |
1264 | ||
1da177e4 LT |
1265 | err = verify_policy_dir(p->dir); |
1266 | if (err) | |
1267 | return err; | |
1268 | ||
1269 | if (p->index) | |
ef41aaa0 | 1270 | xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err); |
df71837d | 1271 | else { |
5424f32e | 1272 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
df71837d TJ |
1273 | struct xfrm_policy tmp; |
1274 | ||
35a7aa08 | 1275 | err = verify_sec_ctx_len(attrs); |
df71837d TJ |
1276 | if (err) |
1277 | return err; | |
1278 | ||
1279 | memset(&tmp, 0, sizeof(struct xfrm_policy)); | |
1280 | if (rt) { | |
5424f32e | 1281 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); |
df71837d TJ |
1282 | |
1283 | if ((err = security_xfrm_policy_alloc(&tmp, uctx))) | |
1284 | return err; | |
1285 | } | |
ef41aaa0 EP |
1286 | xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, |
1287 | delete, &err); | |
df71837d TJ |
1288 | security_xfrm_policy_free(&tmp); |
1289 | } | |
1da177e4 LT |
1290 | if (xp == NULL) |
1291 | return -ENOENT; | |
1292 | ||
1293 | if (!delete) { | |
1294 | struct sk_buff *resp_skb; | |
1295 | ||
1296 | resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq); | |
1297 | if (IS_ERR(resp_skb)) { | |
1298 | err = PTR_ERR(resp_skb); | |
1299 | } else { | |
082a1ad5 TG |
1300 | err = nlmsg_unicast(xfrm_nl, resp_skb, |
1301 | NETLINK_CB(skb).pid); | |
1da177e4 | 1302 | } |
26b15dad | 1303 | } else { |
ab5f5e8b JL |
1304 | xfrm_audit_policy_delete(xp, err ? 0 : 1, |
1305 | NETLINK_CB(skb).loginuid, | |
1306 | NETLINK_CB(skb).sid); | |
13fcfbb0 DM |
1307 | |
1308 | if (err != 0) | |
c8c05a8e | 1309 | goto out; |
13fcfbb0 | 1310 | |
e7443892 | 1311 | c.data.byid = p->index; |
f60f6b8f | 1312 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1313 | c.seq = nlh->nlmsg_seq; |
1314 | c.pid = nlh->nlmsg_pid; | |
1315 | km_policy_notify(xp, p->dir, &c); | |
1da177e4 LT |
1316 | } |
1317 | ||
c8c05a8e | 1318 | out: |
ef41aaa0 | 1319 | xfrm_pol_put(xp); |
1da177e4 LT |
1320 | return err; |
1321 | } | |
1322 | ||
22e70050 | 1323 | static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1324 | struct nlattr **attrs) |
1da177e4 | 1325 | { |
26b15dad | 1326 | struct km_event c; |
7b67c857 | 1327 | struct xfrm_usersa_flush *p = nlmsg_data(nlh); |
161a09e7 | 1328 | struct xfrm_audit audit_info; |
4aa2e62c | 1329 | int err; |
1da177e4 | 1330 | |
161a09e7 JL |
1331 | audit_info.loginuid = NETLINK_CB(skb).loginuid; |
1332 | audit_info.secid = NETLINK_CB(skb).sid; | |
4aa2e62c JL |
1333 | err = xfrm_state_flush(p->proto, &audit_info); |
1334 | if (err) | |
1335 | return err; | |
bf08867f | 1336 | c.data.proto = p->proto; |
f60f6b8f | 1337 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1338 | c.seq = nlh->nlmsg_seq; |
1339 | c.pid = nlh->nlmsg_pid; | |
1340 | km_state_notify(NULL, &c); | |
1341 | ||
1da177e4 LT |
1342 | return 0; |
1343 | } | |
1344 | ||
7deb2264 TG |
1345 | static inline size_t xfrm_aevent_msgsize(void) |
1346 | { | |
1347 | return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id)) | |
1348 | + nla_total_size(sizeof(struct xfrm_replay_state)) | |
1349 | + nla_total_size(sizeof(struct xfrm_lifetime_cur)) | |
1350 | + nla_total_size(4) /* XFRM_AE_RTHR */ | |
1351 | + nla_total_size(4); /* XFRM_AE_ETHR */ | |
1352 | } | |
d51d081d JHS |
1353 | |
1354 | static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) | |
1355 | { | |
1356 | struct xfrm_aevent_id *id; | |
1357 | struct nlmsghdr *nlh; | |
d51d081d | 1358 | |
79b8b7f4 TG |
1359 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0); |
1360 | if (nlh == NULL) | |
1361 | return -EMSGSIZE; | |
d51d081d | 1362 | |
7b67c857 | 1363 | id = nlmsg_data(nlh); |
2b5f6dcc | 1364 | memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr)); |
d51d081d JHS |
1365 | id->sa_id.spi = x->id.spi; |
1366 | id->sa_id.family = x->props.family; | |
1367 | id->sa_id.proto = x->id.proto; | |
2b5f6dcc JHS |
1368 | memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr)); |
1369 | id->reqid = x->props.reqid; | |
d51d081d JHS |
1370 | id->flags = c->data.aevent; |
1371 | ||
c0144bea TG |
1372 | NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay); |
1373 | NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft); | |
d51d081d | 1374 | |
c0144bea TG |
1375 | if (id->flags & XFRM_AE_RTHR) |
1376 | NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff); | |
d51d081d | 1377 | |
c0144bea TG |
1378 | if (id->flags & XFRM_AE_ETHR) |
1379 | NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH, | |
1380 | x->replay_maxage * 10 / HZ); | |
d51d081d | 1381 | |
9825069d | 1382 | return nlmsg_end(skb, nlh); |
d51d081d | 1383 | |
c0144bea | 1384 | nla_put_failure: |
9825069d TG |
1385 | nlmsg_cancel(skb, nlh); |
1386 | return -EMSGSIZE; | |
d51d081d JHS |
1387 | } |
1388 | ||
22e70050 | 1389 | static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1390 | struct nlattr **attrs) |
d51d081d JHS |
1391 | { |
1392 | struct xfrm_state *x; | |
1393 | struct sk_buff *r_skb; | |
1394 | int err; | |
1395 | struct km_event c; | |
7b67c857 | 1396 | struct xfrm_aevent_id *p = nlmsg_data(nlh); |
d51d081d JHS |
1397 | struct xfrm_usersa_id *id = &p->sa_id; |
1398 | ||
7deb2264 | 1399 | r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); |
d51d081d JHS |
1400 | if (r_skb == NULL) |
1401 | return -ENOMEM; | |
1402 | ||
1403 | x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family); | |
1404 | if (x == NULL) { | |
b08d5840 | 1405 | kfree_skb(r_skb); |
d51d081d JHS |
1406 | return -ESRCH; |
1407 | } | |
1408 | ||
1409 | /* | |
1410 | * XXX: is this lock really needed - none of the other | |
1411 | * gets lock (the concern is things getting updated | |
1412 | * while we are still reading) - jhs | |
1413 | */ | |
1414 | spin_lock_bh(&x->lock); | |
1415 | c.data.aevent = p->flags; | |
1416 | c.seq = nlh->nlmsg_seq; | |
1417 | c.pid = nlh->nlmsg_pid; | |
1418 | ||
1419 | if (build_aevent(r_skb, x, &c) < 0) | |
1420 | BUG(); | |
082a1ad5 | 1421 | err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid); |
d51d081d JHS |
1422 | spin_unlock_bh(&x->lock); |
1423 | xfrm_state_put(x); | |
1424 | return err; | |
1425 | } | |
1426 | ||
22e70050 | 1427 | static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1428 | struct nlattr **attrs) |
d51d081d JHS |
1429 | { |
1430 | struct xfrm_state *x; | |
1431 | struct km_event c; | |
1432 | int err = - EINVAL; | |
7b67c857 | 1433 | struct xfrm_aevent_id *p = nlmsg_data(nlh); |
5424f32e TG |
1434 | struct nlattr *rp = attrs[XFRMA_REPLAY_VAL]; |
1435 | struct nlattr *lt = attrs[XFRMA_LTIME_VAL]; | |
d51d081d JHS |
1436 | |
1437 | if (!lt && !rp) | |
1438 | return err; | |
1439 | ||
1440 | /* pedantic mode - thou shalt sayeth replaceth */ | |
1441 | if (!(nlh->nlmsg_flags&NLM_F_REPLACE)) | |
1442 | return err; | |
1443 | ||
1444 | x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family); | |
1445 | if (x == NULL) | |
1446 | return -ESRCH; | |
1447 | ||
1448 | if (x->km.state != XFRM_STATE_VALID) | |
1449 | goto out; | |
1450 | ||
1451 | spin_lock_bh(&x->lock); | |
35a7aa08 | 1452 | xfrm_update_ae_params(x, attrs); |
d51d081d | 1453 | spin_unlock_bh(&x->lock); |
d51d081d JHS |
1454 | |
1455 | c.event = nlh->nlmsg_type; | |
1456 | c.seq = nlh->nlmsg_seq; | |
1457 | c.pid = nlh->nlmsg_pid; | |
1458 | c.data.aevent = XFRM_AE_CU; | |
1459 | km_state_notify(x, &c); | |
1460 | err = 0; | |
1461 | out: | |
1462 | xfrm_state_put(x); | |
1463 | return err; | |
1464 | } | |
1465 | ||
22e70050 | 1466 | static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1467 | struct nlattr **attrs) |
1da177e4 | 1468 | { |
f7b6983f | 1469 | struct km_event c; |
b798a9ed | 1470 | u8 type = XFRM_POLICY_TYPE_MAIN; |
f7b6983f | 1471 | int err; |
161a09e7 | 1472 | struct xfrm_audit audit_info; |
f7b6983f | 1473 | |
35a7aa08 | 1474 | err = copy_from_user_policy_type(&type, attrs); |
f7b6983f MN |
1475 | if (err) |
1476 | return err; | |
26b15dad | 1477 | |
161a09e7 JL |
1478 | audit_info.loginuid = NETLINK_CB(skb).loginuid; |
1479 | audit_info.secid = NETLINK_CB(skb).sid; | |
4aa2e62c JL |
1480 | err = xfrm_policy_flush(type, &audit_info); |
1481 | if (err) | |
1482 | return err; | |
f7b6983f | 1483 | c.data.type = type; |
f60f6b8f | 1484 | c.event = nlh->nlmsg_type; |
26b15dad JHS |
1485 | c.seq = nlh->nlmsg_seq; |
1486 | c.pid = nlh->nlmsg_pid; | |
1487 | km_policy_notify(NULL, 0, &c); | |
1da177e4 LT |
1488 | return 0; |
1489 | } | |
1490 | ||
22e70050 | 1491 | static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1492 | struct nlattr **attrs) |
6c5c8ca7 JHS |
1493 | { |
1494 | struct xfrm_policy *xp; | |
7b67c857 | 1495 | struct xfrm_user_polexpire *up = nlmsg_data(nlh); |
6c5c8ca7 | 1496 | struct xfrm_userpolicy_info *p = &up->pol; |
b798a9ed | 1497 | u8 type = XFRM_POLICY_TYPE_MAIN; |
6c5c8ca7 JHS |
1498 | int err = -ENOENT; |
1499 | ||
35a7aa08 | 1500 | err = copy_from_user_policy_type(&type, attrs); |
f7b6983f MN |
1501 | if (err) |
1502 | return err; | |
1503 | ||
6c5c8ca7 | 1504 | if (p->index) |
ef41aaa0 | 1505 | xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err); |
6c5c8ca7 | 1506 | else { |
5424f32e | 1507 | struct nlattr *rt = attrs[XFRMA_SEC_CTX]; |
6c5c8ca7 JHS |
1508 | struct xfrm_policy tmp; |
1509 | ||
35a7aa08 | 1510 | err = verify_sec_ctx_len(attrs); |
6c5c8ca7 JHS |
1511 | if (err) |
1512 | return err; | |
1513 | ||
1514 | memset(&tmp, 0, sizeof(struct xfrm_policy)); | |
1515 | if (rt) { | |
5424f32e | 1516 | struct xfrm_user_sec_ctx *uctx = nla_data(rt); |
6c5c8ca7 JHS |
1517 | |
1518 | if ((err = security_xfrm_policy_alloc(&tmp, uctx))) | |
1519 | return err; | |
1520 | } | |
ef41aaa0 EP |
1521 | xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, |
1522 | 0, &err); | |
6c5c8ca7 JHS |
1523 | security_xfrm_policy_free(&tmp); |
1524 | } | |
1525 | ||
1526 | if (xp == NULL) | |
ef41aaa0 EP |
1527 | return -ENOENT; |
1528 | read_lock(&xp->lock); | |
6c5c8ca7 JHS |
1529 | if (xp->dead) { |
1530 | read_unlock(&xp->lock); | |
1531 | goto out; | |
1532 | } | |
1533 | ||
1534 | read_unlock(&xp->lock); | |
1535 | err = 0; | |
1536 | if (up->hard) { | |
1537 | xfrm_policy_delete(xp, p->dir); | |
ab5f5e8b JL |
1538 | xfrm_audit_policy_delete(xp, 1, NETLINK_CB(skb).loginuid, |
1539 | NETLINK_CB(skb).sid); | |
161a09e7 | 1540 | |
6c5c8ca7 JHS |
1541 | } else { |
1542 | // reset the timers here? | |
1543 | printk("Dont know what to do with soft policy expire\n"); | |
1544 | } | |
1545 | km_policy_expired(xp, p->dir, up->hard, current->pid); | |
1546 | ||
1547 | out: | |
1548 | xfrm_pol_put(xp); | |
1549 | return err; | |
1550 | } | |
1551 | ||
22e70050 | 1552 | static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1553 | struct nlattr **attrs) |
53bc6b4d JHS |
1554 | { |
1555 | struct xfrm_state *x; | |
1556 | int err; | |
7b67c857 | 1557 | struct xfrm_user_expire *ue = nlmsg_data(nlh); |
53bc6b4d JHS |
1558 | struct xfrm_usersa_info *p = &ue->state; |
1559 | ||
1560 | x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family); | |
53bc6b4d | 1561 | |
3a765aa5 | 1562 | err = -ENOENT; |
53bc6b4d JHS |
1563 | if (x == NULL) |
1564 | return err; | |
1565 | ||
53bc6b4d | 1566 | spin_lock_bh(&x->lock); |
3a765aa5 | 1567 | err = -EINVAL; |
53bc6b4d JHS |
1568 | if (x->km.state != XFRM_STATE_VALID) |
1569 | goto out; | |
1570 | km_state_expired(x, ue->hard, current->pid); | |
1571 | ||
161a09e7 | 1572 | if (ue->hard) { |
53bc6b4d | 1573 | __xfrm_state_delete(x); |
ab5f5e8b JL |
1574 | xfrm_audit_state_delete(x, 1, NETLINK_CB(skb).loginuid, |
1575 | NETLINK_CB(skb).sid); | |
161a09e7 | 1576 | } |
3a765aa5 | 1577 | err = 0; |
53bc6b4d JHS |
1578 | out: |
1579 | spin_unlock_bh(&x->lock); | |
1580 | xfrm_state_put(x); | |
1581 | return err; | |
1582 | } | |
1583 | ||
22e70050 | 1584 | static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, |
5424f32e | 1585 | struct nlattr **attrs) |
980ebd25 JHS |
1586 | { |
1587 | struct xfrm_policy *xp; | |
1588 | struct xfrm_user_tmpl *ut; | |
1589 | int i; | |
5424f32e | 1590 | struct nlattr *rt = attrs[XFRMA_TMPL]; |
980ebd25 | 1591 | |
7b67c857 | 1592 | struct xfrm_user_acquire *ua = nlmsg_data(nlh); |
980ebd25 JHS |
1593 | struct xfrm_state *x = xfrm_state_alloc(); |
1594 | int err = -ENOMEM; | |
1595 | ||
1596 | if (!x) | |
1597 | return err; | |
1598 | ||
1599 | err = verify_newpolicy_info(&ua->policy); | |
1600 | if (err) { | |
1601 | printk("BAD policy passed\n"); | |
1602 | kfree(x); | |
1603 | return err; | |
1604 | } | |
1605 | ||
1606 | /* build an XP */ | |
5424f32e | 1607 | xp = xfrm_policy_construct(&ua->policy, attrs, &err); |
b4ad86bf | 1608 | if (!xp) { |
980ebd25 JHS |
1609 | kfree(x); |
1610 | return err; | |
1611 | } | |
1612 | ||
1613 | memcpy(&x->id, &ua->id, sizeof(ua->id)); | |
1614 | memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr)); | |
1615 | memcpy(&x->sel, &ua->sel, sizeof(ua->sel)); | |
1616 | ||
5424f32e | 1617 | ut = nla_data(rt); |
980ebd25 JHS |
1618 | /* extract the templates and for each call km_key */ |
1619 | for (i = 0; i < xp->xfrm_nr; i++, ut++) { | |
1620 | struct xfrm_tmpl *t = &xp->xfrm_vec[i]; | |
1621 | memcpy(&x->id, &t->id, sizeof(x->id)); | |
1622 | x->props.mode = t->mode; | |
1623 | x->props.reqid = t->reqid; | |
1624 | x->props.family = ut->family; | |
1625 | t->aalgos = ua->aalgos; | |
1626 | t->ealgos = ua->ealgos; | |
1627 | t->calgos = ua->calgos; | |
1628 | err = km_query(x, t, xp); | |
1629 | ||
1630 | } | |
1631 | ||
1632 | kfree(x); | |
1633 | kfree(xp); | |
1634 | ||
1635 | return 0; | |
1636 | } | |
1637 | ||
5c79de6e | 1638 | #ifdef CONFIG_XFRM_MIGRATE |
5c79de6e | 1639 | static int copy_from_user_migrate(struct xfrm_migrate *ma, |
5424f32e | 1640 | struct nlattr **attrs, int *num) |
5c79de6e | 1641 | { |
5424f32e | 1642 | struct nlattr *rt = attrs[XFRMA_MIGRATE]; |
5c79de6e SS |
1643 | struct xfrm_user_migrate *um; |
1644 | int i, num_migrate; | |
1645 | ||
5424f32e TG |
1646 | um = nla_data(rt); |
1647 | num_migrate = nla_len(rt) / sizeof(*um); | |
5c79de6e SS |
1648 | |
1649 | if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) | |
1650 | return -EINVAL; | |
1651 | ||
1652 | for (i = 0; i < num_migrate; i++, um++, ma++) { | |
1653 | memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr)); | |
1654 | memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr)); | |
1655 | memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr)); | |
1656 | memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr)); | |
1657 | ||
1658 | ma->proto = um->proto; | |
1659 | ma->mode = um->mode; | |
1660 | ma->reqid = um->reqid; | |
1661 | ||
1662 | ma->old_family = um->old_family; | |
1663 | ma->new_family = um->new_family; | |
1664 | } | |
1665 | ||
1666 | *num = i; | |
1667 | return 0; | |
1668 | } | |
1669 | ||
1670 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 1671 | struct nlattr **attrs) |
5c79de6e | 1672 | { |
7b67c857 | 1673 | struct xfrm_userpolicy_id *pi = nlmsg_data(nlh); |
5c79de6e SS |
1674 | struct xfrm_migrate m[XFRM_MAX_DEPTH]; |
1675 | u8 type; | |
1676 | int err; | |
1677 | int n = 0; | |
1678 | ||
35a7aa08 | 1679 | if (attrs[XFRMA_MIGRATE] == NULL) |
cf5cb79f | 1680 | return -EINVAL; |
5c79de6e | 1681 | |
5424f32e | 1682 | err = copy_from_user_policy_type(&type, attrs); |
5c79de6e SS |
1683 | if (err) |
1684 | return err; | |
1685 | ||
1686 | err = copy_from_user_migrate((struct xfrm_migrate *)m, | |
5424f32e | 1687 | attrs, &n); |
5c79de6e SS |
1688 | if (err) |
1689 | return err; | |
1690 | ||
1691 | if (!n) | |
1692 | return 0; | |
1693 | ||
1694 | xfrm_migrate(&pi->sel, pi->dir, type, m, n); | |
1695 | ||
1696 | return 0; | |
1697 | } | |
1698 | #else | |
1699 | static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh, | |
5424f32e | 1700 | struct nlattr **attrs) |
5c79de6e SS |
1701 | { |
1702 | return -ENOPROTOOPT; | |
1703 | } | |
1704 | #endif | |
1705 | ||
1706 | #ifdef CONFIG_XFRM_MIGRATE | |
1707 | static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb) | |
1708 | { | |
1709 | struct xfrm_user_migrate um; | |
1710 | ||
1711 | memset(&um, 0, sizeof(um)); | |
1712 | um.proto = m->proto; | |
1713 | um.mode = m->mode; | |
1714 | um.reqid = m->reqid; | |
1715 | um.old_family = m->old_family; | |
1716 | memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr)); | |
1717 | memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr)); | |
1718 | um.new_family = m->new_family; | |
1719 | memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr)); | |
1720 | memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr)); | |
1721 | ||
c0144bea | 1722 | return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um); |
5c79de6e SS |
1723 | } |
1724 | ||
7deb2264 TG |
1725 | static inline size_t xfrm_migrate_msgsize(int num_migrate) |
1726 | { | |
1727 | return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id)) | |
1728 | + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate) | |
1729 | + userpolicy_type_attrsize(); | |
1730 | } | |
1731 | ||
5c79de6e SS |
1732 | static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m, |
1733 | int num_migrate, struct xfrm_selector *sel, | |
1734 | u8 dir, u8 type) | |
1735 | { | |
1736 | struct xfrm_migrate *mp; | |
1737 | struct xfrm_userpolicy_id *pol_id; | |
1738 | struct nlmsghdr *nlh; | |
5c79de6e SS |
1739 | int i; |
1740 | ||
79b8b7f4 TG |
1741 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0); |
1742 | if (nlh == NULL) | |
1743 | return -EMSGSIZE; | |
5c79de6e | 1744 | |
7b67c857 | 1745 | pol_id = nlmsg_data(nlh); |
5c79de6e SS |
1746 | /* copy data from selector, dir, and type to the pol_id */ |
1747 | memset(pol_id, 0, sizeof(*pol_id)); | |
1748 | memcpy(&pol_id->sel, sel, sizeof(pol_id->sel)); | |
1749 | pol_id->dir = dir; | |
1750 | ||
1751 | if (copy_to_user_policy_type(type, skb) < 0) | |
1752 | goto nlmsg_failure; | |
1753 | ||
1754 | for (i = 0, mp = m ; i < num_migrate; i++, mp++) { | |
1755 | if (copy_to_user_migrate(mp, skb) < 0) | |
1756 | goto nlmsg_failure; | |
1757 | } | |
1758 | ||
9825069d | 1759 | return nlmsg_end(skb, nlh); |
5c79de6e | 1760 | nlmsg_failure: |
9825069d TG |
1761 | nlmsg_cancel(skb, nlh); |
1762 | return -EMSGSIZE; | |
5c79de6e SS |
1763 | } |
1764 | ||
1765 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
1766 | struct xfrm_migrate *m, int num_migrate) | |
1767 | { | |
1768 | struct sk_buff *skb; | |
5c79de6e | 1769 | |
7deb2264 | 1770 | skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC); |
5c79de6e SS |
1771 | if (skb == NULL) |
1772 | return -ENOMEM; | |
1773 | ||
1774 | /* build migrate */ | |
1775 | if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0) | |
1776 | BUG(); | |
1777 | ||
082a1ad5 | 1778 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC); |
5c79de6e SS |
1779 | } |
1780 | #else | |
1781 | static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type, | |
1782 | struct xfrm_migrate *m, int num_migrate) | |
1783 | { | |
1784 | return -ENOPROTOOPT; | |
1785 | } | |
1786 | #endif | |
d51d081d | 1787 | |
a7bd9a45 | 1788 | #define XMSGSIZE(type) sizeof(struct type) |
492b558b TG |
1789 | |
1790 | static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = { | |
1791 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | |
1792 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | |
1793 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id), | |
1794 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), | |
1795 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | |
1796 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), | |
1797 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info), | |
980ebd25 | 1798 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire), |
53bc6b4d | 1799 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire), |
492b558b TG |
1800 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info), |
1801 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info), | |
6c5c8ca7 | 1802 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire), |
492b558b | 1803 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush), |
a7bd9a45 | 1804 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0, |
d51d081d JHS |
1805 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), |
1806 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id), | |
97a64b45 | 1807 | [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report), |
5c79de6e | 1808 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id), |
a7bd9a45 TG |
1809 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32), |
1810 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32), | |
1da177e4 LT |
1811 | }; |
1812 | ||
492b558b TG |
1813 | #undef XMSGSIZE |
1814 | ||
cf5cb79f TG |
1815 | static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = { |
1816 | [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) }, | |
1817 | [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) }, | |
1818 | [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) }, | |
1819 | [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) }, | |
1820 | [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) }, | |
1821 | [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) }, | |
1822 | [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) }, | |
1823 | [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) }, | |
1824 | [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 }, | |
1825 | [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 }, | |
1826 | [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) }, | |
1827 | [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) }, | |
1828 | [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)}, | |
1829 | [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) }, | |
1830 | }; | |
1831 | ||
1da177e4 | 1832 | static struct xfrm_link { |
5424f32e | 1833 | int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **); |
1da177e4 | 1834 | int (*dump)(struct sk_buff *, struct netlink_callback *); |
492b558b TG |
1835 | } xfrm_dispatch[XFRM_NR_MSGTYPES] = { |
1836 | [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | |
1837 | [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa }, | |
1838 | [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa, | |
1839 | .dump = xfrm_dump_sa }, | |
1840 | [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, | |
1841 | [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy }, | |
1842 | [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy, | |
1843 | .dump = xfrm_dump_policy }, | |
1844 | [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi }, | |
980ebd25 | 1845 | [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire }, |
53bc6b4d | 1846 | [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire }, |
492b558b TG |
1847 | [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy }, |
1848 | [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa }, | |
6c5c8ca7 | 1849 | [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire}, |
492b558b TG |
1850 | [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa }, |
1851 | [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy }, | |
d51d081d JHS |
1852 | [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae }, |
1853 | [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae }, | |
5c79de6e | 1854 | [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate }, |
566ec034 | 1855 | [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo }, |
ecfd6b18 | 1856 | [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo }, |
1da177e4 LT |
1857 | }; |
1858 | ||
1d00a4eb | 1859 | static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
1da177e4 | 1860 | { |
35a7aa08 | 1861 | struct nlattr *attrs[XFRMA_MAX+1]; |
1da177e4 | 1862 | struct xfrm_link *link; |
a7bd9a45 | 1863 | int type, err; |
1da177e4 | 1864 | |
1da177e4 | 1865 | type = nlh->nlmsg_type; |
1da177e4 | 1866 | if (type > XFRM_MSG_MAX) |
1d00a4eb | 1867 | return -EINVAL; |
1da177e4 LT |
1868 | |
1869 | type -= XFRM_MSG_BASE; | |
1870 | link = &xfrm_dispatch[type]; | |
1871 | ||
1872 | /* All operations require privileges, even GET */ | |
1d00a4eb TG |
1873 | if (security_netlink_recv(skb, CAP_NET_ADMIN)) |
1874 | return -EPERM; | |
1da177e4 | 1875 | |
492b558b TG |
1876 | if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) || |
1877 | type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) && | |
1878 | (nlh->nlmsg_flags & NLM_F_DUMP)) { | |
1da177e4 | 1879 | if (link->dump == NULL) |
1d00a4eb | 1880 | return -EINVAL; |
88fc2c84 | 1881 | |
c702e804 | 1882 | return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL); |
1da177e4 LT |
1883 | } |
1884 | ||
35a7aa08 | 1885 | err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX, |
cf5cb79f | 1886 | xfrma_policy); |
a7bd9a45 TG |
1887 | if (err < 0) |
1888 | return err; | |
1da177e4 LT |
1889 | |
1890 | if (link->doit == NULL) | |
1d00a4eb | 1891 | return -EINVAL; |
1da177e4 | 1892 | |
5424f32e | 1893 | return link->doit(skb, nlh, attrs); |
1da177e4 LT |
1894 | } |
1895 | ||
cd40b7d3 | 1896 | static void xfrm_netlink_rcv(struct sk_buff *skb) |
1da177e4 | 1897 | { |
cd40b7d3 DL |
1898 | mutex_lock(&xfrm_cfg_mutex); |
1899 | netlink_rcv_skb(skb, &xfrm_user_rcv_msg); | |
1900 | mutex_unlock(&xfrm_cfg_mutex); | |
1da177e4 LT |
1901 | } |
1902 | ||
7deb2264 TG |
1903 | static inline size_t xfrm_expire_msgsize(void) |
1904 | { | |
1905 | return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)); | |
1906 | } | |
1907 | ||
d51d081d | 1908 | static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c) |
1da177e4 LT |
1909 | { |
1910 | struct xfrm_user_expire *ue; | |
1911 | struct nlmsghdr *nlh; | |
1da177e4 | 1912 | |
79b8b7f4 TG |
1913 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0); |
1914 | if (nlh == NULL) | |
1915 | return -EMSGSIZE; | |
1da177e4 | 1916 | |
7b67c857 | 1917 | ue = nlmsg_data(nlh); |
1da177e4 | 1918 | copy_to_user_state(x, &ue->state); |
d51d081d | 1919 | ue->hard = (c->data.hard != 0) ? 1 : 0; |
1da177e4 | 1920 | |
9825069d | 1921 | return nlmsg_end(skb, nlh); |
1da177e4 LT |
1922 | } |
1923 | ||
26b15dad | 1924 | static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c) |
1da177e4 LT |
1925 | { |
1926 | struct sk_buff *skb; | |
1927 | ||
7deb2264 | 1928 | skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC); |
1da177e4 LT |
1929 | if (skb == NULL) |
1930 | return -ENOMEM; | |
1931 | ||
d51d081d | 1932 | if (build_expire(skb, x, c) < 0) |
1da177e4 LT |
1933 | BUG(); |
1934 | ||
082a1ad5 | 1935 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); |
1da177e4 LT |
1936 | } |
1937 | ||
d51d081d JHS |
1938 | static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c) |
1939 | { | |
1940 | struct sk_buff *skb; | |
d51d081d | 1941 | |
7deb2264 | 1942 | skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC); |
d51d081d JHS |
1943 | if (skb == NULL) |
1944 | return -ENOMEM; | |
1945 | ||
1946 | if (build_aevent(skb, x, c) < 0) | |
1947 | BUG(); | |
1948 | ||
082a1ad5 | 1949 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC); |
d51d081d JHS |
1950 | } |
1951 | ||
26b15dad JHS |
1952 | static int xfrm_notify_sa_flush(struct km_event *c) |
1953 | { | |
1954 | struct xfrm_usersa_flush *p; | |
1955 | struct nlmsghdr *nlh; | |
1956 | struct sk_buff *skb; | |
7deb2264 | 1957 | int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush)); |
26b15dad | 1958 | |
7deb2264 | 1959 | skb = nlmsg_new(len, GFP_ATOMIC); |
26b15dad JHS |
1960 | if (skb == NULL) |
1961 | return -ENOMEM; | |
26b15dad | 1962 | |
79b8b7f4 TG |
1963 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0); |
1964 | if (nlh == NULL) { | |
1965 | kfree_skb(skb); | |
1966 | return -EMSGSIZE; | |
1967 | } | |
26b15dad | 1968 | |
7b67c857 | 1969 | p = nlmsg_data(nlh); |
bf08867f | 1970 | p->proto = c->data.proto; |
26b15dad | 1971 | |
9825069d | 1972 | nlmsg_end(skb, nlh); |
26b15dad | 1973 | |
082a1ad5 | 1974 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); |
26b15dad JHS |
1975 | } |
1976 | ||
7deb2264 | 1977 | static inline size_t xfrm_sa_len(struct xfrm_state *x) |
26b15dad | 1978 | { |
7deb2264 | 1979 | size_t l = 0; |
26b15dad | 1980 | if (x->aalg) |
7deb2264 | 1981 | l += nla_total_size(alg_len(x->aalg)); |
26b15dad | 1982 | if (x->ealg) |
7deb2264 | 1983 | l += nla_total_size(alg_len(x->ealg)); |
26b15dad | 1984 | if (x->calg) |
7deb2264 | 1985 | l += nla_total_size(sizeof(*x->calg)); |
26b15dad | 1986 | if (x->encap) |
7deb2264 | 1987 | l += nla_total_size(sizeof(*x->encap)); |
68325d3b HX |
1988 | if (x->security) |
1989 | l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) + | |
1990 | x->security->ctx_len); | |
1991 | if (x->coaddr) | |
1992 | l += nla_total_size(sizeof(*x->coaddr)); | |
1993 | ||
1994 | /* Must count this as this may become non-zero behind our back. */ | |
1995 | l += nla_total_size(sizeof(x->lastused)); | |
26b15dad JHS |
1996 | |
1997 | return l; | |
1998 | } | |
1999 | ||
2000 | static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c) | |
2001 | { | |
2002 | struct xfrm_usersa_info *p; | |
0603eac0 | 2003 | struct xfrm_usersa_id *id; |
26b15dad JHS |
2004 | struct nlmsghdr *nlh; |
2005 | struct sk_buff *skb; | |
26b15dad | 2006 | int len = xfrm_sa_len(x); |
0603eac0 HX |
2007 | int headlen; |
2008 | ||
2009 | headlen = sizeof(*p); | |
2010 | if (c->event == XFRM_MSG_DELSA) { | |
7deb2264 | 2011 | len += nla_total_size(headlen); |
0603eac0 HX |
2012 | headlen = sizeof(*id); |
2013 | } | |
7deb2264 | 2014 | len += NLMSG_ALIGN(headlen); |
26b15dad | 2015 | |
7deb2264 | 2016 | skb = nlmsg_new(len, GFP_ATOMIC); |
26b15dad JHS |
2017 | if (skb == NULL) |
2018 | return -ENOMEM; | |
26b15dad | 2019 | |
79b8b7f4 TG |
2020 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); |
2021 | if (nlh == NULL) | |
c0144bea | 2022 | goto nla_put_failure; |
26b15dad | 2023 | |
7b67c857 | 2024 | p = nlmsg_data(nlh); |
0603eac0 | 2025 | if (c->event == XFRM_MSG_DELSA) { |
c0144bea TG |
2026 | struct nlattr *attr; |
2027 | ||
7b67c857 | 2028 | id = nlmsg_data(nlh); |
0603eac0 HX |
2029 | memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr)); |
2030 | id->spi = x->id.spi; | |
2031 | id->family = x->props.family; | |
2032 | id->proto = x->id.proto; | |
2033 | ||
c0144bea TG |
2034 | attr = nla_reserve(skb, XFRMA_SA, sizeof(*p)); |
2035 | if (attr == NULL) | |
2036 | goto nla_put_failure; | |
2037 | ||
2038 | p = nla_data(attr); | |
0603eac0 HX |
2039 | } |
2040 | ||
68325d3b HX |
2041 | if (copy_to_user_state_extra(x, p, skb)) |
2042 | goto nla_put_failure; | |
26b15dad | 2043 | |
9825069d | 2044 | nlmsg_end(skb, nlh); |
26b15dad | 2045 | |
082a1ad5 | 2046 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC); |
26b15dad | 2047 | |
c0144bea | 2048 | nla_put_failure: |
68325d3b HX |
2049 | /* Somebody screwed up with xfrm_sa_len! */ |
2050 | WARN_ON(1); | |
26b15dad JHS |
2051 | kfree_skb(skb); |
2052 | return -1; | |
2053 | } | |
2054 | ||
2055 | static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c) | |
2056 | { | |
2057 | ||
2058 | switch (c->event) { | |
f60f6b8f | 2059 | case XFRM_MSG_EXPIRE: |
26b15dad | 2060 | return xfrm_exp_state_notify(x, c); |
d51d081d JHS |
2061 | case XFRM_MSG_NEWAE: |
2062 | return xfrm_aevent_state_notify(x, c); | |
f60f6b8f HX |
2063 | case XFRM_MSG_DELSA: |
2064 | case XFRM_MSG_UPDSA: | |
2065 | case XFRM_MSG_NEWSA: | |
26b15dad | 2066 | return xfrm_notify_sa(x, c); |
f60f6b8f | 2067 | case XFRM_MSG_FLUSHSA: |
26b15dad JHS |
2068 | return xfrm_notify_sa_flush(c); |
2069 | default: | |
2070 | printk("xfrm_user: Unknown SA event %d\n", c->event); | |
2071 | break; | |
2072 | } | |
2073 | ||
2074 | return 0; | |
2075 | ||
2076 | } | |
2077 | ||
7deb2264 TG |
2078 | static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x, |
2079 | struct xfrm_policy *xp) | |
2080 | { | |
2081 | return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire)) | |
2082 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) | |
2083 | + nla_total_size(xfrm_user_sec_ctx_size(x->security)) | |
2084 | + userpolicy_type_attrsize(); | |
2085 | } | |
2086 | ||
1da177e4 LT |
2087 | static int build_acquire(struct sk_buff *skb, struct xfrm_state *x, |
2088 | struct xfrm_tmpl *xt, struct xfrm_policy *xp, | |
2089 | int dir) | |
2090 | { | |
2091 | struct xfrm_user_acquire *ua; | |
2092 | struct nlmsghdr *nlh; | |
1da177e4 LT |
2093 | __u32 seq = xfrm_get_acqseq(); |
2094 | ||
79b8b7f4 TG |
2095 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0); |
2096 | if (nlh == NULL) | |
2097 | return -EMSGSIZE; | |
1da177e4 | 2098 | |
7b67c857 | 2099 | ua = nlmsg_data(nlh); |
1da177e4 LT |
2100 | memcpy(&ua->id, &x->id, sizeof(ua->id)); |
2101 | memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr)); | |
2102 | memcpy(&ua->sel, &x->sel, sizeof(ua->sel)); | |
2103 | copy_to_user_policy(xp, &ua->policy, dir); | |
2104 | ua->aalgos = xt->aalgos; | |
2105 | ua->ealgos = xt->ealgos; | |
2106 | ua->calgos = xt->calgos; | |
2107 | ua->seq = x->km.seq = seq; | |
2108 | ||
2109 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2110 | goto nlmsg_failure; | |
0d681623 | 2111 | if (copy_to_user_state_sec_ctx(x, skb)) |
df71837d | 2112 | goto nlmsg_failure; |
1459bb36 | 2113 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 2114 | goto nlmsg_failure; |
1da177e4 | 2115 | |
9825069d | 2116 | return nlmsg_end(skb, nlh); |
1da177e4 LT |
2117 | |
2118 | nlmsg_failure: | |
9825069d TG |
2119 | nlmsg_cancel(skb, nlh); |
2120 | return -EMSGSIZE; | |
1da177e4 LT |
2121 | } |
2122 | ||
2123 | static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt, | |
2124 | struct xfrm_policy *xp, int dir) | |
2125 | { | |
2126 | struct sk_buff *skb; | |
1da177e4 | 2127 | |
7deb2264 | 2128 | skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC); |
1da177e4 LT |
2129 | if (skb == NULL) |
2130 | return -ENOMEM; | |
2131 | ||
2132 | if (build_acquire(skb, x, xt, xp, dir) < 0) | |
2133 | BUG(); | |
2134 | ||
082a1ad5 | 2135 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC); |
1da177e4 LT |
2136 | } |
2137 | ||
2138 | /* User gives us xfrm_user_policy_info followed by an array of 0 | |
2139 | * or more templates. | |
2140 | */ | |
cb969f07 | 2141 | static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt, |
1da177e4 LT |
2142 | u8 *data, int len, int *dir) |
2143 | { | |
2144 | struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data; | |
2145 | struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1); | |
2146 | struct xfrm_policy *xp; | |
2147 | int nr; | |
2148 | ||
cb969f07 | 2149 | switch (sk->sk_family) { |
1da177e4 LT |
2150 | case AF_INET: |
2151 | if (opt != IP_XFRM_POLICY) { | |
2152 | *dir = -EOPNOTSUPP; | |
2153 | return NULL; | |
2154 | } | |
2155 | break; | |
2156 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
2157 | case AF_INET6: | |
2158 | if (opt != IPV6_XFRM_POLICY) { | |
2159 | *dir = -EOPNOTSUPP; | |
2160 | return NULL; | |
2161 | } | |
2162 | break; | |
2163 | #endif | |
2164 | default: | |
2165 | *dir = -EINVAL; | |
2166 | return NULL; | |
2167 | } | |
2168 | ||
2169 | *dir = -EINVAL; | |
2170 | ||
2171 | if (len < sizeof(*p) || | |
2172 | verify_newpolicy_info(p)) | |
2173 | return NULL; | |
2174 | ||
2175 | nr = ((len - sizeof(*p)) / sizeof(*ut)); | |
b4ad86bf | 2176 | if (validate_tmpl(nr, ut, p->sel.family)) |
1da177e4 LT |
2177 | return NULL; |
2178 | ||
a4f1bac6 HX |
2179 | if (p->dir > XFRM_POLICY_OUT) |
2180 | return NULL; | |
2181 | ||
1da177e4 LT |
2182 | xp = xfrm_policy_alloc(GFP_KERNEL); |
2183 | if (xp == NULL) { | |
2184 | *dir = -ENOBUFS; | |
2185 | return NULL; | |
2186 | } | |
2187 | ||
2188 | copy_from_user_policy(xp, p); | |
f7b6983f | 2189 | xp->type = XFRM_POLICY_TYPE_MAIN; |
1da177e4 LT |
2190 | copy_templates(xp, ut, nr); |
2191 | ||
2192 | *dir = p->dir; | |
2193 | ||
2194 | return xp; | |
2195 | } | |
2196 | ||
7deb2264 TG |
2197 | static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp) |
2198 | { | |
2199 | return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire)) | |
2200 | + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr) | |
2201 | + nla_total_size(xfrm_user_sec_ctx_size(xp->security)) | |
2202 | + userpolicy_type_attrsize(); | |
2203 | } | |
2204 | ||
1da177e4 | 2205 | static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp, |
d51d081d | 2206 | int dir, struct km_event *c) |
1da177e4 LT |
2207 | { |
2208 | struct xfrm_user_polexpire *upe; | |
2209 | struct nlmsghdr *nlh; | |
d51d081d | 2210 | int hard = c->data.hard; |
1da177e4 | 2211 | |
79b8b7f4 TG |
2212 | nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0); |
2213 | if (nlh == NULL) | |
2214 | return -EMSGSIZE; | |
1da177e4 | 2215 | |
7b67c857 | 2216 | upe = nlmsg_data(nlh); |
1da177e4 LT |
2217 | copy_to_user_policy(xp, &upe->pol, dir); |
2218 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2219 | goto nlmsg_failure; | |
df71837d TJ |
2220 | if (copy_to_user_sec_ctx(xp, skb)) |
2221 | goto nlmsg_failure; | |
1459bb36 | 2222 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 2223 | goto nlmsg_failure; |
1da177e4 LT |
2224 | upe->hard = !!hard; |
2225 | ||
9825069d | 2226 | return nlmsg_end(skb, nlh); |
1da177e4 LT |
2227 | |
2228 | nlmsg_failure: | |
9825069d TG |
2229 | nlmsg_cancel(skb, nlh); |
2230 | return -EMSGSIZE; | |
1da177e4 LT |
2231 | } |
2232 | ||
26b15dad | 2233 | static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) |
1da177e4 LT |
2234 | { |
2235 | struct sk_buff *skb; | |
1da177e4 | 2236 | |
7deb2264 | 2237 | skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC); |
1da177e4 LT |
2238 | if (skb == NULL) |
2239 | return -ENOMEM; | |
2240 | ||
d51d081d | 2241 | if (build_polexpire(skb, xp, dir, c) < 0) |
1da177e4 LT |
2242 | BUG(); |
2243 | ||
082a1ad5 | 2244 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC); |
1da177e4 LT |
2245 | } |
2246 | ||
26b15dad JHS |
2247 | static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c) |
2248 | { | |
2249 | struct xfrm_userpolicy_info *p; | |
0603eac0 | 2250 | struct xfrm_userpolicy_id *id; |
26b15dad JHS |
2251 | struct nlmsghdr *nlh; |
2252 | struct sk_buff *skb; | |
7deb2264 | 2253 | int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr); |
0603eac0 HX |
2254 | int headlen; |
2255 | ||
2256 | headlen = sizeof(*p); | |
2257 | if (c->event == XFRM_MSG_DELPOLICY) { | |
7deb2264 | 2258 | len += nla_total_size(headlen); |
0603eac0 HX |
2259 | headlen = sizeof(*id); |
2260 | } | |
cfbfd45a | 2261 | len += userpolicy_type_attrsize(); |
7deb2264 | 2262 | len += NLMSG_ALIGN(headlen); |
26b15dad | 2263 | |
7deb2264 | 2264 | skb = nlmsg_new(len, GFP_ATOMIC); |
26b15dad JHS |
2265 | if (skb == NULL) |
2266 | return -ENOMEM; | |
26b15dad | 2267 | |
79b8b7f4 TG |
2268 | nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0); |
2269 | if (nlh == NULL) | |
2270 | goto nlmsg_failure; | |
26b15dad | 2271 | |
7b67c857 | 2272 | p = nlmsg_data(nlh); |
0603eac0 | 2273 | if (c->event == XFRM_MSG_DELPOLICY) { |
c0144bea TG |
2274 | struct nlattr *attr; |
2275 | ||
7b67c857 | 2276 | id = nlmsg_data(nlh); |
0603eac0 HX |
2277 | memset(id, 0, sizeof(*id)); |
2278 | id->dir = dir; | |
2279 | if (c->data.byid) | |
2280 | id->index = xp->index; | |
2281 | else | |
2282 | memcpy(&id->sel, &xp->selector, sizeof(id->sel)); | |
2283 | ||
c0144bea TG |
2284 | attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p)); |
2285 | if (attr == NULL) | |
2286 | goto nlmsg_failure; | |
2287 | ||
2288 | p = nla_data(attr); | |
0603eac0 | 2289 | } |
26b15dad | 2290 | |
26b15dad JHS |
2291 | copy_to_user_policy(xp, p, dir); |
2292 | if (copy_to_user_tmpl(xp, skb) < 0) | |
2293 | goto nlmsg_failure; | |
1459bb36 | 2294 | if (copy_to_user_policy_type(xp->type, skb) < 0) |
f7b6983f | 2295 | goto nlmsg_failure; |
26b15dad | 2296 | |
9825069d | 2297 | nlmsg_end(skb, nlh); |
26b15dad | 2298 | |
082a1ad5 | 2299 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); |
26b15dad JHS |
2300 | |
2301 | nlmsg_failure: | |
2302 | kfree_skb(skb); | |
2303 | return -1; | |
2304 | } | |
2305 | ||
2306 | static int xfrm_notify_policy_flush(struct km_event *c) | |
2307 | { | |
2308 | struct nlmsghdr *nlh; | |
2309 | struct sk_buff *skb; | |
26b15dad | 2310 | |
7deb2264 | 2311 | skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC); |
26b15dad JHS |
2312 | if (skb == NULL) |
2313 | return -ENOMEM; | |
26b15dad | 2314 | |
79b8b7f4 TG |
2315 | nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0); |
2316 | if (nlh == NULL) | |
2317 | goto nlmsg_failure; | |
0c51f53c JHS |
2318 | if (copy_to_user_policy_type(c->data.type, skb) < 0) |
2319 | goto nlmsg_failure; | |
26b15dad | 2320 | |
9825069d | 2321 | nlmsg_end(skb, nlh); |
26b15dad | 2322 | |
082a1ad5 | 2323 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC); |
26b15dad JHS |
2324 | |
2325 | nlmsg_failure: | |
2326 | kfree_skb(skb); | |
2327 | return -1; | |
2328 | } | |
2329 | ||
2330 | static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c) | |
2331 | { | |
2332 | ||
2333 | switch (c->event) { | |
f60f6b8f HX |
2334 | case XFRM_MSG_NEWPOLICY: |
2335 | case XFRM_MSG_UPDPOLICY: | |
2336 | case XFRM_MSG_DELPOLICY: | |
26b15dad | 2337 | return xfrm_notify_policy(xp, dir, c); |
f60f6b8f | 2338 | case XFRM_MSG_FLUSHPOLICY: |
26b15dad | 2339 | return xfrm_notify_policy_flush(c); |
f60f6b8f | 2340 | case XFRM_MSG_POLEXPIRE: |
26b15dad JHS |
2341 | return xfrm_exp_policy_notify(xp, dir, c); |
2342 | default: | |
2343 | printk("xfrm_user: Unknown Policy event %d\n", c->event); | |
2344 | } | |
2345 | ||
2346 | return 0; | |
2347 | ||
2348 | } | |
2349 | ||
7deb2264 TG |
2350 | static inline size_t xfrm_report_msgsize(void) |
2351 | { | |
2352 | return NLMSG_ALIGN(sizeof(struct xfrm_user_report)); | |
2353 | } | |
2354 | ||
97a64b45 MN |
2355 | static int build_report(struct sk_buff *skb, u8 proto, |
2356 | struct xfrm_selector *sel, xfrm_address_t *addr) | |
2357 | { | |
2358 | struct xfrm_user_report *ur; | |
2359 | struct nlmsghdr *nlh; | |
97a64b45 | 2360 | |
79b8b7f4 TG |
2361 | nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0); |
2362 | if (nlh == NULL) | |
2363 | return -EMSGSIZE; | |
97a64b45 | 2364 | |
7b67c857 | 2365 | ur = nlmsg_data(nlh); |
97a64b45 MN |
2366 | ur->proto = proto; |
2367 | memcpy(&ur->sel, sel, sizeof(ur->sel)); | |
2368 | ||
2369 | if (addr) | |
c0144bea | 2370 | NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr); |
97a64b45 | 2371 | |
9825069d | 2372 | return nlmsg_end(skb, nlh); |
97a64b45 | 2373 | |
c0144bea | 2374 | nla_put_failure: |
9825069d TG |
2375 | nlmsg_cancel(skb, nlh); |
2376 | return -EMSGSIZE; | |
97a64b45 MN |
2377 | } |
2378 | ||
2379 | static int xfrm_send_report(u8 proto, struct xfrm_selector *sel, | |
2380 | xfrm_address_t *addr) | |
2381 | { | |
2382 | struct sk_buff *skb; | |
97a64b45 | 2383 | |
7deb2264 | 2384 | skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC); |
97a64b45 MN |
2385 | if (skb == NULL) |
2386 | return -ENOMEM; | |
2387 | ||
2388 | if (build_report(skb, proto, sel, addr) < 0) | |
2389 | BUG(); | |
2390 | ||
082a1ad5 | 2391 | return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC); |
97a64b45 MN |
2392 | } |
2393 | ||
1da177e4 LT |
2394 | static struct xfrm_mgr netlink_mgr = { |
2395 | .id = "netlink", | |
2396 | .notify = xfrm_send_state_notify, | |
2397 | .acquire = xfrm_send_acquire, | |
2398 | .compile_policy = xfrm_compile_policy, | |
2399 | .notify_policy = xfrm_send_policy_notify, | |
97a64b45 | 2400 | .report = xfrm_send_report, |
5c79de6e | 2401 | .migrate = xfrm_send_migrate, |
1da177e4 LT |
2402 | }; |
2403 | ||
2404 | static int __init xfrm_user_init(void) | |
2405 | { | |
be33690d PM |
2406 | struct sock *nlsk; |
2407 | ||
654b32c6 | 2408 | printk(KERN_INFO "Initializing XFRM netlink socket\n"); |
1da177e4 | 2409 | |
b4b51029 | 2410 | nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX, |
af65bdfc | 2411 | xfrm_netlink_rcv, NULL, THIS_MODULE); |
be33690d | 2412 | if (nlsk == NULL) |
1da177e4 | 2413 | return -ENOMEM; |
be33690d | 2414 | rcu_assign_pointer(xfrm_nl, nlsk); |
1da177e4 LT |
2415 | |
2416 | xfrm_register_km(&netlink_mgr); | |
2417 | ||
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | static void __exit xfrm_user_exit(void) | |
2422 | { | |
be33690d PM |
2423 | struct sock *nlsk = xfrm_nl; |
2424 | ||
1da177e4 | 2425 | xfrm_unregister_km(&netlink_mgr); |
be33690d PM |
2426 | rcu_assign_pointer(xfrm_nl, NULL); |
2427 | synchronize_rcu(); | |
2428 | sock_release(nlsk->sk_socket); | |
1da177e4 LT |
2429 | } |
2430 | ||
2431 | module_init(xfrm_user_init); | |
2432 | module_exit(xfrm_user_exit); | |
2433 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 | 2434 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM); |
f8cd5488 | 2435 |