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