attr.c 10 KB

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