nlattr.c 27 KB

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