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