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