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