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