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