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