2 * Copyright (c) 2008 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
30 * This regression test creates a raw IPv6 socket and confirms that it can
31 * set and get filters on the socket. No attempt is made to validate that
32 * the filter is implemented, just that it can be properly retrieved, set,
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/icmp6.h>
47 * Reference filters to set/test.
49 static struct icmp6_filter ic6f_passall;
50 static struct icmp6_filter ic6f_blockall;
53 main(int argc, char *argv[])
55 struct icmp6_filter ic6f;
59 ICMP6_FILTER_SETPASSALL(&ic6f_passall);
60 ICMP6_FILTER_SETBLOCKALL(&ic6f_blockall);
62 s = socket(PF_INET6, SOCK_RAW, 0);
64 err(-1, "socket(PF_INET6, SOCK_RAW, 0)");
67 * Confirm that we can read before the first set, and that the
68 * default is to pass all ICMP.
71 if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0)
72 err(-1, "1: getsockopt(ICMP6_FILTER)");
73 if (memcmp(&ic6f, &ic6f_passall, sizeof(ic6f)) != 0)
74 errx(-1, "1: getsockopt(ICMP6_FILTER) - default not passall");
77 * Confirm that we can write a pass all filter to the socket.
80 ICMP6_FILTER_SETPASSALL(&ic6f);
81 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0)
82 err(-1, "2: setsockopt(ICMP6_FILTER, PASSALL)");
85 * Confirm that we can still read a pass all filter.
88 if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0)
89 err(-1, "3: getsockopt(ICMP6_FILTER)");
90 if (memcmp(&ic6f, &ic6f_passall, sizeof(ic6f)) != 0)
91 errx(-1, "3: getsockopt(ICMP6_FILTER) - not passall");
94 * Confirm that we can write a block all filter to the socket.
97 ICMP6_FILTER_SETBLOCKALL(&ic6f);
98 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0)
99 err(-1, "4: setsockopt(ICMP6_FILTER, BLOCKALL)");
102 * Confirm that we can read back a block all filter.
105 if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0)
106 err(-1, "5: getsockopt(ICMP6_FILTER)");
107 if (memcmp(&ic6f, &ic6f_blockall, sizeof(ic6f)) != 0)
108 errx(-1, "5: getsockopt(ICMP6_FILTER) - not blockall");
111 * For completeness, confirm that we can reset to the default.
114 ICMP6_FILTER_SETPASSALL(&ic6f);
115 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0)
116 err(-1, "6: setsockopt(ICMP6_FILTER, PASSALL)");
119 * ... And that we can read back the pass all rule again.
122 if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0)
123 err(-1, "7: getsockopt(ICMP6_FILTER)");
124 if (memcmp(&ic6f, &ic6f_passall, sizeof(ic6f)) != 0)
125 errx(-1, "7: getsockopt(ICMP6_FILTER) - not passall");