xref: /freebsd/sys/netlink/netlink_ctl.h (revision 4ac8f4067096a9d4a00e41cd53bd5c4fa295fd15)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
37 MALLOC_DECLARE(M_NETLINK);
38 
39 /*
40  * Macro for handling attribute TLVs
41  */
42 #define _roundup2(x, y)         (((x)+((y)-1))&(~((y)-1)))
43 
44 #define NETLINK_ALIGN_SIZE      sizeof(uint32_t)
45 #define NETLINK_ALIGN(_len)     _roundup2(_len, NETLINK_ALIGN_SIZE)
46 
47 #define NLA_ALIGN_SIZE          sizeof(uint32_t)
48 #define NLA_ALIGN(_len)         _roundup2(_len, NLA_ALIGN_SIZE)
49 #define	NLA_HDRLEN		((int)sizeof(struct nlattr))
50 #define	NLA_DATA_LEN(_nla)	((int)((_nla)->nla_len - NLA_HDRLEN))
51 #define	NLA_DATA(_nla)		NL_ITEM_DATA(_nla, NLA_HDRLEN)
52 #define	NLA_DATA_CONST(_nla)	NL_ITEM_DATA_CONST(_nla, NLA_HDRLEN)
53 #define	NLA_TYPE(_nla)		((_nla)->nla_type & 0x3FFF)
54 
55 #ifndef	typeof
56 #define	typeof	__typeof
57 #endif
58 
59 #define NLA_NEXT(_attr) (struct nlattr *)((char *)_attr + NLA_ALIGN(_attr->nla_len))
60 #define	_NLA_END(_start, _len)	((char *)(_start) + (_len))
61 #define NLA_FOREACH(_attr, _start, _len)      \
62         for (typeof(_attr) _end = (typeof(_attr))_NLA_END(_start, _len), _attr = (_start);		\
63 		((char *)_attr < (char *)_end) && \
64 		((char *)NLA_NEXT(_attr) <= (char *)_end);	\
65 		_attr = (_len -= NLA_ALIGN(_attr->nla_len), NLA_NEXT(_attr)))
66 
67 #define	NL_ARRAY_LEN(_a)	(sizeof(_a) / sizeof((_a)[0]))
68 
69 #include <netlink/netlink_message_writer.h>
70 #include <netlink/netlink_message_parser.h>
71 
72 
73 /* Protocol handlers */
74 struct nl_pstate;
75 typedef int (*nl_handler_f)(struct nlmsghdr *hdr, struct nl_pstate *npt);
76 
77 bool netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler);
78 bool netlink_unregister_proto(int proto);
79 
80 /* Common helpers */
81 bool nl_has_listeners(int netlink_family, uint32_t groups_mask);
82 bool nlp_has_priv(struct nlpcb *nlp, int priv);
83 
84 /* netlink_generic.c */
85 struct genl_cmd {
86 	const char	*cmd_name;
87 	nl_handler_f	cmd_cb;
88 	uint32_t	cmd_flags;
89 	uint32_t	cmd_priv;
90 	uint32_t	cmd_num;
91 };
92 
93 uint32_t genl_register_family(const char *family_name, size_t hdrsize,
94     int family_version, int max_attr_idx);
95 bool genl_unregister_family(const char *family_name);
96 bool genl_register_cmds(const char *family_name, const struct genl_cmd *cmds,
97     int count);
98 uint32_t genl_register_group(const char *family_name, const char *group_name);
99 
100 /* Debug */
101 uint32_t nlp_get_pid(const struct nlpcb *nlp);
102 
103 #endif
104 #endif
105