nlattr.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <linux/netlink.h>
  11. /* avoid multiple definition of netlink features */
  12. #define __LINUX_NETLINK_H
  13. /**
  14. * Standard attribute types to specify validation policy
  15. */
  16. enum {
  17. LIBBPF_NLA_UNSPEC, /**< Unspecified type, binary data chunk */
  18. LIBBPF_NLA_U8, /**< 8 bit integer */
  19. LIBBPF_NLA_U16, /**< 16 bit integer */
  20. LIBBPF_NLA_U32, /**< 32 bit integer */
  21. LIBBPF_NLA_U64, /**< 64 bit integer */
  22. LIBBPF_NLA_STRING, /**< NUL terminated character string */
  23. LIBBPF_NLA_FLAG, /**< Flag */
  24. LIBBPF_NLA_MSECS, /**< Micro seconds (64bit) */
  25. LIBBPF_NLA_NESTED, /**< Nested attributes */
  26. __LIBBPF_NLA_TYPE_MAX,
  27. };
  28. #define LIBBPF_NLA_TYPE_MAX (__LIBBPF_NLA_TYPE_MAX - 1)
  29. /**
  30. * @ingroup attr
  31. * Attribute validation policy.
  32. *
  33. * See section @core_doc{core_attr_parse,Attribute Parsing} for more details.
  34. */
  35. struct libbpf_nla_policy {
  36. /** Type of attribute or LIBBPF_NLA_UNSPEC */
  37. uint16_t type;
  38. /** Minimal length of payload required */
  39. uint16_t minlen;
  40. /** Maximal length of payload allowed */
  41. uint16_t maxlen;
  42. };
  43. /**
  44. * @ingroup attr
  45. * Iterate over a stream of attributes
  46. * @arg pos loop counter, set to current attribute
  47. * @arg head head of attribute stream
  48. * @arg len length of attribute stream
  49. * @arg rem initialized to len, holds bytes currently remaining in stream
  50. */
  51. #define libbpf_nla_for_each_attr(pos, head, len, rem) \
  52. for (pos = head, rem = len; \
  53. nla_ok(pos, rem); \
  54. pos = nla_next(pos, &(rem)))
  55. /**
  56. * libbpf_nla_data - head of payload
  57. * @nla: netlink attribute
  58. */
  59. static inline void *libbpf_nla_data(const struct nlattr *nla)
  60. {
  61. return (char *) nla + NLA_HDRLEN;
  62. }
  63. static inline uint8_t libbpf_nla_getattr_u8(const struct nlattr *nla)
  64. {
  65. return *(uint8_t *)libbpf_nla_data(nla);
  66. }
  67. static inline uint32_t libbpf_nla_getattr_u32(const struct nlattr *nla)
  68. {
  69. return *(uint32_t *)libbpf_nla_data(nla);
  70. }
  71. static inline const char *libbpf_nla_getattr_str(const struct nlattr *nla)
  72. {
  73. return (const char *)libbpf_nla_data(nla);
  74. }
  75. /**
  76. * libbpf_nla_len - length of payload
  77. * @nla: netlink attribute
  78. */
  79. static inline int libbpf_nla_len(const struct nlattr *nla)
  80. {
  81. return nla->nla_len - NLA_HDRLEN;
  82. }
  83. int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
  84. int len, struct libbpf_nla_policy *policy);
  85. int libbpf_nla_parse_nested(struct nlattr *tb[], int maxtype,
  86. struct nlattr *nla,
  87. struct libbpf_nla_policy *policy);
  88. int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh);
  89. #endif /* __LIBBPF_NLATTR_H */