nlattr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * NETLINK Netlink attributes
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation version 2.1
  8. * of the License.
  9. *
  10. * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
  11. */
  12. #include <errno.h>
  13. #include "nlattr.h"
  14. #include <linux/rtnetlink.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = {
  18. [NLA_U8] = sizeof(uint8_t),
  19. [NLA_U16] = sizeof(uint16_t),
  20. [NLA_U32] = sizeof(uint32_t),
  21. [NLA_U64] = sizeof(uint64_t),
  22. [NLA_STRING] = 1,
  23. [NLA_FLAG] = 0,
  24. };
  25. static int nla_len(const struct nlattr *nla)
  26. {
  27. return nla->nla_len - NLA_HDRLEN;
  28. }
  29. static struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
  30. {
  31. int totlen = NLA_ALIGN(nla->nla_len);
  32. *remaining -= totlen;
  33. return (struct nlattr *) ((char *) nla + totlen);
  34. }
  35. static int nla_ok(const struct nlattr *nla, int remaining)
  36. {
  37. return remaining >= sizeof(*nla) &&
  38. nla->nla_len >= sizeof(*nla) &&
  39. nla->nla_len <= remaining;
  40. }
  41. static void *nla_data(const struct nlattr *nla)
  42. {
  43. return (char *) nla + NLA_HDRLEN;
  44. }
  45. static int nla_type(const struct nlattr *nla)
  46. {
  47. return nla->nla_type & NLA_TYPE_MASK;
  48. }
  49. static int validate_nla(struct nlattr *nla, int maxtype,
  50. struct nla_policy *policy)
  51. {
  52. struct nla_policy *pt;
  53. unsigned int minlen = 0;
  54. int type = nla_type(nla);
  55. if (type < 0 || type > maxtype)
  56. return 0;
  57. pt = &policy[type];
  58. if (pt->type > NLA_TYPE_MAX)
  59. return 0;
  60. if (pt->minlen)
  61. minlen = pt->minlen;
  62. else if (pt->type != NLA_UNSPEC)
  63. minlen = nla_attr_minlen[pt->type];
  64. if (nla_len(nla) < minlen)
  65. return -1;
  66. if (pt->maxlen && nla_len(nla) > pt->maxlen)
  67. return -1;
  68. if (pt->type == NLA_STRING) {
  69. char *data = nla_data(nla);
  70. if (data[nla_len(nla) - 1] != '\0')
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. static inline int nlmsg_len(const struct nlmsghdr *nlh)
  76. {
  77. return nlh->nlmsg_len - NLMSG_HDRLEN;
  78. }
  79. /**
  80. * Create attribute index based on a stream of attributes.
  81. * @arg tb Index array to be filled (maxtype+1 elements).
  82. * @arg maxtype Maximum attribute type expected and accepted.
  83. * @arg head Head of attribute stream.
  84. * @arg len Length of attribute stream.
  85. * @arg policy Attribute validation policy.
  86. *
  87. * Iterates over the stream of attributes and stores a pointer to each
  88. * attribute in the index array using the attribute type as index to
  89. * the array. Attribute with a type greater than the maximum type
  90. * specified will be silently ignored in order to maintain backwards
  91. * compatibility. If \a policy is not NULL, the attribute will be
  92. * validated using the specified policy.
  93. *
  94. * @see nla_validate
  95. * @return 0 on success or a negative error code.
  96. */
  97. static int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
  98. struct nla_policy *policy)
  99. {
  100. struct nlattr *nla;
  101. int rem, err;
  102. memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
  103. nla_for_each_attr(nla, head, len, rem) {
  104. int type = nla_type(nla);
  105. if (type > maxtype)
  106. continue;
  107. if (policy) {
  108. err = validate_nla(nla, maxtype, policy);
  109. if (err < 0)
  110. goto errout;
  111. }
  112. if (tb[type])
  113. fprintf(stderr, "Attribute of type %#x found multiple times in message, "
  114. "previous attribute is being ignored.\n", type);
  115. tb[type] = nla;
  116. }
  117. err = 0;
  118. errout:
  119. return err;
  120. }
  121. /* dump netlink extended ack error message */
  122. int nla_dump_errormsg(struct nlmsghdr *nlh)
  123. {
  124. struct nla_policy extack_policy[NLMSGERR_ATTR_MAX + 1] = {
  125. [NLMSGERR_ATTR_MSG] = { .type = NLA_STRING },
  126. [NLMSGERR_ATTR_OFFS] = { .type = NLA_U32 },
  127. };
  128. struct nlattr *tb[NLMSGERR_ATTR_MAX + 1], *attr;
  129. struct nlmsgerr *err;
  130. char *errmsg = NULL;
  131. int hlen, alen;
  132. /* no TLVs, nothing to do here */
  133. if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
  134. return 0;
  135. err = (struct nlmsgerr *)NLMSG_DATA(nlh);
  136. hlen = sizeof(*err);
  137. /* if NLM_F_CAPPED is set then the inner err msg was capped */
  138. if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
  139. hlen += nlmsg_len(&err->msg);
  140. attr = (struct nlattr *) ((void *) err + hlen);
  141. alen = nlh->nlmsg_len - hlen;
  142. if (nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, extack_policy) != 0) {
  143. fprintf(stderr,
  144. "Failed to parse extended error attributes\n");
  145. return 0;
  146. }
  147. if (tb[NLMSGERR_ATTR_MSG])
  148. errmsg = (char *) nla_data(tb[NLMSGERR_ATTR_MSG]);
  149. fprintf(stderr, "Kernel error message: %s\n", errmsg);
  150. return 0;
  151. }