nlattr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NETLINK Netlink attributes
  4. *
  5. * Authors: Thomas Graf <tgraf@suug.ch>
  6. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  7. */
  8. #include <linux/export.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. #include <net/netlink.h>
  16. /* For these data types, attribute length should be exactly the given
  17. * size. However, to maintain compatibility with broken commands, if the
  18. * attribute length does not match the expected size a warning is emitted
  19. * to the user that the command is sending invalid data and needs to be fixed.
  20. */
  21. static const u8 nla_attr_len[NLA_TYPE_MAX+1] = {
  22. [NLA_U8] = sizeof(u8),
  23. [NLA_U16] = sizeof(u16),
  24. [NLA_U32] = sizeof(u32),
  25. [NLA_U64] = sizeof(u64),
  26. [NLA_S8] = sizeof(s8),
  27. [NLA_S16] = sizeof(s16),
  28. [NLA_S32] = sizeof(s32),
  29. [NLA_S64] = sizeof(s64),
  30. };
  31. static const u8 nla_attr_minlen[NLA_TYPE_MAX+1] = {
  32. [NLA_U8] = sizeof(u8),
  33. [NLA_U16] = sizeof(u16),
  34. [NLA_U32] = sizeof(u32),
  35. [NLA_U64] = sizeof(u64),
  36. [NLA_MSECS] = sizeof(u64),
  37. [NLA_NESTED] = NLA_HDRLEN,
  38. [NLA_S8] = sizeof(s8),
  39. [NLA_S16] = sizeof(s16),
  40. [NLA_S32] = sizeof(s32),
  41. [NLA_S64] = sizeof(s64),
  42. };
  43. /*
  44. * Nested policies might refer back to the original
  45. * policy in some cases, and userspace could try to
  46. * abuse that and recurse by nesting in the right
  47. * ways. Limit recursion to avoid this problem.
  48. */
  49. #define MAX_POLICY_RECURSION_DEPTH 10
  50. static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
  51. const struct nla_policy *policy,
  52. unsigned int validate,
  53. struct netlink_ext_ack *extack,
  54. struct nlattr **tb, unsigned int depth);
  55. static int validate_nla_bitfield32(const struct nlattr *nla,
  56. const u32 valid_flags_mask)
  57. {
  58. const struct nla_bitfield32 *bf = nla_data(nla);
  59. if (!valid_flags_mask)
  60. return -EINVAL;
  61. /*disallow invalid bit selector */
  62. if (bf->selector & ~valid_flags_mask)
  63. return -EINVAL;
  64. /*disallow invalid bit values */
  65. if (bf->value & ~valid_flags_mask)
  66. return -EINVAL;
  67. /*disallow valid bit values that are not selected*/
  68. if (bf->value & ~bf->selector)
  69. return -EINVAL;
  70. return 0;
  71. }
  72. static int nla_validate_array(const struct nlattr *head, int len, int maxtype,
  73. const struct nla_policy *policy,
  74. struct netlink_ext_ack *extack,
  75. unsigned int validate, unsigned int depth)
  76. {
  77. const struct nlattr *entry;
  78. int rem;
  79. nla_for_each_attr(entry, head, len, rem) {
  80. int ret;
  81. if (nla_len(entry) == 0)
  82. continue;
  83. if (nla_len(entry) < NLA_HDRLEN) {
  84. NL_SET_ERR_MSG_ATTR_POL(extack, entry, policy,
  85. "Array element too short");
  86. return -ERANGE;
  87. }
  88. ret = __nla_validate_parse(nla_data(entry), nla_len(entry),
  89. maxtype, policy, validate, extack,
  90. NULL, depth + 1);
  91. if (ret < 0)
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. void nla_get_range_unsigned(const struct nla_policy *pt,
  97. struct netlink_range_validation *range)
  98. {
  99. WARN_ON_ONCE(pt->validation_type != NLA_VALIDATE_RANGE_PTR &&
  100. (pt->min < 0 || pt->max < 0));
  101. range->min = 0;
  102. switch (pt->type) {
  103. case NLA_U8:
  104. range->max = U8_MAX;
  105. break;
  106. case NLA_U16:
  107. case NLA_BINARY:
  108. range->max = U16_MAX;
  109. break;
  110. case NLA_U32:
  111. range->max = U32_MAX;
  112. break;
  113. case NLA_U64:
  114. case NLA_MSECS:
  115. range->max = U64_MAX;
  116. break;
  117. default:
  118. WARN_ON_ONCE(1);
  119. return;
  120. }
  121. switch (pt->validation_type) {
  122. case NLA_VALIDATE_RANGE:
  123. case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
  124. range->min = pt->min;
  125. range->max = pt->max;
  126. break;
  127. case NLA_VALIDATE_RANGE_PTR:
  128. *range = *pt->range;
  129. break;
  130. case NLA_VALIDATE_MIN:
  131. range->min = pt->min;
  132. break;
  133. case NLA_VALIDATE_MAX:
  134. range->max = pt->max;
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. static int nla_validate_range_unsigned(const struct nla_policy *pt,
  141. const struct nlattr *nla,
  142. struct netlink_ext_ack *extack,
  143. unsigned int validate)
  144. {
  145. struct netlink_range_validation range;
  146. u64 value;
  147. switch (pt->type) {
  148. case NLA_U8:
  149. value = nla_get_u8(nla);
  150. break;
  151. case NLA_U16:
  152. value = nla_get_u16(nla);
  153. break;
  154. case NLA_U32:
  155. value = nla_get_u32(nla);
  156. break;
  157. case NLA_U64:
  158. case NLA_MSECS:
  159. value = nla_get_u64(nla);
  160. break;
  161. case NLA_BINARY:
  162. value = nla_len(nla);
  163. break;
  164. default:
  165. return -EINVAL;
  166. }
  167. nla_get_range_unsigned(pt, &range);
  168. if (pt->validation_type == NLA_VALIDATE_RANGE_WARN_TOO_LONG &&
  169. pt->type == NLA_BINARY && value > range.max) {
  170. pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
  171. current->comm, pt->type);
  172. if (validate & NL_VALIDATE_STRICT_ATTRS) {
  173. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  174. "invalid attribute length");
  175. return -EINVAL;
  176. }
  177. /* this assumes min <= max (don't validate against min) */
  178. return 0;
  179. }
  180. if (value < range.min || value > range.max) {
  181. bool binary = pt->type == NLA_BINARY;
  182. if (binary)
  183. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  184. "binary attribute size out of range");
  185. else
  186. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  187. "integer out of range");
  188. return -ERANGE;
  189. }
  190. return 0;
  191. }
  192. void nla_get_range_signed(const struct nla_policy *pt,
  193. struct netlink_range_validation_signed *range)
  194. {
  195. switch (pt->type) {
  196. case NLA_S8:
  197. range->min = S8_MIN;
  198. range->max = S8_MAX;
  199. break;
  200. case NLA_S16:
  201. range->min = S16_MIN;
  202. range->max = S16_MAX;
  203. break;
  204. case NLA_S32:
  205. range->min = S32_MIN;
  206. range->max = S32_MAX;
  207. break;
  208. case NLA_S64:
  209. range->min = S64_MIN;
  210. range->max = S64_MAX;
  211. break;
  212. default:
  213. WARN_ON_ONCE(1);
  214. return;
  215. }
  216. switch (pt->validation_type) {
  217. case NLA_VALIDATE_RANGE:
  218. range->min = pt->min;
  219. range->max = pt->max;
  220. break;
  221. case NLA_VALIDATE_RANGE_PTR:
  222. *range = *pt->range_signed;
  223. break;
  224. case NLA_VALIDATE_MIN:
  225. range->min = pt->min;
  226. break;
  227. case NLA_VALIDATE_MAX:
  228. range->max = pt->max;
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. static int nla_validate_int_range_signed(const struct nla_policy *pt,
  235. const struct nlattr *nla,
  236. struct netlink_ext_ack *extack)
  237. {
  238. struct netlink_range_validation_signed range;
  239. s64 value;
  240. switch (pt->type) {
  241. case NLA_S8:
  242. value = nla_get_s8(nla);
  243. break;
  244. case NLA_S16:
  245. value = nla_get_s16(nla);
  246. break;
  247. case NLA_S32:
  248. value = nla_get_s32(nla);
  249. break;
  250. case NLA_S64:
  251. value = nla_get_s64(nla);
  252. break;
  253. default:
  254. return -EINVAL;
  255. }
  256. nla_get_range_signed(pt, &range);
  257. if (value < range.min || value > range.max) {
  258. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  259. "integer out of range");
  260. return -ERANGE;
  261. }
  262. return 0;
  263. }
  264. static int nla_validate_int_range(const struct nla_policy *pt,
  265. const struct nlattr *nla,
  266. struct netlink_ext_ack *extack,
  267. unsigned int validate)
  268. {
  269. switch (pt->type) {
  270. case NLA_U8:
  271. case NLA_U16:
  272. case NLA_U32:
  273. case NLA_U64:
  274. case NLA_MSECS:
  275. case NLA_BINARY:
  276. return nla_validate_range_unsigned(pt, nla, extack, validate);
  277. case NLA_S8:
  278. case NLA_S16:
  279. case NLA_S32:
  280. case NLA_S64:
  281. return nla_validate_int_range_signed(pt, nla, extack);
  282. default:
  283. WARN_ON(1);
  284. return -EINVAL;
  285. }
  286. }
  287. static int nla_validate_mask(const struct nla_policy *pt,
  288. const struct nlattr *nla,
  289. struct netlink_ext_ack *extack)
  290. {
  291. u64 value;
  292. switch (pt->type) {
  293. case NLA_U8:
  294. value = nla_get_u8(nla);
  295. break;
  296. case NLA_U16:
  297. value = nla_get_u16(nla);
  298. break;
  299. case NLA_U32:
  300. value = nla_get_u32(nla);
  301. break;
  302. case NLA_U64:
  303. value = nla_get_u64(nla);
  304. break;
  305. default:
  306. return -EINVAL;
  307. }
  308. if (value & ~(u64)pt->mask) {
  309. NL_SET_ERR_MSG_ATTR(extack, nla, "reserved bit set");
  310. return -EINVAL;
  311. }
  312. return 0;
  313. }
  314. static int validate_nla(const struct nlattr *nla, int maxtype,
  315. const struct nla_policy *policy, unsigned int validate,
  316. struct netlink_ext_ack *extack, unsigned int depth)
  317. {
  318. u16 strict_start_type = policy[0].strict_start_type;
  319. const struct nla_policy *pt;
  320. int minlen = 0, attrlen = nla_len(nla), type = nla_type(nla);
  321. int err = -ERANGE;
  322. if (strict_start_type && type >= strict_start_type)
  323. validate |= NL_VALIDATE_STRICT;
  324. if (type <= 0 || type > maxtype)
  325. return 0;
  326. pt = &policy[type];
  327. BUG_ON(pt->type > NLA_TYPE_MAX);
  328. if (nla_attr_len[pt->type] && attrlen != nla_attr_len[pt->type]) {
  329. pr_warn_ratelimited("netlink: '%s': attribute type %d has an invalid length.\n",
  330. current->comm, type);
  331. if (validate & NL_VALIDATE_STRICT_ATTRS) {
  332. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  333. "invalid attribute length");
  334. return -EINVAL;
  335. }
  336. }
  337. if (validate & NL_VALIDATE_NESTED) {
  338. if ((pt->type == NLA_NESTED || pt->type == NLA_NESTED_ARRAY) &&
  339. !(nla->nla_type & NLA_F_NESTED)) {
  340. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  341. "NLA_F_NESTED is missing");
  342. return -EINVAL;
  343. }
  344. if (pt->type != NLA_NESTED && pt->type != NLA_NESTED_ARRAY &&
  345. pt->type != NLA_UNSPEC && (nla->nla_type & NLA_F_NESTED)) {
  346. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  347. "NLA_F_NESTED not expected");
  348. return -EINVAL;
  349. }
  350. }
  351. switch (pt->type) {
  352. case NLA_REJECT:
  353. if (extack && pt->reject_message) {
  354. NL_SET_BAD_ATTR(extack, nla);
  355. extack->_msg = pt->reject_message;
  356. return -EINVAL;
  357. }
  358. err = -EINVAL;
  359. goto out_err;
  360. case NLA_FLAG:
  361. if (attrlen > 0)
  362. goto out_err;
  363. break;
  364. case NLA_BITFIELD32:
  365. if (attrlen != sizeof(struct nla_bitfield32))
  366. goto out_err;
  367. err = validate_nla_bitfield32(nla, pt->bitfield32_valid);
  368. if (err)
  369. goto out_err;
  370. break;
  371. case NLA_NUL_STRING:
  372. if (pt->len)
  373. minlen = min_t(int, attrlen, pt->len + 1);
  374. else
  375. minlen = attrlen;
  376. if (!minlen || memchr(nla_data(nla), '\0', minlen) == NULL) {
  377. err = -EINVAL;
  378. goto out_err;
  379. }
  380. /* fall through */
  381. case NLA_STRING:
  382. if (attrlen < 1)
  383. goto out_err;
  384. if (pt->len) {
  385. char *buf = nla_data(nla);
  386. if (buf[attrlen - 1] == '\0')
  387. attrlen--;
  388. if (attrlen > pt->len)
  389. goto out_err;
  390. }
  391. break;
  392. case NLA_BINARY:
  393. if (pt->len && attrlen > pt->len)
  394. goto out_err;
  395. break;
  396. case NLA_NESTED:
  397. /* a nested attributes is allowed to be empty; if its not,
  398. * it must have a size of at least NLA_HDRLEN.
  399. */
  400. if (attrlen == 0)
  401. break;
  402. if (attrlen < NLA_HDRLEN)
  403. goto out_err;
  404. if (pt->nested_policy) {
  405. err = __nla_validate_parse(nla_data(nla), nla_len(nla),
  406. pt->len, pt->nested_policy,
  407. validate, extack, NULL,
  408. depth + 1);
  409. if (err < 0) {
  410. /*
  411. * return directly to preserve the inner
  412. * error message/attribute pointer
  413. */
  414. return err;
  415. }
  416. }
  417. break;
  418. case NLA_NESTED_ARRAY:
  419. /* a nested array attribute is allowed to be empty; if its not,
  420. * it must have a size of at least NLA_HDRLEN.
  421. */
  422. if (attrlen == 0)
  423. break;
  424. if (attrlen < NLA_HDRLEN)
  425. goto out_err;
  426. if (pt->nested_policy) {
  427. int err;
  428. err = nla_validate_array(nla_data(nla), nla_len(nla),
  429. pt->len, pt->nested_policy,
  430. extack, validate, depth);
  431. if (err < 0) {
  432. /*
  433. * return directly to preserve the inner
  434. * error message/attribute pointer
  435. */
  436. return err;
  437. }
  438. }
  439. break;
  440. case NLA_UNSPEC:
  441. if (validate & NL_VALIDATE_UNSPEC) {
  442. NL_SET_ERR_MSG_ATTR(extack, nla,
  443. "Unsupported attribute");
  444. return -EINVAL;
  445. }
  446. if (attrlen < pt->len)
  447. goto out_err;
  448. break;
  449. default:
  450. if (pt->len)
  451. minlen = pt->len;
  452. else
  453. minlen = nla_attr_minlen[pt->type];
  454. if (attrlen < minlen)
  455. goto out_err;
  456. }
  457. /* further validation */
  458. switch (pt->validation_type) {
  459. case NLA_VALIDATE_NONE:
  460. /* nothing to do */
  461. break;
  462. case NLA_VALIDATE_RANGE_PTR:
  463. case NLA_VALIDATE_RANGE:
  464. case NLA_VALIDATE_RANGE_WARN_TOO_LONG:
  465. case NLA_VALIDATE_MIN:
  466. case NLA_VALIDATE_MAX:
  467. err = nla_validate_int_range(pt, nla, extack, validate);
  468. if (err)
  469. return err;
  470. break;
  471. case NLA_VALIDATE_MASK:
  472. err = nla_validate_mask(pt, nla, extack);
  473. if (err)
  474. return err;
  475. break;
  476. case NLA_VALIDATE_FUNCTION:
  477. if (pt->validate) {
  478. err = pt->validate(nla, extack);
  479. if (err)
  480. return err;
  481. }
  482. break;
  483. }
  484. return 0;
  485. out_err:
  486. NL_SET_ERR_MSG_ATTR_POL(extack, nla, pt,
  487. "Attribute failed policy validation");
  488. return err;
  489. }
  490. static int __nla_validate_parse(const struct nlattr *head, int len, int maxtype,
  491. const struct nla_policy *policy,
  492. unsigned int validate,
  493. struct netlink_ext_ack *extack,
  494. struct nlattr **tb, unsigned int depth)
  495. {
  496. const struct nlattr *nla;
  497. int rem;
  498. if (depth >= MAX_POLICY_RECURSION_DEPTH) {
  499. NL_SET_ERR_MSG(extack,
  500. "allowed policy recursion depth exceeded");
  501. return -EINVAL;
  502. }
  503. if (tb)
  504. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  505. nla_for_each_attr(nla, head, len, rem) {
  506. u16 type = nla_type(nla);
  507. if (type == 0 || type > maxtype) {
  508. if (validate & NL_VALIDATE_MAXTYPE) {
  509. NL_SET_ERR_MSG_ATTR(extack, nla,
  510. "Unknown attribute type");
  511. return -EINVAL;
  512. }
  513. continue;
  514. }
  515. if (policy) {
  516. int err = validate_nla(nla, maxtype, policy,
  517. validate, extack, depth);
  518. if (err < 0)
  519. return err;
  520. }
  521. if (tb)
  522. tb[type] = (struct nlattr *)nla;
  523. }
  524. if (unlikely(rem > 0)) {
  525. pr_warn_ratelimited("netlink: %d bytes leftover after parsing attributes in process `%s'.\n",
  526. rem, current->comm);
  527. NL_SET_ERR_MSG(extack, "bytes leftover after parsing attributes");
  528. if (validate & NL_VALIDATE_TRAILING)
  529. return -EINVAL;
  530. }
  531. return 0;
  532. }
  533. /**
  534. * __nla_validate - Validate a stream of attributes
  535. * @head: head of attribute stream
  536. * @len: length of attribute stream
  537. * @maxtype: maximum attribute type to be expected
  538. * @policy: validation policy
  539. * @validate: validation strictness
  540. * @extack: extended ACK report struct
  541. *
  542. * Validates all attributes in the specified attribute stream against the
  543. * specified policy. Validation depends on the validate flags passed, see
  544. * &enum netlink_validation for more details on that.
  545. * See documenation of struct nla_policy for more details.
  546. *
  547. * Returns 0 on success or a negative error code.
  548. */
  549. int __nla_validate(const struct nlattr *head, int len, int maxtype,
  550. const struct nla_policy *policy, unsigned int validate,
  551. struct netlink_ext_ack *extack)
  552. {
  553. return __nla_validate_parse(head, len, maxtype, policy, validate,
  554. extack, NULL, 0);
  555. }
  556. EXPORT_SYMBOL(__nla_validate);
  557. /**
  558. * nla_policy_len - Determin the max. length of a policy
  559. * @policy: policy to use
  560. * @n: number of policies
  561. *
  562. * Determines the max. length of the policy. It is currently used
  563. * to allocated Netlink buffers roughly the size of the actual
  564. * message.
  565. *
  566. * Returns 0 on success or a negative error code.
  567. */
  568. int
  569. nla_policy_len(const struct nla_policy *p, int n)
  570. {
  571. int i, len = 0;
  572. for (i = 0; i < n; i++, p++) {
  573. if (p->len)
  574. len += nla_total_size(p->len);
  575. else if (nla_attr_len[p->type])
  576. len += nla_total_size(nla_attr_len[p->type]);
  577. else if (nla_attr_minlen[p->type])
  578. len += nla_total_size(nla_attr_minlen[p->type]);
  579. }
  580. return len;
  581. }
  582. EXPORT_SYMBOL(nla_policy_len);
  583. /**
  584. * __nla_parse - Parse a stream of attributes into a tb buffer
  585. * @tb: destination array with maxtype+1 elements
  586. * @maxtype: maximum attribute type to be expected
  587. * @head: head of attribute stream
  588. * @len: length of attribute stream
  589. * @policy: validation policy
  590. * @validate: validation strictness
  591. * @extack: extended ACK pointer
  592. *
  593. * Parses a stream of attributes and stores a pointer to each attribute in
  594. * the tb array accessible via the attribute type.
  595. * Validation is controlled by the @validate parameter.
  596. *
  597. * Returns 0 on success or a negative error code.
  598. */
  599. int __nla_parse(struct nlattr **tb, int maxtype,
  600. const struct nlattr *head, int len,
  601. const struct nla_policy *policy, unsigned int validate,
  602. struct netlink_ext_ack *extack)
  603. {
  604. return __nla_validate_parse(head, len, maxtype, policy, validate,
  605. extack, tb, 0);
  606. }
  607. EXPORT_SYMBOL(__nla_parse);
  608. /**
  609. * nla_find - Find a specific attribute in a stream of attributes
  610. * @head: head of attribute stream
  611. * @len: length of attribute stream
  612. * @attrtype: type of attribute to look for
  613. *
  614. * Returns the first attribute in the stream matching the specified type.
  615. */
  616. struct nlattr *nla_find(const struct nlattr *head, int len, int attrtype)
  617. {
  618. const struct nlattr *nla;
  619. int rem;
  620. nla_for_each_attr(nla, head, len, rem)
  621. if (nla_type(nla) == attrtype)
  622. return (struct nlattr *)nla;
  623. return NULL;
  624. }
  625. EXPORT_SYMBOL(nla_find);
  626. /**
  627. * nla_strlcpy - Copy string attribute payload into a sized buffer
  628. * @dst: where to copy the string to
  629. * @nla: attribute to copy the string from
  630. * @dstsize: size of destination buffer
  631. *
  632. * Copies at most dstsize - 1 bytes into the destination buffer.
  633. * The result is always a valid NUL-terminated string. Unlike
  634. * strlcpy the destination buffer is always padded out.
  635. *
  636. * Returns the length of the source buffer.
  637. */
  638. size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
  639. {
  640. size_t srclen = nla_len(nla);
  641. char *src = nla_data(nla);
  642. if (srclen > 0 && src[srclen - 1] == '\0')
  643. srclen--;
  644. if (dstsize > 0) {
  645. size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
  646. memset(dst, 0, dstsize);
  647. memcpy(dst, src, len);
  648. }
  649. return srclen;
  650. }
  651. EXPORT_SYMBOL(nla_strlcpy);
  652. /**
  653. * nla_strdup - Copy string attribute payload into a newly allocated buffer
  654. * @nla: attribute to copy the string from
  655. * @flags: the type of memory to allocate (see kmalloc).
  656. *
  657. * Returns a pointer to the allocated buffer or NULL on error.
  658. */
  659. char *nla_strdup(const struct nlattr *nla, gfp_t flags)
  660. {
  661. size_t srclen = nla_len(nla);
  662. char *src = nla_data(nla), *dst;
  663. if (srclen > 0 && src[srclen - 1] == '\0')
  664. srclen--;
  665. dst = kmalloc(srclen + 1, flags);
  666. if (dst != NULL) {
  667. memcpy(dst, src, srclen);
  668. dst[srclen] = '\0';
  669. }
  670. return dst;
  671. }
  672. EXPORT_SYMBOL(nla_strdup);
  673. /**
  674. * nla_memcpy - Copy a netlink attribute into another memory area
  675. * @dest: where to copy to memcpy
  676. * @src: netlink attribute to copy from
  677. * @count: size of the destination area
  678. *
  679. * Note: The number of bytes copied is limited by the length of
  680. * attribute's payload. memcpy
  681. *
  682. * Returns the number of bytes copied.
  683. */
  684. int nla_memcpy(void *dest, const struct nlattr *src, int count)
  685. {
  686. int minlen = min_t(int, count, nla_len(src));
  687. memcpy(dest, nla_data(src), minlen);
  688. if (count > minlen)
  689. memset(dest + minlen, 0, count - minlen);
  690. return minlen;
  691. }
  692. EXPORT_SYMBOL(nla_memcpy);
  693. /**
  694. * nla_memcmp - Compare an attribute with sized memory area
  695. * @nla: netlink attribute
  696. * @data: memory area
  697. * @size: size of memory area
  698. */
  699. int nla_memcmp(const struct nlattr *nla, const void *data,
  700. size_t size)
  701. {
  702. int d = nla_len(nla) - size;
  703. if (d == 0)
  704. d = memcmp(nla_data(nla), data, size);
  705. return d;
  706. }
  707. EXPORT_SYMBOL(nla_memcmp);
  708. /**
  709. * nla_strcmp - Compare a string attribute against a string
  710. * @nla: netlink string attribute
  711. * @str: another string
  712. */
  713. int nla_strcmp(const struct nlattr *nla, const char *str)
  714. {
  715. int len = strlen(str);
  716. char *buf = nla_data(nla);
  717. int attrlen = nla_len(nla);
  718. int d;
  719. while (attrlen > 0 && buf[attrlen - 1] == '\0')
  720. attrlen--;
  721. d = attrlen - len;
  722. if (d == 0)
  723. d = memcmp(nla_data(nla), str, len);
  724. return d;
  725. }
  726. EXPORT_SYMBOL(nla_strcmp);
  727. #ifdef CONFIG_NET
  728. /**
  729. * __nla_reserve - reserve room for attribute on the skb
  730. * @skb: socket buffer to reserve room on
  731. * @attrtype: attribute type
  732. * @attrlen: length of attribute payload
  733. *
  734. * Adds a netlink attribute header to a socket buffer and reserves
  735. * room for the payload but does not copy it.
  736. *
  737. * The caller is responsible to ensure that the skb provides enough
  738. * tailroom for the attribute header and payload.
  739. */
  740. struct nlattr *__nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  741. {
  742. struct nlattr *nla;
  743. nla = skb_put(skb, nla_total_size(attrlen));
  744. nla->nla_type = attrtype;
  745. nla->nla_len = nla_attr_size(attrlen);
  746. memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
  747. return nla;
  748. }
  749. EXPORT_SYMBOL(__nla_reserve);
  750. /**
  751. * __nla_reserve_64bit - reserve room for attribute on the skb and align it
  752. * @skb: socket buffer to reserve room on
  753. * @attrtype: attribute type
  754. * @attrlen: length of attribute payload
  755. * @padattr: attribute type for the padding
  756. *
  757. * Adds a netlink attribute header to a socket buffer and reserves
  758. * room for the payload but does not copy it. It also ensure that this
  759. * attribute will have a 64-bit aligned nla_data() area.
  760. *
  761. * The caller is responsible to ensure that the skb provides enough
  762. * tailroom for the attribute header and payload.
  763. */
  764. struct nlattr *__nla_reserve_64bit(struct sk_buff *skb, int attrtype,
  765. int attrlen, int padattr)
  766. {
  767. nla_align_64bit(skb, padattr);
  768. return __nla_reserve(skb, attrtype, attrlen);
  769. }
  770. EXPORT_SYMBOL(__nla_reserve_64bit);
  771. /**
  772. * __nla_reserve_nohdr - reserve room for attribute without header
  773. * @skb: socket buffer to reserve room on
  774. * @attrlen: length of attribute payload
  775. *
  776. * Reserves room for attribute payload without a header.
  777. *
  778. * The caller is responsible to ensure that the skb provides enough
  779. * tailroom for the payload.
  780. */
  781. void *__nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  782. {
  783. return skb_put_zero(skb, NLA_ALIGN(attrlen));
  784. }
  785. EXPORT_SYMBOL(__nla_reserve_nohdr);
  786. /**
  787. * nla_reserve - reserve room for attribute on the skb
  788. * @skb: socket buffer to reserve room on
  789. * @attrtype: attribute type
  790. * @attrlen: length of attribute payload
  791. *
  792. * Adds a netlink attribute header to a socket buffer and reserves
  793. * room for the payload but does not copy it.
  794. *
  795. * Returns NULL if the tailroom of the skb is insufficient to store
  796. * the attribute header and payload.
  797. */
  798. struct nlattr *nla_reserve(struct sk_buff *skb, int attrtype, int attrlen)
  799. {
  800. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  801. return NULL;
  802. return __nla_reserve(skb, attrtype, attrlen);
  803. }
  804. EXPORT_SYMBOL(nla_reserve);
  805. /**
  806. * nla_reserve_64bit - reserve room for attribute on the skb and align it
  807. * @skb: socket buffer to reserve room on
  808. * @attrtype: attribute type
  809. * @attrlen: length of attribute payload
  810. * @padattr: attribute type for the padding
  811. *
  812. * Adds a netlink attribute header to a socket buffer and reserves
  813. * room for the payload but does not copy it. It also ensure that this
  814. * attribute will have a 64-bit aligned nla_data() area.
  815. *
  816. * Returns NULL if the tailroom of the skb is insufficient to store
  817. * the attribute header and payload.
  818. */
  819. struct nlattr *nla_reserve_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  820. int padattr)
  821. {
  822. size_t len;
  823. if (nla_need_padding_for_64bit(skb))
  824. len = nla_total_size_64bit(attrlen);
  825. else
  826. len = nla_total_size(attrlen);
  827. if (unlikely(skb_tailroom(skb) < len))
  828. return NULL;
  829. return __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
  830. }
  831. EXPORT_SYMBOL(nla_reserve_64bit);
  832. /**
  833. * nla_reserve_nohdr - reserve room for attribute without header
  834. * @skb: socket buffer to reserve room on
  835. * @attrlen: length of attribute payload
  836. *
  837. * Reserves room for attribute payload without a header.
  838. *
  839. * Returns NULL if the tailroom of the skb is insufficient to store
  840. * the attribute payload.
  841. */
  842. void *nla_reserve_nohdr(struct sk_buff *skb, int attrlen)
  843. {
  844. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  845. return NULL;
  846. return __nla_reserve_nohdr(skb, attrlen);
  847. }
  848. EXPORT_SYMBOL(nla_reserve_nohdr);
  849. /**
  850. * __nla_put - Add a netlink attribute to a socket buffer
  851. * @skb: socket buffer to add attribute to
  852. * @attrtype: attribute type
  853. * @attrlen: length of attribute payload
  854. * @data: head of attribute payload
  855. *
  856. * The caller is responsible to ensure that the skb provides enough
  857. * tailroom for the attribute header and payload.
  858. */
  859. void __nla_put(struct sk_buff *skb, int attrtype, int attrlen,
  860. const void *data)
  861. {
  862. struct nlattr *nla;
  863. nla = __nla_reserve(skb, attrtype, attrlen);
  864. memcpy(nla_data(nla), data, attrlen);
  865. }
  866. EXPORT_SYMBOL(__nla_put);
  867. /**
  868. * __nla_put_64bit - Add a netlink attribute to a socket buffer and align it
  869. * @skb: socket buffer to add attribute to
  870. * @attrtype: attribute type
  871. * @attrlen: length of attribute payload
  872. * @data: head of attribute payload
  873. * @padattr: attribute type for the padding
  874. *
  875. * The caller is responsible to ensure that the skb provides enough
  876. * tailroom for the attribute header and payload.
  877. */
  878. void __nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  879. const void *data, int padattr)
  880. {
  881. struct nlattr *nla;
  882. nla = __nla_reserve_64bit(skb, attrtype, attrlen, padattr);
  883. memcpy(nla_data(nla), data, attrlen);
  884. }
  885. EXPORT_SYMBOL(__nla_put_64bit);
  886. /**
  887. * __nla_put_nohdr - Add a netlink attribute without header
  888. * @skb: socket buffer to add attribute to
  889. * @attrlen: length of attribute payload
  890. * @data: head of attribute payload
  891. *
  892. * The caller is responsible to ensure that the skb provides enough
  893. * tailroom for the attribute payload.
  894. */
  895. void __nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  896. {
  897. void *start;
  898. start = __nla_reserve_nohdr(skb, attrlen);
  899. memcpy(start, data, attrlen);
  900. }
  901. EXPORT_SYMBOL(__nla_put_nohdr);
  902. /**
  903. * nla_put - Add a netlink attribute to a socket buffer
  904. * @skb: socket buffer to add attribute to
  905. * @attrtype: attribute type
  906. * @attrlen: length of attribute payload
  907. * @data: head of attribute payload
  908. *
  909. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  910. * the attribute header and payload.
  911. */
  912. int nla_put(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
  913. {
  914. if (unlikely(skb_tailroom(skb) < nla_total_size(attrlen)))
  915. return -EMSGSIZE;
  916. __nla_put(skb, attrtype, attrlen, data);
  917. return 0;
  918. }
  919. EXPORT_SYMBOL(nla_put);
  920. /**
  921. * nla_put_64bit - Add a netlink attribute to a socket buffer and align it
  922. * @skb: socket buffer to add attribute to
  923. * @attrtype: attribute type
  924. * @attrlen: length of attribute payload
  925. * @data: head of attribute payload
  926. * @padattr: attribute type for the padding
  927. *
  928. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  929. * the attribute header and payload.
  930. */
  931. int nla_put_64bit(struct sk_buff *skb, int attrtype, int attrlen,
  932. const void *data, int padattr)
  933. {
  934. size_t len;
  935. if (nla_need_padding_for_64bit(skb))
  936. len = nla_total_size_64bit(attrlen);
  937. else
  938. len = nla_total_size(attrlen);
  939. if (unlikely(skb_tailroom(skb) < len))
  940. return -EMSGSIZE;
  941. __nla_put_64bit(skb, attrtype, attrlen, data, padattr);
  942. return 0;
  943. }
  944. EXPORT_SYMBOL(nla_put_64bit);
  945. /**
  946. * nla_put_nohdr - Add a netlink attribute without header
  947. * @skb: socket buffer to add attribute to
  948. * @attrlen: length of attribute payload
  949. * @data: head of attribute payload
  950. *
  951. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  952. * the attribute payload.
  953. */
  954. int nla_put_nohdr(struct sk_buff *skb, int attrlen, const void *data)
  955. {
  956. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  957. return -EMSGSIZE;
  958. __nla_put_nohdr(skb, attrlen, data);
  959. return 0;
  960. }
  961. EXPORT_SYMBOL(nla_put_nohdr);
  962. /**
  963. * nla_append - Add a netlink attribute without header or padding
  964. * @skb: socket buffer to add attribute to
  965. * @attrlen: length of attribute payload
  966. * @data: head of attribute payload
  967. *
  968. * Returns -EMSGSIZE if the tailroom of the skb is insufficient to store
  969. * the attribute payload.
  970. */
  971. int nla_append(struct sk_buff *skb, int attrlen, const void *data)
  972. {
  973. if (unlikely(skb_tailroom(skb) < NLA_ALIGN(attrlen)))
  974. return -EMSGSIZE;
  975. skb_put_data(skb, data, attrlen);
  976. return 0;
  977. }
  978. EXPORT_SYMBOL(nla_append);
  979. #endif