xref: /freebsd/sys/netlink/netlink_ctl.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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 #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 #define	NL_ARRAY_LEN(_a)	(sizeof(_a) / sizeof((_a)[0]))
69 
70 #include <netlink/netlink_message_writer.h>
71 #include <netlink/netlink_message_parser.h>
72 
73 
74 /* Protocol handlers */
75 struct nl_pstate;
76 typedef int (*nl_handler_f)(struct nlmsghdr *hdr, struct nl_pstate *npt);
77 
78 bool netlink_register_proto(int proto, const char *proto_name, nl_handler_f handler);
79 bool netlink_unregister_proto(int proto);
80 
81 /* Common helpers */
82 bool nl_has_listeners(int netlink_family, uint32_t groups_mask);
83 bool nlp_has_priv(struct nlpcb *nlp, int priv);
84 struct ucred *nlp_get_cred(struct nlpcb *nlp);
85 uint32_t nlp_get_pid(const struct nlpcb *nlp);
86 bool nlp_unconstrained_vnet(const struct nlpcb *nlp);
87 
88 /* netlink_generic.c */
89 struct genl_cmd {
90 	const char	*cmd_name;
91 	nl_handler_f	cmd_cb;
92 	uint32_t	cmd_flags;
93 	uint32_t	cmd_priv;
94 	uint32_t	cmd_num;
95 };
96 
97 uint32_t genl_register_family(const char *family_name, size_t hdrsize,
98     int family_version, int max_attr_idx);
99 bool genl_unregister_family(const char *family_name);
100 bool genl_register_cmds(const char *family_name, const struct genl_cmd *cmds,
101     int count);
102 uint32_t genl_register_group(const char *family_name, const char *group_name);
103 
104 struct genl_family;
105 const char *genl_get_family_name(const struct genl_family *gf);
106 uint32_t genl_get_family_id(const struct genl_family *gf);
107 
108 typedef void (*genl_family_event_handler_t)(void *arg, const struct genl_family *gf, int action);
109 EVENTHANDLER_DECLARE(genl_family_event, genl_family_event_handler_t);
110 
111 #endif
112 #endif
113