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