1180966Srwatson/*- 2180966Srwatson * Copyright (c) 2008 Robert N. M. Watson 3180966Srwatson * All rights reserved. 4180966Srwatson * 5180966Srwatson * Redistribution and use in source and binary forms, with or without 6180966Srwatson * modification, are permitted provided that the following conditions 7180966Srwatson * are met: 8180966Srwatson * 1. Redistributions of source code must retain the above copyright 9180966Srwatson * notice, this list of conditions and the following disclaimer. 10180966Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11180966Srwatson * notice, this list of conditions and the following disclaimer in the 12180966Srwatson * documentation and/or other materials provided with the distribution. 13180966Srwatson * 14180966Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15180966Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16180966Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17180966Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18180966Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19180966Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20180966Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21180966Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22180966Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23180966Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24180966Srwatson * SUCH DAMAGE. 25180966Srwatson * 26180966Srwatson * $FreeBSD: releng/10.2/tools/regression/netinet6/icmp6_filter/icmp6_filter.c 180967 2008-07-29 18:38:37Z rwatson $ 27180966Srwatson */ 28180966Srwatson 29180966Srwatson/* 30180966Srwatson * This regression test creates a raw IPv6 socket and confirms that it can 31180966Srwatson * set and get filters on the socket. No attempt is made to validate that 32180966Srwatson * the filter is implemented, just that it can be properly retrieved, set, 33180966Srwatson * etc. 34180966Srwatson */ 35180966Srwatson 36180966Srwatson#include <sys/types.h> 37180966Srwatson#include <sys/socket.h> 38180966Srwatson 39180966Srwatson#include <netinet/in.h> 40180966Srwatson#include <netinet/icmp6.h> 41180966Srwatson 42180966Srwatson#include <err.h> 43180966Srwatson#include <string.h> 44180966Srwatson#include <unistd.h> 45180966Srwatson 46180966Srwatson/* 47180966Srwatson * Reference filters to set/test. 48180966Srwatson */ 49180966Srwatsonstatic struct icmp6_filter ic6f_passall; 50180966Srwatsonstatic struct icmp6_filter ic6f_blockall; 51180966Srwatson 52180966Srwatsonint 53180966Srwatsonmain(int argc, char *argv[]) 54180966Srwatson{ 55180966Srwatson struct icmp6_filter ic6f; 56180966Srwatson socklen_t len; 57180966Srwatson int s; 58180966Srwatson 59180966Srwatson ICMP6_FILTER_SETPASSALL(&ic6f_passall); 60180966Srwatson ICMP6_FILTER_SETBLOCKALL(&ic6f_blockall); 61180966Srwatson 62180966Srwatson s = socket(PF_INET6, SOCK_RAW, 0); 63180966Srwatson if (s < 0) 64180966Srwatson err(-1, "socket(PF_INET6, SOCK_RAW, 0)"); 65180966Srwatson 66180966Srwatson /* 67180966Srwatson * Confirm that we can read before the first set, and that the 68180966Srwatson * default is to pass all ICMP. 69180966Srwatson */ 70180966Srwatson len = sizeof(ic6f); 71180966Srwatson if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0) 72180966Srwatson err(-1, "1: getsockopt(ICMP6_FILTER)"); 73180966Srwatson if (memcmp(&ic6f, &ic6f_passall, sizeof(ic6f)) != 0) 74180966Srwatson errx(-1, "1: getsockopt(ICMP6_FILTER) - default not passall"); 75180966Srwatson 76180966Srwatson /* 77180966Srwatson * Confirm that we can write a pass all filter to the socket. 78180966Srwatson */ 79180966Srwatson len = sizeof(ic6f); 80180966Srwatson ICMP6_FILTER_SETPASSALL(&ic6f); 81180966Srwatson if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0) 82180966Srwatson err(-1, "2: setsockopt(ICMP6_FILTER, PASSALL)"); 83180966Srwatson 84180966Srwatson /* 85180966Srwatson * Confirm that we can still read a pass all filter. 86180966Srwatson */ 87180966Srwatson len = sizeof(ic6f); 88180966Srwatson if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0) 89180966Srwatson err(-1, "3: getsockopt(ICMP6_FILTER)"); 90180966Srwatson if (memcmp(&ic6f, &ic6f_passall, sizeof(ic6f)) != 0) 91180966Srwatson errx(-1, "3: getsockopt(ICMP6_FILTER) - not passall"); 92180966Srwatson 93180966Srwatson /* 94180966Srwatson * Confirm that we can write a block all filter to the socket. 95180966Srwatson */ 96180966Srwatson len = sizeof(ic6f); 97180966Srwatson ICMP6_FILTER_SETBLOCKALL(&ic6f); 98180966Srwatson if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0) 99180966Srwatson err(-1, "4: setsockopt(ICMP6_FILTER, BLOCKALL)"); 100180966Srwatson 101180966Srwatson /* 102180966Srwatson * Confirm that we can read back a block all filter. 103180966Srwatson */ 104180966Srwatson len = sizeof(ic6f); 105180966Srwatson if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0) 106180966Srwatson err(-1, "5: getsockopt(ICMP6_FILTER)"); 107180966Srwatson if (memcmp(&ic6f, &ic6f_blockall, sizeof(ic6f)) != 0) 108180966Srwatson errx(-1, "5: getsockopt(ICMP6_FILTER) - not blockall"); 109180966Srwatson 110180966Srwatson /* 111180966Srwatson * For completeness, confirm that we can reset to the default. 112180966Srwatson */ 113180966Srwatson len = sizeof(ic6f); 114180966Srwatson ICMP6_FILTER_SETPASSALL(&ic6f); 115180966Srwatson if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, len) < 0) 116180966Srwatson err(-1, "6: setsockopt(ICMP6_FILTER, PASSALL)"); 117180966Srwatson 118180966Srwatson /* 119180966Srwatson * ... And that we can read back the pass all rule again. 120180966Srwatson */ 121180966Srwatson len = sizeof(ic6f); 122180966Srwatson if (getsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &ic6f, &len) < 0) 123180966Srwatson err(-1, "7: getsockopt(ICMP6_FILTER)"); 124180966Srwatson if (memcmp(&ic6f, &ic6f_passall, sizeof(ic6f)) != 0) 125180966Srwatson errx(-1, "7: getsockopt(ICMP6_FILTER) - not passall"); 126180966Srwatson 127180966Srwatson close(s); 128180966Srwatson return (0); 129180966Srwatson} 130