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 ((uint16_t)sizeof(struct nlattr))
51 #define NLA_DATA_LEN(_nla) ((_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 nlp_has_priv(struct nlpcb *nlp, int priv);
81 struct ucred *nlp_get_cred(struct nlpcb *nlp);
82 uint32_t nlp_get_pid(const struct nlpcb *nlp);
83 bool nlp_unconstrained_vnet(const struct nlpcb *nlp);
84
85 /* netlink_generic.c */
86 struct genl_cmd {
87 const char *cmd_name;
88 nl_handler_f cmd_cb;
89 uint32_t cmd_flags;
90 uint32_t cmd_priv;
91 uint32_t cmd_num;
92 };
93
94 uint16_t genl_register_family(const char *family_name, size_t hdrsize,
95 uint16_t family_version, uint16_t max_attr_idx);
96 void genl_unregister_family(uint16_t family);
97 bool genl_register_cmds(uint16_t family, const struct genl_cmd *cmds,
98 u_int count);
99 uint32_t genl_register_group(uint16_t family, const char *group_name);
100 void genl_unregister_group(uint16_t family, uint32_t group);
101
102 typedef void (*genl_family_event_handler_t)(void *arg, const char *family_name,
103 uint16_t family_id, u_int action);
104 EVENTHANDLER_DECLARE(genl_family_event, genl_family_event_handler_t);
105
106 struct thread;
107 #if defined(NETLINK) || defined(NETLINK_MODULE)
108 /* Provide optimized calls to the functions inside the same linking unit */
109 struct nlpcb *_nl_get_thread_nlp(struct thread *td);
110
111 static inline struct nlpcb *
nl_get_thread_nlp(struct thread * td)112 nl_get_thread_nlp(struct thread *td)
113 {
114 return (_nl_get_thread_nlp(td));
115 }
116
117 #else
118 /* Provide access to the functions via netlink_glue.c */
119 struct nlpcb *nl_get_thread_nlp(struct thread *td);
120
121 #endif /* defined(NETLINK) || defined(NETLINK_MODULE) */
122
123 #endif
124 #endif
125