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