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