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