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