netlink_ctl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #ifndef _NETLINK_NETLINK_CTL_H_
  28. #define _NETLINK_NETLINK_CTL_H_
  29. #ifdef _KERNEL
  30. /*
  31. * This file provides headers for the public KPI of the netlink
  32. * subsystem
  33. */
  34. #include <sys/_eventhandler.h>
  35. MALLOC_DECLARE(M_NETLINK);
  36. /*
  37. * Macro for handling attribute TLVs
  38. */
  39. #define _roundup2(x, y) (((x)+((y)-1))&(~((y)-1)))
  40. #define NETLINK_ALIGN_SIZE sizeof(uint32_t)
  41. #define NETLINK_ALIGN(_len) _roundup2(_len, NETLINK_ALIGN_SIZE)
  42. #define NLA_ALIGN_SIZE sizeof(uint32_t)
  43. #define NLA_ALIGN(_len) _roundup2(_len, NLA_ALIGN_SIZE)
  44. #define NLA_HDRLEN ((int)sizeof(struct nlattr))
  45. #define NLA_DATA_LEN(_nla) ((int)((_nla)->nla_len - NLA_HDRLEN))
  46. #define NLA_DATA(_nla) NL_ITEM_DATA(_nla, NLA_HDRLEN)
  47. #define NLA_DATA_CONST(_nla) NL_ITEM_DATA_CONST(_nla, NLA_HDRLEN)
  48. #define NLA_TYPE(_nla) ((_nla)->nla_type & 0x3FFF)
  49. #ifndef typeof
  50. #define typeof __typeof
  51. #endif
  52. #define NLA_NEXT(_attr) (struct nlattr *)((char *)_attr + NLA_ALIGN(_attr->nla_len))
  53. #define _NLA_END(_start, _len) ((char *)(_start) + (_len))
  54. #define NLA_FOREACH(_attr, _start, _len) \
  55. for (typeof(_attr) _end = (typeof(_attr))_NLA_END(_start, _len), _attr = (_start); \
  56. ((char *)_attr < (char *)_end) && \
  57. ((char *)NLA_NEXT(_attr) <= (char *)_end); \
  58. _attr = (_len -= NLA_ALIGN(_attr->nla_len), NLA_NEXT(_attr)))
  59. #define NL_ARRAY_LEN(_a) (sizeof(_a) / sizeof((_a)[0]))
  60. #include <netlink/netlink_message_writer.h>
  61. #include <netlink/netlink_message_parser.h>
  62. /* Protocol handlers */
  63. struct nl_pstate;
  64. typedef int (*nl_handler_f)(struct nlmsghdr *hdr, struct nl_pstate *npt);
  65. bool netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler);
  66. bool netlink_unregister_proto(int proto);
  67. /* Common helpers */
  68. bool nl_has_listeners(int netlink_family, uint32_t groups_mask);
  69. bool nlp_has_priv(struct nlpcb *nlp, int priv);
  70. struct ucred *nlp_get_cred(struct nlpcb *nlp);
  71. uint32_t nlp_get_pid(const struct nlpcb *nlp);
  72. bool nlp_unconstrained_vnet(const struct nlpcb *nlp);
  73. /* netlink_generic.c */
  74. struct genl_cmd {
  75. const char *cmd_name;
  76. nl_handler_f cmd_cb;
  77. uint32_t cmd_flags;
  78. uint32_t cmd_priv;
  79. uint32_t cmd_num;
  80. };
  81. uint32_t genl_register_family(const char *family_name, size_t hdrsize,
  82. int family_version, int max_attr_idx);
  83. bool genl_unregister_family(const char *family_name);
  84. bool genl_register_cmds(const char *family_name, const struct genl_cmd *cmds,
  85. int count);
  86. uint32_t genl_register_group(const char *family_name, const char *group_name);
  87. struct genl_family;
  88. const char *genl_get_family_name(const struct genl_family *gf);
  89. uint32_t genl_get_family_id(const struct genl_family *gf);
  90. typedef void (*genl_family_event_handler_t)(void *arg, const struct genl_family *gf, int action);
  91. EVENTHANDLER_DECLARE(genl_family_event, genl_family_event_handler_t);
  92. struct thread;
  93. #if defined(NETLINK) || defined(NETLINK_MODULE)
  94. /* Provide optimized calls to the functions inside the same linking unit */
  95. struct nlpcb *_nl_get_thread_nlp(struct thread *td);
  96. static inline struct nlpcb *
  97. nl_get_thread_nlp(struct thread *td)
  98. {
  99. return (_nl_get_thread_nlp(td));
  100. }
  101. #else
  102. /* Provide access to the functions via netlink_glue.c */
  103. struct nlpcb *nl_get_thread_nlp(struct thread *td);
  104. #endif /* defined(NETLINK) || defined(NETLINK_MODULE) */
  105. #endif
  106. #endif