bpf.c (159078) | bpf.c (159180) |
---|---|
1/*- 2 * Copyright (c) 1990, 1991, 1993 3 * The Regents of the University of California. 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. --- 19 unchanged lines hidden (view full) --- 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)bpf.c 8.4 (Berkeley) 1/9/95 35 * | 1/*- 2 * Copyright (c) 1990, 1991, 1993 3 * The Regents of the University of California. 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. --- 19 unchanged lines hidden (view full) --- 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)bpf.c 8.4 (Berkeley) 1/9/95 35 * |
36 * $FreeBSD: head/sys/net/bpf.c 159078 2006-05-30 19:24:01Z ru $ | 36 * $FreeBSD: head/sys/net/bpf.c 159180 2006-06-02 19:59:33Z csjp $ |
37 */ 38 39#include "opt_bpf.h" 40#include "opt_mac.h" 41#include "opt_netgraph.h" 42 43#include <sys/types.h> 44#include <sys/param.h> --- 251 unchanged lines hidden (view full) --- 296 * Finally, point the driver's bpf cookie at the interface so 297 * it will divert packets to bpf. 298 */ 299 BPFIF_LOCK(bp); 300 d->bd_bif = bp; 301 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 302 303 bpf_bpfd_cnt++; | 37 */ 38 39#include "opt_bpf.h" 40#include "opt_mac.h" 41#include "opt_netgraph.h" 42 43#include <sys/types.h> 44#include <sys/param.h> --- 251 unchanged lines hidden (view full) --- 296 * Finally, point the driver's bpf cookie at the interface so 297 * it will divert packets to bpf. 298 */ 299 BPFIF_LOCK(bp); 300 d->bd_bif = bp; 301 LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next); 302 303 bpf_bpfd_cnt++; |
304 *bp->bif_driverp = bp; | |
305 BPFIF_UNLOCK(bp); 306} 307 308/* 309 * Detach a file from its interface. 310 */ 311static void 312bpf_detachd(d) --- 9 unchanged lines hidden (view full) --- 322 ifp = d->bd_bif->bif_ifp; 323 324 /* 325 * Remove d from the interface's descriptor list. 326 */ 327 LIST_REMOVE(d, bd_next); 328 329 bpf_bpfd_cnt--; | 304 BPFIF_UNLOCK(bp); 305} 306 307/* 308 * Detach a file from its interface. 309 */ 310static void 311bpf_detachd(d) --- 9 unchanged lines hidden (view full) --- 321 ifp = d->bd_bif->bif_ifp; 322 323 /* 324 * Remove d from the interface's descriptor list. 325 */ 326 LIST_REMOVE(d, bd_next); 327 328 bpf_bpfd_cnt--; |
330 /* 331 * Let the driver know that there are no more listeners. 332 */ 333 if (LIST_EMPTY(&bp->bif_dlist)) 334 *bp->bif_driverp = NULL; 335 | |
336 d->bd_bif = NULL; 337 BPFD_UNLOCK(d); 338 BPFIF_UNLOCK(bp); 339 340 /* 341 * Check if this descriptor had requested promiscuous mode. 342 * If so, turn it off. 343 */ --- 757 unchanged lines hidden (view full) --- 1101 struct bpf_d *d; 1102 struct ifreq *ifr; 1103{ 1104 struct bpf_if *bp; 1105 int error; 1106 struct ifnet *theywant; 1107 1108 theywant = ifunit(ifr->ifr_name); | 329 d->bd_bif = NULL; 330 BPFD_UNLOCK(d); 331 BPFIF_UNLOCK(bp); 332 333 /* 334 * Check if this descriptor had requested promiscuous mode. 335 * If so, turn it off. 336 */ --- 757 unchanged lines hidden (view full) --- 1094 struct bpf_d *d; 1095 struct ifreq *ifr; 1096{ 1097 struct bpf_if *bp; 1098 int error; 1099 struct ifnet *theywant; 1100 1101 theywant = ifunit(ifr->ifr_name); |
1109 if (theywant == NULL) 1110 return ENXIO; | 1102 if (theywant == NULL || theywant->if_bpf == NULL) 1103 return (ENXIO); |
1111 | 1104 |
1105 bp = theywant->if_bpf; |
|
1112 /* | 1106 /* |
1113 * Look through attached interfaces for the named one. | 1107 * Allocate the packet buffers if we need to. 1108 * If we're already attached to requested interface, 1109 * just flush the buffer. |
1114 */ | 1110 */ |
1115 mtx_lock(&bpf_mtx); 1116 LIST_FOREACH(bp, &bpf_iflist, bif_next) { 1117 struct ifnet *ifp = bp->bif_ifp; | 1111 if (d->bd_sbuf == NULL) { 1112 error = bpf_allocbufs(d); 1113 if (error != 0) 1114 return (error); 1115 } 1116 if (bp != d->bd_bif) { 1117 if (d->bd_bif) 1118 /* 1119 * Detach if attached to something else. 1120 */ 1121 bpf_detachd(d); |
1118 | 1122 |
1119 if (ifp == NULL || ifp != theywant) 1120 continue; 1121 /* skip additional entry */ 1122 if (bp->bif_driverp != &ifp->if_bpf) 1123 continue; 1124 1125 mtx_unlock(&bpf_mtx); 1126 /* 1127 * We found the requested interface. 1128 * Allocate the packet buffers if we need to. 1129 * If we're already attached to requested interface, 1130 * just flush the buffer. 1131 */ 1132 if (d->bd_sbuf == NULL) { 1133 error = bpf_allocbufs(d); 1134 if (error != 0) 1135 return (error); 1136 } 1137 if (bp != d->bd_bif) { 1138 if (d->bd_bif) 1139 /* 1140 * Detach if attached to something else. 1141 */ 1142 bpf_detachd(d); 1143 1144 bpf_attachd(d, bp); 1145 } 1146 BPFD_LOCK(d); 1147 reset_d(d); 1148 BPFD_UNLOCK(d); 1149 return (0); | 1123 bpf_attachd(d, bp); |
1150 } | 1124 } |
1151 mtx_unlock(&bpf_mtx); 1152 /* Not found. */ 1153 return (ENXIO); | 1125 BPFD_LOCK(d); 1126 reset_d(d); 1127 BPFD_UNLOCK(d); 1128 return (0); |
1154} 1155 1156/* 1157 * Support for select() and poll() system calls 1158 * 1159 * Return true iff the specific operation will not block indefinitely. 1160 * Otherwise, return false but make a note that a selwakeup() must be done. 1161 */ --- 105 unchanged lines hidden (view full) --- 1267bpf_tap(bp, pkt, pktlen) 1268 struct bpf_if *bp; 1269 u_char *pkt; 1270 u_int pktlen; 1271{ 1272 struct bpf_d *d; 1273 u_int slen; 1274 | 1129} 1130 1131/* 1132 * Support for select() and poll() system calls 1133 * 1134 * Return true iff the specific operation will not block indefinitely. 1135 * Otherwise, return false but make a note that a selwakeup() must be done. 1136 */ --- 105 unchanged lines hidden (view full) --- 1242bpf_tap(bp, pkt, pktlen) 1243 struct bpf_if *bp; 1244 u_char *pkt; 1245 u_int pktlen; 1246{ 1247 struct bpf_d *d; 1248 u_int slen; 1249 |
1275 /* 1276 * Lockless read to avoid cost of locking the interface if there are 1277 * no descriptors attached. 1278 */ 1279 if (LIST_EMPTY(&bp->bif_dlist)) 1280 return; 1281 | |
1282 BPFIF_LOCK(bp); 1283 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1284 BPFD_LOCK(d); 1285 ++d->bd_rcount; 1286#ifdef BPF_JITTER 1287 if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL) 1288 slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen); 1289 else --- 44 unchanged lines hidden (view full) --- 1334void 1335bpf_mtap(bp, m) 1336 struct bpf_if *bp; 1337 struct mbuf *m; 1338{ 1339 struct bpf_d *d; 1340 u_int pktlen, slen; 1341 | 1250 BPFIF_LOCK(bp); 1251 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1252 BPFD_LOCK(d); 1253 ++d->bd_rcount; 1254#ifdef BPF_JITTER 1255 if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL) 1256 slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen); 1257 else --- 44 unchanged lines hidden (view full) --- 1302void 1303bpf_mtap(bp, m) 1304 struct bpf_if *bp; 1305 struct mbuf *m; 1306{ 1307 struct bpf_d *d; 1308 u_int pktlen, slen; 1309 |
1342 /* 1343 * Lockless read to avoid cost of locking the interface if there are 1344 * no descriptors attached. 1345 */ 1346 if (LIST_EMPTY(&bp->bif_dlist)) 1347 return; 1348 | |
1349 pktlen = m_length(m, NULL); 1350 1351 BPFIF_LOCK(bp); 1352 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1353 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL)) 1354 continue; 1355 BPFD_LOCK(d); 1356 ++d->bd_rcount; --- 29 unchanged lines hidden (view full) --- 1386 void *data; 1387 u_int dlen; 1388 struct mbuf *m; 1389{ 1390 struct mbuf mb; 1391 struct bpf_d *d; 1392 u_int pktlen, slen; 1393 | 1310 pktlen = m_length(m, NULL); 1311 1312 BPFIF_LOCK(bp); 1313 LIST_FOREACH(d, &bp->bif_dlist, bd_next) { 1314 if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL)) 1315 continue; 1316 BPFD_LOCK(d); 1317 ++d->bd_rcount; --- 29 unchanged lines hidden (view full) --- 1347 void *data; 1348 u_int dlen; 1349 struct mbuf *m; 1350{ 1351 struct mbuf mb; 1352 struct bpf_d *d; 1353 u_int pktlen, slen; 1354 |
1394 /* 1395 * Lockless read to avoid cost of locking the interface if there are 1396 * no descriptors attached. 1397 */ 1398 if (LIST_EMPTY(&bp->bif_dlist)) 1399 return; 1400 | |
1401 pktlen = m_length(m, NULL); 1402 /* 1403 * Craft on-stack mbuf suitable for passing to bpf_filter. 1404 * Note that we cut corners here; we only setup what's 1405 * absolutely needed--this mbuf should never go anywhere else. 1406 */ 1407 mb.m_next = m; 1408 mb.m_data = data; --- 175 unchanged lines hidden (view full) --- 1584 struct bpf_if **driverp; 1585{ 1586 struct bpf_if *bp; 1587 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 1588 if (bp == NULL) 1589 panic("bpfattach"); 1590 1591 LIST_INIT(&bp->bif_dlist); | 1355 pktlen = m_length(m, NULL); 1356 /* 1357 * Craft on-stack mbuf suitable for passing to bpf_filter. 1358 * Note that we cut corners here; we only setup what's 1359 * absolutely needed--this mbuf should never go anywhere else. 1360 */ 1361 mb.m_next = m; 1362 mb.m_data = data; --- 175 unchanged lines hidden (view full) --- 1538 struct bpf_if **driverp; 1539{ 1540 struct bpf_if *bp; 1541 bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO); 1542 if (bp == NULL) 1543 panic("bpfattach"); 1544 1545 LIST_INIT(&bp->bif_dlist); |
1592 bp->bif_driverp = driverp; | |
1593 bp->bif_ifp = ifp; 1594 bp->bif_dlt = dlt; 1595 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF); | 1546 bp->bif_ifp = ifp; 1547 bp->bif_dlt = dlt; 1548 mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF); |
1549 KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized")); 1550 *driverp = bp; |
|
1596 1597 mtx_lock(&bpf_mtx); 1598 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next); 1599 mtx_unlock(&bpf_mtx); 1600 | 1551 1552 mtx_lock(&bpf_mtx); 1553 LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next); 1554 mtx_unlock(&bpf_mtx); 1555 |
1601 *bp->bif_driverp = NULL; 1602 | |
1603 /* 1604 * Compute the length of the bpf header. This is not necessarily 1605 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1606 * that the network layer header begins on a longword boundary (for 1607 * performance reasons and to alleviate alignment restrictions). 1608 */ 1609 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1610 --- 294 unchanged lines hidden --- | 1556 /* 1557 * Compute the length of the bpf header. This is not necessarily 1558 * equal to SIZEOF_BPF_HDR because we want to insert spacing such 1559 * that the network layer header begins on a longword boundary (for 1560 * performance reasons and to alleviate alignment restrictions). 1561 */ 1562 bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen; 1563 --- 294 unchanged lines hidden --- |