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