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