attr.c 11 KB

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