This source file includes following definitions.
- m_pulldown
- m_dup1
- m_tag_get
- m_tag_prepend
- m_tag_delete
- m_tag_delete_chain
- m_tag_find
- m_tag_copy
- m_tag_copy_chain
- m_tag_init
- m_tag_first
- m_tag_next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/proc.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70
71
72 static struct mbuf *m_dup1(struct mbuf *, int, int, int);
73
74
75
76
77
78
79
80
81
82
83
84 struct mbuf *
85 m_pulldown(struct mbuf *m, int off, int len, int *offp)
86 {
87 struct mbuf *n, *o;
88 int hlen, tlen, olen;
89 int sharedcluster;
90
91
92 if (m == NULL)
93 panic("m == NULL in m_pulldown()");
94 if (len > MCLBYTES) {
95 m_freem(m);
96 return (NULL);
97 }
98
99 n = m;
100 while (n != NULL && off > 0) {
101 if (n->m_len > off)
102 break;
103 off -= n->m_len;
104 n = n->m_next;
105 }
106
107 while (n != NULL && n->m_len == 0)
108 n = n->m_next;
109 if (!n) {
110 m_freem(m);
111 return (NULL);
112 }
113
114 sharedcluster = M_READONLY(n);
115
116
117
118
119
120 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
121 goto ok;
122
123
124
125
126
127
128
129 if (len <= n->m_len - off) {
130 struct mbuf *mlast;
131
132 o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
133 if (o == NULL) {
134 m_freem(m);
135 return (NULL);
136 }
137 for (mlast = o; mlast->m_next != NULL; mlast = mlast->m_next)
138 ;
139 n->m_len = off;
140 mlast->m_next = n->m_next;
141 n->m_next = o;
142 n = o;
143 off = 0;
144 goto ok;
145 }
146
147
148
149
150
151
152 hlen = n->m_len - off;
153 tlen = len - hlen;
154
155
156
157
158
159 olen = 0;
160 for (o = n->m_next; o != NULL; o = o->m_next)
161 olen += o->m_len;
162 if (hlen + olen < len) {
163 m_freem(m);
164 return (NULL);
165 }
166
167
168
169
170
171 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
172 !sharedcluster) {
173 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
174 n->m_len += tlen;
175 m_adj(n->m_next, tlen);
176 goto ok;
177 }
178 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
179 !sharedcluster && n->m_next->m_len >= tlen) {
180 n->m_next->m_data -= hlen;
181 n->m_next->m_len += hlen;
182 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
183 n->m_len -= hlen;
184 n = n->m_next;
185 off = 0;
186 goto ok;
187 }
188
189
190
191
192
193 MGET(o, M_DONTWAIT, m->m_type);
194 if (o && len > MLEN) {
195 MCLGET(o, M_DONTWAIT);
196 if ((o->m_flags & M_EXT) == 0) {
197 m_free(o);
198 o = NULL;
199 }
200 }
201 if (!o) {
202 m_freem(m);
203 return (NULL);
204 }
205
206 o->m_len = hlen;
207 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
208 n->m_len -= hlen;
209
210 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
211 o->m_len += tlen;
212 m_adj(n->m_next, tlen);
213 o->m_next = n->m_next;
214 n->m_next = o;
215 n = o;
216 off = 0;
217
218 ok:
219 if (offp)
220 *offp = off;
221 return (n);
222 }
223
224 static struct mbuf *
225 m_dup1(struct mbuf *m, int off, int len, int wait)
226 {
227 struct mbuf *n;
228 int l;
229
230 if (len > MCLBYTES)
231 return (NULL);
232 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
233 MGETHDR(n, wait, m->m_type);
234 if (n == NULL)
235 return (NULL);
236 M_DUP_PKTHDR(n, m);
237 l = MHLEN;
238 } else {
239 MGET(n, wait, m->m_type);
240 l = MLEN;
241 }
242 if (n && len > l) {
243 MCLGET(n, wait);
244 if ((n->m_flags & M_EXT) == 0) {
245 m_free(n);
246 n = NULL;
247 }
248 }
249 if (!n)
250 return (NULL);
251
252 m_copydata(m, off, len, mtod(n, caddr_t));
253 n->m_len = len;
254
255 return (n);
256 }
257
258
259 struct m_tag *
260 m_tag_get(int type, int len, int wait)
261 {
262 struct m_tag *t;
263
264 if (len < 0)
265 return (NULL);
266 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
267 if (t == NULL)
268 return (NULL);
269 t->m_tag_id = type;
270 t->m_tag_len = len;
271 return (t);
272 }
273
274
275 void
276 m_tag_prepend(struct mbuf *m, struct m_tag *t)
277 {
278 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
279 }
280
281
282 void
283 m_tag_delete(struct mbuf *m, struct m_tag *t)
284 {
285 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
286 free(t, M_PACKET_TAGS);
287 }
288
289
290 void
291 m_tag_delete_chain(struct mbuf *m)
292 {
293 struct m_tag *p;
294
295 while ((p = SLIST_FIRST(&m->m_pkthdr.tags)) != NULL) {
296 SLIST_REMOVE_HEAD(&m->m_pkthdr.tags, m_tag_link);
297 free(p, M_PACKET_TAGS);
298 }
299 }
300
301
302 struct m_tag *
303 m_tag_find(struct mbuf *m, int type, struct m_tag *t)
304 {
305 struct m_tag *p;
306
307 if (t == NULL)
308 p = SLIST_FIRST(&m->m_pkthdr.tags);
309 else
310 p = SLIST_NEXT(t, m_tag_link);
311 while (p != NULL) {
312 if (p->m_tag_id == type)
313 return (p);
314 p = SLIST_NEXT(p, m_tag_link);
315 }
316 return (NULL);
317 }
318
319
320 struct m_tag *
321 m_tag_copy(struct m_tag *t)
322 {
323 struct m_tag *p;
324
325 p = m_tag_get(t->m_tag_id, t->m_tag_len, M_NOWAIT);
326 if (p == NULL)
327 return (NULL);
328 bcopy(t + 1, p + 1, t->m_tag_len);
329 return (p);
330 }
331
332
333
334
335
336
337
338 int
339 m_tag_copy_chain(struct mbuf *to, struct mbuf *from)
340 {
341 struct m_tag *p, *t, *tprev = NULL;
342
343 m_tag_delete_chain(to);
344 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
345 t = m_tag_copy(p);
346 if (t == NULL) {
347 m_tag_delete_chain(to);
348 return (0);
349 }
350 if (tprev == NULL)
351 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
352 else
353 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
354 tprev = t;
355 }
356 return (1);
357 }
358
359
360 void
361 m_tag_init(struct mbuf *m)
362 {
363 SLIST_INIT(&m->m_pkthdr.tags);
364 }
365
366
367 struct m_tag *
368 m_tag_first(struct mbuf *m)
369 {
370 return (SLIST_FIRST(&m->m_pkthdr.tags));
371 }
372
373
374 struct m_tag *
375 m_tag_next(struct mbuf *m, struct m_tag *t)
376 {
377 return (SLIST_NEXT(t, m_tag_link));
378 }