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