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