nlattr.c 12 KB

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