nlattr.c 12 KB

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