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