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