nlattr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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_64bit - reserve room for attribute on the skb and align it
  306. * @skb: socket buffer to reserve room on
  307. * @attrtype: attribute type
  308. * @attrlen: length of attribute payload
  309. * @padattr: attribute type for the padding
  310. *
  311. * Adds a netlink attribute header to a socket buffer and reserves
  312. * room for the payload but does not copy it. It also ensure that this
  313. * attribute will have a 64-bit aligned nla_data() area.
  314. *
  315. * The caller is responsible to ensure that the skb provides enough
  316. * tailroom for the attribute header and payload.
  317. */
  318. struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
  319. int attrlen, int padattr)
  320. {
  321. if (nla_need_padding_for_64bit(skb))
  322. nla_align_64bit(skb, padattr);
  323. return __nla_reserve(skb, attrtype, attrlen);
  324. }
  325. EXPORT_SYMBOL(__nla_reserve_64bit);
  326. /**
  327. * __nla_reserve_nohdr - reserve room for attribute without header
  328. * @skb: socket buffer to reserve room on
  329. * @attrlen: length of attribute payload
  330. *
  331. * Reserves room for attribute payload without a header.
  332. *
  333. * The caller is responsible to ensure that the skb provides enough
  334. * tailroom for the payload.
  335. */
  336. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  337. {
  338. void *start;
  339. start = skb_put(skb, NLA_ALIGN(attrlen));
  340. memset(start, 0, NLA_ALIGN(attrlen));
  341. return start;
  342. }
  343. EXPORT_SYMBOL(__nla_reserve_nohdr);
  344. /**
  345. * nla_reserve - reserve room for attribute on the skb
  346. * @skb: socket buffer to reserve room on
  347. * @attrtype: attribute type
  348. * @attrlen: length of attribute payload
  349. *
  350. * Adds a netlink attribute header to a socket buffer and reserves
  351. * room for the payload but does not copy it.
  352. *
  353. * Returns NULL if the tailroom of the skb is insufficient to store
  354. * the attribute header and payload.
  355. */
  356. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  357. {
  358. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  359. return NULL;
  360. return __nla_reserve(skb, attrtype, attrlen);
  361. }
  362. EXPORT_SYMBOL(nla_reserve);
  363. /**
  364. * nla_reserve_64bit - reserve room for attribute on the skb and align it
  365. * @skb: socket buffer to reserve room on
  366. * @attrtype: attribute type
  367. * @attrlen: length of attribute payload
  368. * @padattr: attribute type for the padding
  369. *
  370. * Adds a netlink attribute header to a socket buffer and reserves
  371. * room for the payload but does not copy it. It also ensure that this
  372. * attribute will have a 64-bit aligned nla_data() area.
  373. *
  374. * Returns NULL if the tailroom of the skb is insufficient to store
  375. * the attribute header and payload.
  376. */
  377. struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  378. int padattr)
  379. {
  380. size_t len;
  381. if (nla_need_padding_for_64bit(skb))
  382. len = nla_total_size_64bit(attrlen);
  383. else
  384. len = nla_total_size(attrlen);
  385. if (unlikely(skb_tailroom(skb) < len))
  386. return NULL;
  387. return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
  388. }
  389. EXPORT_SYMBOL(nla_reserve_64bit);
  390. /**
  391. * nla_reserve_nohdr - reserve room for attribute without header
  392. * @skb: socket buffer to reserve room on
  393. * @attrlen: length of attribute payload
  394. *
  395. * Reserves room for attribute payload without a header.
  396. *
  397. * Returns NULL if the tailroom of the skb is insufficient to store
  398. * the attribute payload.
  399. */
  400. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  401. {
  402. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  403. return NULL;
  404. return __nla_reserve_nohdr(skb, attrlen);
  405. }
  406. EXPORT_SYMBOL(nla_reserve_nohdr);
  407. /**
  408. * __nla_put - Add a netlink attribute to a socket buffer
  409. * @skb: socket buffer to add attribute to
  410. * @attrtype: attribute type
  411. * @attrlen: length of attribute payload
  412. * @data: head of attribute payload
  413. *
  414. * The caller is responsible to ensure that the skb provides enough
  415. * tailroom for the attribute header and payload.
  416. */
  417. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  418. const void *data)
  419. {
  420. struct nlattr *nla;
  421. nla = __nla_reserve(skb, attrtype, attrlen);
  422. memcpy(nla_data(nla), data, attrlen);
  423. }
  424. EXPORT_SYMBOL(__nla_put);
  425. /**
  426. * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
  427. * @skb: socket buffer to add attribute to
  428. * @attrtype: attribute type
  429. * @attrlen: length of attribute payload
  430. * @data: head of attribute payload
  431. * @padattr: attribute type for the padding
  432. *
  433. * The caller is responsible to ensure that the skb provides enough
  434. * tailroom for the attribute header and payload.
  435. */
  436. void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  437. const void *data, int padattr)
  438. {
  439. struct nlattr *nla;
  440. nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
  441. memcpy(nla_data(nla), data, attrlen);
  442. }
  443. EXPORT_SYMBOL(__nla_put_64bit);
  444. /**
  445. * __nla_put_nohdr - Add a netlink attribute without header
  446. * @skb: socket buffer to add attribute to
  447. * @attrlen: length of attribute payload
  448. * @data: head of attribute payload
  449. *
  450. * The caller is responsible to ensure that the skb provides enough
  451. * tailroom for the attribute payload.
  452. */
  453. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  454. {
  455. void *start;
  456. start = __nla_reserve_nohdr(skb, attrlen);
  457. memcpy(start, data, attrlen);
  458. }
  459. EXPORT_SYMBOL(__nla_put_nohdr);
  460. /**
  461. * nla_put - Add a netlink attribute to a socket buffer
  462. * @skb: socket buffer to add attribute to
  463. * @attrtype: attribute type
  464. * @attrlen: length of attribute payload
  465. * @data: head of attribute payload
  466. *
  467. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  468. * the attribute header and payload.
  469. */
  470. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  471. {
  472. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  473. return -EMSGSIZE;
  474. __nla_put(skb, attrtype, attrlen, data);
  475. return 0;
  476. }
  477. EXPORT_SYMBOL(nla_put);
  478. /**
  479. * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
  480. * @skb: socket buffer to add attribute to
  481. * @attrtype: attribute type
  482. * @attrlen: length of attribute payload
  483. * @data: head of attribute payload
  484. * @padattr: attribute type for the padding
  485. *
  486. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  487. * the attribute header and payload.
  488. */
  489. int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  490. const void *data, int padattr)
  491. {
  492. size_t len;
  493. if (nla_need_padding_for_64bit(skb))
  494. len = nla_total_size_64bit(attrlen);
  495. else
  496. len = nla_total_size(attrlen);
  497. if (unlikely(skb_tailroom(skb) < len))
  498. return -EMSGSIZE;
  499. __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
  500. return 0;
  501. }
  502. EXPORT_SYMBOL(nla_put_64bit);
  503. /**
  504. * nla_put_nohdr - Add a netlink attribute without header
  505. * @skb: socket buffer to add attribute to
  506. * @attrlen: length of attribute payload
  507. * @data: head of attribute payload
  508. *
  509. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  510. * the attribute payload.
  511. */
  512. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  513. {
  514. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  515. return -EMSGSIZE;
  516. __nla_put_nohdr(skb, attrlen, data);
  517. return 0;
  518. }
  519. EXPORT_SYMBOL(nla_put_nohdr);
  520. /**
  521. * nla_append - Add a netlink attribute without header or padding
  522. * @skb: socket buffer to add attribute to
  523. * @attrlen: length of attribute payload
  524. * @data: head of attribute payload
  525. *
  526. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  527. * the attribute payload.
  528. */
  529. int nla_append(struct sk_buff *skb, int attrlen, const void *data)
  530. {
  531. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  532. return -EMSGSIZE;
  533. memcpy(skb_put(skb, attrlen), data, attrlen);
  534. return 0;
  535. }
  536. EXPORT_SYMBOL(nla_append);
  537. #endif