attr.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * NETLINK Netlink attributes
  3. *
  4. * Authors: Thomas Graf <tgraf@suug.ch>
  5. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/config.h>
  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. static u16 nla_attr_minlen[NLA_TYPE_MAX+1] __read_mostly = {
  18. [NLA_U8] = sizeof(u8),
  19. [NLA_U16] = sizeof(u16),
  20. [NLA_U32] = sizeof(u32),
  21. [NLA_U64] = sizeof(u64),
  22. [NLA_STRING] = 1,
  23. [NLA_NESTED] = NLA_HDRLEN,
  24. };
  25. static int validate_nla(struct nlattr *nla, int maxtype,
  26. struct nla_policy *policy)
  27. {
  28. struct nla_policy *pt;
  29. int minlen = 0;
  30. if (nla->nla_type <= 0 || nla->nla_type > maxtype)
  31. return 0;
  32. pt = &policy[nla->nla_type];
  33. BUG_ON(pt->type > NLA_TYPE_MAX);
  34. if (pt->minlen)
  35. minlen = pt->minlen;
  36. else if (pt->type != NLA_UNSPEC)
  37. minlen = nla_attr_minlen[pt->type];
  38. if (pt->type == NLA_FLAG && nla_len(nla) > 0)
  39. return -ERANGE;
  40. if (nla_len(nla) < minlen)
  41. return -ERANGE;
  42. return 0;
  43. }
  44. /**
  45. * nla_validate - Validate a stream of attributes
  46. * @head: head of attribute stream
  47. * @len: length of attribute stream
  48. * @maxtype: maximum attribute type to be expected
  49. * @policy: validation policy
  50. *
  51. * Validates all attributes in the specified attribute stream against the
  52. * specified policy. Attributes with a type exceeding maxtype will be
  53. * ignored. See documenation of struct nla_policy for more details.
  54. *
  55. * Returns 0 on success or a negative error code.
  56. */
  57. int nla_validate(struct nlattr *head, int len, int maxtype,
  58. struct nla_policy *policy)
  59. {
  60. struct nlattr *nla;
  61. int rem, err;
  62. nla_for_each_attr(nla, head, len, rem) {
  63. err = validate_nla(nla, maxtype, policy);
  64. if (err < 0)
  65. goto errout;
  66. }
  67. err = 0;
  68. errout:
  69. return err;
  70. }
  71. /**
  72. * nla_parse - Parse a stream of attributes into a tb buffer
  73. * @tb: destination array with maxtype+1 elements
  74. * @maxtype: maximum attribute type to be expected
  75. * @head: head of attribute stream
  76. * @len: length of attribute stream
  77. *
  78. * Parses a stream of attributes and stores a pointer to each attribute in
  79. * the tb array accessable via the attribute type. Attributes with a type
  80. * exceeding maxtype will be silently ignored for backwards compatibility
  81. * reasons. policy may be set to NULL if no validation is required.
  82. *
  83. * Returns 0 on success or a negative error code.
  84. */
  85. int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
  86. struct nla_policy *policy)
  87. {
  88. struct nlattr *nla;
  89. int rem, err;
  90. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  91. nla_for_each_attr(nla, head, len, rem) {
  92. u16 type = nla->nla_type;
  93. if (type > 0 && type <= maxtype) {
  94. if (policy) {
  95. err = validate_nla(nla, maxtype, policy);
  96. if (err < 0)
  97. goto errout;
  98. }
  99. tb[type] = nla;
  100. }
  101. }
  102. if (unlikely(rem > 0))
  103. printk(KERN_WARNING "netlink: %d bytes leftover after parsing "
  104. "attributes.\n", rem);
  105. err = 0;
  106. errout:
  107. return err;
  108. }
  109. /**
  110. * nla_find - Find a specific attribute in a stream of attributes
  111. * @head: head of attribute stream
  112. * @len: length of attribute stream
  113. * @attrtype: type of attribute to look for
  114. *
  115. * Returns the first attribute in the stream matching the specified type.
  116. */
  117. struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
  118. {
  119. struct nlattr *nla;
  120. int rem;
  121. nla_for_each_attr(nla, head, len, rem)
  122. if (nla->nla_type == attrtype)
  123. return nla;
  124. return NULL;
  125. }
  126. /**
  127. * nla_strlcpy - Copy string attribute payload into a sized buffer
  128. * @dst: where to copy the string to
  129. * @src: attribute to copy the string from
  130. * @dstsize: size of destination buffer
  131. *
  132. * Copies at most dstsize - 1 bytes into the destination buffer.
  133. * The result is always a valid NUL-terminated string. Unlike
  134. * strlcpy the destination buffer is always padded out.
  135. *
  136. * Returns the length of the source buffer.
  137. */
  138. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
  139. {
  140. size_t srclen = nla_len(nla);
  141. char *src = nla_data(nla);
  142. if (srclen > 0 && src[srclen - 1] == '\0')
  143. srclen--;
  144. if (dstsize > 0) {
  145. size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
  146. memset(dst, 0, dstsize);
  147. memcpy(dst, src, len);
  148. }
  149. return srclen;
  150. }
  151. /**
  152. * nla_memcpy - Copy a netlink attribute into another memory area
  153. * @dest: where to copy to memcpy
  154. * @src: netlink attribute to copy from
  155. * @count: size of the destination area
  156. *
  157. * Note: The number of bytes copied is limited by the length of
  158. * attribute's payload. memcpy
  159. *
  160. * Returns the number of bytes copied.
  161. */
  162. int nla_memcpy(void *dest, struct nlattr *src, int count)
  163. {
  164. int minlen = min_t(int, count, nla_len(src));
  165. memcpy(dest, nla_data(src), minlen);
  166. return minlen;
  167. }
  168. /**
  169. * nla_memcmp - Compare an attribute with sized memory area
  170. * @nla: netlink attribute
  171. * @data: memory area
  172. * @size: size of memory area
  173. */
  174. int nla_memcmp(const struct nlattr *nla, const void *data,
  175. size_t size)
  176. {
  177. int d = nla_len(nla) - size;
  178. if (d == 0)
  179. d = memcmp(nla_data(nla), data, size);
  180. return d;
  181. }
  182. /**
  183. * nla_strcmp - Compare a string attribute against a string
  184. * @nla: netlink string attribute
  185. * @str: another string
  186. */
  187. int nla_strcmp(const struct nlattr *nla, const char *str)
  188. {
  189. int len = strlen(str) + 1;
  190. int d = nla_len(nla) - len;
  191. if (d == 0)
  192. d = memcmp(nla_data(nla), str, len);
  193. return d;
  194. }
  195. /**
  196. * __nla_reserve - reserve room for attribute on the skb
  197. * @skb: socket buffer to reserve room on
  198. * @attrtype: attribute type
  199. * @attrlen: length of attribute payload
  200. *
  201. * Adds a netlink attribute header to a socket buffer and reserves
  202. * room for the payload but does not copy it.
  203. *
  204. * The caller is responsible to ensure that the skb provides enough
  205. * tailroom for the attribute header and payload.
  206. */
  207. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  208. {
  209. struct nlattr *nla;
  210. nla = (struct nlattr *) skb_put(skb, nla_total_size(attrlen));
  211. nla->nla_type = attrtype;
  212. nla->nla_len = nla_attr_size(attrlen);
  213. memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
  214. return nla;
  215. }
  216. /**
  217. * nla_reserve - reserve room for attribute on the skb
  218. * @skb: socket buffer to reserve room on
  219. * @attrtype: attribute type
  220. * @attrlen: length of attribute payload
  221. *
  222. * Adds a netlink attribute header to a socket buffer and reserves
  223. * room for the payload but does not copy it.
  224. *
  225. * Returns NULL if the tailroom of the skb is insufficient to store
  226. * the attribute header and payload.
  227. */
  228. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  229. {
  230. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  231. return NULL;
  232. return __nla_reserve(skb, attrtype, attrlen);
  233. }
  234. /**
  235. * __nla_put - Add a netlink attribute to a socket buffer
  236. * @skb: socket buffer to add attribute to
  237. * @attrtype: attribute type
  238. * @attrlen: length of attribute payload
  239. * @data: head of attribute payload
  240. *
  241. * The caller is responsible to ensure that the skb provides enough
  242. * tailroom for the attribute header and payload.
  243. */
  244. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  245. const void *data)
  246. {
  247. struct nlattr *nla;
  248. nla = __nla_reserve(skb, attrtype, attrlen);
  249. memcpy(nla_data(nla), data, attrlen);
  250. }
  251. /**
  252. * nla_put - Add a netlink attribute to a socket buffer
  253. * @skb: socket buffer to add attribute to
  254. * @attrtype: attribute type
  255. * @attrlen: length of attribute payload
  256. * @data: head of attribute payload
  257. *
  258. * Returns -1 if the tailroom of the skb is insufficient to store
  259. * the attribute header and payload.
  260. */
  261. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  262. {
  263. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  264. return -1;
  265. __nla_put(skb, attrtype, attrlen, data);
  266. return 0;
  267. }
  268. EXPORT_SYMBOL(nla_validate);
  269. EXPORT_SYMBOL(nla_parse);
  270. EXPORT_SYMBOL(nla_find);
  271. EXPORT_SYMBOL(nla_strlcpy);
  272. EXPORT_SYMBOL(__nla_reserve);
  273. EXPORT_SYMBOL(nla_reserve);
  274. EXPORT_SYMBOL(__nla_put);
  275. EXPORT_SYMBOL(nla_put);
  276. EXPORT_SYMBOL(nla_memcpy);
  277. EXPORT_SYMBOL(nla_memcmp);
  278. EXPORT_SYMBOL(nla_strcmp);