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