nlattr.c 12 KB

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