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 /* 35 * It is not meant to be included directly 36 */ 37 38 struct mbuf; 39 struct nl_writer; 40 typedef bool nl_writer_cb(struct nl_writer *nw, void *buf, int buflen, int cnt); 41 42 struct nl_writer { 43 int alloc_len; /* allocated buffer length */ 44 int offset; /* offset from the start of the buffer */ 45 struct nlmsghdr *hdr; /* Pointer to the currently-filled msg */ 46 char *data; /* pointer to the contiguous storage */ 47 void *_storage; /* Underlying storage pointer */ 48 nl_writer_cb *cb; /* Callback to flush data */ 49 union { 50 void *arg_ptr; /* Callback argument as pointer */ 51 uint64_t arg_uint; /* Callback argument as int */ 52 }; 53 int num_messages; /* Number of messages in the buffer */ 54 int malloc_flag; /* M_WAITOK or M_NOWAIT */ 55 uint8_t writer_type; /* NS_WRITER_TYPE_* */ 56 uint8_t writer_target; /* NS_WRITER_TARGET_* */ 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 #define NS_WRITER_TARGET_SOCKET 0 62 #define NS_WRITER_TARGET_GROUP 1 63 #define NS_WRITER_TARGET_CHAIN 2 64 65 #define NS_WRITER_TYPE_MBUF 0 66 #define NS_WRITER_TYPE_BUF 1 67 #define NS_WRITER_TYPE_LBUF 2 68 #define NS_WRITER_TYPE_MBUFC 3 69 70 71 #define NLMSG_SMALL 128 72 #define NLMSG_LARGE 2048 73 74 /* Message and attribute writing */ 75 76 struct nlpcb; 77 bool nlmsg_get_unicast_writer(struct nl_writer *nw, int expected_size, struct nlpcb *nlp); 78 bool nlmsg_get_group_writer(struct nl_writer *nw, int expected_size, int proto, int group_id); 79 bool nlmsg_get_chain_writer(struct nl_writer *nw, int expected_size, struct mbuf **pm); 80 bool nlmsg_flush(struct nl_writer *nw); 81 void nlmsg_ignore_limit(struct nl_writer *nw); 82 83 bool nlmsg_refill_buffer(struct nl_writer *nw, int required_size); 84 bool nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type, 85 uint16_t flags, uint32_t len); 86 bool nlmsg_end(struct nl_writer *nw); 87 void nlmsg_abort(struct nl_writer *nw); 88 89 bool nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr); 90 91 static inline bool 92 nlmsg_reply(struct nl_writer *nw, const struct nlmsghdr *hdr, int payload_len) 93 { 94 return (nlmsg_add(nw, hdr->nlmsg_pid, hdr->nlmsg_seq, hdr->nlmsg_type, 95 hdr->nlmsg_flags, payload_len)); 96 } 97 98 #define nlmsg_data(_hdr) ((void *)((_hdr) + 1)) 99 100 /* 101 * KPI similar to mtodo(): 102 * current (uncompleted) header is guaranteed to be contiguous, 103 * but can be reallocated, thus pointers may need to be readjusted. 104 */ 105 static inline int 106 nlattr_save_offset(const struct nl_writer *nw) 107 { 108 return (nw->offset - ((char *)nw->hdr - nw->data)); 109 } 110 111 static inline void * 112 _nlattr_restore_offset(const struct nl_writer *nw, int off) 113 { 114 return ((void *)((char *)nw->hdr + off)); 115 } 116 #define nlattr_restore_offset(_ns, _off, _t) ((_t *)_nlattr_restore_offset(_ns, _off)) 117 118 static inline void 119 nlattr_set_len(const struct nl_writer *nw, int off) 120 { 121 struct nlattr *nla = nlattr_restore_offset(nw, off, struct nlattr); 122 nla->nla_len = nlattr_save_offset(nw) - off; 123 } 124 125 static inline void * 126 nlmsg_reserve_data_raw(struct nl_writer *nw, size_t sz) 127 { 128 sz = NETLINK_ALIGN(sz); 129 130 if (__predict_false(nw->offset + sz > nw->alloc_len)) { 131 if (!nlmsg_refill_buffer(nw, sz)) 132 return (NULL); 133 } 134 135 void *data_ptr = &nw->data[nw->offset]; 136 nw->offset += sz; 137 138 return (data_ptr); 139 } 140 #define nlmsg_reserve_object(_ns, _t) ((_t *)nlmsg_reserve_data_raw(_ns, sizeof(_t))) 141 #define nlmsg_reserve_data(_ns, _sz, _t) ((_t *)nlmsg_reserve_data_raw(_ns, _sz)) 142 143 static inline int 144 nlattr_add_nested(struct nl_writer *nw, uint16_t nla_type) 145 { 146 int off = nlattr_save_offset(nw); 147 struct nlattr *nla = nlmsg_reserve_data(nw, sizeof(struct nlattr), struct nlattr); 148 if (__predict_false(nla == NULL)) 149 return (0); 150 nla->nla_type = nla_type; 151 return (off); 152 } 153 154 static inline void * 155 _nlmsg_reserve_attr(struct nl_writer *nw, uint16_t nla_type, uint16_t sz) 156 { 157 sz += sizeof(struct nlattr); 158 159 struct nlattr *nla = nlmsg_reserve_data(nw, sz, struct nlattr); 160 if (__predict_false(nla == NULL)) 161 return (NULL); 162 nla->nla_type = nla_type; 163 nla->nla_len = sz; 164 165 return ((void *)(nla + 1)); 166 } 167 #define nlmsg_reserve_attr(_ns, _at, _t) ((_t *)_nlmsg_reserve_attr(_ns, _at, NLA_ALIGN(sizeof(_t)))) 168 169 static inline bool 170 nlattr_add(struct nl_writer *nw, int attr_type, int attr_len, const void *data) 171 { 172 int required_len = NLA_ALIGN(attr_len + sizeof(struct nlattr)); 173 174 if (__predict_false(nw->offset + required_len > nw->alloc_len)) { 175 if (!nlmsg_refill_buffer(nw, required_len)) 176 return (false); 177 } 178 179 struct nlattr *nla = (struct nlattr *)(&nw->data[nw->offset]); 180 181 nla->nla_len = attr_len + sizeof(struct nlattr); 182 nla->nla_type = attr_type; 183 if (attr_len > 0) { 184 if ((attr_len % 4) != 0) { 185 /* clear padding bytes */ 186 bzero((char *)nla + required_len - 4, 4); 187 } 188 memcpy((nla + 1), data, attr_len); 189 } 190 nw->offset += required_len; 191 return (true); 192 } 193 194 static inline bool 195 nlattr_add_raw(struct nl_writer *nw, const struct nlattr *nla_src) 196 { 197 int attr_len = nla_src->nla_len - sizeof(struct nlattr); 198 199 MPASS(attr_len >= 0); 200 201 return (nlattr_add(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1))); 202 } 203 204 static inline bool 205 nlattr_add_u8(struct nl_writer *nw, int attrtype, uint8_t value) 206 { 207 return (nlattr_add(nw, attrtype, sizeof(uint8_t), &value)); 208 } 209 210 static inline bool 211 nlattr_add_u16(struct nl_writer *nw, int attrtype, uint16_t value) 212 { 213 return (nlattr_add(nw, attrtype, sizeof(uint16_t), &value)); 214 } 215 216 static inline bool 217 nlattr_add_u32(struct nl_writer *nw, int attrtype, uint32_t value) 218 { 219 return (nlattr_add(nw, attrtype, sizeof(uint32_t), &value)); 220 } 221 222 static inline bool 223 nlattr_add_u64(struct nl_writer *nw, int attrtype, uint64_t value) 224 { 225 return (nlattr_add(nw, attrtype, sizeof(uint64_t), &value)); 226 } 227 228 static inline bool 229 nlattr_add_s8(struct nl_writer *nw, int attrtype, int8_t value) 230 { 231 return (nlattr_add(nw, attrtype, sizeof(int8_t), &value)); 232 } 233 234 static inline bool 235 nlattr_add_s16(struct nl_writer *nw, int attrtype, int16_t value) 236 { 237 return (nlattr_add(nw, attrtype, sizeof(int16_t), &value)); 238 } 239 240 static inline bool 241 nlattr_add_s32(struct nl_writer *nw, int attrtype, int32_t value) 242 { 243 return (nlattr_add(nw, attrtype, sizeof(int32_t), &value)); 244 } 245 246 static inline bool 247 nlattr_add_s64(struct nl_writer *nw, int attrtype, int64_t value) 248 { 249 return (nlattr_add(nw, attrtype, sizeof(int64_t), &value)); 250 } 251 252 struct in_addr; 253 bool nlattr_add_in_addr(struct nl_writer *nw, int attrtype, const struct in_addr *in); 254 255 struct in6_addr; 256 bool nlattr_add_in6_addr(struct nl_writer *nw, int attrtype, const struct in6_addr *in6); 257 258 static inline bool 259 nlattr_add_flag(struct nl_writer *nw, int attrtype) 260 { 261 return (nlattr_add(nw, attrtype, 0, NULL)); 262 } 263 264 static inline bool 265 nlattr_add_string(struct nl_writer *nw, int attrtype, const char *str) 266 { 267 return (nlattr_add(nw, attrtype, strlen(str) + 1, str)); 268 } 269 270 271 #endif 272 #endif 273