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