2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/module.h>
31 #include <sys/socket.h>
33 #include <netinet/in.h>
43 #define ACCF_NAME "dataready"
46 * A number of small tests to confirm that attaching ACCF_DATA accept filters
47 * to inet4 ports works as expected. We test:
49 * - That no accept filter is attached on a newly created socket.
50 * - That bind() has no affect on the accept filter state.
51 * - That we can't attach an accept filter to a socket that isn't in the
53 * - That after we fail to attach the filter, querying the kernel shows no
55 * - That we can attach an accept filter to a socket that is in the listen
57 * - That once an accept filter is attached, we can query to make sure it is
59 * - That once an accept filter is attached, we can remove it and query to
60 * make sure it is removed.
65 struct accept_filter_arg afa;
66 struct sockaddr_in sin;
71 /* XXX: PLAIN_TEST_REQUIRE_MODULE "backport" for stable/9 */
72 const char *_mod_name = "accf_data";
74 if (modfind(_mod_name) == -1) {
75 printf("1..0 # SKIP - module %s could not be resolved: %s\n",
76 _mod_name, strerror(errno));
79 /* XXX: PLAIN_TEST_REQUIRE_MODULE for stable/9 */
84 * Step 0. Open socket().
86 lso = socket(PF_INET, SOCK_STREAM, 0);
88 errx(-1, "not ok 1 - socket: %s", strerror(errno));
89 printf("ok 1 - socket\n");
92 * Step 1. After socket(). Should return EINVAL, since no accept
93 * filter should be attached.
95 bzero(&afa, sizeof(afa));
97 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
99 errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
101 errx(-1, "not ok 2 - getsockopt() after socket() failed with "
102 "%d (%s)", errno, strerror(errno));
103 printf("ok 2 - getsockopt\n");
106 * Step 2. Bind(). Ideally this will succeed.
108 setsockopt(lso, SOL_SOCKET, SO_REUSEADDR, &lso, sizeof(lso));
109 bzero(&sin, sizeof(sin));
110 sin.sin_len = sizeof(sin);
111 sin.sin_family = AF_INET;
112 sin.sin_port = htons(8080);
113 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
114 if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)
115 errx(-1, "not ok 3 - bind %s", strerror(errno));
116 printf("ok 3 - bind\n");
119 * Step 3: After bind(). getsockopt() should return EINVAL, since no
120 * accept filter should be attached.
123 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
125 errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
127 errx(-1, "not ok 4 - getsockopt() after bind() failed with %d (%s)",
128 errno, strerror(errno));
129 printf("ok 4 - getsockopt\n");
132 * Step 4: Setsockopt() before listen(). Should fail, since it's not
133 * yet a listen() socket.
135 bzero(&afa, sizeof(afa));
136 strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
137 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
139 errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
140 printf("ok 5 - setsockopt\n");
143 * Step 5: Getsockopt() after pre-listen() setsockopt(). Should
144 * fail with EINVAL, since setsockopt() should have failed.
147 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
149 errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
152 errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
153 errno, strerror(errno));
154 printf("ok 6 - getsockopt\n");
159 if (listen(lso, -1) < 0)
160 errx(-1, "not ok 7 - listen: %s", strerror(errno));
161 printf("ok 7 - listen\n");
164 * Step 7: Getsockopt() after listen(). Should fail with EINVAL,
165 * since we have not installed accept filter yet.
168 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
170 errx(-1, "not ok 8 - getsockopt() after listen() but before "
171 "setsockopt() succeeded");
173 errx(-1, "not ok 8 - getsockopt() after listen() but before "
174 "setsockopt() failed with %d (%s)", errno, strerror(errno));
175 printf("ok 8 - getsockopt\n");
178 * Step 8: After listen(). This call to setsockopt() should succeed.
180 bzero(&afa, sizeof(afa));
181 strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
182 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
184 errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "
185 "(%s)", errno, strerror(errno));
186 printf("ok 9 - setsockopt\n");
189 * Step 9: After setsockopt(). Should succeed and identify
192 bzero(&afa, sizeof(afa));
194 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
196 errx(-1, "not ok 10 - getsockopt() after listen() setsockopt() "
197 "failed with %d (%s)", errno, strerror(errno));
198 if (len != sizeof(afa))
199 errx(-1, "not ok 10 - getsockopt() after setsockopet() after "
200 "listen() returned wrong size (got %d expected %zd)", len,
202 if (strcmp(afa.af_name, ACCF_NAME) != 0)
203 errx(-1, "not ok 10 - getsockopt() after setsockopt() after "
204 "listen() mismatch (got %s expected %s)", afa.af_name,
206 printf("ok 10 - getsockopt\n");
209 * Step 10: Set listening socket to non blocking mode. Open
210 * connection to our listening socket and try to accept. Should
211 * no succeed. Write a byte of data and try again. Should accept.
213 i = fcntl(lso, F_GETFL);
215 errx(-1, "not ok 11 - ioctl(F_GETFL): %s", strerror(errno));
217 if (fcntl(lso, F_SETFL, i) != 0)
218 errx(-1, "not ok 11 - ioctl(F_SETFL): %s", strerror(errno));
219 so = socket(PF_INET, SOCK_STREAM, 0);
221 errx(-1, "not ok 11 - socket: %s", strerror(errno));
222 if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
223 errx(-1, "not ok 11 - connect %s", strerror(errno));
224 if (accept(lso, NULL, 0) != -1 && errno != EWOULDBLOCK)
225 errx(-1, "not ok 11 - accept #1 %s", strerror(errno));
226 if (write(so, "0", 1) != 1)
227 errx(-1, "not ok 11 - write %s", strerror(errno));
229 * XXXGL: ugly, but we need to make sure that our write reaches
230 * remote side of the socket.
233 if (accept(lso, NULL, 0) < 1)
234 errx(-1, "not ok 11 - accept #2 %s", strerror(errno));
236 errx(-1, "not ok 11 - close(): %s", strerror(errno));
237 printf("ok 11 - accept\n");
240 * Step 12: reset connection before accept filter allows it.
241 * In this case the connection must make it to the listen
242 * queue, but with ECONNABORTED code.
244 so = socket(PF_INET, SOCK_STREAM, 0);
246 errx(-1, "not ok 12 - socket: %s", strerror(errno));
247 if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
248 errx(-1, "not ok 12 - connect %s", strerror(errno));
251 ret = setsockopt(so, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
253 errx(-1, "not ok 12 - setsockopt(SO_LINGER) failed with %d "
254 "(%s)", errno, strerror(errno));
256 errx(-1, "not ok 12 - close(): %s", strerror(errno));
257 if (accept(lso, NULL, 0) != -1 && errno != ECONNABORTED)
258 errx(-1, "not ok 12 - accept #3 %s", strerror(errno));
259 printf("ok 12 - accept\n");
263 * XXXGL: this doesn't belong to the test itself, but is known
264 * to examine rarely examined paths in the kernel. Intentionally
265 * leave a socket on the incomplete queue, before the program
268 so = socket(PF_INET, SOCK_STREAM, 0);
270 errx(-1, "not ok 13 - socket: %s", strerror(errno));
271 if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
272 errx(-1, "not ok 13 - connect %s", strerror(errno));
276 * Step 12: Remove accept filter. After removing the accept filter
277 * getsockopt() should fail with EINVAL.
279 ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
281 errx(-1, "not ok 13 - setsockopt() after listen() "
282 "failed with %d (%s)", errno, strerror(errno));
283 bzero(&afa, sizeof(afa));
285 ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
287 errx(-1, "not ok 13 - getsockopt() after removing "
288 "the accept filter returns valid accept filter %s",
291 errx(-1, "not ok 13 - getsockopt() after removing the accept"
292 "filter failed with %d (%s)", errno, strerror(errno));
294 errx(-1, "not ok 13 - close() of listening socket: %s",
296 printf("ok 13 - setsockopt\n");