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