attr.c 10 KB

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