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