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