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