mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: asapi_05 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * These tests are for the "Advanced Sockets API" (RFC 3542) |
| 25 | * Verify that in6 and sockaddr fields are present. |
| 26 | * |
| 27 | * Usage: <for command-line> |
| 28 | * asapi_05 |
| 29 | * |
| 30 | * HISTORY |
| 31 | * 04/2005 written by David L Stevens |
| 32 | * |
| 33 | * RESTRICTIONS: |
| 34 | * None. |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <unistd.h> |
| 40 | #include <errno.h> |
| 41 | |
| 42 | #include <sys/wait.h> |
subrata_modak | 7fe6451 | 2008-12-12 13:41:11 +0000 | [diff] [blame] | 43 | #include <sys/socket.h> |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 44 | |
| 45 | #include <netinet/in.h> |
| 46 | #include <netinet/ip6.h> |
| 47 | #include <netinet/icmp6.h> |
| 48 | |
| 49 | #include "test.h" |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 50 | #include "runcc.h" |
| 51 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 52 | char *TCID = "asapi_05"; /* Test program identifier. */ |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 53 | |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 54 | void setup(void); |
| 55 | void cleanup(void); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 56 | |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 57 | void icmp6_et(void); |
| 58 | void icmp6_ft(void); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 59 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | int main(int argc, char *argv[]) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 61 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 63 | const char *msg; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 64 | |
| 65 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 66 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 67 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 68 | |
| 69 | setup(); |
| 70 | |
| 71 | for (lc = 0; TEST_LOOPING(lc); ++lc) { |
| 72 | icmp6_et(); |
| 73 | icmp6_ft(); |
| 74 | } |
| 75 | |
| 76 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 77 | |
| 78 | tst_exit(); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | enum ttype { EXISTS, ALIAS, VALUE, DEFINED }; |
| 82 | |
| 83 | struct etent { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | char *et_tname; /* test name */ |
| 85 | int et_type; /* test type */ |
| 86 | char *et_incl; /* include file list */ |
| 87 | char *et_struct; /* structure name */ |
| 88 | char *et_field; /* field name */ |
| 89 | char *et_offset; /* field offset */ |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 90 | union { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | char *fu_value; /* field size or value */ |
| 92 | char *fu_dname; /* #define name */ |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 93 | } ftun; |
| 94 | #define et_value ftun.fu_value |
| 95 | #define et_dname ftun.fu_dname |
| 96 | } etab[] = { |
| 97 | /* existence checks, RFC 3542 section 3 */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | { |
| 99 | "icmp6_filter icmp6_filt", EXISTS, ICMP6_H, "icmp6_filter", |
| 100 | "icmp6_filt", "0", { |
| 101 | "32"}}, { |
| 102 | "icmp6_filter icmp6_filt[0]", EXISTS, ICMP6_H, "icmp6_filter", |
| 103 | "icmp6_filt[0]", "0", { |
| 104 | "4"}}, { |
| 105 | "ICMP6_FILTER_WILLPASS", DEFINED, ICMP6_H, |
| 106 | "ICMP6_FILTER_WILLPASS", NULL, NULL, { |
| 107 | 0}}, { |
| 108 | "ICMP6_FILTER_WILLBLOCK", DEFINED, ICMP6_H, |
| 109 | "ICMP6_FILTER_WILLBLOCK", NULL, NULL, { |
| 110 | 0}}, { |
| 111 | "ICMP6_FILTER_SETPASS", DEFINED, ICMP6_H, |
| 112 | "ICMP6_FILTER_SETPASS", NULL, NULL, { |
| 113 | 0}}, { |
| 114 | "ICMP6_FILTER_SETBLOCK", DEFINED, ICMP6_H, |
| 115 | "ICMP6_FILTER_SETBLOCK", NULL, NULL, { |
| 116 | 0}}, { |
| 117 | "ICMP6_FILTER_SETPASSALL", DEFINED, ICMP6_H, |
| 118 | "ICMP6_FILTER_SETPASSALL", NULL, NULL, { |
| 119 | 0}}, { |
| 120 | "ICMP6_FILTER_SETBLOCKALL", DEFINED, ICMP6_H, |
| 121 | "ICMP6_FILTER_SETBLOCKALL", NULL, NULL, { |
| 122 | 0}}, { |
| 123 | "ICMP6_FILTER", DEFINED, ICMP6_H, "ICMP6_FILTER", NULL, NULL, { |
| 124 | 0}}, |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 125 | /* existence checks, RFC 3542 section 4 */ |
| 126 | /* socket options */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | { |
| 128 | "IPV6_RECVPKTINFO", VALUE, IN_H, "IPV6_RECVPKTINFO", NULL, NULL, { |
| 129 | "IPV6_RECVPKTINFO"}}, { |
| 130 | "IPV6_RECVHOPLIMIT", VALUE, IN_H, "IPV6_RECVHOPLIMIT", NULL, |
| 131 | NULL, { |
| 132 | "IPV6_RECVHOPLIMIT"}}, { |
| 133 | "IPV6_RECVRTHDR", VALUE, IN_H, "IPV6_RECVRTHDR", NULL, NULL, { |
| 134 | "IPV6_RECVRTHDR"}}, { |
| 135 | "IPV6_RECVHOPOPTS", VALUE, IN_H, "IPV6_RECVHOPOPTS", NULL, NULL, { |
| 136 | "IPV6_RECVHOPOPTS"}}, { |
| 137 | "IPV6_RECVDSTOPTS", VALUE, IN_H, "IPV6_RECVDSTOPTS", NULL, NULL, { |
| 138 | "IPV6_RECVDSTOPTS"}}, { |
| 139 | "IPV6_RECVTCLASS", VALUE, IN_H, "IPV6_RECVTCLASS", NULL, NULL, { |
| 140 | "IPV6_RECVTCLASS"}}, |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 141 | /* cmsg types */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | { |
| 143 | "IPV6_PKTINFO", DEFINED, IN_H, "IPV6_PKTINFO", NULL, NULL, { |
| 144 | 0}}, { |
| 145 | "IPV6_HOPLIMIT", DEFINED, IN_H, "IPV6_HOPLIMIT", NULL, NULL, { |
| 146 | 0}}, { |
| 147 | "IPV6_NEXTHOP", DEFINED, IN_H, "IPV6_NEXTHOP", NULL, NULL, { |
| 148 | 0}}, { |
| 149 | "IPV6_RTHDR", DEFINED, IN_H, "IPV6_RTHDR", NULL, NULL, { |
| 150 | 0}}, { |
| 151 | "IPV6_HOPOPTS", DEFINED, IN_H, "IPV6_HOPOPTS", NULL, NULL, { |
| 152 | 0}}, { |
| 153 | "IPV6_DSTOPTS", DEFINED, IN_H, "IPV6_DSTOPTS", NULL, NULL, { |
| 154 | 0}}, { |
| 155 | "IPV6_RTHDRDSTOPTS", DEFINED, IN_H, "IPV6_RTHDRDSTOPTS", NULL, |
| 156 | NULL, { |
| 157 | 0}}, { |
| 158 | "IPV6_TCLASS", DEFINED, IN_H, "IPV6_TCLASS", NULL, NULL, { |
| 159 | 0}},}; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 160 | |
| 161 | #define ETCOUNT (sizeof(etab)/sizeof(etab[0])) |
| 162 | |
| 163 | /* existence tests */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | void icmp6_et(void) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 165 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | int i; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 167 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | for (i = 0; i < ETCOUNT; ++i) { |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 169 | switch (etab[i].et_type) { |
| 170 | case EXISTS: |
| 171 | structcheck(etab[i].et_tname, etab[i].et_incl, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | etab[i].et_struct, etab[i].et_field, |
| 173 | etab[i].et_offset, etab[i].et_value); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 174 | break; |
| 175 | case ALIAS: |
| 176 | aliascheck(etab[i].et_tname, etab[i].et_incl, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 177 | etab[i].et_struct, etab[i].et_field, |
| 178 | etab[i].et_dname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 179 | break; |
| 180 | case VALUE: |
| 181 | valuecheck(etab[i].et_tname, etab[i].et_incl, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | etab[i].et_struct, etab[i].et_dname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 183 | break; |
| 184 | case DEFINED: |
| 185 | funccheck(etab[i].et_tname, etab[i].et_incl, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 186 | etab[i].et_struct); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 187 | break; |
| 188 | default: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 189 | tst_resm(TBROK, "invalid type %d", etab[i].et_type); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 195 | void setup(void) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 196 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | TEST_PAUSE; /* if -P option specified */ |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 198 | tst_require_root(NULL); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 201 | void cleanup(void) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 202 | { |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* |
| 206 | * This is for old, broken glibc-header icmp6_filter structure definitions. |
| 207 | * If icmp6.h has struct icmp6_filter with field named "data" instead |
| 208 | * of the standard "icmp_filt", uncomment this line. |
| 209 | */ |
| 210 | /*#define icmp_filt data */ |
| 211 | |
| 212 | enum tt { T_WILLPASS, T_WILLBLOCK, T_SETPASS, T_SETBLOCK, T_SETPASSALL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | T_SETBLOCKALL |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 214 | }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 215 | struct ftent { |
| 216 | char *ft_tname; /* test name, for logging */ |
| 217 | unsigned char ft_sndtype; /* send type field */ |
| 218 | unsigned char ft_flttype; /* filter type field */ |
| 219 | enum tt ft_test; /* what macro to test */ |
| 220 | int ft_expected; /* packet should pass? */ |
| 221 | } ftab[] = { |
| 222 | { |
| 223 | "ICMP6_FILTER_SETPASS s 20 f 20", 20, 20, T_SETPASS, 1}, { |
| 224 | "ICMP6_FILTER_SETPASS s 20 f 21", 20, 21, T_SETPASS, 0}, { |
| 225 | "ICMP6_FILTER_SETBLOCK s 20 f 20", 20, 20, T_SETBLOCK, 0}, { |
| 226 | "ICMP6_FILTER_SETBLOCK s 20 f 21", 20, 21, T_SETBLOCK, 1}, { |
| 227 | "ICMP6_FILTER_PASSALL s 20", 20, 0, T_SETPASSALL, 1}, { |
| 228 | "ICMP6_FILTER_PASSALL s 20", 21, 0, T_SETPASSALL, 1}, { |
| 229 | "ICMP6_FILTER_BLOCKALL s 20", 20, 0, T_SETBLOCKALL, 0}, { |
| 230 | "ICMP6_FILTER_BLOCKALL s 20", 21, 0, T_SETBLOCKALL, 0}, { |
| 231 | "ICMP6_FILTER_WILLBLOCK s 20 f 21", 20, 21, T_WILLBLOCK, 0}, { |
| 232 | "ICMP6_FILTER_WILLBLOCK s 20 f 20", 20, 20, T_WILLBLOCK, 1}, { |
| 233 | "ICMP6_FILTER_WILLPASS s 20 f 21", 20, 21, T_WILLPASS, 0}, { |
| 234 | "ICMP6_FILTER_WILLPASS s 22 f 22", 22, 22, T_WILLPASS, 1},}; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 235 | |
| 236 | #define FTCOUNT (sizeof(ftab)/sizeof(ftab[0])) |
| 237 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 238 | int ic6_send1(char *tname, unsigned char type) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 239 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 240 | struct sockaddr_in6 sin6; |
| 241 | struct icmp6_hdr ic6; |
| 242 | int s; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 243 | |
| 244 | s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 245 | if (s == -1) { |
| 246 | tst_resm(TBROK, "%s: socket failed", tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 247 | return 1; |
| 248 | } |
| 249 | memset(&ic6, 0, sizeof(ic6)); |
| 250 | ic6.icmp6_type = type; |
| 251 | ic6.icmp6_data32[0] = htonl(getpid()); |
| 252 | |
| 253 | memset(&sin6, 0, sizeof(sin6)); |
| 254 | sin6.sin6_family = AF_INET6; |
| 255 | sin6.sin6_addr = in6addr_loopback; |
| 256 | if (sendto(s, &ic6, sizeof(ic6), 0, (struct sockaddr *)&sin6, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 257 | sizeof(sin6)) == -1) { |
| 258 | tst_resm(TBROK | TERRNO, "%s: sendto failed", tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 259 | return 1; |
| 260 | } |
| 261 | return 0; |
| 262 | } |
| 263 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 264 | int ic6_recv1(char *tname, int sall, int sf) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 265 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 266 | fd_set readfds, readfds_saved; |
| 267 | struct timeval tv; |
| 268 | int maxfd, nfds; |
| 269 | int gotall, gotone; |
| 270 | int cc; |
| 271 | static unsigned char rbuf[2048]; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 272 | |
| 273 | tv.tv_sec = 0; |
| 274 | tv.tv_usec = 250000; |
| 275 | |
| 276 | FD_ZERO(&readfds_saved); |
| 277 | FD_SET(sall, &readfds_saved); |
| 278 | FD_SET(sf, &readfds_saved); |
| 279 | maxfd = MAX(sall, sf); |
| 280 | |
| 281 | memcpy(&readfds, &readfds_saved, sizeof(readfds)); |
| 282 | |
| 283 | gotall = gotone = 0; |
| 284 | /* |
| 285 | * Note: this relies on linux-specific behavior (select |
| 286 | * updating tv with time elapsed) |
| 287 | */ |
| 288 | while (!gotall || !gotone) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 289 | struct icmp6_hdr *pic6 = (struct icmp6_hdr *)rbuf; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 290 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 291 | nfds = select(maxfd + 1, &readfds, 0, 0, &tv); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 292 | if (nfds == 0) |
| 293 | break; /* timed out */ |
| 294 | if (nfds < 0) { |
| 295 | if (errno == EINTR) |
| 296 | continue; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 297 | tst_resm(TBROK | TERRNO, "%s: select failed", tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 298 | } |
| 299 | if (FD_ISSET(sall, &readfds)) { |
| 300 | cc = recv(sall, rbuf, sizeof(rbuf), 0); |
| 301 | if (cc < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 302 | tst_resm(TBROK | TERRNO, |
| 303 | "%s: recv(sall, ..) failed", tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 304 | return -1; |
| 305 | } |
| 306 | /* if packet check succeeds... */ |
| 307 | if (htonl(pic6->icmp6_data32[0]) == getpid()) |
| 308 | gotall = 1; |
| 309 | } |
| 310 | if (FD_ISSET(sf, &readfds)) { |
| 311 | cc = recv(sf, rbuf, sizeof(rbuf), 0); |
| 312 | if (cc < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 313 | tst_resm(TBROK | TERRNO, |
| 314 | "%s: recv(sf, ..) failed", tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 315 | return -1; |
| 316 | } |
| 317 | /* if packet check succeeds... */ |
| 318 | if (htonl(pic6->icmp6_data32[0]) == getpid()) |
| 319 | gotone = 1; |
| 320 | } |
| 321 | memcpy(&readfds, &readfds_saved, sizeof(readfds)); |
| 322 | } |
| 323 | if (!gotall) { |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 324 | tst_resm(TBROK, "%s: recv all timed out", tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | if (gotone) |
| 328 | return 1; |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | /* functional tests */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 333 | void icmp6_ft(void) |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 334 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 335 | struct icmp6_filter i6f; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 336 | int sall, sf; |
| 337 | int i; |
| 338 | |
| 339 | sall = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
| 340 | if (sall < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 341 | tst_resm(TBROK | TERRNO, |
| 342 | "icmp6_ft socket: can't create sall socket"); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | ICMP6_FILTER_SETPASSALL(&i6f); |
| 346 | if (setsockopt(sall, IPPROTO_ICMPV6, ICMP6_FILTER, &i6f, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 347 | sizeof(i6f)) < 0) { |
| 348 | tst_resm(TBROK | TERRNO, |
| 349 | "setsockopt pass all ICMP6_FILTER failed"); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | sf = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
| 353 | if (sf < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 354 | tst_resm(TBROK | TERRNO, |
| 355 | "icmp6_ft socket: can't create test socket"); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 356 | return; |
| 357 | } |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 358 | |
| 359 | int rv; |
| 360 | |
| 361 | for (i = 0; i < FTCOUNT; ++i) { |
| 362 | |
| 363 | rv = -1; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 364 | |
| 365 | switch (ftab[i].ft_test) { |
| 366 | case T_SETPASS: |
| 367 | ICMP6_FILTER_SETBLOCKALL(&i6f); |
| 368 | ICMP6_FILTER_SETPASS(ftab[i].ft_flttype, &i6f); |
| 369 | break; |
| 370 | case T_SETPASSALL: |
| 371 | ICMP6_FILTER_SETPASSALL(&i6f); |
| 372 | break; |
| 373 | case T_SETBLOCK: |
| 374 | ICMP6_FILTER_SETPASSALL(&i6f); |
| 375 | ICMP6_FILTER_SETBLOCK(ftab[i].ft_flttype, &i6f); |
| 376 | break; |
| 377 | case T_SETBLOCKALL: |
| 378 | ICMP6_FILTER_SETBLOCKALL(&i6f); |
| 379 | break; |
| 380 | case T_WILLBLOCK: |
| 381 | ICMP6_FILTER_SETPASSALL(&i6f); |
| 382 | ICMP6_FILTER_SETBLOCK(ftab[i].ft_flttype, &i6f); |
| 383 | rv = ICMP6_FILTER_WILLBLOCK(ftab[i].ft_sndtype, &i6f); |
| 384 | break; |
| 385 | case T_WILLPASS: |
| 386 | ICMP6_FILTER_SETBLOCKALL(&i6f); |
| 387 | ICMP6_FILTER_SETPASS(ftab[i].ft_flttype, &i6f); |
| 388 | rv = ICMP6_FILTER_WILLPASS(ftab[i].ft_sndtype, &i6f); |
| 389 | break; |
| 390 | default: |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 391 | tst_resm(TBROK, "%s: unknown test type %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 392 | ftab[i].ft_tname, ftab[i].ft_test); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 393 | continue; |
| 394 | } |
| 395 | if (ftab[i].ft_test != T_WILLBLOCK && |
| 396 | ftab[i].ft_test != T_WILLPASS) { |
| 397 | if (setsockopt(sf, IPPROTO_ICMPV6, ICMP6_FILTER, &i6f, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 398 | sizeof(i6f)) < 0) { |
| 399 | tst_resm(TFAIL | TERRNO, |
| 400 | "setsockopt ICMP6_FILTER"); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | if (ic6_send1(ftab[i].ft_tname, ftab[i].ft_sndtype)) |
| 404 | continue; |
| 405 | rv = ic6_recv1(ftab[i].ft_tname, sall, sf); |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 406 | } else |
| 407 | rv = -1; |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 408 | |
| 409 | if (rv < 0) |
| 410 | continue; |
| 411 | if (rv != ftab[i].ft_expected) |
Garrett Cooper | 38097ab | 2010-12-17 03:23:52 -0800 | [diff] [blame] | 412 | tst_resm(TFAIL, "%s: rv %d != expected %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 413 | ftab[i].ft_tname, rv, ftab[i].ft_expected); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 414 | else |
Ken Dreyer | 8c97e9d | 2014-12-09 16:26:00 -0700 | [diff] [blame] | 415 | tst_resm(TPASS, "%s", ftab[i].ft_tname); |
mridge | 949c5b1 | 2006-01-03 23:01:28 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 419 | int TST_TOTAL = ETCOUNT; |