Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 2 | /* |
| 3 | * NETLINK Netlink attributes |
| 4 | * |
| 5 | * Authors: Thomas Graf <tgraf@suug.ch> |
| 6 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 7 | */ |
| 8 | |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/jiffies.h> |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <net/netlink.h> |
| 17 | |
David Ahern | 6e237d0 | 2017-12-06 20:09:12 -0800 | [diff] [blame] | 18 | /* For these data types, attribute length should be exactly the given |
| 19 | * size. However, to maintain compatibility with broken commands, if the |
| 20 | * attribute length does not match the expected size a warning is emitted |
| 21 | * to the user that the command is sending invalid data and needs to be fixed. |
| 22 | */ |
David Ahern | 28033ae | 2017-11-07 21:59:40 -0800 | [diff] [blame] | 23 | static const u8 nla_attr_len[NLA_TYPE_MAX+1] = { |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 24 | [NLA_U8] = sizeof(u8), |
| 25 | [NLA_U16] = sizeof(u16), |
| 26 | [NLA_U32] = sizeof(u32), |
| 27 | [NLA_U64] = sizeof(u64), |
Julian Anastasov | 9eca2eb | 2012-08-25 22:47:57 +0000 | [diff] [blame] | 28 | [NLA_S8] = sizeof(s8), |
| 29 | [NLA_S16] = sizeof(s16), |
| 30 | [NLA_S32] = sizeof(s32), |
| 31 | [NLA_S64] = sizeof(s64), |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
David Ahern | 28033ae | 2017-11-07 21:59:40 -0800 | [diff] [blame] | 34 | static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = { |
David Ahern | 6e237d0 | 2017-12-06 20:09:12 -0800 | [diff] [blame] | 35 | [NLA_U8] = sizeof(u8), |
| 36 | [NLA_U16] = sizeof(u16), |
| 37 | [NLA_U32] = sizeof(u32), |
| 38 | [NLA_U64] = sizeof(u64), |
David Ahern | 28033ae | 2017-11-07 21:59:40 -0800 | [diff] [blame] | 39 | [NLA_MSECS] = sizeof(u64), |
| 40 | [NLA_NESTED] = NLA_HDRLEN, |
David Ahern | 6e237d0 | 2017-12-06 20:09:12 -0800 | [diff] [blame] | 41 | [NLA_S8] = sizeof(s8), |
| 42 | [NLA_S16] = sizeof(s16), |
| 43 | [NLA_S32] = sizeof(s32), |
| 44 | [NLA_S64] = sizeof(s64), |
David Ahern | 28033ae | 2017-11-07 21:59:40 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 47 | /* |
| 48 | * Nested policies might refer back to the original |
| 49 | * policy in some cases, and userspace could try to |
| 50 | * abuse that and recurse by nesting in the right |
| 51 | * ways. Limit recursion to avoid this problem. |
| 52 | */ |
| 53 | #define MAX_POLICY_RECURSION_DEPTH 10 |
| 54 | |
| 55 | static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, |
| 56 | const struct nla_policy *policy, |
| 57 | unsigned int validate, |
| 58 | struct netlink_ext_ack *extack, |
| 59 | struct nlattr **tb, unsigned int depth); |
| 60 | |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 61 | static int validate_nla_bitfield32(const struct nlattr *nla, |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 62 | const u32 valid_flags_mask) |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 63 | { |
| 64 | const struct nla_bitfield32 *bf = nla_data(nla); |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 65 | |
Johannes Berg | 48fde90 | 2018-09-26 11:15:31 +0200 | [diff] [blame] | 66 | if (!valid_flags_mask) |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | |
| 69 | /*disallow invalid bit selector */ |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 70 | if (bf->selector & ~valid_flags_mask) |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 71 | return -EINVAL; |
| 72 | |
| 73 | /*disallow invalid bit values */ |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 74 | if (bf->value & ~valid_flags_mask) |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 75 | return -EINVAL; |
| 76 | |
| 77 | /*disallow valid bit values that are not selected*/ |
| 78 | if (bf->value & ~bf->selector) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Johannes Berg | 1501d13 | 2018-09-26 11:15:34 +0200 | [diff] [blame] | 84 | static int nla_validate_array(const struct nlattr *head, int len, int maxtype, |
| 85 | const struct nla_policy *policy, |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 86 | struct netlink_ext_ack *extack, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 87 | unsigned int validate, unsigned int depth) |
Johannes Berg | 1501d13 | 2018-09-26 11:15:34 +0200 | [diff] [blame] | 88 | { |
| 89 | const struct nlattr *entry; |
| 90 | int rem; |
| 91 | |
| 92 | nla_for_each_attr(entry, head, len, rem) { |
| 93 | int ret; |
| 94 | |
| 95 | if (nla_len(entry) == 0) |
| 96 | continue; |
| 97 | |
| 98 | if (nla_len(entry) < NLA_HDRLEN) { |
| 99 | NL_SET_ERR_MSG_ATTR(extack, entry, |
| 100 | "Array element too short"); |
| 101 | return -ERANGE; |
| 102 | } |
| 103 | |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 104 | ret = __nla_validate_parse(nla_data(entry), nla_len(entry), |
| 105 | maxtype, policy, validate, extack, |
| 106 | NULL, depth + 1); |
Johannes Berg | 1501d13 | 2018-09-26 11:15:34 +0200 | [diff] [blame] | 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 114 | void nla_get_range_unsigned(const struct nla_policy *pt, |
| 115 | struct netlink_range_validation *range) |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 116 | { |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 117 | WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR && |
| 118 | (pt->min < 0 || pt->max < 0)); |
| 119 | |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 120 | range->min = 0; |
| 121 | |
| 122 | switch (pt->type) { |
| 123 | case NLA_U8: |
| 124 | range->max = U8_MAX; |
| 125 | break; |
| 126 | case NLA_U16: |
| 127 | range->max = U16_MAX; |
| 128 | break; |
| 129 | case NLA_U32: |
| 130 | range->max = U32_MAX; |
| 131 | break; |
| 132 | case NLA_U64: |
| 133 | case NLA_MSECS: |
| 134 | range->max = U64_MAX; |
| 135 | break; |
| 136 | default: |
| 137 | WARN_ON_ONCE(1); |
| 138 | return; |
| 139 | } |
| 140 | |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 141 | switch (pt->validation_type) { |
| 142 | case NLA_VALIDATE_RANGE: |
| 143 | range->min = pt->min; |
| 144 | range->max = pt->max; |
| 145 | break; |
| 146 | case NLA_VALIDATE_RANGE_PTR: |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 147 | *range = *pt->range; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 148 | break; |
| 149 | case NLA_VALIDATE_MIN: |
| 150 | range->min = pt->min; |
| 151 | break; |
| 152 | case NLA_VALIDATE_MAX: |
| 153 | range->max = pt->max; |
| 154 | break; |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 155 | default: |
| 156 | break; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 157 | } |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int nla_validate_int_range_unsigned(const struct nla_policy *pt, |
| 161 | const struct nlattr *nla, |
| 162 | struct netlink_ext_ack *extack) |
| 163 | { |
| 164 | struct netlink_range_validation range; |
| 165 | u64 value; |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 166 | |
| 167 | switch (pt->type) { |
| 168 | case NLA_U8: |
| 169 | value = nla_get_u8(nla); |
| 170 | break; |
| 171 | case NLA_U16: |
| 172 | value = nla_get_u16(nla); |
| 173 | break; |
| 174 | case NLA_U32: |
| 175 | value = nla_get_u32(nla); |
| 176 | break; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 177 | case NLA_U64: |
Johannes Berg | da4063b | 2020-04-30 22:13:09 +0200 | [diff] [blame] | 178 | case NLA_MSECS: |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 179 | value = nla_get_u64(nla); |
| 180 | break; |
| 181 | default: |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 185 | nla_get_range_unsigned(pt, &range); |
| 186 | |
| 187 | if (value < range.min || value > range.max) { |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 188 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 189 | "integer out of range"); |
| 190 | return -ERANGE; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 196 | void nla_get_range_signed(const struct nla_policy *pt, |
| 197 | struct netlink_range_validation_signed *range) |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 198 | { |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 199 | switch (pt->type) { |
| 200 | case NLA_S8: |
| 201 | range->min = S8_MIN; |
| 202 | range->max = S8_MAX; |
| 203 | break; |
| 204 | case NLA_S16: |
| 205 | range->min = S16_MIN; |
| 206 | range->max = S16_MAX; |
| 207 | break; |
| 208 | case NLA_S32: |
| 209 | range->min = S32_MIN; |
| 210 | range->max = S32_MAX; |
| 211 | break; |
| 212 | case NLA_S64: |
| 213 | range->min = S64_MIN; |
| 214 | range->max = S64_MAX; |
| 215 | break; |
| 216 | default: |
| 217 | WARN_ON_ONCE(1); |
| 218 | return; |
| 219 | } |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 220 | |
| 221 | switch (pt->validation_type) { |
| 222 | case NLA_VALIDATE_RANGE: |
| 223 | range->min = pt->min; |
| 224 | range->max = pt->max; |
| 225 | break; |
| 226 | case NLA_VALIDATE_RANGE_PTR: |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 227 | *range = *pt->range_signed; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 228 | break; |
| 229 | case NLA_VALIDATE_MIN: |
| 230 | range->min = pt->min; |
| 231 | break; |
| 232 | case NLA_VALIDATE_MAX: |
| 233 | range->max = pt->max; |
| 234 | break; |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 235 | default: |
| 236 | break; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 237 | } |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static int nla_validate_int_range_signed(const struct nla_policy *pt, |
| 241 | const struct nlattr *nla, |
| 242 | struct netlink_ext_ack *extack) |
| 243 | { |
| 244 | struct netlink_range_validation_signed range; |
| 245 | s64 value; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 246 | |
| 247 | switch (pt->type) { |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 248 | case NLA_S8: |
| 249 | value = nla_get_s8(nla); |
| 250 | break; |
| 251 | case NLA_S16: |
| 252 | value = nla_get_s16(nla); |
| 253 | break; |
| 254 | case NLA_S32: |
| 255 | value = nla_get_s32(nla); |
| 256 | break; |
| 257 | case NLA_S64: |
| 258 | value = nla_get_s64(nla); |
| 259 | break; |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 260 | default: |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 261 | return -EINVAL; |
| 262 | } |
| 263 | |
Johannes Berg | 2c28ae4 | 2020-04-30 22:13:11 +0200 | [diff] [blame] | 264 | nla_get_range_signed(pt, &range); |
| 265 | |
| 266 | if (value < range.min || value > range.max) { |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 267 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 268 | "integer out of range"); |
| 269 | return -ERANGE; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 275 | static int nla_validate_int_range(const struct nla_policy *pt, |
| 276 | const struct nlattr *nla, |
| 277 | struct netlink_ext_ack *extack) |
| 278 | { |
| 279 | switch (pt->type) { |
| 280 | case NLA_U8: |
| 281 | case NLA_U16: |
| 282 | case NLA_U32: |
| 283 | case NLA_U64: |
Johannes Berg | da4063b | 2020-04-30 22:13:09 +0200 | [diff] [blame] | 284 | case NLA_MSECS: |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 285 | return nla_validate_int_range_unsigned(pt, nla, extack); |
| 286 | case NLA_S8: |
| 287 | case NLA_S16: |
| 288 | case NLA_S32: |
| 289 | case NLA_S64: |
| 290 | return nla_validate_int_range_signed(pt, nla, extack); |
| 291 | default: |
| 292 | WARN_ON(1); |
| 293 | return -EINVAL; |
| 294 | } |
| 295 | } |
| 296 | |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 297 | static int validate_nla(const struct nlattr *nla, int maxtype, |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 298 | const struct nla_policy *policy, unsigned int validate, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 299 | struct netlink_ext_ack *extack, unsigned int depth) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 300 | { |
Johannes Berg | 56738f4 | 2019-04-26 14:07:30 +0200 | [diff] [blame] | 301 | u16 strict_start_type = policy[0].strict_start_type; |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 302 | const struct nla_policy *pt; |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 303 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 304 | int err = -ERANGE; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 305 | |
Johannes Berg | 56738f4 | 2019-04-26 14:07:30 +0200 | [diff] [blame] | 306 | if (strict_start_type && type >= strict_start_type) |
| 307 | validate |= NL_VALIDATE_STRICT; |
| 308 | |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 309 | if (type <= 0 || type > maxtype) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 310 | return 0; |
| 311 | |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 312 | pt = &policy[type]; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 313 | |
| 314 | BUG_ON(pt->type > NLA_TYPE_MAX); |
| 315 | |
Johannes Berg | b60b87f | 2018-09-17 11:57:29 +0200 | [diff] [blame] | 316 | if ((nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) || |
Johannes Berg | c7721c0 | 2020-04-30 22:13:10 +0200 | [diff] [blame] | 317 | (pt->type == NLA_EXACT_LEN && |
| 318 | pt->validation_type == NLA_VALIDATE_WARN_TOO_LONG && |
| 319 | attrlen != pt->len)) { |
David Ahern | 6e237d0 | 2017-12-06 20:09:12 -0800 | [diff] [blame] | 320 | pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n", |
| 321 | current->comm, type); |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 322 | if (validate & NL_VALIDATE_STRICT_ATTRS) { |
| 323 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 324 | "invalid attribute length"); |
| 325 | return -EINVAL; |
| 326 | } |
David Ahern | 28033ae | 2017-11-07 21:59:40 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Michal Kubecek | b424e43 | 2019-05-02 16:15:10 +0200 | [diff] [blame] | 329 | if (validate & NL_VALIDATE_NESTED) { |
| 330 | if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) && |
| 331 | !(nla->nla_type & NLA_F_NESTED)) { |
| 332 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 333 | "NLA_F_NESTED is missing"); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY && |
| 337 | pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) { |
| 338 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 339 | "NLA_F_NESTED not expected"); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | } |
| 343 | |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 344 | switch (pt->type) { |
Johannes Berg | 568b742 | 2018-09-17 11:57:28 +0200 | [diff] [blame] | 345 | case NLA_REJECT: |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 346 | if (extack && pt->reject_message) { |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 347 | NL_SET_BAD_ATTR(extack, nla); |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 348 | extack->_msg = pt->reject_message; |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 349 | return -EINVAL; |
| 350 | } |
| 351 | err = -EINVAL; |
| 352 | goto out_err; |
Johannes Berg | 568b742 | 2018-09-17 11:57:28 +0200 | [diff] [blame] | 353 | |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 354 | case NLA_FLAG: |
| 355 | if (attrlen > 0) |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 356 | goto out_err; |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 357 | break; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 358 | |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 359 | case NLA_BITFIELD32: |
| 360 | if (attrlen != sizeof(struct nla_bitfield32)) |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 361 | goto out_err; |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 362 | |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 363 | err = validate_nla_bitfield32(nla, pt->bitfield32_valid); |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 364 | if (err) |
| 365 | goto out_err; |
| 366 | break; |
Jamal Hadi Salim | 64c83d8 | 2017-07-30 13:24:49 -0400 | [diff] [blame] | 367 | |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 368 | case NLA_NUL_STRING: |
| 369 | if (pt->len) |
| 370 | minlen = min_t(int, attrlen, pt->len + 1); |
| 371 | else |
| 372 | minlen = attrlen; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 373 | |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 374 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) { |
| 375 | err = -EINVAL; |
| 376 | goto out_err; |
| 377 | } |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 378 | /* fall through */ |
| 379 | |
| 380 | case NLA_STRING: |
| 381 | if (attrlen < 1) |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 382 | goto out_err; |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 383 | |
| 384 | if (pt->len) { |
| 385 | char *buf = nla_data(nla); |
| 386 | |
| 387 | if (buf[attrlen - 1] == '\0') |
| 388 | attrlen--; |
| 389 | |
| 390 | if (attrlen > pt->len) |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 391 | goto out_err; |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 392 | } |
| 393 | break; |
| 394 | |
Johannes Berg | d30045a | 2007-03-23 11:37:48 -0700 | [diff] [blame] | 395 | case NLA_BINARY: |
| 396 | if (pt->len && attrlen > pt->len) |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 397 | goto out_err; |
Johannes Berg | d30045a | 2007-03-23 11:37:48 -0700 | [diff] [blame] | 398 | break; |
| 399 | |
Patrick McHardy | ea5693c | 2008-11-28 03:05:19 -0800 | [diff] [blame] | 400 | case NLA_NESTED: |
| 401 | /* a nested attributes is allowed to be empty; if its not, |
| 402 | * it must have a size of at least NLA_HDRLEN. |
| 403 | */ |
| 404 | if (attrlen == 0) |
| 405 | break; |
Johannes Berg | 9a659a35 | 2018-09-26 11:15:33 +0200 | [diff] [blame] | 406 | if (attrlen < NLA_HDRLEN) |
| 407 | goto out_err; |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 408 | if (pt->nested_policy) { |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 409 | err = __nla_validate_parse(nla_data(nla), nla_len(nla), |
| 410 | pt->len, pt->nested_policy, |
| 411 | validate, extack, NULL, |
| 412 | depth + 1); |
Johannes Berg | 9a659a35 | 2018-09-26 11:15:33 +0200 | [diff] [blame] | 413 | if (err < 0) { |
| 414 | /* |
| 415 | * return directly to preserve the inner |
| 416 | * error message/attribute pointer |
| 417 | */ |
| 418 | return err; |
| 419 | } |
| 420 | } |
| 421 | break; |
Johannes Berg | 1501d13 | 2018-09-26 11:15:34 +0200 | [diff] [blame] | 422 | case NLA_NESTED_ARRAY: |
| 423 | /* a nested array attribute is allowed to be empty; if its not, |
| 424 | * it must have a size of at least NLA_HDRLEN. |
| 425 | */ |
| 426 | if (attrlen == 0) |
| 427 | break; |
| 428 | if (attrlen < NLA_HDRLEN) |
| 429 | goto out_err; |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 430 | if (pt->nested_policy) { |
Johannes Berg | 1501d13 | 2018-09-26 11:15:34 +0200 | [diff] [blame] | 431 | int err; |
| 432 | |
| 433 | err = nla_validate_array(nla_data(nla), nla_len(nla), |
Johannes Berg | 47a1494 | 2020-04-30 22:13:05 +0200 | [diff] [blame] | 434 | pt->len, pt->nested_policy, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 435 | extack, validate, depth); |
Johannes Berg | 1501d13 | 2018-09-26 11:15:34 +0200 | [diff] [blame] | 436 | if (err < 0) { |
| 437 | /* |
| 438 | * return directly to preserve the inner |
| 439 | * error message/attribute pointer |
| 440 | */ |
| 441 | return err; |
| 442 | } |
| 443 | } |
| 444 | break; |
Johannes Berg | 6f455f5 | 2019-04-26 14:07:27 +0200 | [diff] [blame] | 445 | |
| 446 | case NLA_UNSPEC: |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 447 | if (validate & NL_VALIDATE_UNSPEC) { |
| 448 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 449 | "Unsupported attribute"); |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | /* fall through */ |
Johannes Berg | 6f455f5 | 2019-04-26 14:07:27 +0200 | [diff] [blame] | 453 | case NLA_MIN_LEN: |
| 454 | if (attrlen < pt->len) |
| 455 | goto out_err; |
| 456 | break; |
| 457 | |
Johannes Berg | c7721c0 | 2020-04-30 22:13:10 +0200 | [diff] [blame] | 458 | case NLA_EXACT_LEN: |
| 459 | if (pt->validation_type != NLA_VALIDATE_WARN_TOO_LONG) { |
| 460 | if (attrlen != pt->len) |
| 461 | goto out_err; |
| 462 | break; |
| 463 | } |
| 464 | /* fall through */ |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 465 | default: |
| 466 | if (pt->len) |
| 467 | minlen = pt->len; |
Johannes Berg | 6f455f5 | 2019-04-26 14:07:27 +0200 | [diff] [blame] | 468 | else |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 469 | minlen = nla_attr_minlen[pt->type]; |
| 470 | |
| 471 | if (attrlen < minlen) |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 472 | goto out_err; |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 473 | } |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 474 | |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 475 | /* further validation */ |
| 476 | switch (pt->validation_type) { |
| 477 | case NLA_VALIDATE_NONE: |
| 478 | /* nothing to do */ |
| 479 | break; |
Johannes Berg | d06a09b | 2020-04-30 22:13:08 +0200 | [diff] [blame] | 480 | case NLA_VALIDATE_RANGE_PTR: |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 481 | case NLA_VALIDATE_RANGE: |
| 482 | case NLA_VALIDATE_MIN: |
| 483 | case NLA_VALIDATE_MAX: |
| 484 | err = nla_validate_int_range(pt, nla, extack); |
| 485 | if (err) |
| 486 | return err; |
| 487 | break; |
Johannes Berg | 33188bd | 2018-09-27 11:28:36 +0200 | [diff] [blame] | 488 | case NLA_VALIDATE_FUNCTION: |
| 489 | if (pt->validate) { |
| 490 | err = pt->validate(nla, extack); |
| 491 | if (err) |
| 492 | return err; |
| 493 | } |
| 494 | break; |
Johannes Berg | 3e48be0 | 2018-09-27 11:28:35 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 497 | return 0; |
Johannes Berg | c29f184 | 2018-09-26 11:15:32 +0200 | [diff] [blame] | 498 | out_err: |
| 499 | NL_SET_ERR_MSG_ATTR(extack, nla, "Attribute failed policy validation"); |
| 500 | return err; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 503 | static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype, |
| 504 | const struct nla_policy *policy, |
| 505 | unsigned int validate, |
| 506 | struct netlink_ext_ack *extack, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 507 | struct nlattr **tb, unsigned int depth) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 508 | { |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 509 | const struct nlattr *nla; |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 510 | int rem; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 511 | |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 512 | if (depth >= MAX_POLICY_RECURSION_DEPTH) { |
| 513 | NL_SET_ERR_MSG(extack, |
| 514 | "allowed policy recursion depth exceeded"); |
| 515 | return -EINVAL; |
| 516 | } |
| 517 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 518 | if (tb) |
| 519 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 520 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 521 | nla_for_each_attr(nla, head, len, rem) { |
| 522 | u16 type = nla_type(nla); |
| 523 | |
| 524 | if (type == 0 || type > maxtype) { |
| 525 | if (validate & NL_VALIDATE_MAXTYPE) { |
Michal Kubecek | d54a16b | 2019-05-02 16:15:10 +0200 | [diff] [blame] | 526 | NL_SET_ERR_MSG_ATTR(extack, nla, |
| 527 | "Unknown attribute type"); |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 528 | return -EINVAL; |
| 529 | } |
| 530 | continue; |
| 531 | } |
| 532 | if (policy) { |
| 533 | int err = validate_nla(nla, maxtype, policy, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 534 | validate, extack, depth); |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 535 | |
| 536 | if (err < 0) |
| 537 | return err; |
| 538 | } |
| 539 | |
| 540 | if (tb) |
| 541 | tb[type] = (struct nlattr *)nla; |
| 542 | } |
| 543 | |
| 544 | if (unlikely(rem > 0)) { |
| 545 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
| 546 | rem, current->comm); |
| 547 | NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes"); |
| 548 | if (validate & NL_VALIDATE_TRAILING) |
| 549 | return -EINVAL; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 552 | return 0; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 553 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 554 | |
| 555 | /** |
| 556 | * __nla_validate - Validate a stream of attributes |
| 557 | * @head: head of attribute stream |
| 558 | * @len: length of attribute stream |
| 559 | * @maxtype: maximum attribute type to be expected |
| 560 | * @policy: validation policy |
| 561 | * @validate: validation strictness |
| 562 | * @extack: extended ACK report struct |
| 563 | * |
| 564 | * Validates all attributes in the specified attribute stream against the |
| 565 | * specified policy. Validation depends on the validate flags passed, see |
| 566 | * &enum netlink_validation for more details on that. |
| 567 | * See documenation of struct nla_policy for more details. |
| 568 | * |
| 569 | * Returns 0 on success or a negative error code. |
| 570 | */ |
| 571 | int __nla_validate(const struct nlattr *head, int len, int maxtype, |
| 572 | const struct nla_policy *policy, unsigned int validate, |
| 573 | struct netlink_ext_ack *extack) |
| 574 | { |
| 575 | return __nla_validate_parse(head, len, maxtype, policy, validate, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 576 | extack, NULL, 0); |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 577 | } |
| 578 | EXPORT_SYMBOL(__nla_validate); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 579 | |
| 580 | /** |
Holger Eitzenberger | e487eb99 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 581 | * nla_policy_len - Determin the max. length of a policy |
| 582 | * @policy: policy to use |
| 583 | * @n: number of policies |
| 584 | * |
| 585 | * Determines the max. length of the policy. It is currently used |
| 586 | * to allocated Netlink buffers roughly the size of the actual |
| 587 | * message. |
| 588 | * |
| 589 | * Returns 0 on success or a negative error code. |
| 590 | */ |
| 591 | int |
| 592 | nla_policy_len(const struct nla_policy *p, int n) |
| 593 | { |
| 594 | int i, len = 0; |
| 595 | |
Lars Ellenberg | e3fa3af | 2011-02-28 12:38:25 -0800 | [diff] [blame] | 596 | for (i = 0; i < n; i++, p++) { |
Holger Eitzenberger | e487eb99 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 597 | if (p->len) |
| 598 | len += nla_total_size(p->len); |
David Ahern | 28033ae | 2017-11-07 21:59:40 -0800 | [diff] [blame] | 599 | else if (nla_attr_len[p->type]) |
| 600 | len += nla_total_size(nla_attr_len[p->type]); |
Holger Eitzenberger | e487eb99 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 601 | else if (nla_attr_minlen[p->type]) |
| 602 | len += nla_total_size(nla_attr_minlen[p->type]); |
| 603 | } |
| 604 | |
| 605 | return len; |
| 606 | } |
Fabian Frederick | 6d6a138 | 2014-06-04 16:11:57 -0700 | [diff] [blame] | 607 | EXPORT_SYMBOL(nla_policy_len); |
Holger Eitzenberger | e487eb99 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 608 | |
| 609 | /** |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 610 | * __nla_parse - Parse a stream of attributes into a tb buffer |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 611 | * @tb: destination array with maxtype+1 elements |
| 612 | * @maxtype: maximum attribute type to be expected |
| 613 | * @head: head of attribute stream |
| 614 | * @len: length of attribute stream |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 615 | * @policy: validation policy |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 616 | * @validate: validation strictness |
| 617 | * @extack: extended ACK pointer |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 618 | * |
| 619 | * Parses a stream of attributes and stores a pointer to each attribute in |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 620 | * the tb array accessible via the attribute type. |
| 621 | * Validation is controlled by the @validate parameter. |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 622 | * |
| 623 | * Returns 0 on success or a negative error code. |
| 624 | */ |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 625 | int __nla_parse(struct nlattr **tb, int maxtype, |
| 626 | const struct nlattr *head, int len, |
| 627 | const struct nla_policy *policy, unsigned int validate, |
| 628 | struct netlink_ext_ack *extack) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 629 | { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 630 | return __nla_validate_parse(head, len, maxtype, policy, validate, |
Johannes Berg | 7690aa1 | 2020-04-30 22:13:06 +0200 | [diff] [blame] | 631 | extack, tb, 0); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 632 | } |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 633 | EXPORT_SYMBOL(__nla_parse); |
David Ahern | a5f6cba | 2018-10-07 20:16:25 -0700 | [diff] [blame] | 634 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 635 | /** |
| 636 | * nla_find - Find a specific attribute in a stream of attributes |
| 637 | * @head: head of attribute stream |
| 638 | * @len: length of attribute stream |
| 639 | * @attrtype: type of attribute to look for |
| 640 | * |
| 641 | * Returns the first attribute in the stream matching the specified type. |
| 642 | */ |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 643 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 644 | { |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 645 | const struct nlattr *nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 646 | int rem; |
| 647 | |
| 648 | nla_for_each_attr(nla, head, len, rem) |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 649 | if (nla_type(nla) == attrtype) |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 650 | return (struct nlattr *)nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 651 | |
| 652 | return NULL; |
| 653 | } |
Fabian Frederick | 6d6a138 | 2014-06-04 16:11:57 -0700 | [diff] [blame] | 654 | EXPORT_SYMBOL(nla_find); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 655 | |
| 656 | /** |
| 657 | * nla_strlcpy - Copy string attribute payload into a sized buffer |
| 658 | * @dst: where to copy the string to |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 659 | * @nla: attribute to copy the string from |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 660 | * @dstsize: size of destination buffer |
| 661 | * |
| 662 | * Copies at most dstsize - 1 bytes into the destination buffer. |
| 663 | * The result is always a valid NUL-terminated string. Unlike |
| 664 | * strlcpy the destination buffer is always padded out. |
| 665 | * |
| 666 | * Returns the length of the source buffer. |
| 667 | */ |
| 668 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) |
| 669 | { |
| 670 | size_t srclen = nla_len(nla); |
| 671 | char *src = nla_data(nla); |
| 672 | |
| 673 | if (srclen > 0 && src[srclen - 1] == '\0') |
| 674 | srclen--; |
| 675 | |
| 676 | if (dstsize > 0) { |
| 677 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; |
| 678 | |
| 679 | memset(dst, 0, dstsize); |
| 680 | memcpy(dst, src, len); |
| 681 | } |
| 682 | |
| 683 | return srclen; |
| 684 | } |
Fabian Frederick | 6d6a138 | 2014-06-04 16:11:57 -0700 | [diff] [blame] | 685 | EXPORT_SYMBOL(nla_strlcpy); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 686 | |
| 687 | /** |
Phil Sutter | 2cf0c8b | 2017-07-27 16:56:40 +0200 | [diff] [blame] | 688 | * nla_strdup - Copy string attribute payload into a newly allocated buffer |
| 689 | * @nla: attribute to copy the string from |
| 690 | * @flags: the type of memory to allocate (see kmalloc). |
| 691 | * |
| 692 | * Returns a pointer to the allocated buffer or NULL on error. |
| 693 | */ |
| 694 | char *nla_strdup(const struct nlattr *nla, gfp_t flags) |
| 695 | { |
| 696 | size_t srclen = nla_len(nla); |
| 697 | char *src = nla_data(nla), *dst; |
| 698 | |
| 699 | if (srclen > 0 && src[srclen - 1] == '\0') |
| 700 | srclen--; |
| 701 | |
| 702 | dst = kmalloc(srclen + 1, flags); |
| 703 | if (dst != NULL) { |
| 704 | memcpy(dst, src, srclen); |
| 705 | dst[srclen] = '\0'; |
| 706 | } |
| 707 | return dst; |
| 708 | } |
| 709 | EXPORT_SYMBOL(nla_strdup); |
| 710 | |
| 711 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 712 | * nla_memcpy - Copy a netlink attribute into another memory area |
| 713 | * @dest: where to copy to memcpy |
| 714 | * @src: netlink attribute to copy from |
| 715 | * @count: size of the destination area |
| 716 | * |
| 717 | * Note: The number of bytes copied is limited by the length of |
| 718 | * attribute's payload. memcpy |
| 719 | * |
| 720 | * Returns the number of bytes copied. |
| 721 | */ |
Patrick McHardy | b057efd | 2008-10-28 11:59:11 -0700 | [diff] [blame] | 722 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 723 | { |
| 724 | int minlen = min_t(int, count, nla_len(src)); |
| 725 | |
| 726 | memcpy(dest, nla_data(src), minlen); |
Jiri Benc | 5899f04 | 2015-03-29 16:05:28 +0200 | [diff] [blame] | 727 | if (count > minlen) |
| 728 | memset(dest + minlen, 0, count - minlen); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 729 | |
| 730 | return minlen; |
| 731 | } |
Fabian Frederick | 6d6a138 | 2014-06-04 16:11:57 -0700 | [diff] [blame] | 732 | EXPORT_SYMBOL(nla_memcpy); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 733 | |
| 734 | /** |
| 735 | * nla_memcmp - Compare an attribute with sized memory area |
| 736 | * @nla: netlink attribute |
| 737 | * @data: memory area |
| 738 | * @size: size of memory area |
| 739 | */ |
| 740 | int nla_memcmp(const struct nlattr *nla, const void *data, |
| 741 | size_t size) |
| 742 | { |
| 743 | int d = nla_len(nla) - size; |
| 744 | |
| 745 | if (d == 0) |
| 746 | d = memcmp(nla_data(nla), data, size); |
| 747 | |
| 748 | return d; |
| 749 | } |
Fabian Frederick | 6d6a138 | 2014-06-04 16:11:57 -0700 | [diff] [blame] | 750 | EXPORT_SYMBOL(nla_memcmp); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 751 | |
| 752 | /** |
| 753 | * nla_strcmp - Compare a string attribute against a string |
| 754 | * @nla: netlink string attribute |
| 755 | * @str: another string |
| 756 | */ |
| 757 | int nla_strcmp(const struct nlattr *nla, const char *str) |
| 758 | { |
Pablo Neira | 8b7b932 | 2014-04-01 19:38:44 +0200 | [diff] [blame] | 759 | int len = strlen(str); |
| 760 | char *buf = nla_data(nla); |
| 761 | int attrlen = nla_len(nla); |
| 762 | int d; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 763 | |
Pablo Neira | 8b7b932 | 2014-04-01 19:38:44 +0200 | [diff] [blame] | 764 | if (attrlen > 0 && buf[attrlen - 1] == '\0') |
| 765 | attrlen--; |
| 766 | |
| 767 | d = attrlen - len; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 768 | if (d == 0) |
| 769 | d = memcmp(nla_data(nla), str, len); |
| 770 | |
| 771 | return d; |
| 772 | } |
Fabian Frederick | 6d6a138 | 2014-06-04 16:11:57 -0700 | [diff] [blame] | 773 | EXPORT_SYMBOL(nla_strcmp); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 774 | |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 775 | #ifdef CONFIG_NET |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 776 | /** |
| 777 | * __nla_reserve - reserve room for attribute on the skb |
| 778 | * @skb: socket buffer to reserve room on |
| 779 | * @attrtype: attribute type |
| 780 | * @attrlen: length of attribute payload |
| 781 | * |
| 782 | * Adds a netlink attribute header to a socket buffer and reserves |
| 783 | * room for the payload but does not copy it. |
| 784 | * |
| 785 | * The caller is responsible to ensure that the skb provides enough |
| 786 | * tailroom for the attribute header and payload. |
| 787 | */ |
| 788 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) |
| 789 | { |
| 790 | struct nlattr *nla; |
| 791 | |
Johannes Berg | 4df864c | 2017-06-16 14:29:21 +0200 | [diff] [blame] | 792 | nla = skb_put(skb, nla_total_size(attrlen)); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 793 | nla->nla_type = attrtype; |
| 794 | nla->nla_len = nla_attr_size(attrlen); |
| 795 | |
| 796 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); |
| 797 | |
| 798 | return nla; |
| 799 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 800 | EXPORT_SYMBOL(__nla_reserve); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 801 | |
| 802 | /** |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 803 | * __nla_reserve_64bit - reserve room for attribute on the skb and align it |
| 804 | * @skb: socket buffer to reserve room on |
| 805 | * @attrtype: attribute type |
| 806 | * @attrlen: length of attribute payload |
Nicolas Dichtel | 11a9957 | 2016-04-22 17:31:16 +0200 | [diff] [blame] | 807 | * @padattr: attribute type for the padding |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 808 | * |
| 809 | * Adds a netlink attribute header to a socket buffer and reserves |
| 810 | * room for the payload but does not copy it. It also ensure that this |
Nicolas Dichtel | 11a9957 | 2016-04-22 17:31:16 +0200 | [diff] [blame] | 811 | * attribute will have a 64-bit aligned nla_data() area. |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 812 | * |
| 813 | * The caller is responsible to ensure that the skb provides enough |
| 814 | * tailroom for the attribute header and payload. |
| 815 | */ |
| 816 | struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype, |
| 817 | int attrlen, int padattr) |
| 818 | { |
| 819 | if (nla_need_padding_for_64bit(skb)) |
| 820 | nla_align_64bit(skb, padattr); |
| 821 | |
| 822 | return __nla_reserve(skb, attrtype, attrlen); |
| 823 | } |
| 824 | EXPORT_SYMBOL(__nla_reserve_64bit); |
| 825 | |
| 826 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 827 | * __nla_reserve_nohdr - reserve room for attribute without header |
| 828 | * @skb: socket buffer to reserve room on |
| 829 | * @attrlen: length of attribute payload |
| 830 | * |
| 831 | * Reserves room for attribute payload without a header. |
| 832 | * |
| 833 | * The caller is responsible to ensure that the skb provides enough |
| 834 | * tailroom for the payload. |
| 835 | */ |
| 836 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) |
| 837 | { |
yuan linyu | b952f4d | 2017-06-18 22:52:04 +0800 | [diff] [blame] | 838 | return skb_put_zero(skb, NLA_ALIGN(attrlen)); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 839 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 840 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 841 | |
| 842 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 843 | * nla_reserve - reserve room for attribute on the skb |
| 844 | * @skb: socket buffer to reserve room on |
| 845 | * @attrtype: attribute type |
| 846 | * @attrlen: length of attribute payload |
| 847 | * |
| 848 | * Adds a netlink attribute header to a socket buffer and reserves |
| 849 | * room for the payload but does not copy it. |
| 850 | * |
| 851 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 852 | * the attribute header and payload. |
| 853 | */ |
| 854 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) |
| 855 | { |
| 856 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) |
| 857 | return NULL; |
| 858 | |
| 859 | return __nla_reserve(skb, attrtype, attrlen); |
| 860 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 861 | EXPORT_SYMBOL(nla_reserve); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 862 | |
| 863 | /** |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 864 | * nla_reserve_64bit - reserve room for attribute on the skb and align it |
| 865 | * @skb: socket buffer to reserve room on |
| 866 | * @attrtype: attribute type |
| 867 | * @attrlen: length of attribute payload |
Nicolas Dichtel | 11a9957 | 2016-04-22 17:31:16 +0200 | [diff] [blame] | 868 | * @padattr: attribute type for the padding |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 869 | * |
| 870 | * Adds a netlink attribute header to a socket buffer and reserves |
| 871 | * room for the payload but does not copy it. It also ensure that this |
Nicolas Dichtel | 11a9957 | 2016-04-22 17:31:16 +0200 | [diff] [blame] | 872 | * attribute will have a 64-bit aligned nla_data() area. |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 873 | * |
| 874 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 875 | * the attribute header and payload. |
| 876 | */ |
| 877 | struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen, |
| 878 | int padattr) |
| 879 | { |
| 880 | size_t len; |
| 881 | |
| 882 | if (nla_need_padding_for_64bit(skb)) |
| 883 | len = nla_total_size_64bit(attrlen); |
| 884 | else |
| 885 | len = nla_total_size(attrlen); |
| 886 | if (unlikely(skb_tailroom(skb) < len)) |
| 887 | return NULL; |
| 888 | |
| 889 | return __nla_reserve_64bit(skb, attrtype, attrlen, padattr); |
| 890 | } |
| 891 | EXPORT_SYMBOL(nla_reserve_64bit); |
| 892 | |
| 893 | /** |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 894 | * nla_reserve_nohdr - reserve room for attribute without header |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 895 | * @skb: socket buffer to reserve room on |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 896 | * @attrlen: length of attribute payload |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 897 | * |
| 898 | * Reserves room for attribute payload without a header. |
| 899 | * |
| 900 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 901 | * the attribute payload. |
| 902 | */ |
| 903 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) |
| 904 | { |
| 905 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) |
| 906 | return NULL; |
| 907 | |
| 908 | return __nla_reserve_nohdr(skb, attrlen); |
| 909 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 910 | EXPORT_SYMBOL(nla_reserve_nohdr); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 911 | |
| 912 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 913 | * __nla_put - Add a netlink attribute to a socket buffer |
| 914 | * @skb: socket buffer to add attribute to |
| 915 | * @attrtype: attribute type |
| 916 | * @attrlen: length of attribute payload |
| 917 | * @data: head of attribute payload |
| 918 | * |
| 919 | * The caller is responsible to ensure that the skb provides enough |
| 920 | * tailroom for the attribute header and payload. |
| 921 | */ |
| 922 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, |
| 923 | const void *data) |
| 924 | { |
| 925 | struct nlattr *nla; |
| 926 | |
| 927 | nla = __nla_reserve(skb, attrtype, attrlen); |
| 928 | memcpy(nla_data(nla), data, attrlen); |
| 929 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 930 | EXPORT_SYMBOL(__nla_put); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 931 | |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 932 | /** |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 933 | * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it |
| 934 | * @skb: socket buffer to add attribute to |
| 935 | * @attrtype: attribute type |
| 936 | * @attrlen: length of attribute payload |
| 937 | * @data: head of attribute payload |
Nicolas Dichtel | 11a9957 | 2016-04-22 17:31:16 +0200 | [diff] [blame] | 938 | * @padattr: attribute type for the padding |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 939 | * |
| 940 | * The caller is responsible to ensure that the skb provides enough |
| 941 | * tailroom for the attribute header and payload. |
| 942 | */ |
| 943 | void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, |
| 944 | const void *data, int padattr) |
| 945 | { |
| 946 | struct nlattr *nla; |
| 947 | |
| 948 | nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr); |
| 949 | memcpy(nla_data(nla), data, attrlen); |
| 950 | } |
| 951 | EXPORT_SYMBOL(__nla_put_64bit); |
| 952 | |
| 953 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 954 | * __nla_put_nohdr - Add a netlink attribute without header |
| 955 | * @skb: socket buffer to add attribute to |
| 956 | * @attrlen: length of attribute payload |
| 957 | * @data: head of attribute payload |
| 958 | * |
| 959 | * The caller is responsible to ensure that the skb provides enough |
| 960 | * tailroom for the attribute payload. |
| 961 | */ |
| 962 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) |
| 963 | { |
| 964 | void *start; |
| 965 | |
| 966 | start = __nla_reserve_nohdr(skb, attrlen); |
| 967 | memcpy(start, data, attrlen); |
| 968 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 969 | EXPORT_SYMBOL(__nla_put_nohdr); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 970 | |
| 971 | /** |
| 972 | * nla_put - Add a netlink attribute to a socket buffer |
| 973 | * @skb: socket buffer to add attribute to |
| 974 | * @attrtype: attribute type |
| 975 | * @attrlen: length of attribute payload |
| 976 | * @data: head of attribute payload |
| 977 | * |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 978 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 979 | * the attribute header and payload. |
| 980 | */ |
| 981 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) |
| 982 | { |
| 983 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 984 | return -EMSGSIZE; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 985 | |
| 986 | __nla_put(skb, attrtype, attrlen, data); |
| 987 | return 0; |
| 988 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 989 | EXPORT_SYMBOL(nla_put); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 990 | |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 991 | /** |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 992 | * nla_put_64bit - Add a netlink attribute to a socket buffer and align it |
| 993 | * @skb: socket buffer to add attribute to |
| 994 | * @attrtype: attribute type |
| 995 | * @attrlen: length of attribute payload |
| 996 | * @data: head of attribute payload |
Nicolas Dichtel | 11a9957 | 2016-04-22 17:31:16 +0200 | [diff] [blame] | 997 | * @padattr: attribute type for the padding |
Nicolas Dichtel | 089bf1a | 2016-04-21 18:58:24 +0200 | [diff] [blame] | 998 | * |
| 999 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
| 1000 | * the attribute header and payload. |
| 1001 | */ |
| 1002 | int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen, |
| 1003 | const void *data, int padattr) |
| 1004 | { |
| 1005 | size_t len; |
| 1006 | |
| 1007 | if (nla_need_padding_for_64bit(skb)) |
| 1008 | len = nla_total_size_64bit(attrlen); |
| 1009 | else |
| 1010 | len = nla_total_size(attrlen); |
| 1011 | if (unlikely(skb_tailroom(skb) < len)) |
| 1012 | return -EMSGSIZE; |
| 1013 | |
| 1014 | __nla_put_64bit(skb, attrtype, attrlen, data, padattr); |
| 1015 | return 0; |
| 1016 | } |
| 1017 | EXPORT_SYMBOL(nla_put_64bit); |
| 1018 | |
| 1019 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 1020 | * nla_put_nohdr - Add a netlink attribute without header |
| 1021 | * @skb: socket buffer to add attribute to |
| 1022 | * @attrlen: length of attribute payload |
| 1023 | * @data: head of attribute payload |
| 1024 | * |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 1025 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 1026 | * the attribute payload. |
| 1027 | */ |
| 1028 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) |
| 1029 | { |
| 1030 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 1031 | return -EMSGSIZE; |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 1032 | |
| 1033 | __nla_put_nohdr(skb, attrlen, data); |
| 1034 | return 0; |
| 1035 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 1036 | EXPORT_SYMBOL(nla_put_nohdr); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 1037 | |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 1038 | /** |
| 1039 | * nla_append - Add a netlink attribute without header or padding |
| 1040 | * @skb: socket buffer to add attribute to |
| 1041 | * @attrlen: length of attribute payload |
| 1042 | * @data: head of attribute payload |
| 1043 | * |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 1044 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 1045 | * the attribute payload. |
| 1046 | */ |
| 1047 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) |
| 1048 | { |
| 1049 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 1050 | return -EMSGSIZE; |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 1051 | |
Johannes Berg | 59ae1d1 | 2017-06-16 14:29:20 +0200 | [diff] [blame] | 1052 | skb_put_data(skb, data, attrlen); |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 1055 | EXPORT_SYMBOL(nla_append); |
| 1056 | #endif |