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