1*11be35a1SLionel Sambuc /* $NetBSD: t_bpf.c,v 1.5 2012/08/14 19:40:30 alnsn Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5*11be35a1SLionel Sambuc *
6*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc * are met:
9*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc *
15*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16*11be35a1SLionel Sambuc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*11be35a1SLionel Sambuc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18*11be35a1SLionel Sambuc * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*11be35a1SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*11be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21*11be35a1SLionel Sambuc * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*11be35a1SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*11be35a1SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*11be35a1SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*11be35a1SLionel Sambuc * SUCH DAMAGE.
26*11be35a1SLionel Sambuc */
27*11be35a1SLionel Sambuc #include <sys/cdefs.h>
28*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_bpf.c,v 1.5 2012/08/14 19:40:30 alnsn Exp $");
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc #include <sys/param.h>
31*11be35a1SLionel Sambuc #include <sys/ioctl.h>
32*11be35a1SLionel Sambuc #include <sys/socket.h>
33*11be35a1SLionel Sambuc #include <sys/mbuf.h>
34*11be35a1SLionel Sambuc #include <sys/sysctl.h>
35*11be35a1SLionel Sambuc #include <sys/mman.h>
36*11be35a1SLionel Sambuc #include <unistd.h>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <net/if.h>
39*11be35a1SLionel Sambuc #include <net/bpf.h>
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc #include <fcntl.h>
42*11be35a1SLionel Sambuc #include <stdio.h>
43*11be35a1SLionel Sambuc #include <string.h>
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc #include <rump/rump.h>
46*11be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
47*11be35a1SLionel Sambuc
48*11be35a1SLionel Sambuc /* XXX: atf-c.h has collisions with mbuf */
49*11be35a1SLionel Sambuc #undef m_type
50*11be35a1SLionel Sambuc #undef m_data
51*11be35a1SLionel Sambuc #include <atf-c.h>
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc #include "../../h_macros.h"
54*11be35a1SLionel Sambuc #include "../config/netconfig.c"
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc ATF_TC(bpfwriteleak);
ATF_TC_HEAD(bpfwriteleak,tc)57*11be35a1SLionel Sambuc ATF_TC_HEAD(bpfwriteleak, tc)
58*11be35a1SLionel Sambuc {
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that writing to /dev/bpf "
61*11be35a1SLionel Sambuc "does not leak mbufs");
62*11be35a1SLionel Sambuc }
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc static int
getmtdata(void)65*11be35a1SLionel Sambuc getmtdata(void)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc struct mbstat mbstat;
68*11be35a1SLionel Sambuc size_t mbstatlen = sizeof(mbstat);
69*11be35a1SLionel Sambuc const int mbstat_mib[] = { CTL_KERN, KERN_MBUF, MBUF_STATS };
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc RL(rump_sys___sysctl(mbstat_mib, __arraycount(mbstat_mib),
72*11be35a1SLionel Sambuc &mbstat, &mbstatlen, NULL, 0));
73*11be35a1SLionel Sambuc return mbstat.m_mtypes[MT_DATA];
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc
ATF_TC_BODY(bpfwriteleak,tc)76*11be35a1SLionel Sambuc ATF_TC_BODY(bpfwriteleak, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc char buf[28]; /* sizeof(garbage) > etherhdrlen */
79*11be35a1SLionel Sambuc struct ifreq ifr;
80*11be35a1SLionel Sambuc int ifnum, bpfd;
81*11be35a1SLionel Sambuc
82*11be35a1SLionel Sambuc RZ(rump_init());
83*11be35a1SLionel Sambuc RZ(rump_pub_shmif_create(NULL, &ifnum));
84*11be35a1SLionel Sambuc sprintf(ifr.ifr_name, "shmif%d", ifnum);
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
87*11be35a1SLionel Sambuc RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
88*11be35a1SLionel Sambuc RL(rump_sys_ioctl(bpfd, BIOCSFEEDBACK, &ifr));
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc if (getmtdata() != 0)
91*11be35a1SLionel Sambuc atf_tc_fail("test precondition failed: MT_DATA mbufs != 0");
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(ENETDOWN, rump_sys_write(bpfd, buf, sizeof(buf))==-1);
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(getmtdata(), 0);
96*11be35a1SLionel Sambuc }
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc #if (SIZE_MAX > UINT_MAX)
99*11be35a1SLionel Sambuc ATF_TC(bpfwritetrunc);
ATF_TC_HEAD(bpfwritetrunc,tc)100*11be35a1SLionel Sambuc ATF_TC_HEAD(bpfwritetrunc, tc)
101*11be35a1SLionel Sambuc {
102*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that write to /dev/bpf "
103*11be35a1SLionel Sambuc "does not truncate size_t to int");
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc
ATF_TC_BODY(bpfwritetrunc,tc)106*11be35a1SLionel Sambuc ATF_TC_BODY(bpfwritetrunc, tc)
107*11be35a1SLionel Sambuc {
108*11be35a1SLionel Sambuc int bpfd;
109*11be35a1SLionel Sambuc struct ifreq ifr;
110*11be35a1SLionel Sambuc struct iovec *iov;
111*11be35a1SLionel Sambuc size_t iovlen, sz;
112*11be35a1SLionel Sambuc const size_t extra_bytes = 28;
113*11be35a1SLionel Sambuc const size_t total = extra_bytes + UINT_MAX + 1;
114*11be35a1SLionel Sambuc long iov_max, vm_page_size; /* round_page wants vm_page_size variable */
115*11be35a1SLionel Sambuc
116*11be35a1SLionel Sambuc memset(&ifr, 0, sizeof(ifr));
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc iov_max = sysconf(_SC_IOV_MAX);
119*11be35a1SLionel Sambuc vm_page_size = sysconf(_SC_PAGE_SIZE);
120*11be35a1SLionel Sambuc ATF_REQUIRE(iov_max > 1 && vm_page_size > 1);
121*11be35a1SLionel Sambuc
122*11be35a1SLionel Sambuc /*
123*11be35a1SLionel Sambuc * Minimize memory consumption by using many iovecs
124*11be35a1SLionel Sambuc * all pointing to one memory region.
125*11be35a1SLionel Sambuc */
126*11be35a1SLionel Sambuc iov = calloc(iov_max, sizeof(struct iovec));
127*11be35a1SLionel Sambuc ATF_REQUIRE(iov != NULL);
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc sz = round_page((total + (iov_max - 1)) / iov_max);
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc iov[0].iov_len = sz;
132*11be35a1SLionel Sambuc iov[0].iov_base = mmap(NULL, sz, PROT_READ, MAP_ANON, -1, 0);
133*11be35a1SLionel Sambuc ATF_REQUIRE(iov[0].iov_base != MAP_FAILED);
134*11be35a1SLionel Sambuc
135*11be35a1SLionel Sambuc iovlen = 1;
136*11be35a1SLionel Sambuc while (sz + iov[0].iov_len <= total)
137*11be35a1SLionel Sambuc {
138*11be35a1SLionel Sambuc iov[iovlen].iov_len = iov[0].iov_len;
139*11be35a1SLionel Sambuc iov[iovlen].iov_base = iov[0].iov_base;
140*11be35a1SLionel Sambuc sz += iov[0].iov_len;
141*11be35a1SLionel Sambuc iovlen++;
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc if (sz < total)
145*11be35a1SLionel Sambuc {
146*11be35a1SLionel Sambuc iov[iovlen].iov_len = total - sz;
147*11be35a1SLionel Sambuc iov[iovlen].iov_base = iov[0].iov_base;
148*11be35a1SLionel Sambuc iovlen++;
149*11be35a1SLionel Sambuc }
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc /* Sanity checks */
152*11be35a1SLionel Sambuc ATF_REQUIRE(iovlen >= 1 && iovlen <= (size_t)iov_max);
153*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(iov[iovlen-1].iov_len, total % iov[0].iov_len);
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc RZ(rump_init());
156*11be35a1SLionel Sambuc netcfg_rump_makeshmif("bpfwritetrunc", ifr.ifr_name);
157*11be35a1SLionel Sambuc netcfg_rump_if(ifr.ifr_name, "10.1.1.1", "255.0.0.0");
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
160*11be35a1SLionel Sambuc RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambuc ATF_CHECK_ERRNO(EMSGSIZE, rump_sys_writev(bpfd, iov, iovlen) == -1);
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc munmap(iov[0].iov_base, iov[0].iov_len);
165*11be35a1SLionel Sambuc free(iov);
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc #endif /* #if (SIZE_MAX > UINT_MAX) */
168*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)169*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
170*11be35a1SLionel Sambuc {
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bpfwriteleak);
173*11be35a1SLionel Sambuc #if (SIZE_MAX > UINT_MAX)
174*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bpfwritetrunc);
175*11be35a1SLionel Sambuc #endif
176*11be35a1SLionel Sambuc return atf_no_error();
177*11be35a1SLionel Sambuc }
178