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