1/* 2 * NETLINK Netlink attributes 3 * 4 * Authors: Thomas Graf <tgraf@suug.ch> 5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> 6 */ 7 8#include <linux/module.h> 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 18static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = { 19 [NLA_U8] = sizeof(u8), 20 [NLA_U16] = sizeof(u16), 21 [NLA_U32] = sizeof(u32), 22 [NLA_U64] = sizeof(u64), 23 [NLA_NESTED] = NLA_HDRLEN, 24}; 25 26static int validate_nla(struct nlattr *nla, int maxtype, 27 const struct nla_policy *policy) 28{ 29 const struct nla_policy *pt; 30 int minlen = 0, attrlen = nla_len(nla); 31 32 if (nla->nla_type <= 0 || nla->nla_type > maxtype) 33 return 0; 34 35 pt = &policy[nla->nla_type]; 36 37 BUG_ON(pt->type > NLA_TYPE_MAX); 38 39 switch (pt->type) { 40 case NLA_FLAG: 41 if (attrlen > 0) 42 return -ERANGE; 43 break; 44 45 case NLA_NUL_STRING: 46 if (pt->len) 47 minlen = min_t(int, attrlen, pt->len + 1); 48 else 49 minlen = attrlen; 50 51 if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) 52 return -EINVAL; 53 /* fall through */ 54 55 case NLA_STRING: 56 if (attrlen < 1) 57 return -ERANGE; 58 59 if (pt->len) { 60 char *buf = nla_data(nla); 61 62 if (buf[attrlen - 1] == '\0') 63 attrlen--; 64 65 if (attrlen > pt->len) 66 return -ERANGE; 67 } 68 break; 69 70 case NLA_BINARY: 71 if (pt->len && attrlen > pt->len) 72 return -ERANGE; 73 break; 74 75 default: 76 if (pt->len) 77 minlen = pt->len; 78 else if (pt->type != NLA_UNSPEC) 79 minlen = nla_attr_minlen[pt->type]; 80 81 if (attrlen < minlen) 82 return -ERANGE; 83 } 84 85 return 0; 86} 87 88/** 89 * nla_validate - Validate a stream of attributes 90 * @head: head of attribute stream 91 * @len: length of attribute stream 92 * @maxtype: maximum attribute type to be expected 93 * @policy: validation policy 94 * 95 * Validates all attributes in the specified attribute stream against the 96 * specified policy. Attributes with a type exceeding maxtype will be 97 * ignored. See documenation of struct nla_policy for more details. 98 * 99 * Returns 0 on success or a negative error code. 100 */ 101int nla_validate(struct nlattr *head, int len, int maxtype, 102 const struct nla_policy *policy) 103{ 104 struct nlattr *nla; 105 int rem, err; 106 107 nla_for_each_attr(nla, head, len, rem) { 108 err = validate_nla(nla, maxtype, policy); 109 if (err < 0) 110 goto errout; 111 } 112 113 err = 0; 114errout: 115 return err; 116} 117 118/** 119 * nla_parse - Parse a stream of attributes into a tb buffer 120 * @tb: destination array with maxtype+1 elements 121 * @maxtype: maximum attribute type to be expected 122 * @head: head of attribute stream 123 * @len: length of attribute stream 124 * 125 * Parses a stream of attributes and stores a pointer to each attribute in 126 * the tb array accessable via the attribute type. Attributes with a type 127 * exceeding maxtype will be silently ignored for backwards compatibility 128 * reasons. policy may be set to NULL if no validation is required. 129 * 130 * Returns 0 on success or a negative error code. 131 */ 132int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, 133 const struct nla_policy *policy) 134{ 135 struct nlattr *nla; 136 int rem, err; 137 138 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1)); 139 140 nla_for_each_attr(nla, head, len, rem) { 141 u16 type = nla->nla_type; 142 143 if (type > 0 && type <= maxtype) { 144 if (policy) { 145 err = validate_nla(nla, maxtype, policy); 146 if (err < 0) 147 goto errout; 148 } 149 150 tb[type] = nla; 151 } 152 } 153 154 if (unlikely(rem > 0)) 155 printk(KERN_WARNING "netlink: %d bytes leftover after parsing " 156 "attributes.\n", rem); 157 158 err = 0; 159errout: 160 return err; 161} 162 163/** 164 * nla_find - Find a specific attribute in a stream of attributes 165 * @head: head of attribute stream 166 * @len: length of attribute stream 167 * @attrtype: type of attribute to look for 168 * 169 * Returns the first attribute in the stream matching the specified type. 170 */ 171struct nlattr *nla_find(struct nlattr *head, int len, int attrtype) 172{ 173 struct nlattr *nla; 174 int rem; 175 176 nla_for_each_attr(nla, head, len, rem) 177 if (nla->nla_type == attrtype) 178 return nla; 179 180 return NULL; 181} 182 183/** 184 * nla_strlcpy - Copy string attribute payload into a sized buffer 185 * @dst: where to copy the string to 186 * @src: attribute to copy the string from 187 * @dstsize: size of destination buffer 188 * 189 * Copies at most dstsize - 1 bytes into the destination buffer. 190 * The result is always a valid NUL-terminated string. Unlike 191 * strlcpy the destination buffer is always padded out. 192 * 193 * Returns the length of the source buffer. 194 */ 195size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize) 196{ 197 size_t srclen = nla_len(nla); 198 char *src = nla_data(nla); 199 200 if (srclen > 0 && src[srclen - 1] == '\0') 201 srclen--; 202 203 if (dstsize > 0) { 204 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen; 205 206 memset(dst, 0, dstsize); 207 memcpy(dst, src, len); 208 } 209 210 return srclen; 211} 212 213/** 214 * nla_memcpy - Copy a netlink attribute into another memory area 215 * @dest: where to copy to memcpy 216 * @src: netlink attribute to copy from 217 * @count: size of the destination area 218 * 219 * Note: The number of bytes copied is limited by the length of 220 * attribute's payload. memcpy 221 * 222 * Returns the number of bytes copied. 223 */ 224int nla_memcpy(void *dest, struct nlattr *src, int count) 225{ 226 int minlen = min_t(int, count, nla_len(src)); 227 228 memcpy(dest, nla_data(src), minlen); 229 230 return minlen; 231} 232 233/** 234 * nla_memcmp - Compare an attribute with sized memory area 235 * @nla: netlink attribute 236 * @data: memory area 237 * @size: size of memory area 238 */ 239int nla_memcmp(const struct nlattr *nla, const void *data, 240 size_t size) 241{ 242 int d = nla_len(nla) - size; 243 244 if (d == 0) 245 d = memcmp(nla_data(nla), data, size); 246 247 return d; 248} 249 250/** 251 * nla_strcmp - Compare a string attribute against a string 252 * @nla: netlink string attribute 253 * @str: another string 254 */ 255int nla_strcmp(const struct nlattr *nla, const char *str) 256{ 257 int len = strlen(str) + 1; 258 int d = nla_len(nla) - len; 259 260 if (d == 0) 261 d = memcmp(nla_data(nla), str, len); 262 263 return d; 264} 265 266/** 267 * __nla_reserve - reserve room for attribute on the skb 268 * @skb: socket buffer to reserve room on 269 * @attrtype: attribute type 270 * @attrlen: length of attribute payload 271 * 272 * Adds a netlink attribute header to a socket buffer and reserves 273 * room for the payload but does not copy it. 274 * 275 * The caller is responsible to ensure that the skb provides enough 276 * tailroom for the attribute header and payload. 277 */ 278struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) 279{ 280 struct nlattr *nla; 281 282 nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen)); 283 nla->nla_type = attrtype; 284 nla->nla_len = nla_attr_size(attrlen); 285 286 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen)); 287 288 return nla; 289} 290 291/** 292 * __nla_reserve_nohdr - reserve room for attribute without header 293 * @skb: socket buffer to reserve room on 294 * @attrlen: length of attribute payload 295 * 296 * Reserves room for attribute payload without a header. 297 * 298 * The caller is responsible to ensure that the skb provides enough 299 * tailroom for the payload. 300 */ 301void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen) 302{ 303 void *start; 304 305 start = skb_put(skb, NLA_ALIGN(attrlen)); 306 memset(start, 0, NLA_ALIGN(attrlen)); 307 308 return start; 309} 310 311/** 312 * nla_reserve - reserve room for attribute on the skb 313 * @skb: socket buffer to reserve room on 314 * @attrtype: attribute type 315 * @attrlen: length of attribute payload 316 * 317 * Adds a netlink attribute header to a socket buffer and reserves 318 * room for the payload but does not copy it. 319 * 320 * Returns NULL if the tailroom of the skb is insufficient to store 321 * the attribute header and payload. 322 */ 323struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen) 324{ 325 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) 326 return NULL; 327 328 return __nla_reserve(skb, attrtype, attrlen); 329} 330 331/** 332 * nla_reserve - reserve room for attribute without header 333 * @skb: socket buffer to reserve room on 334 * @len: length of attribute payload 335 * 336 * Reserves room for attribute payload without a header. 337 * 338 * Returns NULL if the tailroom of the skb is insufficient to store 339 * the attribute payload. 340 */ 341void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen) 342{ 343 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) 344 return NULL; 345 346 return __nla_reserve_nohdr(skb, attrlen); 347} 348 349/** 350 * __nla_put - Add a netlink attribute to a socket buffer 351 * @skb: socket buffer to add attribute to 352 * @attrtype: attribute type 353 * @attrlen: length of attribute payload 354 * @data: head of attribute payload 355 * 356 * The caller is responsible to ensure that the skb provides enough 357 * tailroom for the attribute header and payload. 358 */ 359void __nla_put(struct sk_buff *skb, int attrtype, int attrlen, 360 const void *data) 361{ 362 struct nlattr *nla; 363 364 nla = __nla_reserve(skb, attrtype, attrlen); 365 memcpy(nla_data(nla), data, attrlen); 366} 367 368/** 369 * __nla_put_nohdr - Add a netlink attribute without header 370 * @skb: socket buffer to add attribute to 371 * @attrlen: length of attribute payload 372 * @data: head of attribute payload 373 * 374 * The caller is responsible to ensure that the skb provides enough 375 * tailroom for the attribute payload. 376 */ 377void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) 378{ 379 void *start; 380 381 start = __nla_reserve_nohdr(skb, attrlen); 382 memcpy(start, data, attrlen); 383} 384 385/** 386 * nla_put - Add a netlink attribute to a socket buffer 387 * @skb: socket buffer to add attribute to 388 * @attrtype: attribute type 389 * @attrlen: length of attribute payload 390 * @data: head of attribute payload 391 * 392 * Returns -1 if the tailroom of the skb is insufficient to store 393 * the attribute header and payload. 394 */ 395int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 396{ 397 if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen))) 398 return -1; 399 400 __nla_put(skb, attrtype, attrlen, data); 401 return 0; 402} 403 404/** 405 * nla_put_nohdr - Add a netlink attribute without header 406 * @skb: socket buffer to add attribute to 407 * @attrlen: length of attribute payload 408 * @data: head of attribute payload 409 * 410 * Returns -1 if the tailroom of the skb is insufficient to store 411 * the attribute payload. 412 */ 413int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data) 414{ 415 if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen))) 416 return -1; 417 418 __nla_put_nohdr(skb, attrlen, data); 419 return 0; 420} 421 422EXPORT_SYMBOL(nla_validate); 423EXPORT_SYMBOL(nla_parse); 424EXPORT_SYMBOL(nla_find); 425EXPORT_SYMBOL(nla_strlcpy); 426EXPORT_SYMBOL(__nla_reserve); 427EXPORT_SYMBOL(__nla_reserve_nohdr); 428EXPORT_SYMBOL(nla_reserve); 429EXPORT_SYMBOL(nla_reserve_nohdr); 430EXPORT_SYMBOL(__nla_put); 431EXPORT_SYMBOL(__nla_put_nohdr); 432EXPORT_SYMBOL(nla_put); 433EXPORT_SYMBOL(nla_put_nohdr); 434EXPORT_SYMBOL(nla_memcpy); 435EXPORT_SYMBOL(nla_memcmp); 436EXPORT_SYMBOL(nla_strcmp); 437