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> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/types.h> |
Michal Schmidt | 11d6e45 | 2014-06-02 18:25:02 +0200 | [diff] [blame] | 16 | #include <linux/ratelimit.h> |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 17 | #include <net/netlink.h> |
| 18 | |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 19 | static const u16 nla_attr_minlen[NLA_TYPE_MAX+1] = { |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 20 | [NLA_U8] = sizeof(u8), |
| 21 | [NLA_U16] = sizeof(u16), |
| 22 | [NLA_U32] = sizeof(u32), |
| 23 | [NLA_U64] = sizeof(u64), |
Johannes Berg | c30bc94 | 2011-11-03 00:07:32 +0000 | [diff] [blame] | 24 | [NLA_MSECS] = sizeof(u64), |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 25 | [NLA_NESTED] = NLA_HDRLEN, |
| 26 | }; |
| 27 | |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 28 | static int validate_nla(const struct nlattr *nla, int maxtype, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 29 | const struct nla_policy *policy) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 30 | { |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 31 | const struct nla_policy *pt; |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 32 | int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 33 | |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 34 | if (type <= 0 || type > maxtype) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 35 | return 0; |
| 36 | |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 37 | pt = &policy[type]; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 38 | |
| 39 | BUG_ON(pt->type > NLA_TYPE_MAX); |
| 40 | |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 41 | switch (pt->type) { |
| 42 | case NLA_FLAG: |
| 43 | if (attrlen > 0) |
| 44 | return -ERANGE; |
| 45 | break; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 46 | |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 47 | case NLA_NUL_STRING: |
| 48 | if (pt->len) |
| 49 | minlen = min_t(int, attrlen, pt->len + 1); |
| 50 | else |
| 51 | minlen = attrlen; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 52 | |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 53 | if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) |
| 54 | return -EINVAL; |
| 55 | /* fall through */ |
| 56 | |
| 57 | case NLA_STRING: |
| 58 | if (attrlen < 1) |
| 59 | return -ERANGE; |
| 60 | |
| 61 | if (pt->len) { |
| 62 | char *buf = nla_data(nla); |
| 63 | |
| 64 | if (buf[attrlen - 1] == '\0') |
| 65 | attrlen--; |
| 66 | |
| 67 | if (attrlen > pt->len) |
| 68 | return -ERANGE; |
| 69 | } |
| 70 | break; |
| 71 | |
Johannes Berg | d30045a | 2007-03-23 11:37:48 -0700 | [diff] [blame] | 72 | case NLA_BINARY: |
| 73 | if (pt->len && attrlen > pt->len) |
| 74 | return -ERANGE; |
| 75 | break; |
| 76 | |
Patrick McHardy | 1092cb2 | 2007-06-25 13:49:35 -0700 | [diff] [blame] | 77 | case NLA_NESTED_COMPAT: |
| 78 | if (attrlen < pt->len) |
| 79 | return -ERANGE; |
| 80 | if (attrlen < NLA_ALIGN(pt->len)) |
| 81 | break; |
| 82 | if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN) |
| 83 | return -ERANGE; |
| 84 | nla = nla_data(nla) + NLA_ALIGN(pt->len); |
| 85 | if (attrlen < NLA_ALIGN(pt->len) + NLA_HDRLEN + nla_len(nla)) |
| 86 | return -ERANGE; |
| 87 | break; |
Patrick McHardy | ea5693c | 2008-11-28 03:05:19 -0800 | [diff] [blame] | 88 | case NLA_NESTED: |
| 89 | /* a nested attributes is allowed to be empty; if its not, |
| 90 | * it must have a size of at least NLA_HDRLEN. |
| 91 | */ |
| 92 | if (attrlen == 0) |
| 93 | break; |
Thomas Graf | a5531a5 | 2006-08-26 20:11:47 -0700 | [diff] [blame] | 94 | default: |
| 95 | if (pt->len) |
| 96 | minlen = pt->len; |
| 97 | else if (pt->type != NLA_UNSPEC) |
| 98 | minlen = nla_attr_minlen[pt->type]; |
| 99 | |
| 100 | if (attrlen < minlen) |
| 101 | return -ERANGE; |
| 102 | } |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * nla_validate - Validate a stream of attributes |
| 109 | * @head: head of attribute stream |
| 110 | * @len: length of attribute stream |
| 111 | * @maxtype: maximum attribute type to be expected |
| 112 | * @policy: validation policy |
| 113 | * |
| 114 | * Validates all attributes in the specified attribute stream against the |
| 115 | * specified policy. Attributes with a type exceeding maxtype will be |
| 116 | * ignored. See documenation of struct nla_policy for more details. |
| 117 | * |
| 118 | * Returns 0 on success or a negative error code. |
| 119 | */ |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 120 | int nla_validate(const struct nlattr *head, int len, int maxtype, |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 121 | const struct nla_policy *policy) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 122 | { |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 123 | const struct nlattr *nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 124 | int rem, err; |
| 125 | |
| 126 | nla_for_each_attr(nla, head, len, rem) { |
| 127 | err = validate_nla(nla, maxtype, policy); |
| 128 | if (err < 0) |
| 129 | goto errout; |
| 130 | } |
| 131 | |
| 132 | err = 0; |
| 133 | errout: |
| 134 | return err; |
| 135 | } |
| 136 | |
| 137 | /** |
Holger Eitzenberger | e487eb9 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 138 | * nla_policy_len - Determin the max. length of a policy |
| 139 | * @policy: policy to use |
| 140 | * @n: number of policies |
| 141 | * |
| 142 | * Determines the max. length of the policy. It is currently used |
| 143 | * to allocated Netlink buffers roughly the size of the actual |
| 144 | * message. |
| 145 | * |
| 146 | * Returns 0 on success or a negative error code. |
| 147 | */ |
| 148 | int |
| 149 | nla_policy_len(const struct nla_policy *p, int n) |
| 150 | { |
| 151 | int i, len = 0; |
| 152 | |
Lars Ellenberg | e3fa3af | 2011-02-28 12:38:25 -0800 | [diff] [blame] | 153 | for (i = 0; i < n; i++, p++) { |
Holger Eitzenberger | e487eb9 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 154 | if (p->len) |
| 155 | len += nla_total_size(p->len); |
| 156 | else if (nla_attr_minlen[p->type]) |
| 157 | len += nla_total_size(nla_attr_minlen[p->type]); |
| 158 | } |
| 159 | |
| 160 | return len; |
| 161 | } |
| 162 | |
| 163 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 164 | * nla_parse - Parse a stream of attributes into a tb buffer |
| 165 | * @tb: destination array with maxtype+1 elements |
| 166 | * @maxtype: maximum attribute type to be expected |
| 167 | * @head: head of attribute stream |
| 168 | * @len: length of attribute stream |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 169 | * @policy: validation policy |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 170 | * |
| 171 | * 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] | 172 | * the tb array accessible via the attribute type. Attributes with a type |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 173 | * exceeding maxtype will be silently ignored for backwards compatibility |
| 174 | * reasons. policy may be set to NULL if no validation is required. |
| 175 | * |
| 176 | * Returns 0 on success or a negative error code. |
| 177 | */ |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 178 | int nla_parse(struct nlattr **tb, int maxtype, const struct nlattr *head, |
| 179 | int len, const struct nla_policy *policy) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 180 | { |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 181 | const struct nlattr *nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 182 | int rem, err; |
| 183 | |
| 184 | memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); |
| 185 | |
| 186 | nla_for_each_attr(nla, head, len, rem) { |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 187 | u16 type = nla_type(nla); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 188 | |
| 189 | if (type > 0 && type <= maxtype) { |
| 190 | if (policy) { |
| 191 | err = validate_nla(nla, maxtype, policy); |
| 192 | if (err < 0) |
| 193 | goto errout; |
| 194 | } |
| 195 | |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 196 | tb[type] = (struct nlattr *)nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | if (unlikely(rem > 0)) |
Michal Schmidt | 11d6e45 | 2014-06-02 18:25:02 +0200 | [diff] [blame] | 201 | pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n", |
| 202 | rem, current->comm); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 203 | |
| 204 | err = 0; |
| 205 | errout: |
| 206 | return err; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * nla_find - Find a specific attribute in a stream of attributes |
| 211 | * @head: head of attribute stream |
| 212 | * @len: length of attribute stream |
| 213 | * @attrtype: type of attribute to look for |
| 214 | * |
| 215 | * Returns the first attribute in the stream matching the specified type. |
| 216 | */ |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 217 | struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 218 | { |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 219 | const struct nlattr *nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 220 | int rem; |
| 221 | |
| 222 | nla_for_each_attr(nla, head, len, rem) |
Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 223 | if (nla_type(nla) == attrtype) |
Jan Engelhardt | 3654654 | 2010-11-16 09:52:32 -0800 | [diff] [blame] | 224 | return (struct nlattr *)nla; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * nla_strlcpy - Copy string attribute payload into a sized buffer |
| 231 | * @dst: where to copy the string to |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 232 | * @nla: attribute to copy the string from |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 233 | * @dstsize: size of destination buffer |
| 234 | * |
| 235 | * Copies at most dstsize - 1 bytes into the destination buffer. |
| 236 | * The result is always a valid NUL-terminated string. Unlike |
| 237 | * strlcpy the destination buffer is always padded out. |
| 238 | * |
| 239 | * Returns the length of the source buffer. |
| 240 | */ |
| 241 | size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) |
| 242 | { |
| 243 | size_t srclen = nla_len(nla); |
| 244 | char *src = nla_data(nla); |
| 245 | |
| 246 | if (srclen > 0 && src[srclen - 1] == '\0') |
| 247 | srclen--; |
| 248 | |
| 249 | if (dstsize > 0) { |
| 250 | size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; |
| 251 | |
| 252 | memset(dst, 0, dstsize); |
| 253 | memcpy(dst, src, len); |
| 254 | } |
| 255 | |
| 256 | return srclen; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * nla_memcpy - Copy a netlink attribute into another memory area |
| 261 | * @dest: where to copy to memcpy |
| 262 | * @src: netlink attribute to copy from |
| 263 | * @count: size of the destination area |
| 264 | * |
| 265 | * Note: The number of bytes copied is limited by the length of |
| 266 | * attribute's payload. memcpy |
| 267 | * |
| 268 | * Returns the number of bytes copied. |
| 269 | */ |
Patrick McHardy | b057efd | 2008-10-28 11:59:11 -0700 | [diff] [blame] | 270 | int nla_memcpy(void *dest, const struct nlattr *src, int count) |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 271 | { |
| 272 | int minlen = min_t(int, count, nla_len(src)); |
| 273 | |
| 274 | memcpy(dest, nla_data(src), minlen); |
| 275 | |
| 276 | return minlen; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * nla_memcmp - Compare an attribute with sized memory area |
| 281 | * @nla: netlink attribute |
| 282 | * @data: memory area |
| 283 | * @size: size of memory area |
| 284 | */ |
| 285 | int nla_memcmp(const struct nlattr *nla, const void *data, |
| 286 | size_t size) |
| 287 | { |
| 288 | int d = nla_len(nla) - size; |
| 289 | |
| 290 | if (d == 0) |
| 291 | d = memcmp(nla_data(nla), data, size); |
| 292 | |
| 293 | return d; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * nla_strcmp - Compare a string attribute against a string |
| 298 | * @nla: netlink string attribute |
| 299 | * @str: another string |
| 300 | */ |
| 301 | int nla_strcmp(const struct nlattr *nla, const char *str) |
| 302 | { |
Pablo Neira | 9e51fbd | 2014-04-01 19:38:44 +0200 | [diff] [blame] | 303 | int len = strlen(str); |
| 304 | char *buf = nla_data(nla); |
| 305 | int attrlen = nla_len(nla); |
| 306 | int d; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 307 | |
Pablo Neira | 9e51fbd | 2014-04-01 19:38:44 +0200 | [diff] [blame] | 308 | if (attrlen > 0 && buf[attrlen - 1] == '\0') |
| 309 | attrlen--; |
| 310 | |
| 311 | d = attrlen - len; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 312 | if (d == 0) |
| 313 | d = memcmp(nla_data(nla), str, len); |
| 314 | |
| 315 | return d; |
| 316 | } |
| 317 | |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 318 | #ifdef CONFIG_NET |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 319 | /** |
| 320 | * __nla_reserve - reserve room for attribute on the skb |
| 321 | * @skb: socket buffer to reserve room on |
| 322 | * @attrtype: attribute type |
| 323 | * @attrlen: length of attribute payload |
| 324 | * |
| 325 | * Adds a netlink attribute header to a socket buffer and reserves |
| 326 | * room for the payload but does not copy it. |
| 327 | * |
| 328 | * The caller is responsible to ensure that the skb provides enough |
| 329 | * tailroom for the attribute header and payload. |
| 330 | */ |
| 331 | struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) |
| 332 | { |
| 333 | struct nlattr *nla; |
| 334 | |
| 335 | nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen)); |
| 336 | nla->nla_type = attrtype; |
| 337 | nla->nla_len = nla_attr_size(attrlen); |
| 338 | |
| 339 | memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); |
| 340 | |
| 341 | return nla; |
| 342 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 343 | EXPORT_SYMBOL(__nla_reserve); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 344 | |
| 345 | /** |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 346 | * __nla_reserve_nohdr - reserve room for attribute without header |
| 347 | * @skb: socket buffer to reserve room on |
| 348 | * @attrlen: length of attribute payload |
| 349 | * |
| 350 | * Reserves room for attribute payload without a header. |
| 351 | * |
| 352 | * The caller is responsible to ensure that the skb provides enough |
| 353 | * tailroom for the payload. |
| 354 | */ |
| 355 | void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) |
| 356 | { |
| 357 | void *start; |
| 358 | |
| 359 | start = skb_put(skb, NLA_ALIGN(attrlen)); |
| 360 | memset(start, 0, NLA_ALIGN(attrlen)); |
| 361 | |
| 362 | return start; |
| 363 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 364 | EXPORT_SYMBOL(__nla_reserve_nohdr); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 365 | |
| 366 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 367 | * nla_reserve - reserve room for attribute on the skb |
| 368 | * @skb: socket buffer to reserve room on |
| 369 | * @attrtype: attribute type |
| 370 | * @attrlen: length of attribute payload |
| 371 | * |
| 372 | * Adds a netlink attribute header to a socket buffer and reserves |
| 373 | * room for the payload but does not copy it. |
| 374 | * |
| 375 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 376 | * the attribute header and payload. |
| 377 | */ |
| 378 | struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) |
| 379 | { |
| 380 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) |
| 381 | return NULL; |
| 382 | |
| 383 | return __nla_reserve(skb, attrtype, attrlen); |
| 384 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 385 | EXPORT_SYMBOL(nla_reserve); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 386 | |
| 387 | /** |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 388 | * nla_reserve_nohdr - reserve room for attribute without header |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 389 | * @skb: socket buffer to reserve room on |
Julius Volz | 10b595a | 2008-06-27 20:02:14 -0700 | [diff] [blame] | 390 | * @attrlen: length of attribute payload |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 391 | * |
| 392 | * Reserves room for attribute payload without a header. |
| 393 | * |
| 394 | * Returns NULL if the tailroom of the skb is insufficient to store |
| 395 | * the attribute payload. |
| 396 | */ |
| 397 | void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) |
| 398 | { |
| 399 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) |
| 400 | return NULL; |
| 401 | |
| 402 | return __nla_reserve_nohdr(skb, attrlen); |
| 403 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 404 | EXPORT_SYMBOL(nla_reserve_nohdr); |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 405 | |
| 406 | /** |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 407 | * __nla_put - Add a netlink attribute to a socket buffer |
| 408 | * @skb: socket buffer to add attribute to |
| 409 | * @attrtype: attribute type |
| 410 | * @attrlen: length of attribute payload |
| 411 | * @data: head of attribute payload |
| 412 | * |
| 413 | * The caller is responsible to ensure that the skb provides enough |
| 414 | * tailroom for the attribute header and payload. |
| 415 | */ |
| 416 | void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, |
| 417 | const void *data) |
| 418 | { |
| 419 | struct nlattr *nla; |
| 420 | |
| 421 | nla = __nla_reserve(skb, attrtype, attrlen); |
| 422 | memcpy(nla_data(nla), data, attrlen); |
| 423 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 424 | EXPORT_SYMBOL(__nla_put); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 425 | |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 426 | /** |
| 427 | * __nla_put_nohdr - Add a netlink attribute without header |
| 428 | * @skb: socket buffer to add attribute to |
| 429 | * @attrlen: length of attribute payload |
| 430 | * @data: head of attribute payload |
| 431 | * |
| 432 | * The caller is responsible to ensure that the skb provides enough |
| 433 | * tailroom for the attribute payload. |
| 434 | */ |
| 435 | void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) |
| 436 | { |
| 437 | void *start; |
| 438 | |
| 439 | start = __nla_reserve_nohdr(skb, attrlen); |
| 440 | memcpy(start, data, attrlen); |
| 441 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 442 | EXPORT_SYMBOL(__nla_put_nohdr); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 443 | |
| 444 | /** |
| 445 | * nla_put - Add a netlink attribute to a socket buffer |
| 446 | * @skb: socket buffer to add attribute to |
| 447 | * @attrtype: attribute type |
| 448 | * @attrlen: length of attribute payload |
| 449 | * @data: head of attribute payload |
| 450 | * |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 451 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 452 | * the attribute header and payload. |
| 453 | */ |
| 454 | int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) |
| 455 | { |
| 456 | if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 457 | return -EMSGSIZE; |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 458 | |
| 459 | __nla_put(skb, attrtype, attrlen, data); |
| 460 | return 0; |
| 461 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 462 | EXPORT_SYMBOL(nla_put); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 463 | |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 464 | /** |
| 465 | * nla_put_nohdr - Add a netlink attribute without header |
| 466 | * @skb: socket buffer to add attribute to |
| 467 | * @attrlen: length of attribute payload |
| 468 | * @data: head of attribute payload |
| 469 | * |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 470 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 471 | * the attribute payload. |
| 472 | */ |
| 473 | int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) |
| 474 | { |
| 475 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 476 | return -EMSGSIZE; |
Thomas Graf | fe4944e | 2006-08-04 23:03:05 -0700 | [diff] [blame] | 477 | |
| 478 | __nla_put_nohdr(skb, attrlen, data); |
| 479 | return 0; |
| 480 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 481 | EXPORT_SYMBOL(nla_put_nohdr); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 482 | |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 483 | /** |
| 484 | * nla_append - Add a netlink attribute without header or padding |
| 485 | * @skb: socket buffer to add attribute to |
| 486 | * @attrlen: length of attribute payload |
| 487 | * @data: head of attribute payload |
| 488 | * |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 489 | * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 490 | * the attribute payload. |
| 491 | */ |
| 492 | int nla_append(struct sk_buff *skb, int attrlen, const void *data) |
| 493 | { |
| 494 | if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 495 | return -EMSGSIZE; |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 496 | |
| 497 | memcpy(skb_put(skb, attrlen), data, attrlen); |
| 498 | return 0; |
| 499 | } |
Herbert Xu | 9080021 | 2009-03-11 23:18:32 +0800 | [diff] [blame] | 500 | EXPORT_SYMBOL(nla_append); |
| 501 | #endif |
Patrick McHardy | 01480e1 | 2008-01-22 22:10:59 -0800 | [diff] [blame] | 502 | |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 503 | EXPORT_SYMBOL(nla_validate); |
Holger Eitzenberger | e487eb9 | 2009-03-25 18:26:30 +0100 | [diff] [blame] | 504 | EXPORT_SYMBOL(nla_policy_len); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 505 | EXPORT_SYMBOL(nla_parse); |
| 506 | EXPORT_SYMBOL(nla_find); |
| 507 | EXPORT_SYMBOL(nla_strlcpy); |
Thomas Graf | bfa83a9 | 2005-11-10 02:25:51 +0100 | [diff] [blame] | 508 | EXPORT_SYMBOL(nla_memcpy); |
| 509 | EXPORT_SYMBOL(nla_memcmp); |
| 510 | EXPORT_SYMBOL(nla_strcmp); |