nlattr.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /*
  3. * NETLINK Netlink attributes
  4. *
  5. * Copyright (c) 2003-2013 Thomas Graf <[email protected]>
  6. */
  7. #ifndef __LIBBPF_NLATTR_H
  8. #define __LIBBPF_NLATTR_H
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <linux/netlink.h>
  13. #include <linux/rtnetlink.h>
  14. /* avoid multiple definition of netlink features */
  15. #define __LINUX_NETLINK_H
  16. /**
  17. * Standard attribute types to specify validation policy
  18. */
  19. enum {
  20. LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */
  21. LIBBPF_NLA_U8, /**< 8 bit integer */
  22. LIBBPF_NLA_U16, /**< 16 bit integer */
  23. LIBBPF_NLA_U32, /**< 32 bit integer */
  24. LIBBPF_NLA_U64, /**< 64 bit integer */
  25. LIBBPF_NLA_STRING, /**< NUL terminated character string */
  26. LIBBPF_NLA_FLAG, /**< Flag */
  27. LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */
  28. LIBBPF_NLA_NESTED, /**< Nested attributes */
  29. __LIBBPF_NLA_TYPE_MAX,
  30. };
  31. #define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1)
  32. /**
  33. * @ingroup attr
  34. * Attribute validation policy.
  35. *
  36. * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
  37. */
  38. struct libbpf_nla_policy {
  39. /** Type of attribute or LIBBPF_NLA_UNSPEC */
  40. uint16_t type;
  41. /** Minimal length of payload required */
  42. uint16_t minlen;
  43. /** Maximal length of payload allowed */
  44. uint16_t maxlen;
  45. };
  46. struct libbpf_nla_req {
  47. struct nlmsghdr nh;
  48. union {
  49. struct ifinfomsg ifinfo;
  50. struct tcmsg tc;
  51. };
  52. char buf[128];
  53. };
  54. /**
  55. * @ingroup attr
  56. * Iterate over a stream of attributes
  57. * @arg pos loop counter, set to current attribute
  58. * @arg head head of attribute stream
  59. * @arg len length of attribute stream
  60. * @arg rem initialized to len, holds bytes currently remaining in stream
  61. */
  62. #define libbpf_nla_for_each_attr(pos, head, len, rem) \
  63. for (pos = head, rem = len; \
  64. nla_ok(pos, rem); \
  65. pos = nla_next(pos, &(rem)))
  66. /**
  67. * libbpf_nla_data - head of payload
  68. * @nla: netlink attribute
  69. */
  70. static inline void *libbpf_nla_data(const struct nlattr *nla)
  71. {
  72. return (void *)nla + NLA_HDRLEN;
  73. }
  74. static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
  75. {
  76. return *(uint8_t *)libbpf_nla_data(nla);
  77. }
  78. static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
  79. {
  80. return *(uint32_t *)libbpf_nla_data(nla);
  81. }
  82. static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla)
  83. {
  84. return (const char *)libbpf_nla_data(nla);
  85. }
  86. /**
  87. * libbpf_nla_len - length of payload
  88. * @nla: netlink attribute
  89. */
  90. static inline int libbpf_nla_len(const struct nlattr *nla)
  91. {
  92. return nla->nla_len - NLA_HDRLEN;
  93. }
  94. int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
  95. int len, struct libbpf_nla_policy *policy);
  96. int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
  97. struct nlattr *nla,
  98. struct libbpf_nla_policy *policy);
  99. int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
  100. static inline struct nlattr *nla_data(struct nlattr *nla)
  101. {
  102. return (struct nlattr *)((void *)nla + NLA_HDRLEN);
  103. }
  104. static inline struct nlattr *req_tail(struct libbpf_nla_req *req)
  105. {
  106. return (struct nlattr *)((void *)req + NLMSG_ALIGN(req->nh.nlmsg_len));
  107. }
  108. static inline int nlattr_add(struct libbpf_nla_req *req, int type,
  109. const void *data, int len)
  110. {
  111. struct nlattr *nla;
  112. if (NLMSG_ALIGN(req->nh.nlmsg_len) + NLA_ALIGN(NLA_HDRLEN + len) > sizeof(*req))
  113. return -EMSGSIZE;
  114. if (!!data != !!len)
  115. return -EINVAL;
  116. nla = req_tail(req);
  117. nla->nla_type = type;
  118. nla->nla_len = NLA_HDRLEN + len;
  119. if (data)
  120. memcpy(nla_data(nla), data, len);
  121. req->nh.nlmsg_len = NLMSG_ALIGN(req->nh.nlmsg_len) + NLA_ALIGN(nla->nla_len);
  122. return 0;
  123. }
  124. static inline struct nlattr *nlattr_begin_nested(struct libbpf_nla_req *req, int type)
  125. {
  126. struct nlattr *tail;
  127. tail = req_tail(req);
  128. if (nlattr_add(req, type | NLA_F_NESTED, NULL, 0))
  129. return NULL;
  130. return tail;
  131. }
  132. static inline void nlattr_end_nested(struct libbpf_nla_req *req,
  133. struct nlattr *tail)
  134. {
  135. tail->nla_len = (void *)req_tail(req) - (void *)tail;
  136. }
  137. #endif /* __LIBBPF_NLATTR_H */