attr.c 7.9 KB

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