Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1990-1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from the Stanford/CMU enet packet filter, | |
6 | * (net/enet.c) distributed as part of 4.3BSD, and code contributed | |
7 | * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence | |
8 | * Berkeley Laboratory. | |
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 | * 3. All advertising materials mentioning features or use of this software | |
19 | * must display the following acknowledgement: | |
20 | * This product includes software developed by the University of | |
21 | * California, Berkeley and its contributors. | |
22 | * 4. Neither the name of the University nor the names of its contributors | |
23 | * may be used to endorse or promote products derived from this software | |
24 | * without specific prior written permission. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
36 | * SUCH DAMAGE. | |
37 | * | |
78ed81a3 | 38 | * from: @(#)bpf.c 7.5 (Berkeley) 7/15/91 |
39 | * $Id$ | |
15637ed4 RG |
40 | */ |
41 | ||
42 | #include "bpfilter.h" | |
43 | ||
44 | #if NBPFILTER > 0 | |
45 | ||
46 | #ifdef notyet | |
47 | #ifndef __GNUC__ | |
48 | #define inline | |
49 | #else | |
50 | #define inline __inline__ | |
51 | #endif /* __GNUC__ */ | |
52 | #endif /* notyet */ | |
53 | ||
54 | #include <sys/param.h> | |
55 | #include <sys/systm.h> | |
56 | #include <sys/mbuf.h> | |
57 | #include <sys/buf.h> | |
58 | #include <sys/dir.h> | |
59 | #include <sys/time.h> | |
60 | #include <sys/proc.h> | |
61 | #include <sys/user.h> | |
62 | #include <sys/ioctl.h> | |
63 | /* #include <sys/map.h> */ | |
64 | ||
65 | #include <sys/file.h> | |
66 | #if defined(sparc) && BSD < 199103 | |
67 | #include <sys/stream.h> | |
68 | #endif | |
69 | #include <sys/tty.h> | |
70 | #include <sys/uio.h> | |
71 | ||
72 | #include <sys/protosw.h> | |
73 | #include <sys/socket.h> | |
74 | #include <net/if.h> | |
75 | ||
76 | #include <net/bpf.h> | |
77 | #include <net/bpfdesc.h> | |
78 | ||
79 | #include <sys/errno.h> | |
80 | ||
81 | #include <netinet/in.h> | |
82 | #include <netinet/if_ether.h> | |
83 | #include <sys/kernel.h> | |
84 | ||
85 | /* | |
86 | * Older BSDs don't have kernel malloc. | |
87 | */ | |
88 | #if BSD < 199103 | |
89 | extern bcopy(); | |
90 | static caddr_t bpf_alloc(); | |
91 | #include <net/bpf_compat.h> | |
92 | #define BPF_BUFSIZE (MCLBYTES-8) | |
93 | #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio) | |
94 | #else | |
95 | #define BPF_BUFSIZE 4096 | |
96 | #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio) | |
97 | #endif | |
98 | ||
99 | #define PRINET 26 /* interruptible */ | |
100 | ||
101 | /* | |
102 | * The default read buffer size is patchable. | |
103 | */ | |
104 | int bpf_bufsize = BPF_BUFSIZE; | |
105 | ||
106 | /* | |
107 | * bpf_iflist is the list of interfaces; each corresponds to an ifnet | |
108 | * bpf_dtab holds the descriptors, indexed by minor device # | |
109 | */ | |
110 | struct bpf_if *bpf_iflist; | |
111 | struct bpf_d bpf_dtab[NBPFILTER]; | |
112 | ||
113 | static void bpf_ifname(); | |
114 | static void catchpacket(); | |
115 | static void bpf_freed(); | |
116 | static int bpf_setif(); | |
117 | static int bpf_initd(); | |
118 | static int bpf_allocbufs(); | |
119 | ||
120 | static int | |
121 | bpf_movein(uio, linktype, mp, sockp) | |
122 | register struct uio *uio; | |
123 | int linktype; | |
124 | register struct mbuf **mp; | |
125 | register struct sockaddr *sockp; | |
126 | { | |
127 | struct mbuf *m; | |
128 | int error; | |
129 | int len; | |
130 | int hlen; | |
131 | ||
132 | /* | |
133 | * Build a sockaddr based on the data link layer type. | |
134 | * We do this at this level because the ethernet header | |
135 | * is copied directly into the data field of the sockaddr. | |
136 | * In the case of SLIP, there is no header and the packet | |
137 | * is forwarded as is. | |
138 | * Also, we are careful to leave room at the front of the mbuf | |
139 | * for the link level header. | |
140 | */ | |
141 | switch (linktype) { | |
142 | ||
143 | case DLT_SLIP: | |
144 | sockp->sa_family = AF_INET; | |
145 | hlen = 0; | |
146 | break; | |
147 | ||
148 | case DLT_EN10MB: | |
149 | sockp->sa_family = AF_UNSPEC; | |
150 | /* XXX Would MAXLINKHDR be better? */ | |
151 | hlen = sizeof(struct ether_header); | |
152 | break; | |
153 | ||
154 | case DLT_FDDI: | |
155 | sockp->sa_family = AF_UNSPEC; | |
156 | /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */ | |
157 | hlen = 24; | |
158 | break; | |
159 | ||
160 | case DLT_NULL: | |
161 | sockp->sa_family = AF_UNSPEC; | |
162 | hlen = 0; | |
163 | break; | |
164 | ||
165 | default: | |
166 | return (EIO); | |
167 | } | |
168 | ||
169 | len = uio->uio_resid; | |
170 | if ((unsigned)len > MCLBYTES) | |
171 | return (EIO); | |
172 | ||
173 | MGET(m, M_WAIT, MT_DATA); | |
174 | if (m == 0) | |
175 | return (ENOBUFS); | |
176 | if (len > MLEN) { | |
177 | #if BSD >= 199103 | |
178 | MCLGET(m, M_WAIT); | |
179 | if ((m->m_flags & M_EXT) == 0) { | |
180 | #else | |
181 | MCLGET(m); | |
182 | if (m->m_len != MCLBYTES) { | |
183 | #endif | |
184 | error = ENOBUFS; | |
185 | goto bad; | |
186 | } | |
187 | } | |
188 | m->m_len = len; | |
189 | *mp = m; | |
190 | /* | |
191 | * Make room for link header. | |
192 | */ | |
193 | if (hlen != 0) { | |
194 | m->m_len -= hlen; | |
195 | #if BSD >= 199103 | |
196 | m->m_data += hlen; /* XXX */ | |
197 | #else | |
198 | m->m_off += hlen; | |
199 | #endif | |
200 | error = UIOMOVE((caddr_t)sockp->sa_data, hlen, UIO_WRITE, uio); | |
201 | if (error) | |
202 | goto bad; | |
203 | } | |
204 | error = UIOMOVE(mtod(m, caddr_t), len - hlen, UIO_WRITE, uio); | |
205 | if (!error) | |
206 | return (0); | |
207 | bad: | |
208 | m_freem(m); | |
209 | return (error); | |
210 | } | |
211 | ||
212 | /* | |
213 | * Attach file to the bpf interface, i.e. make d listen on bp. | |
214 | * Must be called at splimp. | |
215 | */ | |
216 | static void | |
217 | bpf_attachd(d, bp) | |
218 | struct bpf_d *d; | |
219 | struct bpf_if *bp; | |
220 | { | |
221 | /* | |
222 | * Point d at bp, and add d to the interface's list of listeners. | |
223 | * Finally, point the driver's bpf cookie at the interface so | |
224 | * it will divert packets to bpf. | |
225 | */ | |
226 | d->bd_bif = bp; | |
227 | d->bd_next = bp->bif_dlist; | |
228 | bp->bif_dlist = d; | |
229 | ||
230 | *bp->bif_driverp = bp; | |
231 | } | |
232 | ||
233 | /* | |
234 | * Detach a file from its interface. | |
235 | */ | |
236 | static void | |
237 | bpf_detachd(d) | |
238 | struct bpf_d *d; | |
239 | { | |
240 | struct bpf_d **p; | |
241 | struct bpf_if *bp; | |
242 | ||
243 | bp = d->bd_bif; | |
244 | /* | |
245 | * Check if this descriptor had requested promiscuous mode. | |
246 | * If so, turn it off. | |
247 | */ | |
248 | if (d->bd_promisc) { | |
249 | d->bd_promisc = 0; | |
250 | if (ifpromisc(bp->bif_ifp, 0)) | |
251 | /* | |
252 | * Something is really wrong if we were able to put | |
253 | * the driver into promiscuous mode, but can't | |
254 | * take it out. | |
255 | */ | |
256 | panic("bpf: ifpromisc failed"); | |
257 | } | |
258 | /* Remove d from the interface's descriptor list. */ | |
259 | p = &bp->bif_dlist; | |
260 | while (*p != d) { | |
261 | p = &(*p)->bd_next; | |
262 | if (*p == 0) | |
263 | panic("bpf_detachd: descriptor not in list"); | |
264 | } | |
265 | *p = (*p)->bd_next; | |
266 | if (bp->bif_dlist == 0) | |
267 | /* | |
268 | * Let the driver know that there are no more listeners. | |
269 | */ | |
270 | *d->bd_bif->bif_driverp = 0; | |
271 | d->bd_bif = 0; | |
272 | } | |
273 | ||
274 | ||
275 | /* | |
276 | * Mark a descriptor free by making it point to itself. | |
277 | * This is probably cheaper than marking with a constant since | |
278 | * the address should be in a register anyway. | |
279 | */ | |
280 | #define D_ISFREE(d) ((d) == (d)->bd_next) | |
281 | #define D_MARKFREE(d) ((d)->bd_next = (d)) | |
282 | #define D_MARKUSED(d) ((d)->bd_next = 0) | |
283 | ||
284 | /* | |
285 | * Open ethernet device. Returns ENXIO for illegal minor device number, | |
286 | * EBUSY if file is open by another process. | |
287 | */ | |
288 | /* ARGSUSED */ | |
289 | int | |
290 | bpfopen(dev, flag) | |
291 | dev_t dev; | |
292 | int flag; | |
293 | { | |
294 | register struct bpf_d *d; | |
295 | ||
296 | if (minor(dev) >= NBPFILTER) | |
297 | return (ENXIO); | |
298 | /* | |
299 | * Each minor can be opened by only one process. If the requested | |
300 | * minor is in use, return EBUSY. | |
301 | */ | |
302 | d = &bpf_dtab[minor(dev)]; | |
303 | if (!D_ISFREE(d)) | |
304 | return (EBUSY); | |
305 | ||
306 | /* Mark "free" and do most initialization. */ | |
307 | bzero((char *)d, sizeof(*d)); | |
308 | d->bd_bufsize = bpf_bufsize; | |
309 | ||
310 | return (0); | |
311 | } | |
312 | ||
313 | /* | |
314 | * Close the descriptor by detaching it from its interface, | |
315 | * deallocating its buffers, and marking it free. | |
316 | */ | |
317 | /* ARGSUSED */ | |
318 | int | |
319 | bpfclose(dev, flag) | |
320 | dev_t dev; | |
321 | int flag; | |
322 | { | |
323 | register struct bpf_d *d = &bpf_dtab[minor(dev)]; | |
324 | register int s; | |
325 | ||
326 | s = splimp(); | |
327 | if (d->bd_bif) | |
328 | bpf_detachd(d); | |
329 | splx(s); | |
330 | bpf_freed(d); | |
331 | ||
332 | return (0); | |
333 | } | |
334 | ||
335 | /* | |
336 | * Support for SunOS, which does not have tsleep. | |
337 | */ | |
338 | #if BSD < 199103 | |
339 | static | |
340 | bpf_timeout(arg) | |
341 | caddr_t arg; | |
342 | { | |
343 | struct bpf_d *d = (struct bpf_d *)arg; | |
344 | d->bd_timedout = 1; | |
345 | wakeup(arg); | |
346 | } | |
347 | ||
348 | #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan) | |
349 | ||
350 | int | |
351 | bpf_sleep(d) | |
352 | register struct bpf_d *d; | |
353 | { | |
354 | register int rto = d->bd_rtout; | |
355 | register int st; | |
356 | ||
357 | if (rto != 0) { | |
358 | d->bd_timedout = 0; | |
359 | timeout(bpf_timeout, (caddr_t)d, rto); | |
360 | } | |
361 | st = sleep((caddr_t)d, PRINET|PCATCH); | |
362 | if (rto != 0) { | |
363 | if (d->bd_timedout == 0) | |
364 | untimeout(bpf_timeout, (caddr_t)d); | |
365 | else if (st == 0) | |
366 | return EWOULDBLOCK; | |
367 | } | |
368 | return (st != 0) ? EINTR : 0; | |
369 | } | |
370 | #else | |
371 | #define BPF_SLEEP tsleep | |
372 | #endif | |
373 | ||
374 | /* | |
375 | * Rotate the packet buffers in descriptor d. Move the store buffer | |
376 | * into the hold slot, and the free buffer into the store slot. | |
377 | * Zero the length of the new store buffer. | |
378 | */ | |
379 | #define ROTATE_BUFFERS(d) \ | |
380 | (d)->bd_hbuf = (d)->bd_sbuf; \ | |
381 | (d)->bd_hlen = (d)->bd_slen; \ | |
382 | (d)->bd_sbuf = (d)->bd_fbuf; \ | |
383 | (d)->bd_slen = 0; \ | |
384 | (d)->bd_fbuf = 0; | |
385 | /* | |
386 | * bpfread - read next chunk of packets from buffers | |
387 | */ | |
388 | int | |
389 | bpfread(dev, uio) | |
390 | dev_t dev; | |
391 | register struct uio *uio; | |
392 | { | |
393 | register struct bpf_d *d = &bpf_dtab[minor(dev)]; | |
394 | int error; | |
395 | int s; | |
396 | ||
397 | /* | |
398 | * Restrict application to use a buffer the same size as | |
399 | * as kernel buffers. | |
400 | */ | |
401 | if (uio->uio_resid != d->bd_bufsize) | |
402 | return (EINVAL); | |
403 | ||
404 | s = splimp(); | |
405 | /* | |
406 | * If the hold buffer is empty, then do a timed sleep, which | |
407 | * ends when the timeout expires or when enough packets | |
408 | * have arrived to fill the store buffer. | |
409 | */ | |
410 | while (d->bd_hbuf == 0) { | |
411 | if (d->bd_immediate && d->bd_slen != 0) { | |
412 | /* | |
413 | * A packet(s) either arrived since the previous | |
414 | * read or arrived while we were asleep. | |
415 | * Rotate the buffers and return what's here. | |
416 | */ | |
417 | ROTATE_BUFFERS(d); | |
418 | break; | |
419 | } | |
420 | error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf", | |
421 | d->bd_rtout); | |
422 | if (error == EINTR || error == ERESTART) { | |
423 | splx(s); | |
424 | return (error); | |
425 | } | |
426 | if (error == EWOULDBLOCK) { | |
427 | /* | |
428 | * On a timeout, return what's in the buffer, | |
429 | * which may be nothing. If there is something | |
430 | * in the store buffer, we can rotate the buffers. | |
431 | */ | |
432 | if (d->bd_hbuf) | |
433 | /* | |
434 | * We filled up the buffer in between | |
435 | * getting the timeout and arriving | |
436 | * here, so we don't need to rotate. | |
437 | */ | |
438 | break; | |
439 | ||
440 | if (d->bd_slen == 0) { | |
441 | splx(s); | |
442 | return (0); | |
443 | } | |
444 | ROTATE_BUFFERS(d); | |
445 | break; | |
446 | } | |
447 | } | |
448 | /* | |
449 | * At this point, we know we have something in the hold slot. | |
450 | */ | |
451 | splx(s); | |
452 | ||
453 | /* | |
454 | * Move data from hold buffer into user space. | |
455 | * We know the entire buffer is transferred since | |
456 | * we checked above that the read buffer is bpf_bufsize bytes. | |
457 | */ | |
458 | error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio); | |
459 | ||
460 | s = splimp(); | |
461 | d->bd_fbuf = d->bd_hbuf; | |
462 | d->bd_hbuf = 0; | |
463 | d->bd_hlen = 0; | |
464 | splx(s); | |
465 | ||
466 | return (error); | |
467 | } | |
468 | ||
469 | ||
470 | /* | |
471 | * If there are processes sleeping on this descriptor, wake them up. | |
472 | */ | |
473 | static inline void | |
474 | bpf_wakeup(d) | |
475 | register struct bpf_d *d; | |
476 | { | |
477 | wakeup((caddr_t)d); | |
478 | #if 0 | |
479 | selwakeup(&d->bd_sel); | |
480 | /* XXX */ | |
481 | d->bd_sel.si_pid = 0; | |
482 | #else | |
483 | if (d->bd_selproc) { | |
484 | selwakeup(d->bd_selproc->p_pid, (int)d->bd_selcoll); | |
485 | d->bd_selcoll = 0; | |
486 | d->bd_selproc = 0; | |
487 | } | |
488 | #endif | |
489 | } | |
490 | ||
491 | int | |
492 | bpfwrite(dev, uio) | |
493 | dev_t dev; | |
494 | struct uio *uio; | |
495 | { | |
496 | register struct bpf_d *d = &bpf_dtab[minor(dev)]; | |
497 | struct ifnet *ifp; | |
498 | struct mbuf *m; | |
499 | int error, s; | |
500 | static struct sockaddr dst; | |
501 | ||
502 | if (d->bd_bif == 0) | |
503 | return (ENXIO); | |
504 | ||
505 | ifp = d->bd_bif->bif_ifp; | |
506 | ||
507 | if (uio->uio_resid == 0) | |
508 | return (0); | |
509 | if (uio->uio_resid > ifp->if_mtu) | |
510 | return (EMSGSIZE); | |
511 | ||
512 | error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst); | |
513 | if (error) | |
514 | return (error); | |
515 | ||
516 | s = splnet(); | |
517 | #if BSD >= 199103 | |
518 | error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0); | |
519 | #else | |
520 | error = (*ifp->if_output)(ifp, m, &dst); | |
521 | #endif | |
522 | splx(s); | |
523 | /* | |
524 | * The driver frees the mbuf. | |
525 | */ | |
526 | return (error); | |
527 | } | |
528 | ||
529 | /* | |
530 | * Reset a descriptor by flushing its packet buffer and clearing the | |
531 | * receive and drop counts. Should be called at splimp. | |
532 | */ | |
533 | static void | |
534 | reset_d(d) | |
535 | struct bpf_d *d; | |
536 | { | |
537 | if (d->bd_hbuf) { | |
538 | /* Free the hold buffer. */ | |
539 | d->bd_fbuf = d->bd_hbuf; | |
540 | d->bd_hbuf = 0; | |
541 | } | |
542 | d->bd_slen = 0; | |
543 | d->bd_hlen = 0; | |
544 | d->bd_rcount = 0; | |
545 | d->bd_dcount = 0; | |
546 | } | |
547 | ||
548 | /* | |
549 | * FIONREAD Check for read packet available. | |
550 | * SIOCGIFADDR Get interface address - convenient hook to driver. | |
551 | * BIOCGBLEN Get buffer len [for read()]. | |
552 | * BIOCSETF Set ethernet read filter. | |
553 | * BIOCFLUSH Flush read packet buffer. | |
554 | * BIOCPROMISC Put interface into promiscuous mode. | |
555 | * BIOCGDLT Get link layer type. | |
556 | * BIOCGETIF Get interface name. | |
557 | * BIOCSETIF Set interface. | |
558 | * BIOCSRTIMEOUT Set read timeout. | |
559 | * BIOCGRTIMEOUT Get read timeout. | |
560 | * BIOCGSTATS Get packet stats. | |
561 | * BIOCIMMEDIATE Set immediate mode. | |
562 | * BIOCVERSION Get filter language version. | |
563 | */ | |
564 | /* ARGSUSED */ | |
565 | int | |
566 | bpfioctl(dev, cmd, addr, flag) | |
567 | dev_t dev; | |
568 | int cmd; | |
569 | caddr_t addr; | |
570 | int flag; | |
571 | { | |
572 | register struct bpf_d *d = &bpf_dtab[minor(dev)]; | |
573 | int s, error = 0; | |
574 | ||
575 | switch (cmd) { | |
576 | ||
577 | default: | |
578 | error = EINVAL; | |
579 | break; | |
580 | ||
581 | /* | |
582 | * Check for read packet available. | |
583 | */ | |
584 | case FIONREAD: | |
585 | { | |
586 | int n; | |
587 | ||
588 | s = splimp(); | |
589 | n = d->bd_slen; | |
590 | if (d->bd_hbuf) | |
591 | n += d->bd_hlen; | |
592 | splx(s); | |
593 | ||
594 | *(int *)addr = n; | |
595 | break; | |
596 | } | |
597 | ||
598 | case SIOCGIFADDR: | |
599 | { | |
600 | struct ifnet *ifp; | |
601 | ||
602 | if (d->bd_bif == 0) | |
603 | error = EINVAL; | |
604 | else { | |
605 | ifp = d->bd_bif->bif_ifp; | |
606 | error = (*ifp->if_ioctl)(ifp, cmd, addr); | |
607 | } | |
608 | break; | |
609 | } | |
610 | ||
611 | /* | |
612 | * Get buffer len [for read()]. | |
613 | */ | |
614 | case BIOCGBLEN: | |
615 | *(u_int *)addr = d->bd_bufsize; | |
616 | break; | |
617 | ||
618 | /* | |
619 | * Set buffer length. | |
620 | */ | |
621 | case BIOCSBLEN: | |
622 | #if BSD < 199103 | |
623 | error = EINVAL; | |
624 | #else | |
625 | if (d->bd_bif != 0) | |
626 | error = EINVAL; | |
627 | else { | |
628 | register u_int size = *(u_int *)addr; | |
629 | ||
630 | if (size > BPF_MAXBUFSIZE) | |
631 | *(u_int *)addr = size = BPF_MAXBUFSIZE; | |
632 | else if (size < BPF_MINBUFSIZE) | |
633 | *(u_int *)addr = size = BPF_MINBUFSIZE; | |
634 | d->bd_bufsize = size; | |
635 | } | |
636 | #endif | |
637 | break; | |
638 | ||
639 | /* | |
640 | * Set link layer read filter. | |
641 | */ | |
642 | case BIOCSETF: | |
643 | error = bpf_setf(d, (struct bpf_program *)addr); | |
644 | break; | |
645 | ||
646 | /* | |
647 | * Flush read packet buffer. | |
648 | */ | |
649 | case BIOCFLUSH: | |
650 | s = splimp(); | |
651 | reset_d(d); | |
652 | splx(s); | |
653 | break; | |
654 | ||
655 | /* | |
656 | * Put interface into promiscuous mode. | |
657 | */ | |
658 | case BIOCPROMISC: | |
659 | if (d->bd_bif == 0) { | |
660 | /* | |
661 | * No interface attached yet. | |
662 | */ | |
663 | error = EINVAL; | |
664 | break; | |
665 | } | |
666 | s = splimp(); | |
667 | if (d->bd_promisc == 0) { | |
668 | error = ifpromisc(d->bd_bif->bif_ifp, 1); | |
669 | if (error == 0) | |
670 | d->bd_promisc = 1; | |
671 | } | |
672 | splx(s); | |
673 | break; | |
674 | ||
675 | /* | |
676 | * Get device parameters. | |
677 | */ | |
678 | case BIOCGDLT: | |
679 | if (d->bd_bif == 0) | |
680 | error = EINVAL; | |
681 | else | |
682 | *(u_int *)addr = d->bd_bif->bif_dlt; | |
683 | break; | |
684 | ||
685 | /* | |
686 | * Set interface name. | |
687 | */ | |
688 | case BIOCGETIF: | |
689 | if (d->bd_bif == 0) | |
690 | error = EINVAL; | |
691 | else | |
692 | bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr); | |
693 | break; | |
694 | ||
695 | /* | |
696 | * Set interface. | |
697 | */ | |
698 | case BIOCSETIF: | |
699 | error = bpf_setif(d, (struct ifreq *)addr); | |
700 | break; | |
701 | ||
702 | /* | |
703 | * Set read timeout. | |
704 | */ | |
705 | case BIOCSRTIMEOUT: | |
706 | { | |
707 | struct timeval *tv = (struct timeval *)addr; | |
708 | u_long msec; | |
709 | ||
710 | /* Compute number of milliseconds. */ | |
711 | msec = tv->tv_sec * 1000 + tv->tv_usec / 1000; | |
712 | /* Scale milliseconds to ticks. Assume hard | |
713 | clock has millisecond or greater resolution | |
714 | (i.e. tick >= 1000). For 10ms hardclock, | |
715 | tick/1000 = 10, so rtout<-msec/10. */ | |
716 | d->bd_rtout = msec / (tick / 1000); | |
717 | break; | |
718 | } | |
719 | ||
720 | /* | |
721 | * Get read timeout. | |
722 | */ | |
723 | case BIOCGRTIMEOUT: | |
724 | { | |
725 | struct timeval *tv = (struct timeval *)addr; | |
726 | u_long msec = d->bd_rtout; | |
727 | ||
728 | msec *= tick / 1000; | |
729 | tv->tv_sec = msec / 1000; | |
730 | tv->tv_usec = msec % 1000; | |
731 | break; | |
732 | } | |
733 | ||
734 | /* | |
735 | * Get packet stats. | |
736 | */ | |
737 | case BIOCGSTATS: | |
738 | { | |
739 | struct bpf_stat *bs = (struct bpf_stat *)addr; | |
740 | ||
741 | bs->bs_recv = d->bd_rcount; | |
742 | bs->bs_drop = d->bd_dcount; | |
743 | break; | |
744 | } | |
745 | ||
746 | /* | |
747 | * Set immediate mode. | |
748 | */ | |
749 | case BIOCIMMEDIATE: | |
750 | d->bd_immediate = *(u_int *)addr; | |
751 | break; | |
752 | ||
753 | case BIOCVERSION: | |
754 | { | |
755 | struct bpf_version *bv = (struct bpf_version *)addr; | |
756 | ||
757 | bv->bv_major = BPF_MAJOR_VERSION; | |
758 | bv->bv_minor = BPF_MINOR_VERSION; | |
759 | break; | |
760 | } | |
761 | } | |
762 | return (error); | |
763 | } | |
764 | ||
765 | /* | |
766 | * Set d's packet filter program to fp. If this file already has a filter, | |
767 | * free it and replace it. Returns EINVAL for bogus requests. | |
768 | */ | |
769 | int | |
770 | bpf_setf(d, fp) | |
771 | struct bpf_d *d; | |
772 | struct bpf_program *fp; | |
773 | { | |
774 | struct bpf_insn *fcode, *old; | |
775 | u_int flen, size; | |
776 | int s; | |
777 | ||
778 | old = d->bd_filter; | |
779 | if (fp->bf_insns == 0) { | |
780 | if (fp->bf_len != 0) | |
781 | return (EINVAL); | |
782 | s = splimp(); | |
783 | d->bd_filter = 0; | |
784 | reset_d(d); | |
785 | splx(s); | |
786 | if (old != 0) | |
787 | free((caddr_t)old, M_DEVBUF); | |
788 | return (0); | |
789 | } | |
790 | flen = fp->bf_len; | |
791 | if (flen > BPF_MAXINSNS) | |
792 | return (EINVAL); | |
793 | ||
794 | size = flen * sizeof(*fp->bf_insns); | |
795 | fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK); | |
796 | if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 && | |
797 | bpf_validate(fcode, (int)flen)) { | |
798 | s = splimp(); | |
799 | d->bd_filter = fcode; | |
800 | reset_d(d); | |
801 | splx(s); | |
802 | if (old != 0) | |
803 | free((caddr_t)old, M_DEVBUF); | |
804 | ||
805 | return (0); | |
806 | } | |
807 | free((caddr_t)fcode, M_DEVBUF); | |
808 | return (EINVAL); | |
809 | } | |
810 | ||
811 | /* | |
812 | * Detach a file from its current interface (if attached at all) and attach | |
813 | * to the interface indicated by the name stored in ifr. | |
814 | * Return an errno or 0. | |
815 | */ | |
816 | static int | |
817 | bpf_setif(d, ifr) | |
818 | struct bpf_d *d; | |
819 | struct ifreq *ifr; | |
820 | { | |
821 | struct bpf_if *bp; | |
822 | char *cp; | |
823 | int unit, s, error; | |
824 | ||
825 | /* | |
826 | * Separate string into name part and unit number. Put a null | |
827 | * byte at the end of the name part, and compute the number. | |
828 | * If the a unit number is unspecified, the default is 0, | |
829 | * as initialized above. XXX This should be common code. | |
830 | */ | |
831 | unit = 0; | |
832 | cp = ifr->ifr_name; | |
833 | cp[sizeof(ifr->ifr_name) - 1] = '\0'; | |
834 | while (*cp++) { | |
835 | if (*cp >= '0' && *cp <= '9') { | |
836 | unit = *cp - '0'; | |
837 | *cp++ = '\0'; | |
838 | while (*cp) | |
839 | unit = 10 * unit + *cp++ - '0'; | |
840 | break; | |
841 | } | |
842 | } | |
843 | /* | |
844 | * Look through attached interfaces for the named one. | |
845 | */ | |
846 | for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) { | |
847 | struct ifnet *ifp = bp->bif_ifp; | |
848 | ||
849 | if (ifp == 0 || unit != ifp->if_unit | |
850 | || strcmp(ifp->if_name, ifr->ifr_name) != 0) | |
851 | continue; | |
852 | /* | |
853 | * We found the requested interface. | |
854 | * If it's not up, return an error. | |
855 | * Allocate the packet buffers if we need to. | |
856 | * If we're already attached to requested interface, | |
857 | * just flush the buffer. | |
858 | */ | |
859 | if ((ifp->if_flags & IFF_UP) == 0) | |
860 | return (ENETDOWN); | |
861 | ||
862 | if (d->bd_sbuf == 0) { | |
863 | error = bpf_allocbufs(d); | |
864 | if (error != 0) | |
865 | return (error); | |
866 | } | |
867 | s = splimp(); | |
868 | if (bp != d->bd_bif) { | |
869 | if (d->bd_bif) | |
870 | /* | |
871 | * Detach if attached to something else. | |
872 | */ | |
873 | bpf_detachd(d); | |
874 | ||
875 | bpf_attachd(d, bp); | |
876 | } | |
877 | reset_d(d); | |
878 | splx(s); | |
879 | return (0); | |
880 | } | |
881 | /* Not found. */ | |
882 | return (ENXIO); | |
883 | } | |
884 | ||
885 | /* | |
886 | * Convert an interface name plus unit number of an ifp to a single | |
887 | * name which is returned in the ifr. | |
888 | */ | |
889 | static void | |
890 | bpf_ifname(ifp, ifr) | |
891 | struct ifnet *ifp; | |
892 | struct ifreq *ifr; | |
893 | { | |
894 | char *s = ifp->if_name; | |
895 | char *d = ifr->ifr_name; | |
896 | ||
897 | while (*d++ = *s++) | |
898 | continue; | |
899 | /* XXX Assume that unit number is less than 10. */ | |
900 | *d++ = ifp->if_unit + '0'; | |
901 | *d = '\0'; | |
902 | } | |
903 | ||
904 | /* | |
905 | * The new select interface passes down the proc pointer; the old select | |
906 | * stubs had to grab it out of the user struct. This glue allows either case. | |
907 | */ | |
908 | #if BSD >= 199103 | |
909 | #define bpf_select bpfselect | |
910 | #else | |
911 | int | |
912 | bpfselect(dev, rw) | |
913 | register dev_t dev; | |
914 | int rw; | |
915 | { | |
916 | return (bpf_select(dev, rw, u.u_procp)); | |
917 | } | |
918 | #endif | |
919 | ||
920 | /* | |
921 | * Support for select() system call | |
922 | * Inspired by the code in tty.c for the same purpose. | |
923 | * | |
924 | * Return true iff the specific operation will not block indefinitely. | |
925 | * Otherwise, return false but make a note that a selwakeup() must be done. | |
926 | */ | |
927 | int | |
928 | bpf_select(dev, rw, p) | |
929 | register dev_t dev; | |
930 | int rw; | |
931 | struct proc *p; | |
932 | { | |
933 | register struct bpf_d *d; | |
934 | register int s; | |
935 | ||
936 | if (rw != FREAD) | |
937 | return (0); | |
938 | /* | |
939 | * An imitation of the FIONREAD ioctl code. | |
940 | */ | |
941 | d = &bpf_dtab[minor(dev)]; | |
942 | ||
943 | s = splimp(); | |
944 | if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) { | |
945 | /* | |
946 | * There is data waiting. | |
947 | */ | |
948 | splx(s); | |
949 | return (1); | |
950 | } | |
951 | #if 0 | |
952 | selrecord(p, &d->bd_sel); | |
953 | #else | |
954 | /* | |
955 | * No data ready. If there's already a select() waiting on this | |
956 | * minor device then this is a collision. This shouldn't happen | |
957 | * because minors really should not be shared, but if a process | |
958 | * forks while one of these is open, it is possible that both | |
959 | * processes could select on the same descriptor. | |
960 | */ | |
961 | if (d->bd_selproc && d->bd_selproc->p_wchan == (caddr_t)&selwait) | |
962 | d->bd_selcoll = 1; | |
963 | else | |
964 | d->bd_selproc = p; | |
965 | #endif | |
966 | splx(s); | |
967 | return (0); | |
968 | } | |
969 | ||
970 | /* | |
971 | * Incoming linkage from device drivers. Process the packet pkt, of length | |
972 | * pktlen, which is stored in a contiguous buffer. The packet is parsed | |
973 | * by each process' filter, and if accepted, stashed into the corresponding | |
974 | * buffer. | |
975 | */ | |
976 | void | |
977 | bpf_tap(arg, pkt, pktlen) | |
978 | caddr_t arg; | |
979 | register u_char *pkt; | |
980 | register u_int pktlen; | |
981 | { | |
982 | struct bpf_if *bp; | |
983 | register struct bpf_d *d; | |
984 | register u_int slen; | |
985 | /* | |
986 | * Note that the ipl does not have to be raised at this point. | |
987 | * The only problem that could arise here is that if two different | |
988 | * interfaces shared any data. This is not the case. | |
989 | */ | |
990 | bp = (struct bpf_if *)arg; | |
991 | for (d = bp->bif_dlist; d != 0; d = d->bd_next) { | |
992 | ++d->bd_rcount; | |
993 | slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen); | |
994 | if (slen != 0) | |
995 | catchpacket(d, pkt, pktlen, slen, bcopy); | |
996 | } | |
997 | } | |
998 | ||
999 | /* | |
1000 | * Copy data from an mbuf chain into a buffer. This code is derived | |
1001 | * from m_copydata in sys/uipc_mbuf.c. | |
1002 | */ | |
1003 | static void | |
1004 | bpf_mcopy(src, dst, len) | |
1005 | u_char *src; | |
1006 | u_char *dst; | |
1007 | register int len; | |
1008 | { | |
1009 | register struct mbuf *m = (struct mbuf *)src; | |
1010 | register unsigned count; | |
1011 | ||
1012 | while (len > 0) { | |
1013 | if (m == 0) | |
1014 | panic("bpf_mcopy"); | |
1015 | count = MIN(m->m_len, len); | |
1016 | bcopy(mtod(m, caddr_t), (caddr_t)dst, count); | |
1017 | m = m->m_next; | |
1018 | dst += count; | |
1019 | len -= count; | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | /* | |
1024 | * Incoming linkage from device drivers, when packet is in an mbuf chain. | |
1025 | */ | |
1026 | void | |
1027 | bpf_mtap(arg, m) | |
1028 | caddr_t arg; | |
1029 | struct mbuf *m; | |
1030 | { | |
1031 | struct bpf_if *bp = (struct bpf_if *)arg; | |
1032 | struct bpf_d *d; | |
1033 | u_int pktlen, slen; | |
1034 | struct mbuf *m0; | |
1035 | ||
1036 | pktlen = 0; | |
1037 | for (m0 = m; m0 != 0; m0 = m0->m_next) | |
1038 | pktlen += m0->m_len; | |
1039 | ||
1040 | for (d = bp->bif_dlist; d != 0; d = d->bd_next) { | |
1041 | ++d->bd_rcount; | |
1042 | slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0); | |
1043 | if (slen != 0) | |
1044 | catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy); | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | /* | |
1049 | * Move the packet data from interface memory (pkt) into the | |
1050 | * store buffer. Return 1 if it's time to wakeup a listener (buffer full), | |
1051 | * otherwise 0. "copy" is the routine called to do the actual data | |
1052 | * transfer. bcopy is passed in to copy contiguous chunks, while | |
1053 | * bpf_mcopy is passed in to copy mbuf chains. In the latter case, | |
1054 | * pkt is really an mbuf. | |
1055 | */ | |
1056 | static void | |
1057 | catchpacket(d, pkt, pktlen, snaplen, cpfn) | |
1058 | register struct bpf_d *d; | |
1059 | register u_char *pkt; | |
1060 | register u_int pktlen, snaplen; | |
1061 | register void (*cpfn)(); | |
1062 | { | |
1063 | register struct bpf_hdr *hp; | |
1064 | register int totlen, curlen; | |
1065 | register int hdrlen = d->bd_bif->bif_hdrlen; | |
1066 | /* | |
1067 | * Figure out how many bytes to move. If the packet is | |
1068 | * greater or equal to the snapshot length, transfer that | |
1069 | * much. Otherwise, transfer the whole packet (unless | |
1070 | * we hit the buffer size limit). | |
1071 | */ | |
1072 | totlen = hdrlen + MIN(snaplen, pktlen); | |
1073 | if (totlen > d->bd_bufsize) | |
1074 | totlen = d->bd_bufsize; | |
1075 | ||
1076 | /* | |
1077 | * Round up the end of the previous packet to the next longword. | |
1078 | */ | |
1079 | curlen = BPF_WORDALIGN(d->bd_slen); | |
1080 | if (curlen + totlen > d->bd_bufsize) { | |
1081 | /* | |
1082 | * This packet will overflow the storage buffer. | |
1083 | * Rotate the buffers if we can, then wakeup any | |
1084 | * pending reads. | |
1085 | */ | |
1086 | if (d->bd_fbuf == 0) { | |
1087 | /* | |
1088 | * We haven't completed the previous read yet, | |
1089 | * so drop the packet. | |
1090 | */ | |
1091 | ++d->bd_dcount; | |
1092 | return; | |
1093 | } | |
1094 | ROTATE_BUFFERS(d); | |
1095 | bpf_wakeup(d); | |
1096 | curlen = 0; | |
1097 | } | |
1098 | else if (d->bd_immediate) | |
1099 | /* | |
1100 | * Immediate mode is set. A packet arrived so any | |
1101 | * reads should be woken up. | |
1102 | */ | |
1103 | bpf_wakeup(d); | |
1104 | ||
1105 | /* | |
1106 | * Append the bpf header. | |
1107 | */ | |
1108 | hp = (struct bpf_hdr *)(d->bd_sbuf + curlen); | |
1109 | #if BSD >= 199103 | |
1110 | microtime(&hp->bh_tstamp); | |
1111 | #elif defined(sun) | |
1112 | uniqtime(&hp->bh_tstamp); | |
1113 | #else | |
1114 | hp->bh_tstamp = time; | |
1115 | #endif | |
1116 | hp->bh_datalen = pktlen; | |
1117 | hp->bh_hdrlen = hdrlen; | |
1118 | /* | |
1119 | * Copy the packet data into the store buffer and update its length. | |
1120 | */ | |
1121 | (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen)); | |
1122 | d->bd_slen = curlen + totlen; | |
1123 | } | |
1124 | ||
1125 | /* | |
1126 | * Initialize all nonzero fields of a descriptor. | |
1127 | */ | |
1128 | static int | |
1129 | bpf_allocbufs(d) | |
1130 | register struct bpf_d *d; | |
1131 | { | |
1132 | d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK); | |
1133 | if (d->bd_fbuf == 0) | |
1134 | return (ENOBUFS); | |
1135 | ||
1136 | d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK); | |
1137 | if (d->bd_sbuf == 0) { | |
1138 | free(d->bd_fbuf, M_DEVBUF); | |
1139 | return (ENOBUFS); | |
1140 | } | |
1141 | d->bd_slen = 0; | |
1142 | d->bd_hlen = 0; | |
1143 | return (0); | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * Free buffers currently in use by a descriptor. | |
1148 | * Called on close. | |
1149 | */ | |
1150 | static void | |
1151 | bpf_freed(d) | |
1152 | register struct bpf_d *d; | |
1153 | { | |
1154 | /* | |
1155 | * We don't need to lock out interrupts since this descriptor has | |
1156 | * been detached from its interface and it yet hasn't been marked | |
1157 | * free. | |
1158 | */ | |
1159 | if (d->bd_sbuf != 0) { | |
1160 | free(d->bd_sbuf, M_DEVBUF); | |
1161 | if (d->bd_hbuf != 0) | |
1162 | free(d->bd_hbuf, M_DEVBUF); | |
1163 | if (d->bd_fbuf != 0) | |
1164 | free(d->bd_fbuf, M_DEVBUF); | |
1165 | } | |
1166 | if (d->bd_filter) | |
1167 | free((caddr_t)d->bd_filter, M_DEVBUF); | |
1168 | ||
1169 | D_MARKFREE(d); | |
1170 | } | |
1171 | ||
1172 | /* | |
1173 | * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *) | |
1174 | * in the driver's softc; dlt is the link layer type; hdrlen is the fixed | |
1175 | * size of the link header (variable length headers not yet supported). | |
1176 | */ | |
1177 | void | |
1178 | bpfattach(driverp, ifp, dlt, hdrlen) | |
1179 | caddr_t *driverp; | |
1180 | struct ifnet *ifp; | |
1181 | u_int dlt, hdrlen; | |
1182 | { | |
1183 | struct bpf_if *bp; | |
1184 | int i; | |
1185 | #if BSD < 199103 | |
1186 | static struct bpf_if bpf_ifs[NBPFILTER]; | |
1187 | static int bpfifno; | |
1188 | ||
1189 | bp = (bpfifno < NBPFILTER) ? &bpf_ifs[bpfifno++] : 0; | |
1190 | #else | |
1191 | bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT); | |
1192 | #endif | |
1193 | if (bp == 0) | |
1194 | panic("bpfattach"); | |
1195 | ||
1196 | bp->bif_dlist = 0; | |
1197 | bp->bif_driverp = (struct bpf_if **)driverp; | |
1198 | bp->bif_ifp = ifp; | |
1199 | bp->bif_dlt = dlt; | |
1200 | ||
1201 | bp->bif_next = bpf_iflist; | |
1202 | bpf_iflist = bp; | |
1203 | ||
1204 | *bp->bif_driverp = 0; | |
1205 | ||
1206 | /* | |
1207 | * Compute the length of the bpf header. This is not necessarily | |
1208 | * equal to SIZEOF_BPF_HDR because we want to insert spacing such | |
1209 | * that the network layer header begins on a longword boundary (for | |
1210 | * performance reasons and to alleviate alignment restrictions). | |
1211 | */ | |
1212 | bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; | |
1213 | ||
1214 | /* | |
1215 | * Mark all the descriptors free if this hasn't been done. | |
1216 | */ | |
1217 | if (!D_ISFREE(&bpf_dtab[0])) | |
1218 | for (i = 0; i < NBPFILTER; ++i) | |
1219 | D_MARKFREE(&bpf_dtab[i]); | |
1220 | ||
1221 | printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit); | |
1222 | } | |
1223 | ||
1224 | #if BSD >= 199103 | |
1225 | /* XXX This routine belongs in net/if.c. */ | |
1226 | /* | |
1227 | * Set/clear promiscuous mode on interface ifp based on the truth value | |
1228 | * of pswitch. The calls are reference counted so that only the first | |
1229 | * "on" request actually has an effect, as does the final "off" request. | |
1230 | * Results are undefined if the "off" and "on" requests are not matched. | |
1231 | */ | |
1232 | int | |
1233 | ifpromisc(ifp, pswitch) | |
1234 | struct ifnet *ifp; | |
1235 | int pswitch; | |
1236 | { | |
1237 | struct ifreq ifr; | |
1238 | /* | |
1239 | * If the device is not configured up, we cannot put it in | |
1240 | * promiscuous mode. | |
1241 | */ | |
1242 | if ((ifp->if_flags & IFF_UP) == 0) | |
1243 | return (ENETDOWN); | |
1244 | ||
1245 | if (pswitch) { | |
1246 | if (ifp->if_pcount++ != 0) | |
1247 | return (0); | |
1248 | ifp->if_flags |= IFF_PROMISC; | |
1249 | } else { | |
1250 | if (--ifp->if_pcount > 0) | |
1251 | return (0); | |
1252 | ifp->if_flags &= ~IFF_PROMISC; | |
1253 | } | |
1254 | ifr.ifr_flags = ifp->if_flags; | |
1255 | return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr)); | |
1256 | } | |
1257 | #endif | |
1258 | ||
1259 | #if BSD < 199103 | |
1260 | /* | |
1261 | * Allocate some memory for bpf. This is temporary SunOS support, and | |
1262 | * is admittedly a hack. | |
1263 | * If resources unavaiable, return 0. | |
1264 | */ | |
1265 | static caddr_t | |
1266 | bpf_alloc(size, canwait) | |
1267 | register int size; | |
1268 | register int canwait; | |
1269 | { | |
1270 | register struct mbuf *m; | |
1271 | ||
1272 | if ((unsigned)size > (MCLBYTES-8)) | |
1273 | return 0; | |
1274 | ||
1275 | MGET(m, canwait, MT_DATA); | |
1276 | if (m == 0) | |
1277 | return 0; | |
1278 | if ((unsigned)size > (MLEN-8)) { | |
1279 | MCLGET(m); | |
1280 | if (m->m_len != MCLBYTES) { | |
1281 | m_freem(m); | |
1282 | return 0; | |
1283 | } | |
1284 | } | |
1285 | *mtod(m, struct mbuf **) = m; | |
1286 | return mtod(m, caddr_t) + 8; | |
1287 | } | |
1288 | #endif | |
1289 | #endif |