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