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