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