netlink.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2021 Ng Peng Nam Sean
  5. * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * Copyright (C) The Internet Society (2003). All Rights Reserved.
  29. *
  30. * This document and translations of it may be copied and furnished to
  31. * others, and derivative works that comment on or otherwise explain it
  32. * or assist in its implementation may be prepared, copied, published
  33. * and distributed, in whole or in part, without restriction of any
  34. * kind, provided that the above copyright notice and this paragraph are
  35. * included on all such copies and derivative works. However, this
  36. * document itself may not be modified in any way, such as by removing
  37. * the copyright notice or references to the Internet Society or other
  38. * Internet organizations, except as needed for the purpose of
  39. * developing Internet standards in which case the procedures for
  40. * copyrights defined in the Internet Standards process must be
  41. * followed, or as required to translate it into languages other than
  42. * English.
  43. *
  44. * The limited permissions granted above are perpetual and will not be
  45. * revoked by the Internet Society or its successors or assignees.
  46. *
  47. * This document and the information contained herein is provided on an
  48. * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
  49. * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  50. * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
  51. * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  52. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  53. */
  54. /*
  55. * This file contains structures and constants for RFC 3549 (Netlink)
  56. * protocol. Some values have been taken from Linux implementation.
  57. */
  58. #ifndef _NETLINK_NETLINK_H_
  59. #define _NETLINK_NETLINK_H_
  60. #include <sys/types.h>
  61. #include <sys/socket.h>
  62. struct sockaddr_nl {
  63. uint8_t nl_len; /* sizeof(sockaddr_nl) */
  64. sa_family_t nl_family; /* netlink family */
  65. uint16_t nl_pad; /* reserved, set to 0 */
  66. uint32_t nl_pid; /* desired port ID, 0 for auto-select */
  67. uint32_t nl_groups; /* multicast groups mask to bind to */
  68. };
  69. #define SOL_NETLINK 270
  70. /* Netlink socket options */
  71. #define NETLINK_ADD_MEMBERSHIP 1 /* Subscribe for the specified group notifications */
  72. #define NETLINK_DROP_MEMBERSHIP 2 /* Unsubscribe from the specified group */
  73. #define NETLINK_PKTINFO 3 /* XXX: not supported */
  74. #define NETLINK_BROADCAST_ERROR 4 /* XXX: not supported */
  75. #define NETLINK_NO_ENOBUFS 5 /* XXX: not supported */
  76. #define NETLINK_RX_RING 6 /* XXX: not supported */
  77. #define NETLINK_TX_RING 7 /* XXX: not supported */
  78. #define NETLINK_LISTEN_ALL_NSID 8 /* XXX: not supported */
  79. #define NETLINK_LIST_MEMBERSHIPS 9
  80. #define NETLINK_CAP_ACK 10 /* Send only original message header in the reply */
  81. #define NETLINK_EXT_ACK 11 /* Ack support for receiving additional TLVs in ack */
  82. #define NETLINK_GET_STRICT_CHK 12 /* Strict header checking */
  83. #define NETLINK_MSG_INFO 257 /* (FreeBSD-specific) Receive message originator data in cmsg */
  84. /*
  85. * RFC 3549, 2.3.2 Netlink Message Header
  86. */
  87. struct nlmsghdr {
  88. uint32_t nlmsg_len; /* Length of message including header */
  89. uint16_t nlmsg_type; /* Message type identifier */
  90. uint16_t nlmsg_flags; /* Flags (NLM_F_) */
  91. uint32_t nlmsg_seq; /* Sequence number */
  92. uint32_t nlmsg_pid; /* Sending process port ID */
  93. };
  94. /*
  95. * RFC 3549, 2.3.2 standard flag bits (nlmsg_flags)
  96. */
  97. #define NLM_F_REQUEST 0x01 /* Indicateds request to kernel */
  98. #define NLM_F_MULTI 0x02 /* Message is part of a group terminated by NLMSG_DONE msg */
  99. #define NLM_F_ACK 0x04 /* Reply with ack message containing resulting error code */
  100. #define NLM_F_ECHO 0x08 /* (not supported) Echo this request back */
  101. #define NLM_F_DUMP_INTR 0x10 /* Dump was inconsistent due to sequence change */
  102. #define NLM_F_DUMP_FILTERED 0x20 /* Dump was filtered as requested */
  103. /*
  104. * RFC 3549, 2.3.2 Additional flag bits for GET requests
  105. */
  106. #define NLM_F_ROOT 0x100 /* Return the complete table */
  107. #define NLM_F_MATCH 0x200 /* Return all entries matching criteria */
  108. #define NLM_F_ATOMIC 0x400 /* Return an atomic snapshot (ignored) */
  109. #define NLM_F_DUMP (NLM_F_ROOT | NLM_F_MATCH)
  110. /*
  111. * RFC 3549, 2.3.2 Additional flag bits for NEW requests
  112. */
  113. #define NLM_F_REPLACE 0x100 /* Replace existing matching config object */
  114. #define NLM_F_EXCL 0x200 /* Don't replace the object if exists */
  115. #define NLM_F_CREATE 0x400 /* Create if it does not exist */
  116. #define NLM_F_APPEND 0x800 /* Add to end of list */
  117. /* Modifiers to DELETE requests */
  118. #define NLM_F_NONREC 0x100 /* Do not delete recursively */
  119. /* Flags for ACK message */
  120. #define NLM_F_CAPPED 0x100 /* request was capped */
  121. #define NLM_F_ACK_TLVS 0x200 /* extended ACK TVLs were included */
  122. /*
  123. * RFC 3549, 2.3.2 standard message types (nlmsg_type).
  124. */
  125. #define NLMSG_NOOP 0x1 /* Message is ignored. */
  126. #define NLMSG_ERROR 0x2 /* reply error code reporting */
  127. #define NLMSG_DONE 0x3 /* Message terminates a multipart message. */
  128. #define NLMSG_OVERRUN 0x4 /* overrun detected, data is lost */
  129. #define NLMSG_MIN_TYPE 0x10 /* < 0x10: reserved control messages */
  130. /*
  131. * Defition of numbers assigned to the netlink subsystems.
  132. */
  133. #define NETLINK_ROUTE 0 /* Routing/device hook */
  134. #define NETLINK_UNUSED 1 /* not supported */
  135. #define NETLINK_USERSOCK 2 /* not supported */
  136. #define NETLINK_FIREWALL 3 /* not supported */
  137. #define NETLINK_SOCK_DIAG 4 /* not supported */
  138. #define NETLINK_NFLOG 5 /* not supported */
  139. #define NETLINK_XFRM 6 /* (not supported) PF_SETKEY */
  140. #define NETLINK_SELINUX 7 /* not supported */
  141. #define NETLINK_ISCSI 8 /* not supported */
  142. #define NETLINK_AUDIT 9 /* not supported */
  143. #define NETLINK_FIB_LOOKUP 10 /* not supported */
  144. #define NETLINK_CONNECTOR 11 /* not supported */
  145. #define NETLINK_NETFILTER 12 /* not supported */
  146. #define NETLINK_IP6_FW 13 /* not supported */
  147. #define NETLINK_DNRTMSG 14 /* not supported */
  148. #define NETLINK_KOBJECT_UEVENT 15 /* not supported */
  149. #define NETLINK_GENERIC 16 /* Generic netlink (dynamic families) */
  150. /*
  151. * RFC 3549, 2.3.2.2 The ACK Netlink Message
  152. */
  153. struct nlmsgerr {
  154. int error;
  155. struct nlmsghdr msg;
  156. };
  157. enum nlmsgerr_attrs {
  158. NLMSGERR_ATTR_UNUSED,
  159. NLMSGERR_ATTR_MSG = 1, /* string, error message */
  160. NLMSGERR_ATTR_OFFS = 2, /* u32, offset of the invalid attr from nl header */
  161. NLMSGERR_ATTR_COOKIE = 3, /* binary, data to pass to userland */
  162. NLMSGERR_ATTR_POLICY = 4, /* not supported */
  163. __NLMSGERR_ATTR_MAX,
  164. NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1
  165. };
  166. /* FreeBSD-specific debugging info */
  167. enum nlmsginfo_attrs {
  168. NLMSGINFO_ATTR_UNUSED,
  169. NLMSGINFO_ATTR_PROCESS_ID = 1, /* u32, source process PID */
  170. NLMSGINFO_ATTR_PORT_ID = 2, /* u32, source socket nl_pid */
  171. NLMSGINFO_ATTR_SEQ_ID = 3, /* u32, source message seq_id */
  172. };
  173. #ifndef roundup2
  174. #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
  175. #endif
  176. #define NL_ITEM_ALIGN_SIZE sizeof(uint32_t)
  177. #define NL_ITEM_ALIGN(_len) roundup2(_len, NL_ITEM_ALIGN_SIZE)
  178. #define NL_ITEM_DATA(_ptr, _off) ((void *)((char *)(_ptr) + _off))
  179. #define NL_ITEM_DATA_CONST(_ptr, _off) ((const void *)((const char *)(_ptr) + _off))
  180. #define NL_ITEM_OK(_ptr, _len, _hlen, _LEN_M) \
  181. ((_len) >= _hlen && _LEN_M(_ptr) >= _hlen && _LEN_M(_ptr) <= (_len))
  182. #define NL_ITEM_NEXT(_ptr, _LEN_M) ((__typeof(_ptr))((char *)(_ptr) + _LEN_M(_ptr)))
  183. #define NL_ITEM_ITER(_ptr, _len, _LEN_MACRO) \
  184. ((_len) -= _LEN_MACRO(_ptr), NL_ITEM_NEXT(_ptr, _LEN_MACRO))
  185. #ifndef _KERNEL
  186. /* part of netlink(3) API */
  187. #define NLMSG_ALIGNTO NL_ITEM_ALIGN_SIZE
  188. #define NLMSG_ALIGN(_len) NL_ITEM_ALIGN(_len)
  189. #define NLMSG_HDRLEN ((int)sizeof(struct nlmsghdr))
  190. #define NLMSG_LENGTH(_len) ((_len) + NLMSG_HDRLEN)
  191. #define NLMSG_SPACE(_len) NLMSG_ALIGN(NLMSG_LENGTH(_len))
  192. #define NLMSG_DATA(_hdr) NL_ITEM_DATA(_hdr, NLMSG_HDRLEN)
  193. #define _NLMSG_LEN(_hdr) ((int)(_hdr)->nlmsg_len)
  194. #define _NLMSG_ALIGNED_LEN(_hdr) NLMSG_ALIGN(_NLMSG_LEN(_hdr))
  195. #define NLMSG_OK(_hdr, _len) NL_ITEM_OK(_hdr, _len, NLMSG_HDRLEN, _NLMSG_LEN)
  196. #define NLMSG_PAYLOAD(_hdr,_len) (_NLMSG_LEN(_hdr) - NLMSG_SPACE((_len)))
  197. #define NLMSG_NEXT(_hdr, _len) NL_ITEM_ITER(_hdr, _len, _NLMSG_ALIGNED_LEN)
  198. #else
  199. #define NLMSG_ALIGNTO 4U
  200. #define NLMSG_ALIGN(len) (((len) + NLMSG_ALIGNTO - 1) & ~(NLMSG_ALIGNTO - 1))
  201. #define NLMSG_HDRLEN ((int)NLMSG_ALIGN(sizeof(struct nlmsghdr)))
  202. #endif
  203. /*
  204. * Base netlink attribute TLV header.
  205. */
  206. struct nlattr {
  207. uint16_t nla_len; /* Total attribute length */
  208. uint16_t nla_type; /* Attribute type */
  209. };
  210. /*
  211. *
  212. * nl_type field enconding:
  213. *
  214. * 0 1
  215. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
  216. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  217. * |N|O| Attribute type |
  218. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  219. * N - attribute contains other attributes (mostly unused)
  220. * O - encoded in network byte order (mostly unused)
  221. * Note: N & O are mutually exclusive
  222. *
  223. * Note: attribute type value scope normally is either parent attribute
  224. * or the message/message group.
  225. */
  226. #define NLA_F_NESTED (1 << 15)
  227. #define NLA_F_NET_BYTEORDER (1 << 14)
  228. #define NLA_TYPE_MASK ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
  229. #ifndef _KERNEL
  230. #define NLA_ALIGNTO NL_ITEM_ALIGN_SIZE
  231. #define NLA_ALIGN(_len) NL_ITEM_ALIGN(_len)
  232. #define NLA_HDRLEN ((int)sizeof(struct nlattr))
  233. #endif
  234. #endif