npf_mbuf.c revision 1.5 1 /* $NetBSD: npf_mbuf.c,v 1.5 2010/11/11 06:30:39 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This material is based upon work partially supported by The
8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * NPF network buffer management interface.
34 *
35 * Network buffer in NetBSD is mbuf. Internal mbuf structures are
36 * abstracted within this source.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: npf_mbuf.c,v 1.5 2010/11/11 06:30:39 rmind Exp $");
41
42 #include <sys/param.h>
43 #include <sys/mbuf.h>
44
45 #include "npf_impl.h"
46
47 /*
48 * nbuf_dataptr: return a pointer to data in nbuf.
49 */
50 void *
51 nbuf_dataptr(nbuf_t *nbuf)
52 {
53 const struct mbuf *m = nbuf;
54
55 return mtod(m, void *);
56 }
57
58 /*
59 * nbuf_advance: advance in mbuf or chain by specified amount of bytes.
60 *
61 * => Returns new pointer to data in mbuf and NULL if offset gets invalid.
62 * => Sets nbuf to current (after advance) mbuf in the chain.
63 */
64 void *
65 nbuf_advance(nbuf_t **nbuf, void *n_ptr, u_int n)
66 {
67 struct mbuf *m = *nbuf;
68 u_int off, wmark;
69 uint8_t *d;
70
71 /* Offset with amount to advance. */
72 off = (uintptr_t)n_ptr - mtod(m, uintptr_t) + n;
73 wmark = m->m_len;
74
75 /* Find the mbuf according to offset. */
76 while (__predict_false(wmark <= off)) {
77 m = m->m_next;
78 if (__predict_false(m == NULL)) {
79 /*
80 * If out of chain, then offset is
81 * higher than packet length.
82 */
83 return NULL;
84 }
85 wmark += m->m_len;
86 }
87
88 /* Offset in mbuf data. */
89 d = mtod(m, uint8_t *);
90 KASSERT(off >= (wmark - m->m_len));
91 d += (off - (wmark - m->m_len));
92
93 *nbuf = (void *)m;
94 return d;
95 }
96
97 /*
98 * nbuf_rw_datum: read or write a datum of specified length at current
99 * offset in the nbuf chain and copy datum into passed buffer.
100 *
101 * => Datum is allowed to overlap between two or more mbufs.
102 * => Note: all data in nbuf is in network byte order.
103 * => Returns 0 on success, error code on failure.
104 *
105 * Note: this function must be static inline with constant operation
106 * parameter - we expect constant propagation.
107 */
108
109 #define NBUF_DATA_READ 0
110 #define NBUF_DATA_WRITE 1
111
112 static inline int
113 nbuf_rw_datum(const int wr, nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
114 {
115 uint8_t *d = n_ptr, *b = buf;
116 struct mbuf *m = nbuf;
117 u_int off, wmark, end;
118
119 /* Current offset in mbuf. */
120 off = (uintptr_t)n_ptr - mtod(m, uintptr_t);
121 KASSERT(off < m->m_len);
122 wmark = m->m_len;
123
124 /* Is datum overlapping? */
125 end = off + len;
126 while (__predict_false(end > wmark)) {
127 u_int l;
128
129 /* Get the part of current mbuf. */
130 l = m->m_len - off;
131 KASSERT(l < len);
132 len -= l;
133 if (wr == NBUF_DATA_WRITE) {
134 while (l--)
135 *d++ = *b++;
136 } else {
137 KASSERT(wr == NBUF_DATA_READ);
138 while (l--)
139 *b++ = *d++;
140 }
141 KASSERT(len > 0);
142
143 /* Take next mbuf and continue. */
144 m = m->m_next;
145 if (__predict_false(m == NULL)) {
146 /*
147 * If out of chain, then offset with datum
148 * length exceed the packet length.
149 */
150 return EINVAL;
151 }
152 wmark += m->m_len;
153 d = mtod(m, uint8_t *);
154 off = 0;
155 }
156 KASSERT(n_ptr == d || mtod(m, uint8_t *) == d);
157 KASSERT(len <= m->m_len);
158
159 /* Non-overlapping case: fetch the actual data. */
160 if (wr == NBUF_DATA_WRITE) {
161 while (len--)
162 *d++ = *b++;
163 } else {
164 KASSERT(wr == NBUF_DATA_READ);
165 while (len--)
166 *b++ = *d++;
167 }
168 return 0;
169 }
170
171 /*
172 * nbuf_{fetch|store}_datum: read/write absraction calls on nbuf_rw_datum().
173 */
174 int
175 nbuf_fetch_datum(nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
176 {
177
178 return nbuf_rw_datum(NBUF_DATA_READ, nbuf, n_ptr, len, buf);
179 }
180
181 int
182 nbuf_store_datum(nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
183 {
184
185 return nbuf_rw_datum(NBUF_DATA_WRITE, nbuf, n_ptr, len, buf);
186 }
187
188 /*
189 * nbuf_advfetch: advance and fetch the datum.
190 */
191 int
192 nbuf_advfetch(nbuf_t **nbuf, void **n_ptr, u_int n, size_t len, void *buf)
193 {
194 nbuf_t *orig_nbuf = *nbuf;
195 void *orig_nptr = *n_ptr;
196 int error;
197
198 *n_ptr = nbuf_advance(nbuf, *n_ptr, n);
199 if (__predict_false(*n_ptr != NULL)) {
200 error = nbuf_fetch_datum(*nbuf, *n_ptr, len, buf);
201 } else {
202 error = EINVAL;
203 }
204 if (__predict_false(error)) {
205 *nbuf = orig_nbuf;
206 *n_ptr = orig_nptr;
207 }
208 return error;
209 }
210
211 /*
212 * nbuf_advstore: advance and store the datum.
213 */
214 int
215 nbuf_advstore(nbuf_t **nbuf, void **n_ptr, u_int n, size_t len, void *buf)
216 {
217 nbuf_t *orig_nbuf = *nbuf;
218 void *orig_nptr = *n_ptr;
219 int error;
220
221 *n_ptr = nbuf_advance(nbuf, *n_ptr, n);
222 if (__predict_false(*n_ptr != NULL)) {
223 error = nbuf_store_datum(*nbuf, *n_ptr, len, buf);
224 } else {
225 error = EINVAL;
226 }
227 if (__predict_false(error)) {
228 *nbuf = orig_nbuf;
229 *n_ptr = orig_nptr;
230 }
231 return error;
232 }
233
234 /*
235 * nbuf_add_tag: add a tag to specified network buffer.
236 *
237 * => Returns 0 on success, or errno on failure.
238 */
239 int
240 nbuf_add_tag(nbuf_t *nbuf, uint32_t key, uint32_t val)
241 {
242 struct mbuf *m = nbuf;
243 struct m_tag *mt;
244 uint32_t *dat;
245
246 mt = m_tag_get(PACKET_TAG_NPF, sizeof(uint32_t), M_NOWAIT);
247 if (__predict_false(mt == NULL)) {
248 return ENOMEM;
249 }
250 dat = (uint32_t *)(mt + 1);
251 *dat = val;
252 m_tag_prepend(m, mt);
253 return 0;
254 }
255
256 /*
257 * nbuf_find_tag: find a tag in specified network buffer.
258 *
259 * => Returns 0 on success, or errno on failure.
260 */
261 int
262 nbuf_find_tag(nbuf_t *nbuf, uint32_t key, void **data)
263 {
264 struct mbuf *m = nbuf;
265 struct m_tag *mt;
266
267 mt = m_tag_find(m, PACKET_TAG_NPF, NULL);
268 if (__predict_false(mt == NULL)) {
269 return EINVAL;
270 }
271 *data = (void *)(mt + 1);
272 return 0;
273 }
274