143cb0b2bSRobert Watson /*-
243cb0b2bSRobert Watson * Copyright (c) 2004 Robert N. M. Watson
343cb0b2bSRobert Watson * All rights reserved.
443cb0b2bSRobert Watson *
543cb0b2bSRobert Watson * Redistribution and use in source and binary forms, with or without
643cb0b2bSRobert Watson * modification, are permitted provided that the following conditions
743cb0b2bSRobert Watson * are met:
843cb0b2bSRobert Watson * 1. Redistributions of source code must retain the above copyright
943cb0b2bSRobert Watson * notice, this list of conditions and the following disclaimer.
1043cb0b2bSRobert Watson * 2. Redistributions in binary form must reproduce the above copyright
1143cb0b2bSRobert Watson * notice, this list of conditions and the following disclaimer in the
1243cb0b2bSRobert Watson * documentation and/or other materials provided with the distribution.
1343cb0b2bSRobert Watson *
1443cb0b2bSRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1543cb0b2bSRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1643cb0b2bSRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1743cb0b2bSRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1843cb0b2bSRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1943cb0b2bSRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2043cb0b2bSRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2143cb0b2bSRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2243cb0b2bSRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2343cb0b2bSRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2443cb0b2bSRobert Watson * SUCH DAMAGE.
2543cb0b2bSRobert Watson */
2643cb0b2bSRobert Watson
2743cb0b2bSRobert Watson #include <sys/types.h>
280e47853eSEnji Cooper #include <sys/module.h>
2943cb0b2bSRobert Watson #include <sys/socket.h>
3043cb0b2bSRobert Watson
3143cb0b2bSRobert Watson #include <netinet/in.h>
3243cb0b2bSRobert Watson
3343cb0b2bSRobert Watson #include <err.h>
3443cb0b2bSRobert Watson #include <errno.h>
35f7ff0c66SGleb Smirnoff #include <fcntl.h>
3643cb0b2bSRobert Watson #include <stdio.h>
3743cb0b2bSRobert Watson #include <stdlib.h>
3843cb0b2bSRobert Watson #include <string.h>
3943cb0b2bSRobert Watson #include <unistd.h>
4043cb0b2bSRobert Watson
4143cb0b2bSRobert Watson #define ACCF_NAME "dataready"
4243cb0b2bSRobert Watson
4343cb0b2bSRobert Watson /*
4443cb0b2bSRobert Watson * A number of small tests to confirm that attaching ACCF_DATA accept filters
4543cb0b2bSRobert Watson * to inet4 ports works as expected. We test:
4643cb0b2bSRobert Watson *
4743cb0b2bSRobert Watson * - That no accept filter is attached on a newly created socket.
4843cb0b2bSRobert Watson * - That bind() has no affect on the accept filter state.
4943cb0b2bSRobert Watson * - That we can't attach an accept filter to a socket that isn't in the
5043cb0b2bSRobert Watson * listen state.
5143cb0b2bSRobert Watson * - That after we fail to attach the filter, querying the kernel shows no
5243cb0b2bSRobert Watson * filter attached.
5343cb0b2bSRobert Watson * - That we can attach an accept filter to a socket that is in the listen
5443cb0b2bSRobert Watson * state.
5543cb0b2bSRobert Watson * - That once an accept filter is attached, we can query to make sure it is
5643cb0b2bSRobert Watson * attached.
57922a5d9cSMaxim Konovalov * - That once an accept filter is attached, we can remove it and query to
58922a5d9cSMaxim Konovalov * make sure it is removed.
5943cb0b2bSRobert Watson */
6043cb0b2bSRobert Watson int
main(void)611aa40dadSEnji Cooper main(void)
6243cb0b2bSRobert Watson {
6343cb0b2bSRobert Watson struct accept_filter_arg afa;
6443cb0b2bSRobert Watson struct sockaddr_in sin;
65*a4658c80SGleb Smirnoff struct linger linger;
6643cb0b2bSRobert Watson socklen_t len;
67f7ff0c66SGleb Smirnoff int lso, so, i, ret;
6843cb0b2bSRobert Watson
690e47853eSEnji Cooper /* XXX: PLAIN_TEST_REQUIRE_MODULE "backport" for stable/9 */
700e47853eSEnji Cooper const char *_mod_name = "accf_data";
710e47853eSEnji Cooper
720e47853eSEnji Cooper if (modfind(_mod_name) == -1) {
730e47853eSEnji Cooper printf("1..0 # SKIP - module %s could not be resolved: %s\n",
740e47853eSEnji Cooper _mod_name, strerror(errno));
750e47853eSEnji Cooper _exit(0);
760e47853eSEnji Cooper }
770e47853eSEnji Cooper /* XXX: PLAIN_TEST_REQUIRE_MODULE for stable/9 */
780e47853eSEnji Cooper
79f7ff0c66SGleb Smirnoff printf("1..12\n");
8000e13b1dSNik Clayton
8143cb0b2bSRobert Watson /*
8243cb0b2bSRobert Watson * Step 0. Open socket().
8343cb0b2bSRobert Watson */
8443cb0b2bSRobert Watson lso = socket(PF_INET, SOCK_STREAM, 0);
8543cb0b2bSRobert Watson if (lso == -1)
8600e13b1dSNik Clayton errx(-1, "not ok 1 - socket: %s", strerror(errno));
8700e13b1dSNik Clayton printf("ok 1 - socket\n");
8843cb0b2bSRobert Watson
8943cb0b2bSRobert Watson /*
9043cb0b2bSRobert Watson * Step 1. After socket(). Should return EINVAL, since no accept
9143cb0b2bSRobert Watson * filter should be attached.
9243cb0b2bSRobert Watson */
9343cb0b2bSRobert Watson bzero(&afa, sizeof(afa));
9443cb0b2bSRobert Watson len = sizeof(afa);
9543cb0b2bSRobert Watson ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
9609745181SRobert Watson if (ret != -1)
9700e13b1dSNik Clayton errx(-1, "not ok 2 - getsockopt() after socket() succeeded");
9809745181SRobert Watson if (errno != EINVAL)
9900e13b1dSNik Clayton errx(-1, "not ok 2 - getsockopt() after socket() failed with "
10009745181SRobert Watson "%d (%s)", errno, strerror(errno));
10100e13b1dSNik Clayton printf("ok 2 - getsockopt\n");
10243cb0b2bSRobert Watson
10343cb0b2bSRobert Watson /*
10443cb0b2bSRobert Watson * Step 2. Bind(). Ideally this will succeed.
10543cb0b2bSRobert Watson */
106f7ff0c66SGleb Smirnoff setsockopt(lso, SOL_SOCKET, SO_REUSEADDR, &lso, sizeof(lso));
10743cb0b2bSRobert Watson bzero(&sin, sizeof(sin));
10843cb0b2bSRobert Watson sin.sin_len = sizeof(sin);
10943cb0b2bSRobert Watson sin.sin_family = AF_INET;
11043cb0b2bSRobert Watson sin.sin_port = htons(8080);
11143cb0b2bSRobert Watson sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
11243cb0b2bSRobert Watson if (bind(lso, (struct sockaddr *)&sin, sizeof(sin)) < 0)
11300e13b1dSNik Clayton errx(-1, "not ok 3 - bind %s", strerror(errno));
11400e13b1dSNik Clayton printf("ok 3 - bind\n");
11543cb0b2bSRobert Watson
11643cb0b2bSRobert Watson /*
11743cb0b2bSRobert Watson * Step 3: After bind(). getsockopt() should return EINVAL, since no
11843cb0b2bSRobert Watson * accept filter should be attached.
11943cb0b2bSRobert Watson */
12043cb0b2bSRobert Watson len = sizeof(afa);
12143cb0b2bSRobert Watson ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
12209745181SRobert Watson if (ret != -1)
12300e13b1dSNik Clayton errx(-1, "not ok 4 - getsockopt() after bind() succeeded");
12409745181SRobert Watson if (errno != EINVAL)
12500e13b1dSNik Clayton errx(-1, "not ok 4 - getsockopt() after bind() failed with %d (%s)",
12609745181SRobert Watson errno, strerror(errno));
12700e13b1dSNik Clayton printf("ok 4 - getsockopt\n");
12843cb0b2bSRobert Watson
12943cb0b2bSRobert Watson /*
13043cb0b2bSRobert Watson * Step 4: Setsockopt() before listen(). Should fail, since it's not
13143cb0b2bSRobert Watson * yet a listen() socket.
13243cb0b2bSRobert Watson */
13343cb0b2bSRobert Watson bzero(&afa, sizeof(afa));
1340e47853eSEnji Cooper strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
13543cb0b2bSRobert Watson ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
13609745181SRobert Watson if (ret == 0)
13700e13b1dSNik Clayton errx(-1, "not ok 5 - setsockopt() before listen() succeeded");
13800e13b1dSNik Clayton printf("ok 5 - setsockopt\n");
13943cb0b2bSRobert Watson
14043cb0b2bSRobert Watson /*
14143cb0b2bSRobert Watson * Step 5: Getsockopt() after pre-listen() setsockopt(). Should
14243cb0b2bSRobert Watson * fail with EINVAL, since setsockopt() should have failed.
14343cb0b2bSRobert Watson */
14443cb0b2bSRobert Watson len = sizeof(afa);
14543cb0b2bSRobert Watson ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
14609745181SRobert Watson if (ret == 0)
14700e13b1dSNik Clayton errx(-1, "not ok 6 - getsockopt() after pre-listen() setsockopt() "
14809745181SRobert Watson "succeeded");
14909745181SRobert Watson if (errno != EINVAL)
15000e13b1dSNik Clayton errx(-1, "not ok 6 - pre-listen() getsockopt() failed with %d (%s)",
15109745181SRobert Watson errno, strerror(errno));
15200e13b1dSNik Clayton printf("ok 6 - getsockopt\n");
15343cb0b2bSRobert Watson
15443cb0b2bSRobert Watson /*
15543cb0b2bSRobert Watson * Step 6: listen().
15643cb0b2bSRobert Watson */
15743cb0b2bSRobert Watson if (listen(lso, -1) < 0)
15800e13b1dSNik Clayton errx(-1, "not ok 7 - listen: %s", strerror(errno));
15900e13b1dSNik Clayton printf("ok 7 - listen\n");
16043cb0b2bSRobert Watson
16143cb0b2bSRobert Watson /*
162922a5d9cSMaxim Konovalov * Step 7: Getsockopt() after listen(). Should fail with EINVAL,
163922a5d9cSMaxim Konovalov * since we have not installed accept filter yet.
164922a5d9cSMaxim Konovalov */
165922a5d9cSMaxim Konovalov len = sizeof(afa);
166922a5d9cSMaxim Konovalov ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
167922a5d9cSMaxim Konovalov if (ret == 0)
168922a5d9cSMaxim Konovalov errx(-1, "not ok 8 - getsockopt() after listen() but before "
169922a5d9cSMaxim Konovalov "setsockopt() succeeded");
170922a5d9cSMaxim Konovalov if (errno != EINVAL)
171922a5d9cSMaxim Konovalov errx(-1, "not ok 8 - getsockopt() after listen() but before "
172922a5d9cSMaxim Konovalov "setsockopt() failed with %d (%s)", errno, strerror(errno));
173922a5d9cSMaxim Konovalov printf("ok 8 - getsockopt\n");
174922a5d9cSMaxim Konovalov
175922a5d9cSMaxim Konovalov /*
176922a5d9cSMaxim Konovalov * Step 8: After listen(). This call to setsockopt() should succeed.
17743cb0b2bSRobert Watson */
17843cb0b2bSRobert Watson bzero(&afa, sizeof(afa));
1790e47853eSEnji Cooper strncpy(afa.af_name, ACCF_NAME, sizeof(afa.af_name));
18043cb0b2bSRobert Watson ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
18109745181SRobert Watson if (ret != 0)
182922a5d9cSMaxim Konovalov errx(-1, "not ok 9 - setsockopt() after listen() failed with %d "
18309745181SRobert Watson "(%s)", errno, strerror(errno));
184922a5d9cSMaxim Konovalov printf("ok 9 - setsockopt\n");
18543cb0b2bSRobert Watson
18643cb0b2bSRobert Watson /*
187922a5d9cSMaxim Konovalov * Step 9: After setsockopt(). Should succeed and identify
18843cb0b2bSRobert Watson * ACCF_NAME.
18943cb0b2bSRobert Watson */
19043cb0b2bSRobert Watson bzero(&afa, sizeof(afa));
19143cb0b2bSRobert Watson len = sizeof(afa);
19243cb0b2bSRobert Watson ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
19309745181SRobert Watson if (ret != 0)
194922a5d9cSMaxim Konovalov errx(-1, "not ok 10 - getsockopt() after listen() setsockopt() "
19509745181SRobert Watson "failed with %d (%s)", errno, strerror(errno));
19609745181SRobert Watson if (len != sizeof(afa))
197922a5d9cSMaxim Konovalov errx(-1, "not ok 10 - getsockopt() after setsockopet() after "
1980cab1e96SJohn Baldwin "listen() returned wrong size (got %d expected %zd)", len,
19909745181SRobert Watson sizeof(afa));
20009745181SRobert Watson if (strcmp(afa.af_name, ACCF_NAME) != 0)
201922a5d9cSMaxim Konovalov errx(-1, "not ok 10 - getsockopt() after setsockopt() after "
20209745181SRobert Watson "listen() mismatch (got %s expected %s)", afa.af_name,
20309745181SRobert Watson ACCF_NAME);
204922a5d9cSMaxim Konovalov printf("ok 10 - getsockopt\n");
205922a5d9cSMaxim Konovalov
206922a5d9cSMaxim Konovalov /*
207f7ff0c66SGleb Smirnoff * Step 10: Set listening socket to non blocking mode. Open
208f7ff0c66SGleb Smirnoff * connection to our listening socket and try to accept. Should
209f7ff0c66SGleb Smirnoff * no succeed. Write a byte of data and try again. Should accept.
210f7ff0c66SGleb Smirnoff */
211f7ff0c66SGleb Smirnoff i = fcntl(lso, F_GETFL);
212f7ff0c66SGleb Smirnoff if (i < 0)
213f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - ioctl(F_GETFL): %s", strerror(errno));
214f7ff0c66SGleb Smirnoff i |= O_NONBLOCK;
215f7ff0c66SGleb Smirnoff if (fcntl(lso, F_SETFL, i) != 0)
216f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - ioctl(F_SETFL): %s", strerror(errno));
217f7ff0c66SGleb Smirnoff so = socket(PF_INET, SOCK_STREAM, 0);
218f7ff0c66SGleb Smirnoff if (so == -1)
219f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - socket: %s", strerror(errno));
220f7ff0c66SGleb Smirnoff if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
221f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - connect %s", strerror(errno));
222f7ff0c66SGleb Smirnoff if (accept(lso, NULL, 0) != -1 && errno != EWOULDBLOCK)
223f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - accept #1 %s", strerror(errno));
224f7ff0c66SGleb Smirnoff if (write(so, "0", 1) != 1)
225f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - write %s", strerror(errno));
226f7ff0c66SGleb Smirnoff /*
227f7ff0c66SGleb Smirnoff * XXXGL: ugly, but we need to make sure that our write reaches
228f7ff0c66SGleb Smirnoff * remote side of the socket.
229f7ff0c66SGleb Smirnoff */
230f7ff0c66SGleb Smirnoff usleep(10000);
231f7ff0c66SGleb Smirnoff if (accept(lso, NULL, 0) < 1)
232f7ff0c66SGleb Smirnoff errx(-1, "not ok 11 - accept #2 %s", strerror(errno));
233*a4658c80SGleb Smirnoff if (close(so) != 0)
234*a4658c80SGleb Smirnoff errx(-1, "not ok 11 - close(): %s", strerror(errno));
235f7ff0c66SGleb Smirnoff printf("ok 11 - accept\n");
236f7ff0c66SGleb Smirnoff
237*a4658c80SGleb Smirnoff /*
238*a4658c80SGleb Smirnoff * Step 12: reset connection before accept filter allows it.
239*a4658c80SGleb Smirnoff * In this case the connection must make it to the listen
240*a4658c80SGleb Smirnoff * queue, but with ECONNABORTED code.
241*a4658c80SGleb Smirnoff */
242*a4658c80SGleb Smirnoff so = socket(PF_INET, SOCK_STREAM, 0);
243*a4658c80SGleb Smirnoff if (so == -1)
244*a4658c80SGleb Smirnoff errx(-1, "not ok 12 - socket: %s", strerror(errno));
245*a4658c80SGleb Smirnoff if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
246*a4658c80SGleb Smirnoff errx(-1, "not ok 12 - connect %s", strerror(errno));
247*a4658c80SGleb Smirnoff linger.l_onoff = 1;
248*a4658c80SGleb Smirnoff linger.l_linger = 0;
249*a4658c80SGleb Smirnoff ret = setsockopt(so, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
250*a4658c80SGleb Smirnoff if (ret != 0)
251*a4658c80SGleb Smirnoff errx(-1, "not ok 12 - setsockopt(SO_LINGER) failed with %d "
252*a4658c80SGleb Smirnoff "(%s)", errno, strerror(errno));
253*a4658c80SGleb Smirnoff if (close(so) != 0)
254*a4658c80SGleb Smirnoff errx(-1, "not ok 12 - close(): %s", strerror(errno));
255*a4658c80SGleb Smirnoff if (accept(lso, NULL, 0) != -1 && errno != ECONNABORTED)
256*a4658c80SGleb Smirnoff errx(-1, "not ok 12 - accept #3 %s", strerror(errno));
257*a4658c80SGleb Smirnoff printf("ok 12 - accept\n");
258*a4658c80SGleb Smirnoff
25962b2dd31SGleb Smirnoff #if 1
26062b2dd31SGleb Smirnoff /*
26162b2dd31SGleb Smirnoff * XXXGL: this doesn't belong to the test itself, but is known
26262b2dd31SGleb Smirnoff * to examine rarely examined paths in the kernel. Intentionally
26362b2dd31SGleb Smirnoff * leave a socket on the incomplete queue, before the program
26462b2dd31SGleb Smirnoff * exits.
26562b2dd31SGleb Smirnoff */
26662b2dd31SGleb Smirnoff so = socket(PF_INET, SOCK_STREAM, 0);
26762b2dd31SGleb Smirnoff if (so == -1)
268*a4658c80SGleb Smirnoff errx(-1, "not ok 13 - socket: %s", strerror(errno));
26962b2dd31SGleb Smirnoff if (connect(so, (struct sockaddr *)&sin, sizeof(sin)) < 0)
270*a4658c80SGleb Smirnoff errx(-1, "not ok 13 - connect %s", strerror(errno));
27162b2dd31SGleb Smirnoff #endif
27262b2dd31SGleb Smirnoff
273f7ff0c66SGleb Smirnoff /*
274*a4658c80SGleb Smirnoff * Step 12: Remove accept filter. After removing the accept filter
275922a5d9cSMaxim Konovalov * getsockopt() should fail with EINVAL.
276922a5d9cSMaxim Konovalov */
277922a5d9cSMaxim Konovalov ret = setsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, NULL, 0);
278922a5d9cSMaxim Konovalov if (ret != 0)
279*a4658c80SGleb Smirnoff errx(-1, "not ok 13 - setsockopt() after listen() "
280922a5d9cSMaxim Konovalov "failed with %d (%s)", errno, strerror(errno));
281922a5d9cSMaxim Konovalov bzero(&afa, sizeof(afa));
282922a5d9cSMaxim Konovalov len = sizeof(afa);
283922a5d9cSMaxim Konovalov ret = getsockopt(lso, SOL_SOCKET, SO_ACCEPTFILTER, &afa, &len);
284922a5d9cSMaxim Konovalov if (ret == 0)
285*a4658c80SGleb Smirnoff errx(-1, "not ok 13 - getsockopt() after removing "
286922a5d9cSMaxim Konovalov "the accept filter returns valid accept filter %s",
287922a5d9cSMaxim Konovalov afa.af_name);
288922a5d9cSMaxim Konovalov if (errno != EINVAL)
289*a4658c80SGleb Smirnoff errx(-1, "not ok 13 - getsockopt() after removing the accept"
290922a5d9cSMaxim Konovalov "filter failed with %d (%s)", errno, strerror(errno));
291*a4658c80SGleb Smirnoff if (close(lso) != 0)
292*a4658c80SGleb Smirnoff errx(-1, "not ok 13 - close() of listening socket: %s",
293*a4658c80SGleb Smirnoff strerror(errno));
294*a4658c80SGleb Smirnoff printf("ok 13 - setsockopt\n");
29543cb0b2bSRobert Watson
29643cb0b2bSRobert Watson return (0);
29743cb0b2bSRobert Watson }
298