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