attr.c 11 KB

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