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