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 28 #ifndef _NETLINK_NETLINK_CTL_H_ 29 #define _NETLINK_NETLINK_CTL_H_ 30 31 #ifdef _KERNEL 32 /* 33 * This file provides headers for the public KPI of the netlink 34 * subsystem 35 */ 36 #include <sys/_eventhandler.h> 37 38 MALLOC_DECLARE(M_NETLINK); 39 40 /* 41 * Macro for handling attribute TLVs 42 */ 43 #define _roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) 44 45 #define NETLINK_ALIGN_SIZE sizeof(uint32_t) 46 #define NETLINK_ALIGN(_len) _roundup2(_len, NETLINK_ALIGN_SIZE) 47 48 #define NLA_ALIGN_SIZE sizeof(uint32_t) 49 #define NLA_ALIGN(_len) _roundup2(_len, NLA_ALIGN_SIZE) 50 #define NLA_HDRLEN ((int)sizeof(struct nlattr)) 51 #define NLA_DATA_LEN(_nla) ((int)((_nla)->nla_len - NLA_HDRLEN)) 52 #define NLA_DATA(_nla) NL_ITEM_DATA(_nla, NLA_HDRLEN) 53 #define NLA_DATA_CONST(_nla) NL_ITEM_DATA_CONST(_nla, NLA_HDRLEN) 54 #define NLA_TYPE(_nla) ((_nla)->nla_type & 0x3FFF) 55 56 #ifndef typeof 57 #define typeof __typeof 58 #endif 59 60 #define NLA_NEXT(_attr) (struct nlattr *)((char *)_attr + NLA_ALIGN(_attr->nla_len)) 61 #define _NLA_END(_start, _len) ((char *)(_start) + (_len)) 62 #define NLA_FOREACH(_attr, _start, _len) \ 63 for (typeof(_attr) _end = (typeof(_attr))_NLA_END(_start, _len), _attr = (_start); \ 64 ((char *)_attr < (char *)_end) && \ 65 ((char *)NLA_NEXT(_attr) <= (char *)_end); \ 66 _attr = (_len -= NLA_ALIGN(_attr->nla_len), NLA_NEXT(_attr))) 67 68 #include <netlink/netlink_message_writer.h> 69 #include <netlink/netlink_message_parser.h> 70 71 72 /* Protocol handlers */ 73 struct nl_pstate; 74 typedef int (*nl_handler_f)(struct nlmsghdr *hdr, struct nl_pstate *npt); 75 76 bool netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler); 77 bool netlink_unregister_proto(int proto); 78 79 /* Common helpers */ 80 bool nl_has_listeners(uint16_t netlink_family, uint32_t groups_mask); 81 bool nlp_has_priv(struct nlpcb *nlp, int priv); 82 struct ucred *nlp_get_cred(struct nlpcb *nlp); 83 uint32_t nlp_get_pid(const struct nlpcb *nlp); 84 bool nlp_unconstrained_vnet(const struct nlpcb *nlp); 85 86 /* netlink_generic.c */ 87 struct genl_cmd { 88 const char *cmd_name; 89 nl_handler_f cmd_cb; 90 uint32_t cmd_flags; 91 uint32_t cmd_priv; 92 uint32_t cmd_num; 93 }; 94 95 uint32_t genl_register_family(const char *family_name, size_t hdrsize, 96 uint16_t family_version, uint16_t max_attr_idx); 97 bool genl_unregister_family(const char *family_name); 98 bool genl_register_cmds(const char *family_name, const struct genl_cmd *cmds, 99 int count); 100 uint32_t genl_register_group(const char *family_name, const char *group_name); 101 102 struct genl_family; 103 const char *genl_get_family_name(const struct genl_family *gf); 104 uint16_t genl_get_family_id(const struct genl_family *gf); 105 106 typedef void (*genl_family_event_handler_t)(void *arg, const struct genl_family *gf, int action); 107 EVENTHANDLER_DECLARE(genl_family_event, genl_family_event_handler_t); 108 109 struct thread; 110 #if defined(NETLINK) || defined(NETLINK_MODULE) 111 /* Provide optimized calls to the functions inside the same linking unit */ 112 struct nlpcb *_nl_get_thread_nlp(struct thread *td); 113 114 static inline struct nlpcb * 115 nl_get_thread_nlp(struct thread *td) 116 { 117 return (_nl_get_thread_nlp(td)); 118 } 119 120 #else 121 /* Provide access to the functions via netlink_glue.c */ 122 struct nlpcb *nl_get_thread_nlp(struct thread *td); 123 124 #endif /* defined(NETLINK) || defined(NETLINK_MODULE) */ 125 126 #endif 127 #endif 128