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