nlattr.h 4.2 KB

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