]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/netinet6/icmp6_filter/icmp6_filter.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / netinet6 / icmp6_filter / icmp6_filter.c
1 /*-
2  * Copyright (c) 2008 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
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,
33  * etc.
34  */
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38
39 #include <netinet/in.h>
40 #include <netinet/icmp6.h>
41
42 #include <err.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 /*
47  * Reference filters to set/test.
48  */
49 static struct icmp6_filter ic6f_passall;
50 static struct icmp6_filter ic6f_blockall;
51
52 int
53 main(int argc, char *argv[])
54 {
55         struct icmp6_filter ic6f;
56         socklen_t len;
57         int s;
58
59         ICMP6_FILTER_SETPASSALL(&ic6f_passall);
60         ICMP6_FILTER_SETBLOCKALL(&ic6f_blockall);
61
62         s = socket(PF_INET6, SOCK_RAW, 0);
63         if (s < 0)
64                 err(-1, "socket(PF_INET6, SOCK_RAW, 0)");
65
66         /*
67          * Confirm that we can read before the first set, and that the
68          * default is to pass all ICMP.
69          */
70         len = sizeof(ic6f);
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");
75
76         /*
77          * Confirm that we can write a pass all filter to the socket.
78          */
79         len = sizeof(ic6f);
80         ICMP6_FILTER_SETPASSALL(&ic6f);
81         if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0)
82                 err(-1, "2: setsockopt(ICMP6_FILTER, PASSALL)");
83
84         /*
85          * Confirm that we can still read a pass all filter.
86          */
87         len = sizeof(ic6f);
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");
92
93         /*
94          * Confirm that we can write a block all filter to the socket.
95          */
96         len = sizeof(ic6f);
97         ICMP6_FILTER_SETBLOCKALL(&ic6f);
98         if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0)
99                 err(-1, "4: setsockopt(ICMP6_FILTER, BLOCKALL)");
100
101         /*
102          * Confirm that we can read back a block all filter.
103          */
104         len = sizeof(ic6f);
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");
109
110         /*
111          * For completeness, confirm that we can reset to the default.
112          */
113         len = sizeof(ic6f);
114         ICMP6_FILTER_SETPASSALL(&ic6f);
115         if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0)
116                 err(-1, "6: setsockopt(ICMP6_FILTER, PASSALL)");
117
118         /*
119          * ... And that we can read back the pass all rule again.
120          */
121         len = sizeof(ic6f);
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");
126
127         close(s);
128         return (0);
129 }