PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/netlink.h

https://bitbucket.org/apanda/ovs-ddc
C Header | 164 lines | 102 code | 19 blank | 43 comment | 4 complexity | 034e7ad5ae1bdc2a33b545d2ebccbf2c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. /*
  2. * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at:
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef NETLINK_H
  17. #define NETLINK_H 1
  18. /* Netlink message helpers.
  19. *
  20. * Netlink is a datagram-based network protocol primarily for communication
  21. * between user processes and the kernel, and mainly on Linux. Netlink is
  22. * specified in RFC 3549, "Linux Netlink as an IP Services Protocol".
  23. *
  24. * Netlink is not suitable for use in physical networks of heterogeneous
  25. * machines because host byte order is used throughout.
  26. *
  27. * This header file defines helper functions for working with Netlink messages.
  28. * For Netlink protocol definitions, see netlink-protocol.h. For
  29. * Linux-specific definitions for Netlink sockets, see netlink-socket.h.
  30. */
  31. #include <stdbool.h>
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #include "netlink-protocol.h"
  35. #include "openvswitch/types.h"
  36. struct ofpbuf;
  37. struct nlattr;
  38. /* Accessing headers and data. */
  39. struct nlmsghdr *nl_msg_nlmsghdr(const struct ofpbuf *);
  40. struct genlmsghdr *nl_msg_genlmsghdr(const struct ofpbuf *);
  41. bool nl_msg_nlmsgerr(const struct ofpbuf *, int *error);
  42. void nl_msg_reserve(struct ofpbuf *, size_t);
  43. /* Appending headers and raw data. */
  44. void nl_msg_put_nlmsghdr(struct ofpbuf *, size_t expected_payload,
  45. uint32_t type, uint32_t flags);
  46. void nl_msg_put_genlmsghdr(struct ofpbuf *, size_t expected_payload,
  47. int family, uint32_t flags,
  48. uint8_t cmd, uint8_t version);
  49. void nl_msg_put(struct ofpbuf *, const void *, size_t);
  50. void *nl_msg_put_uninit(struct ofpbuf *, size_t);
  51. /* Appending attributes. */
  52. void *nl_msg_put_unspec_uninit(struct ofpbuf *, uint16_t type, size_t);
  53. void nl_msg_put_unspec(struct ofpbuf *, uint16_t type, const void *, size_t);
  54. void nl_msg_put_flag(struct ofpbuf *, uint16_t type);
  55. void nl_msg_put_u8(struct ofpbuf *, uint16_t type, uint8_t value);
  56. void nl_msg_put_u16(struct ofpbuf *, uint16_t type, uint16_t value);
  57. void nl_msg_put_u32(struct ofpbuf *, uint16_t type, uint32_t value);
  58. void nl_msg_put_u64(struct ofpbuf *, uint16_t type, uint64_t value);
  59. void nl_msg_put_be16(struct ofpbuf *, uint16_t type, ovs_be16 value);
  60. void nl_msg_put_be32(struct ofpbuf *, uint16_t type, ovs_be32 value);
  61. void nl_msg_put_be64(struct ofpbuf *, uint16_t type, ovs_be64 value);
  62. void nl_msg_put_string(struct ofpbuf *, uint16_t type, const char *value);
  63. size_t nl_msg_start_nested(struct ofpbuf *, uint16_t type);
  64. void nl_msg_end_nested(struct ofpbuf *, size_t offset);
  65. void nl_msg_put_nested(struct ofpbuf *, uint16_t type,
  66. const void *data, size_t size);
  67. /* Separating buffers into individual messages. */
  68. struct nlmsghdr *nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg);
  69. /* Netlink attribute types. */
  70. enum nl_attr_type
  71. {
  72. NL_A_NO_ATTR = 0,
  73. NL_A_UNSPEC,
  74. NL_A_U8,
  75. NL_A_U16,
  76. NL_A_BE16 = NL_A_U16,
  77. NL_A_U32,
  78. NL_A_BE32 = NL_A_U32,
  79. NL_A_U64,
  80. NL_A_BE64 = NL_A_U64,
  81. NL_A_STRING,
  82. NL_A_FLAG,
  83. NL_A_NESTED,
  84. N_NL_ATTR_TYPES
  85. };
  86. /* Netlink attribute iteration. */
  87. static inline struct nlattr *
  88. nl_attr_next(const struct nlattr *nla)
  89. {
  90. return (struct nlattr *) ((uint8_t *) nla + NLA_ALIGN(nla->nla_len));
  91. }
  92. static inline bool
  93. nl_attr_is_valid(const struct nlattr *nla, size_t maxlen)
  94. {
  95. return (maxlen >= sizeof *nla
  96. && nla->nla_len >= sizeof *nla
  97. && NLA_ALIGN(nla->nla_len) <= maxlen);
  98. }
  99. /* This macro is careful to check for attributes with bad lengths. */
  100. #define NL_ATTR_FOR_EACH(ITER, LEFT, ATTRS, ATTRS_LEN) \
  101. for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN); \
  102. nl_attr_is_valid(ITER, LEFT); \
  103. (LEFT) -= NLA_ALIGN((ITER)->nla_len), (ITER) = nl_attr_next(ITER))
  104. /* This macro does not check for attributes with bad lengths. It should only
  105. * be used with messages from trusted sources or with messages that have
  106. * already been validated (e.g. with NL_ATTR_FOR_EACH). */
  107. #define NL_ATTR_FOR_EACH_UNSAFE(ITER, LEFT, ATTRS, ATTRS_LEN) \
  108. for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN); \
  109. (LEFT) > 0; \
  110. (LEFT) -= NLA_ALIGN((ITER)->nla_len), (ITER) = nl_attr_next(ITER))
  111. /* Netlink attribute parsing. */
  112. int nl_attr_type(const struct nlattr *);
  113. const void *nl_attr_get(const struct nlattr *);
  114. size_t nl_attr_get_size(const struct nlattr *);
  115. const void *nl_attr_get_unspec(const struct nlattr *, size_t size);
  116. bool nl_attr_get_flag(const struct nlattr *);
  117. uint8_t nl_attr_get_u8(const struct nlattr *);
  118. uint16_t nl_attr_get_u16(const struct nlattr *);
  119. uint32_t nl_attr_get_u32(const struct nlattr *);
  120. uint64_t nl_attr_get_u64(const struct nlattr *);
  121. ovs_be16 nl_attr_get_be16(const struct nlattr *);
  122. ovs_be32 nl_attr_get_be32(const struct nlattr *);
  123. ovs_be64 nl_attr_get_be64(const struct nlattr *);
  124. const char *nl_attr_get_string(const struct nlattr *);
  125. void nl_attr_get_nested(const struct nlattr *, struct ofpbuf *);
  126. /* Netlink attribute policy.
  127. *
  128. * Specifies how to parse a single attribute from a Netlink message payload.
  129. */
  130. struct nl_policy
  131. {
  132. enum nl_attr_type type;
  133. size_t min_len, max_len;
  134. bool optional;
  135. };
  136. bool nl_policy_parse(const struct ofpbuf *, size_t offset,
  137. const struct nl_policy[],
  138. struct nlattr *[], size_t n_attrs);
  139. bool nl_parse_nested(const struct nlattr *, const struct nl_policy[],
  140. struct nlattr *[], size_t n_attrs);
  141. const struct nlattr *nl_attr_find(const struct ofpbuf *, size_t hdr_len,
  142. uint16_t type);
  143. const struct nlattr *nl_attr_find_nested(const struct nlattr *, uint16_t type);
  144. #endif /* netlink.h */