attr.c 12 KB

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