1/*- 2 * SPDX-License-Identifier: BSD-2-Clause 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#include <netinet/in.h> 35 36/* 37 * It is not meant to be included directly 38 */ 39 40struct nl_buf; 41struct nl_writer; 42typedef bool nl_writer_cb(struct nl_writer *nw); 43 44struct nl_writer { 45 struct nl_buf *buf; /* Underlying storage pointer */ 46 struct nlmsghdr *hdr; /* Pointer to the currently-filled msg */ 47 nl_writer_cb *cb; /* Callback to flush data */ 48 union { 49 struct nlpcb *nlp; 50 struct { 51 uint16_t proto; 52 uint16_t id; 53 } group; 54 }; 55 u_int num_messages; /* Number of messages in the buffer */ 56 int malloc_flag; /* M_WAITOK or M_NOWAIT */ 57 bool ignore_limit; /* If true, ignores RCVBUF limit */ 58 bool enomem; /* True if ENOMEM occured */ 59 bool suppress_ack; /* If true, don't send NLMSG_ERR */ 60}; 61 62#define NLMSG_SMALL 128 63#define NLMSG_LARGE 2048 64 65/* Message and attribute writing */ 66#if defined(NETLINK) || defined(NETLINK_MODULE) 67/* Provide optimized calls to the functions inside the same linking unit */ 68 69bool _nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp); 70bool _nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id); 71bool _nlmsg_flush(struct nl_writer *nw); 72void _nlmsg_ignore_limit(struct nl_writer *nw); 73 74bool _nlmsg_refill_buffer(struct nl_writer *nw, u_int required_len); 75bool _nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type, 76 uint16_t flags, uint32_t len); 77bool _nlmsg_end(struct nl_writer *nw); 78void _nlmsg_abort(struct nl_writer *nw); 79 80bool _nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr); 81 82 83static inline bool 84nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp) 85{ 86 return (_nlmsg_get_unicast_writer(nw, expected_size, nlp)); 87} 88 89static inline bool 90nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id) 91{ 92 return (_nlmsg_get_group_writer(nw, expected_size, proto, group_id)); 93} 94 95static inline bool 96nlmsg_flush(struct nl_writer *nw) 97{ 98 return (_nlmsg_flush(nw)); 99} 100 101static inline void 102nlmsg_ignore_limit(struct nl_writer *nw) 103{ 104 _nlmsg_ignore_limit(nw); 105} 106 107static inline bool 108nlmsg_refill_buffer(struct nl_writer *nw, int required_size) 109{ 110 return (_nlmsg_refill_buffer(nw, required_size)); 111} 112 113static inline bool 114nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type, 115 uint16_t flags, uint32_t len) 116{ 117 return (_nlmsg_add(nw, portid, seq, type, flags, len)); 118} 119 120static inline bool 121nlmsg_end(struct nl_writer *nw) 122{ 123 return (_nlmsg_end(nw)); 124} 125 126static inline void 127nlmsg_abort(struct nl_writer *nw) 128{ 129 return (_nlmsg_abort(nw)); 130} 131 132static inline bool 133nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr) 134{ 135 return (_nlmsg_end_dump(nw, error, hdr)); 136} 137 138#else 139/* Provide access to the functions via netlink_glue.c */ 140 141bool nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp); 142bool nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id); 143bool nlmsg_get_chain_writer(struct nl_writer *nw, int expected_size, struct mbuf **pm); 144bool nlmsg_flush(struct nl_writer *nw); 145void nlmsg_ignore_limit(struct nl_writer *nw); 146 147bool nlmsg_refill_buffer(struct nl_writer *nw, int required_size); 148bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type, 149 uint16_t flags, uint32_t len); 150bool nlmsg_end(struct nl_writer *nw); 151void nlmsg_abort(struct nl_writer *nw); 152 153bool nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr); 154 155#endif /* defined(NETLINK) || defined(NETLINK_MODULE) */ 156 157static inline bool 158nlmsg_reply(struct nl_writer *nw, const struct nlmsghdr *hdr, int payload_len) 159{ 160 return (nlmsg_add(nw, hdr->nlmsg_pid, hdr->nlmsg_seq, hdr->nlmsg_type, 161 hdr->nlmsg_flags, payload_len)); 162} 163 164/* 165 * KPI similar to mtodo(): 166 * current (uncompleted) header is guaranteed to be contiguous, 167 * but can be reallocated, thus pointers may need to be readjusted. 168 */ 169u_int nlattr_save_offset(const struct nl_writer *nw); 170 171static inline void * 172_nlattr_restore_offset(const struct nl_writer *nw, int off) 173{ 174 return ((void *)((char *)nw->hdr + off)); 175} 176#define nlattr_restore_offset(_ns, _off, _t) ((_t *)_nlattr_restore_offset(_ns, _off)) 177 178static inline void 179nlattr_set_len(const struct nl_writer *nw, int off) 180{ 181 struct nlattr *nla = nlattr_restore_offset(nw, off, struct nlattr); 182 nla->nla_len = nlattr_save_offset(nw) - off; 183} 184 185void *nlmsg_reserve_data_raw(struct nl_writer *nw, size_t sz); 186#define nlmsg_reserve_object(_ns, _t) ((_t *)nlmsg_reserve_data_raw(_ns, sizeof(_t))) 187#define nlmsg_reserve_data(_ns, _sz, _t) ((_t *)nlmsg_reserve_data_raw(_ns, _sz)) 188 189static inline int 190nlattr_add_nested(struct nl_writer *nw, uint16_t nla_type) 191{ 192 int off = nlattr_save_offset(nw); 193 struct nlattr *nla = nlmsg_reserve_data(nw, sizeof(struct nlattr), struct nlattr); 194 if (__predict_false(nla == NULL)) 195 return (0); 196 nla->nla_type = nla_type; 197 return (off); 198} 199 200static inline void * 201_nlmsg_reserve_attr(struct nl_writer *nw, uint16_t nla_type, uint16_t sz) 202{ 203 sz += sizeof(struct nlattr); 204 205 struct nlattr *nla = nlmsg_reserve_data(nw, sz, struct nlattr); 206 if (__predict_false(nla == NULL)) 207 return (NULL); 208 nla->nla_type = nla_type; 209 nla->nla_len = sz; 210 211 return ((void *)(nla + 1)); 212} 213#define nlmsg_reserve_attr(_ns, _at, _t) ((_t *)_nlmsg_reserve_attr(_ns, _at, NLA_ALIGN(sizeof(_t)))) 214 215bool nlattr_add(struct nl_writer *nw, int attr_type, int attr_len, 216 const void *data); 217 218static inline bool 219nlattr_add_raw(struct nl_writer *nw, const struct nlattr *nla_src) 220{ 221 int attr_len = nla_src->nla_len - sizeof(struct nlattr); 222 223 MPASS(attr_len >= 0); 224 225 return (nlattr_add(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1))); 226} 227 228static inline bool 229nlattr_add_bool(struct nl_writer *nw, int attrtype, bool value) 230{ 231 return (nlattr_add(nw, attrtype, sizeof(bool), &value)); 232} 233 234static inline bool 235nlattr_add_u8(struct nl_writer *nw, int attrtype, uint8_t value) 236{ 237 return (nlattr_add(nw, attrtype, sizeof(uint8_t), &value)); 238} 239 240static inline bool 241nlattr_add_u16(struct nl_writer *nw, int attrtype, uint16_t value) 242{ 243 return (nlattr_add(nw, attrtype, sizeof(uint16_t), &value)); 244} 245 246static inline bool 247nlattr_add_u32(struct nl_writer *nw, int attrtype, uint32_t value) 248{ 249 return (nlattr_add(nw, attrtype, sizeof(uint32_t), &value)); 250} 251 252static inline bool 253nlattr_add_u64(struct nl_writer *nw, int attrtype, uint64_t value) 254{ 255 return (nlattr_add(nw, attrtype, sizeof(uint64_t), &value)); 256} 257 258static inline bool 259nlattr_add_s8(struct nl_writer *nw, int attrtype, int8_t value) 260{ 261 return (nlattr_add(nw, attrtype, sizeof(int8_t), &value)); 262} 263 264static inline bool 265nlattr_add_s16(struct nl_writer *nw, int attrtype, int16_t value) 266{ 267 return (nlattr_add(nw, attrtype, sizeof(int16_t), &value)); 268} 269 270static inline bool 271nlattr_add_s32(struct nl_writer *nw, int attrtype, int32_t value) 272{ 273 return (nlattr_add(nw, attrtype, sizeof(int32_t), &value)); 274} 275 276static inline bool 277nlattr_add_s64(struct nl_writer *nw, int attrtype, int64_t value) 278{ 279 return (nlattr_add(nw, attrtype, sizeof(int64_t), &value)); 280} 281 282static inline bool 283nlattr_add_flag(struct nl_writer *nw, int attrtype) 284{ 285 return (nlattr_add(nw, attrtype, 0, NULL)); 286} 287 288static inline bool 289nlattr_add_string(struct nl_writer *nw, int attrtype, const char *str) 290{ 291 return (nlattr_add(nw, attrtype, strlen(str) + 1, str)); 292} 293 294static inline bool 295nlattr_add_in_addr(struct nl_writer *nw, int attrtype, const struct in_addr *in) 296{ 297 return (nlattr_add(nw, attrtype, sizeof(*in), in)); 298} 299 300static inline bool 301nlattr_add_in6_addr(struct nl_writer *nw, int attrtype, const struct in6_addr *in6) 302{ 303 return (nlattr_add(nw, attrtype, sizeof(*in6), in6)); 304} 305#endif 306#endif 307