nlattr.c 13 KB

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