1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Ng Peng Nam Sean
5  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _NETLINK_NETLINK_MESSAGE_WRITER_H_
30 #define _NETLINK_NETLINK_MESSAGE_WRITER_H_
31 
32 #ifdef _KERNEL
33 /*
34  * It is not meant to be included directly
35  */
36 
37 struct mbuf;
38 struct nl_writer;
39 typedef bool nl_writer_cb(struct nl_writer *nw, void *buf, int buflen, int cnt);
40 
41 struct nl_writer {
42 	int			alloc_len;	/* allocated buffer length */
43 	int			offset;		/* offset from the start of the buffer */
44 	struct nlmsghdr		*hdr;		/* Pointer to the currently-filled msg */
45 	char			*data;		/* pointer to the contiguous storage */
46 	void			*_storage;	/* Underlying storage pointer */
47 	nl_writer_cb		*cb;		/* Callback to flush data */
48 	union {
49 		void		*arg_ptr;	/* Callback argument as pointer */
50 		uint64_t	arg_uint;	/* Callback argument as int */
51 	};
52 	int			num_messages;	/* Number of messages in the buffer */
53 	int			malloc_flag;	/* M_WAITOK or M_NOWAIT */
54 	uint8_t			writer_type;	/* NS_WRITER_TYPE_* */
55 	uint8_t			writer_target;	/* NS_WRITER_TARGET_*  */
56 	bool			ignore_limit;	/* If true, ignores RCVBUF limit */
57 	bool			enomem;		/* True if ENOMEM occured */
58 	bool			suppress_ack;	/* If true, don't send NLMSG_ERR */
59 };
60 #define	NS_WRITER_TARGET_SOCKET	0
61 #define	NS_WRITER_TARGET_GROUP	1
62 #define	NS_WRITER_TARGET_CHAIN	2
63 
64 #define	NS_WRITER_TYPE_MBUF	0
65 #define NS_WRITER_TYPE_BUF	1
66 #define NS_WRITER_TYPE_LBUF	2
67 #define NS_WRITER_TYPE_MBUFC	3
68 
69 
70 #define	NLMSG_SMALL	128
71 #define	NLMSG_LARGE	2048
72 
73 /* Message and attribute writing */
74 
75 struct nlpcb;
76 bool nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp);
77 bool nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id);
78 bool nlmsg_get_chain_writer(struct nl_writer *nw, int expected_size, struct mbuf **pm);
79 bool nlmsg_flush(struct nl_writer *nw);
80 void nlmsg_ignore_limit(struct nl_writer *nw);
81 
82 bool nlmsg_refill_buffer(struct nl_writer *nw, int required_size);
83 bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
84     uint16_t flags, uint32_t len);
85 bool nlmsg_end(struct nl_writer *nw);
86 void nlmsg_abort(struct nl_writer *nw);
87 
88 bool nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr);
89 
90 static inline bool
91 nlmsg_reply(struct nl_writer *nw, const struct nlmsghdr *hdr, int payload_len)
92 {
93 	return (nlmsg_add(nw, hdr->nlmsg_pid, hdr->nlmsg_seq, hdr->nlmsg_type,
94 	    hdr->nlmsg_flags, payload_len));
95 }
96 
97 #define nlmsg_data(_hdr)        ((void *)((_hdr) + 1))
98 
99 /*
100  * KPI similar to mtodo():
101  * current (uncompleted) header is guaranteed to be contiguous,
102  *  but can be reallocated, thus pointers may need to be readjusted.
103  */
104 static inline int
105 nlattr_save_offset(const struct nl_writer *nw)
106 {
107         return (nw->offset - ((char *)nw->hdr - nw->data));
108 }
109 
110 static inline void *
111 _nlattr_restore_offset(const struct nl_writer *nw, int off)
112 {
113 	return ((void *)((char *)nw->hdr + off));
114 }
115 #define	nlattr_restore_offset(_ns, _off, _t)	((_t *)_nlattr_restore_offset(_ns, _off))
116 
117 static inline void
118 nlattr_set_len(const struct nl_writer *nw, int off)
119 {
120 	struct nlattr *nla = nlattr_restore_offset(nw, off, struct nlattr);
121 	nla->nla_len = nlattr_save_offset(nw) - off;
122 }
123 
124 static inline void *
125 nlmsg_reserve_data_raw(struct nl_writer *nw, size_t sz)
126 {
127         if (__predict_false(nw->offset + NETLINK_ALIGN(sz) > nw->alloc_len)) {
128 		if (!nlmsg_refill_buffer(nw, NETLINK_ALIGN(sz)))
129 			return (NULL);
130         }
131 
132         void *data_ptr = &nw->data[nw->offset];
133         nw->offset += NLMSG_ALIGN(sz);
134 
135         return (data_ptr);
136 }
137 #define nlmsg_reserve_object(_ns, _t)	((_t *)nlmsg_reserve_data_raw(_ns, NLA_ALIGN(sizeof(_t))))
138 #define nlmsg_reserve_data(_ns, _sz, _t)	((_t *)nlmsg_reserve_data_raw(_ns, _sz))
139 
140 static inline int
141 nlattr_add_nested(struct nl_writer *nw, uint16_t nla_type)
142 {
143 	int off = nlattr_save_offset(nw);
144 	struct nlattr *nla = nlmsg_reserve_data(nw, sizeof(struct nlattr), struct nlattr);
145 	if (__predict_false(nla == NULL))
146 		return (0);
147 	nla->nla_type = nla_type;
148 	return (off);
149 }
150 
151 static inline void *
152 _nlmsg_reserve_attr(struct nl_writer *nw, uint16_t nla_type, uint16_t sz)
153 {
154 	sz += sizeof(struct nlattr);
155 
156 	struct nlattr *nla = nlmsg_reserve_data(nw, sz, struct nlattr);
157 	if (__predict_false(nla == NULL))
158 		return (NULL);
159 	nla->nla_type = nla_type;
160 	nla->nla_len = sz;
161 
162 	return ((void *)(nla + 1));
163 }
164 #define	nlmsg_reserve_attr(_ns, _at, _t)	((_t *)_nlmsg_reserve_attr(_ns, _at, NLA_ALIGN(sizeof(_t))))
165 
166 static inline bool
167 nlattr_add(struct nl_writer *nw, int attr_type, int attr_len, const void *data)
168 {
169 	int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr));
170 
171         if (__predict_false(nw->offset + required_len > nw->alloc_len)) {
172 		if (!nlmsg_refill_buffer(nw, required_len))
173 			return (false);
174 	}
175 
176         struct nlattr *nla = (struct nlattr *)(&nw->data[nw->offset]);
177 
178         nla->nla_len = attr_len + sizeof(struct nlattr);
179         nla->nla_type = attr_type;
180         if (attr_len > 0) {
181 		if ((attr_len % 4) != 0) {
182 			/* clear padding bytes */
183 			bzero((char *)nla + required_len - 4, 4);
184 		}
185                 memcpy((nla + 1), data, attr_len);
186 	}
187         nw->offset += required_len;
188         return (true);
189 }
190 
191 static inline bool
192 nlattr_add_u8(struct nl_writer *nw, int attrtype, uint8_t value)
193 {
194 	return (nlattr_add(nw, attrtype, sizeof(uint8_t), &value));
195 }
196 
197 static inline bool
198 nlattr_add_u16(struct nl_writer *nw, int attrtype, uint16_t value)
199 {
200 	return (nlattr_add(nw, attrtype, sizeof(uint16_t), &value));
201 }
202 
203 static inline bool
204 nlattr_add_u32(struct nl_writer *nw, int attrtype, uint32_t value)
205 {
206 	return (nlattr_add(nw, attrtype, sizeof(uint32_t), &value));
207 }
208 
209 static inline bool
210 nlattr_add_u64(struct nl_writer *nw, int attrtype, uint64_t value)
211 {
212 	return (nlattr_add(nw, attrtype, sizeof(uint64_t), &value));
213 }
214 
215 static inline bool
216 nlattr_add_s8(struct nl_writer *nw, int attrtype, int8_t value)
217 {
218 	return (nlattr_add(nw, attrtype, sizeof(int8_t), &value));
219 }
220 
221 static inline bool
222 nlattr_add_s16(struct nl_writer *nw, int attrtype, int16_t value)
223 {
224 	return (nlattr_add(nw, attrtype, sizeof(int16_t), &value));
225 }
226 
227 static inline bool
228 nlattr_add_s32(struct nl_writer *nw, int attrtype, int32_t value)
229 {
230 	return (nlattr_add(nw, attrtype, sizeof(int32_t), &value));
231 }
232 
233 static inline bool
234 nlattr_add_s64(struct nl_writer *nw, int attrtype, int64_t value)
235 {
236 	return (nlattr_add(nw, attrtype, sizeof(int64_t), &value));
237 }
238 
239 static inline bool
240 nlattr_add_flag(struct nl_writer *nw, int attrtype)
241 {
242 	return (nlattr_add(nw, attrtype, 0, NULL));
243 }
244 
245 static inline bool
246 nlattr_add_string(struct nl_writer *nw, int attrtype, const char *str)
247 {
248 	return (nlattr_add(nw, attrtype, strlen(str) + 1, str));
249 }
250 
251 
252 #endif
253 #endif
254