1 /*
2  * Check decoding of ICMP_FILTER.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 #include <stdio.h>
33 #include <sys/socket.h>
34 #include <linux/icmp.h>
35 
36 int
main(void)37 main(void)
38 {
39 	getsockopt(-1, SOL_RAW, ICMP_FILTER, 0, 0);
40 	printf("getsockopt(-1, SOL_RAW, ICMP_FILTER, NULL, NULL) = -1 %s (%m)\n",
41 	       errno2name());
42 
43 	setsockopt(-1, SOL_RAW, ICMP_FILTER, NULL, 0);
44 	printf("setsockopt(-1, SOL_RAW, ICMP_FILTER, NULL, 0) = -1 %s (%m)\n",
45 	       errno2name());
46 
47 	TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, plen);
48 	void *const efault = plen + 1;
49 	TAIL_ALLOC_OBJECT_CONST_PTR(struct icmp_filter, f);
50 
51 	getsockopt(-1, SOL_RAW, ICMP_FILTER, f, plen);
52 	printf("getsockopt(-1, SOL_RAW, ICMP_FILTER, %p, [%d]) = -1 %s (%m)\n",
53 	       f, *plen, errno2name());
54 
55 	setsockopt(-1, SOL_RAW, ICMP_FILTER, efault, sizeof(*f));
56 	printf("setsockopt(-1, SOL_RAW, ICMP_FILTER, %p, %u) = -1 %s (%m)\n",
57 	       efault, (unsigned) sizeof(*f), errno2name());
58 
59 	f->data = ~(
60 		1<<ICMP_ECHOREPLY |
61 		1<<ICMP_DEST_UNREACH |
62 		1<<ICMP_SOURCE_QUENCH |
63 		1<<ICMP_REDIRECT |
64 		1<<ICMP_TIME_EXCEEDED |
65 		1<<ICMP_PARAMETERPROB);
66 
67 	setsockopt(-1, SOL_RAW, ICMP_FILTER, f, -2);
68 	printf("setsockopt(-1, SOL_RAW, ICMP_FILTER, %p, -2) = -1 %s (%m)\n",
69 	       f, errno2name());
70 
71 	setsockopt(-1, SOL_RAW, ICMP_FILTER, f, sizeof(*f));
72 	printf("setsockopt(-1, SOL_RAW, ICMP_FILTER, %s, %u) = -1 %s (%m)\n",
73 	       "~(1<<ICMP_ECHOREPLY|1<<ICMP_DEST_UNREACH|1<<ICMP_SOURCE_QUENCH"
74 	       "|1<<ICMP_REDIRECT|1<<ICMP_TIME_EXCEEDED|1<<ICMP_PARAMETERPROB)",
75 	       (unsigned) sizeof(*f), errno2name());
76 
77 	setsockopt(-1, SOL_RAW, ICMP_FILTER, f, sizeof(*f) * 2);
78 	printf("setsockopt(-1, SOL_RAW, ICMP_FILTER, %s, %u) = -1 %s (%m)\n",
79 	       "~(1<<ICMP_ECHOREPLY|1<<ICMP_DEST_UNREACH|1<<ICMP_SOURCE_QUENCH"
80 	       "|1<<ICMP_REDIRECT|1<<ICMP_TIME_EXCEEDED|1<<ICMP_PARAMETERPROB)",
81 	       (unsigned) sizeof(*f) * 2, errno2name());
82 
83 	puts("+++ exited with 0 +++");
84 	return 0;
85 }
86