xref: /illumos-gate/usr/src/uts/common/netsmb/mchain.h (revision 8329232e00f1048795bae53acb230316243aadb5)
1 /*
2  * Copyright (c) 2000, 2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/sys/mchain.h,v 1.1 2001/02/24 15:44:30 bp Exp $
33  */
34 
35 /*
36  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  *
39  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
40  */
41 
42 #ifndef _MCHAIN_H_
43 #define	_MCHAIN_H_
44 
45 #include <sys/types.h>
46 #include <sys/isa_defs.h>
47 #include <sys/byteorder.h>
48 
49 #ifdef _LITTLE_ENDIAN
50 
51 /* little-endian values on little-endian */
52 #define	htoles(x)	((uint16_t)(x))
53 #define	letohs(x)	((uint16_t)(x))
54 #define	htolel(x)	((uint32_t)(x))
55 #define	letohl(x)	((uint32_t)(x))
56 #define	htoleq(x)	((uint64_t)(x))
57 #define	letohq(x)	((uint64_t)(x))
58 
59 /*
60  * big-endian values on little-endian (swap)
61  *
62  * Use the BSWAP macros because they're fastest, and they're
63  * available in all environments where we use this header.
64  */
65 #define	htobes(x)	BSWAP_16(x)
66 #define	betohs(x)	BSWAP_16(x)
67 #define	htobel(x)	BSWAP_32(x)
68 #define	betohl(x)	BSWAP_32(x)
69 #define	htobeq(x)	BSWAP_64(x)
70 #define	betohq(x)	BSWAP_64(x)
71 
72 #else	/* (BYTE_ORDER == LITTLE_ENDIAN) */
73 
74 /* little-endian values on big-endian (swap) */
75 #define	letohs(x)	BSWAP_16(x)
76 #define	htoles(x)	BSWAP_16(x)
77 #define	letohl(x)	BSWAP_32(x)
78 #define	htolel(x)	BSWAP_32(x)
79 #define	letohq(x)	BSWAP_64(x)
80 #define	htoleq(x)	BSWAP_64(x)
81 
82 /* big-endian values on big-endian */
83 #define	htobes(x)	((uint16_t)(x))
84 #define	betohs(x)	((uint16_t)(x))
85 #define	htobel(x)	((uint32_t)(x))
86 #define	betohl(x)	((uint32_t)(x))
87 #define	htobeq(x)	((uint64_t)(x))
88 #define	betohq(x)	((uint64_t)(x))
89 #endif	/* (BYTE_ORDER == LITTLE_ENDIAN) */
90 
91 
92 /*
93  * Additions for Solaris to replace things that came from
94  * <sys/mbuf.h> in the Darwin code.  These are mostly just
95  * wrappers for streams functions.  See: subr_mchain.c
96  */
97 
98 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
99 
100 /*
101  * BSD-style mbuf "shim" for kernel code.  Note, this
102  * does NOT implement BSD mbufs in the kernel.  Rather,
103  * macros and wrapper functions are used so that code
104  * fomerly using mbuf_t now use STREAMS mblk_t instead.
105  */
106 
107 #include <sys/stream.h> /* mblk_t */
108 typedef mblk_t mbuf_t;
109 
110 /* BEGIN CSTYLED */
111 /*
112  * BSD-style mbufs, vs SysV-style mblks:
113  * One big difference: the mbuf payload is:
114  *   m_data ... (m_data + m_len)
115  * In Unix STREAMS, the mblk payload is:
116  *   b_rptr ... b_wptr
117  *
118  * Here are some handy conversion notes:
119  *
120  * struct mbuf                     struct mblk
121  *   m->m_next                       m->b_cont
122  *   m->m_nextpkt                    m->b_next
123  *   m->m_data                       m->b_rptr
124  *   m->m_len                        MBLKL(m)
125  *   m->m_dat[]                      m->b_datap->db_base
126  *   &m->m_dat[MLEN]                 m->b_datap->db_lim
127  *   M_TRAILINGSPACE(m)              MBLKTAIL(m)
128  *   m_freem(m)                      freemsg(m)
129  *
130  * Note that mbufs chains also have a special "packet" header,
131  * which has the length of the whole message.  In STREAMS one
132  * typically just calls msgdsize(m) to get that.
133  */
134 /* END CSTYLED */
135 
136 #define	mtod(m, t) ((t)((m)->b_rptr))
137 
138 /* length arg for m_copym to "copy all" */
139 #define	M_COPYALL		-1
140 
141 mblk_t *m_copym(mblk_t *, int, int, int);
142 mblk_t *m_pullup(mblk_t *, int);
143 mblk_t *m_split(mblk_t *, int, int);
144 void m_cat(mblk_t *, mblk_t *);
145 #define	m_freem(x)	freemsg(x)
146 mblk_t *m_getblk(int, int);
147 int  m_fixhdr(mblk_t *m);
148 
149 #else	/* _KERNEL */
150 
151 /*
152  * BSD-style mbuf work-alike, for user-level.
153  * See libsmbfs mbuf.c
154  */
155 typedef struct mbuf {
156 	int		m_len;
157 	int		m_maxlen;
158 	char		*m_data;
159 	struct mbuf	*m_next;
160 } mbuf_t;
161 
162 #define	mtod(m, t)	((t)(m)->m_data)
163 
164 int m_get(int, mbuf_t **);
165 void m_freem(mbuf_t *);
166 
167 #endif	/* _KERNEL */
168 
169 /*
170  * BSD-style mbchain/mdchain work-alike
171  */
172 
173 /*
174  * Type of copy for mb_{put|get}_mem()
175  */
176 #define	MB_MSYSTEM	0		/* use bcopy() */
177 #define	MB_MUSER	1		/* use copyin()/copyout() */
178 #define	MB_MINLINE	2		/* use an inline copy loop */
179 #define	MB_MZERO	3		/* bzero(), mb_put_mem only */
180 #define	MB_MCUSTOM	4		/* use an user defined function */
181 
182 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
183 
184 struct mbchain {
185 	mblk_t *mb_top;
186 	mblk_t *mb_cur;
187 	uint_t mb_count;
188 };
189 typedef struct mbchain mbchain_t;
190 
191 struct mdchain {
192 	mblk_t *md_top;		/* head of mblk chain */
193 	mblk_t *md_cur;		/* current mblk */
194 	uchar_t *md_pos;	/* position in md_cur */
195 	/* NB: md_pos is same type as mblk_t b_rptr, b_wptr members. */
196 };
197 typedef struct mdchain mdchain_t;
198 
199 mblk_t *mb_detach(mbchain_t *mbp);
200 int  mb_fixhdr(mbchain_t *mbp);
201 int  mb_put_uio(mbchain_t *mbp, uio_t *uiop, size_t size);
202 
203 void md_append_record(mdchain_t *mdp, mblk_t *top);
204 void md_next_record(mdchain_t *mdp);
205 int  md_get_uio(mdchain_t *mdp, uio_t *uiop, size_t size);
206 
207 #else	/* _KERNEL */
208 
209 /*
210  * user-level code uses the same struct for both (MB, MD)
211  */
212 typedef struct mbdata {
213 	mbuf_t	*mb_top;	/* head of mbuf chain */
214 	mbuf_t	*mb_cur;	/* current mbuf */
215 	char	*mb_pos;	/* position in mb_cur (get) */
216 	/* NB: mb_pos is same type as mbuf_t m_data member. */
217 	int	mb_count;	/* bytes marshalled (put) */
218 } mbdata_t;
219 typedef struct mbdata mbchain_t;
220 typedef struct mbdata mdchain_t;
221 
222 #endif	/* _KERNEL */
223 
224 int  mb_init(mbchain_t *);
225 void mb_initm(mbchain_t *, mbuf_t *);
226 void mb_done(mbchain_t *);
227 void *mb_reserve(mbchain_t *, int size);
228 
229 int  mb_put_padbyte(mbchain_t *mbp);
230 int  mb_put_uint8(mbchain_t *, uint8_t);
231 int  mb_put_uint16be(mbchain_t *, uint16_t);
232 int  mb_put_uint16le(mbchain_t *, uint16_t);
233 int  mb_put_uint32be(mbchain_t *, uint32_t);
234 int  mb_put_uint32le(mbchain_t *, uint32_t);
235 int  mb_put_uint64be(mbchain_t *, uint64_t);
236 int  mb_put_uint64le(mbchain_t *, uint64_t);
237 int  mb_put_mem(mbchain_t *, const void *, int, int);
238 int  mb_put_mbuf(mbchain_t *, mbuf_t *);
239 
240 int  md_init(mdchain_t *mdp);
241 void md_initm(mdchain_t *mbp, mbuf_t *m);
242 void md_done(mdchain_t *mdp);
243 
244 int  md_get_uint8(mdchain_t *, uint8_t *);
245 int  md_get_uint16be(mdchain_t *, uint16_t *);
246 int  md_get_uint16le(mdchain_t *, uint16_t *);
247 int  md_get_uint32be(mdchain_t *, uint32_t *);
248 int  md_get_uint32le(mdchain_t *, uint32_t *);
249 int  md_get_uint64be(mdchain_t *, uint64_t *);
250 int  md_get_uint64le(mdchain_t *, uint64_t *);
251 int  md_get_mem(mdchain_t *, void *, int, int);
252 int  md_get_mbuf(mdchain_t *, int, mbuf_t **);
253 
254 #endif	/* !_MCHAIN_H_ */
255