1 /*- 2 * Copyright (c) 2007-2009 Bruce Simpson. 3 * Copyright (c) 2000 Wilbert De Graaf. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * Diagnostic and test utility for multicast sockets. 33 * XXX: This file currently assumes INET support in the base system. 34 * TODO: Support embedded KAME Scope ID in IPv6 group addresses. 35 * TODO: Use IPv4 link-local address when source address selection 36 * is implemented; use MCAST_JOIN_SOURCE for IPv4. 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/errno.h> 45 #include <sys/socket.h> 46 #include <sys/time.h> 47 #include <sys/ioctl.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/ethernet.h> 52 #ifdef INET 53 #include <netinet/in.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/ip_var.h> 57 #endif 58 #ifdef INET6 59 #include <netinet/in.h> 60 #include <netinet/ip6.h> 61 #endif 62 63 #include <assert.h> 64 #include <stdlib.h> 65 #include <stdio.h> 66 #include <string.h> 67 #include <ctype.h> 68 #include <errno.h> 69 #include <err.h> 70 #include <unistd.h> 71 72 #include <arpa/inet.h> 73 #include <netdb.h> 74 #include <ifaddrs.h> 75 76 union sockunion { 77 struct sockaddr_storage ss; 78 struct sockaddr sa; 79 struct sockaddr_dl sdl; 80 #ifdef INET 81 struct sockaddr_in sin; 82 #endif 83 #ifdef INET6 84 struct sockaddr_in6 sin6; 85 #endif 86 }; 87 typedef union sockunion sockunion_t; 88 89 union mrequnion { 90 #ifdef INET 91 struct ip_mreq mr; 92 struct ip_mreq_source mrs; 93 #endif 94 #ifdef INET6 95 struct ipv6_mreq mr6; 96 struct group_source_req gr; 97 #endif 98 }; 99 typedef union mrequnion mrequnion_t; 100 101 #define MAX_ADDRS 20 102 #define STR_SIZE 20 103 #define LINE_LENGTH 80 104 105 #ifdef INET 106 static int __ifindex_to_primary_ip(const uint32_t, struct in_addr *); 107 #endif 108 static uint32_t parse_cmd_args(sockunion_t *, sockunion_t *, 109 const char *, const char *, const char *); 110 static void process_file(char *, int, int); 111 static void process_cmd(char*, int, int, FILE *); 112 static int su_cmp(const void *, const void *); 113 static void usage(void); 114 115 /* 116 * Ordering predicate for qsort(). 117 */ 118 static int 119 su_cmp(const void *a, const void *b) 120 { 121 const sockunion_t *sua = (const sockunion_t *)a; 122 const sockunion_t *sub = (const sockunion_t *)b; 123 124 assert(sua->sa.sa_family == sub->sa.sa_family); 125 126 switch (sua->sa.sa_family) { 127 #ifdef INET 128 case AF_INET: 129 return ((int)(sua->sin.sin_addr.s_addr - 130 sub->sin.sin_addr.s_addr)); 131 break; 132 #endif 133 #ifdef INET6 134 case AF_INET6: 135 return (memcmp(&sua->sin6.sin6_addr, &sub->sin6.sin6_addr, 136 sizeof(struct in6_addr))); 137 break; 138 #endif 139 default: 140 break; 141 } 142 143 assert(sua->sa.sa_len == sub->sa.sa_len); 144 return (memcmp(sua, sub, sua->sa.sa_len)); 145 } 146 147 #ifdef INET 148 /* 149 * Internal: Map an interface index to primary IPv4 address. 150 * This is somewhat inefficient. This is a useful enough operation 151 * that it probably belongs in the C library. 152 * Return zero if found, -1 on error, 1 on not found. 153 */ 154 static int 155 __ifindex_to_primary_ip(const uint32_t ifindex, struct in_addr *pina) 156 { 157 char ifname[IFNAMSIZ]; 158 struct ifaddrs *ifa; 159 struct ifaddrs *ifaddrs; 160 sockunion_t *psu; 161 int retval; 162 163 assert(ifindex != 0); 164 165 retval = -1; 166 if (if_indextoname(ifindex, ifname) == NULL) 167 return (retval); 168 if (getifaddrs(&ifaddrs) < 0) 169 return (retval); 170 171 /* 172 * Find the ifaddr entry corresponding to the interface name, 173 * and return the first matching IPv4 address. 174 */ 175 retval = 1; 176 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { 177 if (strcmp(ifa->ifa_name, ifname) != 0) 178 continue; 179 psu = (sockunion_t *)ifa->ifa_addr; 180 if (psu && psu->sa.sa_family == AF_INET) { 181 retval = 0; 182 memcpy(pina, &psu->sin.sin_addr, 183 sizeof(struct in_addr)); 184 break; 185 } 186 } 187 188 if (retval != 0) 189 errno = EADDRNOTAVAIL; /* XXX */ 190 191 freeifaddrs(ifaddrs); 192 return (retval); 193 } 194 #endif /* INET */ 195 196 int 197 main(int argc, char **argv) 198 { 199 char line[LINE_LENGTH]; 200 char *p; 201 int i, s, s6; 202 203 s = -1; 204 s6 = -1; 205 #ifdef INET 206 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 207 if (s == -1 && errno != EAFNOSUPPORT) 208 err(1, "can't open IPv4 socket"); 209 #endif 210 #ifdef INET6 211 s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 212 if (s6 == -1 && errno != EAFNOSUPPORT) 213 err(1, "can't open IPv6 socket"); 214 #endif 215 if (s == -1 && s6 == -1) 216 errc(1, EPROTONOSUPPORT, "can't open socket"); 217 218 if (argc < 2) { 219 if (isatty(STDIN_FILENO)) { 220 printf("multicast membership test program; " 221 "enter ? for list of commands\n"); 222 } 223 do { 224 if (fgets(line, sizeof(line), stdin) != NULL) { 225 if (line[0] != 'f') 226 process_cmd(line, s, s6, stdin); 227 else { 228 /* Get the filename */ 229 for (i = 1; isblank(line[i]); i++); 230 if ((p = (char*)strchr(line, '\n')) 231 != NULL) 232 *p = '\0'; 233 process_file(&line[i], s, s6); 234 } 235 } 236 } while (!feof(stdin)); 237 } else { 238 for (i = 1; i < argc; i++) { 239 process_file(argv[i], s, s6); 240 } 241 } 242 243 if (s != -1) 244 close(s); 245 if (s6 != -1) 246 close(s6); 247 248 exit (0); 249 } 250 251 static void 252 process_file(char *fname, int s, int s6) 253 { 254 char line[80]; 255 FILE *fp; 256 char *lineptr; 257 258 fp = fopen(fname, "r"); 259 if (fp == NULL) { 260 warn("fopen"); 261 return; 262 } 263 264 /* Skip comments and empty lines. */ 265 while (fgets(line, sizeof(line), fp) != NULL) { 266 lineptr = line; 267 while (isblank(*lineptr)) 268 lineptr++; 269 if (*lineptr != '#' && *lineptr != '\n') 270 process_cmd(lineptr, s, s6, fp); 271 } 272 273 fclose(fp); 274 } 275 276 /* 277 * Parse join/leave/allow/block arguments, given: 278 * str1: group (as AF_INET or AF_INET6 printable) 279 * str2: ifname 280 * str3: optional source address (may be NULL). 281 * This argument must have the same parsed address family as str1. 282 * Return the ifindex of ifname, or 0 if any parse element failed. 283 */ 284 static uint32_t 285 parse_cmd_args(sockunion_t *psu, sockunion_t *psu2, 286 const char *str1, const char *str2, const char *str3) 287 { 288 struct addrinfo hints; 289 struct addrinfo *res; 290 uint32_t ifindex; 291 int af, error; 292 293 assert(psu != NULL); 294 assert(str1 != NULL); 295 assert(str2 != NULL); 296 297 af = AF_UNSPEC; 298 299 ifindex = if_nametoindex(str2); 300 if (ifindex == 0) 301 return (0); 302 303 memset(&hints, 0, sizeof(struct addrinfo)); 304 hints.ai_flags = AI_NUMERICHOST; 305 hints.ai_family = PF_UNSPEC; 306 hints.ai_socktype = SOCK_DGRAM; 307 308 memset(psu, 0, sizeof(sockunion_t)); 309 psu->sa.sa_family = AF_UNSPEC; 310 311 error = getaddrinfo(str1, "0", &hints, &res); 312 if (error) { 313 warnx("getaddrinfo: %s", gai_strerror(error)); 314 return (0); 315 } 316 assert(res != NULL); 317 af = res->ai_family; 318 memcpy(psu, res->ai_addr, res->ai_addrlen); 319 freeaddrinfo(res); 320 321 /* sscanf() may pass the empty string. */ 322 if (psu2 != NULL && str3 != NULL && *str3 != '\0') { 323 memset(psu2, 0, sizeof(sockunion_t)); 324 psu2->sa.sa_family = AF_UNSPEC; 325 326 /* look for following address family; str3 is *optional*. */ 327 hints.ai_family = af; 328 error = getaddrinfo(str3, "0", &hints, &res); 329 if (error) { 330 warnx("getaddrinfo: %s", gai_strerror(error)); 331 ifindex = 0; 332 } else { 333 if (af != res->ai_family) { 334 errno = EINVAL; /* XXX */ 335 ifindex = 0; 336 } 337 memcpy(psu2, res->ai_addr, res->ai_addrlen); 338 freeaddrinfo(res); 339 } 340 } 341 342 return (ifindex); 343 } 344 345 static __inline int 346 af2sock(const int af, int s, int s6) 347 { 348 349 #ifdef INET 350 if (af == AF_INET) 351 return (s); 352 #endif 353 #ifdef INET6 354 if (af == AF_INET6) 355 return (s6); 356 #endif 357 return (-1); 358 } 359 360 static __inline int 361 af2socklen(const int af) 362 { 363 364 #ifdef INET 365 if (af == AF_INET) 366 return (sizeof(struct sockaddr_in)); 367 #endif 368 #ifdef INET6 369 if (af == AF_INET6) 370 return (sizeof(struct sockaddr_in6)); 371 #endif 372 return (-1); 373 } 374 375 static void 376 process_cmd(char *cmd, int s, int s6, FILE *fp __unused) 377 { 378 char str1[STR_SIZE]; 379 char str2[STR_SIZE]; 380 char str3[STR_SIZE]; 381 mrequnion_t mr; 382 sockunion_t su, su2; 383 struct ifreq ifr; 384 char *line; 385 char *toptname; 386 void *optval; 387 uint32_t fmode, ifindex; 388 socklen_t optlen; 389 int af, error, f, flags, i, level, n, optname; 390 391 af = AF_UNSPEC; 392 su.sa.sa_family = AF_UNSPEC; 393 su2.sa.sa_family = AF_UNSPEC; 394 395 line = cmd; 396 while (isblank(*++line)) 397 ; /* Skip whitespace. */ 398 399 switch (*cmd) { 400 case '?': 401 usage(); 402 break; 403 404 case 'q': 405 close(s); 406 exit(0); 407 408 case 's': 409 if ((sscanf(line, "%d", &n) != 1) || (n < 1)) { 410 printf("-1\n"); 411 break; 412 } 413 sleep(n); 414 printf("ok\n"); 415 break; 416 417 case 'j': 418 case 'l': 419 str3[0] = '\0'; 420 toptname = ""; 421 sscanf(line, "%s %s %s", str1, str2, str3); 422 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3); 423 if (ifindex == 0) { 424 printf("-1\n"); 425 break; 426 } 427 af = su.sa.sa_family; 428 #ifdef INET 429 if (af == AF_INET) { 430 struct in_addr ina; 431 432 error = __ifindex_to_primary_ip(ifindex, &ina); 433 if (error != 0) { 434 warn("primary_ip_lookup %s", str2); 435 printf("-1\n"); 436 break; 437 } 438 level = IPPROTO_IP; 439 440 if (su2.sa.sa_family != AF_UNSPEC) { 441 mr.mrs.imr_multiaddr = su.sin.sin_addr; 442 mr.mrs.imr_sourceaddr = su2.sin.sin_addr; 443 mr.mrs.imr_interface = ina; 444 optname = (*cmd == 'j') ? 445 IP_ADD_SOURCE_MEMBERSHIP : 446 IP_DROP_SOURCE_MEMBERSHIP; 447 toptname = (*cmd == 'j') ? 448 "IP_ADD_SOURCE_MEMBERSHIP" : 449 "IP_DROP_SOURCE_MEMBERSHIP"; 450 optval = (void *)&mr.mrs; 451 optlen = sizeof(mr.mrs); 452 } else { 453 mr.mr.imr_multiaddr = su.sin.sin_addr; 454 mr.mr.imr_interface = ina; 455 optname = (*cmd == 'j') ? 456 IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP; 457 toptname = (*cmd == 'j') ? 458 "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP"; 459 optval = (void *)&mr.mr; 460 optlen = sizeof(mr.mr); 461 } 462 if (s < 0) { 463 warnc(EPROTONOSUPPORT, "setsockopt %s", 464 toptname); 465 } else if (setsockopt(s, level, optname, optval, 466 optlen) == 0) { 467 printf("ok\n"); 468 break; 469 } else { 470 warn("setsockopt %s", toptname); 471 } 472 } 473 #ifdef INET6 474 else 475 #endif /* INET with INET6 */ 476 #endif /* INET */ 477 #ifdef INET6 478 if (af == AF_INET6) { 479 level = IPPROTO_IPV6; 480 if (su2.sa.sa_family != AF_UNSPEC) { 481 mr.gr.gsr_interface = ifindex; 482 mr.gr.gsr_group = su.ss; 483 mr.gr.gsr_source = su2.ss; 484 optname = (*cmd == 'j') ? 485 MCAST_JOIN_SOURCE_GROUP: 486 MCAST_LEAVE_SOURCE_GROUP; 487 toptname = (*cmd == 'j') ? 488 "MCAST_JOIN_SOURCE_GROUP": 489 "MCAST_LEAVE_SOURCE_GROUP"; 490 optval = (void *)&mr.gr; 491 optlen = sizeof(mr.gr); 492 } else { 493 mr.mr6.ipv6mr_multiaddr = su.sin6.sin6_addr; 494 mr.mr6.ipv6mr_interface = ifindex; 495 optname = (*cmd == 'j') ? 496 IPV6_JOIN_GROUP : 497 IPV6_LEAVE_GROUP; 498 toptname = (*cmd == 'j') ? 499 "IPV6_JOIN_GROUP" : 500 "IPV6_LEAVE_GROUP"; 501 optval = (void *)&mr.mr6; 502 optlen = sizeof(mr.mr6); 503 } 504 if (s6 < 0) { 505 warnc(EPROTONOSUPPORT, "setsockopt %s", 506 toptname); 507 } else if (setsockopt(s6, level, optname, optval, 508 optlen) == 0) { 509 printf("ok\n"); 510 break; 511 } else { 512 warn("setsockopt %s", toptname); 513 } 514 } 515 #endif /* INET6 */ 516 /* FALLTHROUGH */ 517 printf("-1\n"); 518 break; 519 520 /* 521 * Set the socket to include or exclude filter mode, and 522 * add some sources to the filterlist, using the full-state API. 523 */ 524 case 'i': 525 case 'e': { 526 sockunion_t sources[MAX_ADDRS]; 527 struct addrinfo hints; 528 struct addrinfo *res; 529 char *cp; 530 int af1; 531 532 n = 0; 533 fmode = (*cmd == 'i') ? MCAST_INCLUDE : MCAST_EXCLUDE; 534 if ((sscanf(line, "%s %s %d", str1, str2, &n)) != 3) { 535 printf("-1\n"); 536 break; 537 } 538 539 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL); 540 if (ifindex == 0 || n < 0 || n > MAX_ADDRS) { 541 printf("-1\n"); 542 break; 543 } 544 af = su.sa.sa_family; 545 if (af2sock(af, s, s6) == -1) { 546 warnc(EPROTONOSUPPORT, "setsourcefilter"); 547 break; 548 } 549 550 memset(&hints, 0, sizeof(struct addrinfo)); 551 hints.ai_flags = AI_NUMERICHOST; 552 hints.ai_family = af; 553 hints.ai_socktype = SOCK_DGRAM; 554 555 for (i = 0; i < n; i++) { 556 sockunion_t *psu = (sockunion_t *)&sources[i]; 557 /* 558 * Trim trailing whitespace, as getaddrinfo() 559 * can't cope with it. 560 */ 561 fgets(str1, sizeof(str1), fp); 562 cp = strchr(str1, '\n'); 563 if (cp != NULL) 564 *cp = '\0'; 565 566 res = NULL; 567 error = getaddrinfo(str1, "0", &hints, &res); 568 if (error) 569 break; 570 assert(res != NULL); 571 572 memset(psu, 0, sizeof(sockunion_t)); 573 af1 = res->ai_family; 574 if (af1 == af) 575 memcpy(psu, res->ai_addr, res->ai_addrlen); 576 freeaddrinfo(res); 577 if (af1 != af) 578 break; 579 } 580 if (i < n) { 581 if (error) 582 warnx("getaddrinfo: %s", gai_strerror(error)); 583 printf("-1\n"); 584 break; 585 } 586 if (setsourcefilter(af2sock(af, s, s6), ifindex, 587 &su.sa, su.sa.sa_len, fmode, n, &sources[0].ss) != 0) 588 warn("setsourcefilter"); 589 else 590 printf("ok\n"); 591 } break; 592 593 /* 594 * Allow or block traffic from a source, using the 595 * delta based api. 596 */ 597 case 't': 598 case 'b': { 599 str3[0] = '\0'; 600 toptname = ""; 601 sscanf(line, "%s %s %s", str1, str2, str3); 602 ifindex = parse_cmd_args(&su, &su2, str1, str2, str3); 603 if (ifindex == 0 || su2.sa.sa_family == AF_UNSPEC) { 604 printf("-1\n"); 605 break; 606 } 607 af = su.sa.sa_family; 608 if (af2sock(af, s, s6) == -1) { 609 warnc(EPROTONOSUPPORT, "getsourcefilter"); 610 break; 611 } 612 613 /* First determine our current filter mode. */ 614 n = 0; 615 if (getsourcefilter(af2sock(af, s, s6), ifindex, 616 &su.sa, su.sa.sa_len, &fmode, &n, NULL) != 0) { 617 warn("getsourcefilter"); 618 break; 619 } 620 #ifdef INET 621 if (af == AF_INET) { 622 struct in_addr ina; 623 624 error = __ifindex_to_primary_ip(ifindex, &ina); 625 if (error != 0) { 626 warn("primary_ip_lookup %s", str2); 627 printf("-1\n"); 628 break; 629 } 630 level = IPPROTO_IP; 631 optval = (void *)&mr.mrs; 632 optlen = sizeof(mr.mrs); 633 mr.mrs.imr_multiaddr = su.sin.sin_addr; 634 mr.mrs.imr_sourceaddr = su2.sin.sin_addr; 635 mr.mrs.imr_interface = ina; 636 if (fmode == MCAST_EXCLUDE) { 637 /* Any-source mode socket membership. */ 638 optname = (*cmd == 't') ? 639 IP_UNBLOCK_SOURCE : 640 IP_BLOCK_SOURCE; 641 toptname = (*cmd == 't') ? 642 "IP_UNBLOCK_SOURCE" : 643 "IP_BLOCK_SOURCE"; 644 } else { 645 /* Source-specific mode socket membership. */ 646 optname = (*cmd == 't') ? 647 IP_ADD_SOURCE_MEMBERSHIP : 648 IP_DROP_SOURCE_MEMBERSHIP; 649 toptname = (*cmd == 't') ? 650 "IP_ADD_SOURCE_MEMBERSHIP" : 651 "IP_DROP_SOURCE_MEMBERSHIP"; 652 } 653 if (setsockopt(s, level, optname, optval, 654 optlen) == 0) { 655 printf("ok\n"); 656 break; 657 } else { 658 warn("setsockopt %s", toptname); 659 } 660 } 661 #ifdef INET6 662 else 663 #endif /* INET with INET6 */ 664 #endif /* INET */ 665 #ifdef INET6 666 if (af == AF_INET6) { 667 level = IPPROTO_IPV6; 668 mr.gr.gsr_interface = ifindex; 669 mr.gr.gsr_group = su.ss; 670 mr.gr.gsr_source = su2.ss; 671 if (fmode == MCAST_EXCLUDE) { 672 /* Any-source mode socket membership. */ 673 optname = (*cmd == 't') ? 674 MCAST_UNBLOCK_SOURCE : 675 MCAST_BLOCK_SOURCE; 676 toptname = (*cmd == 't') ? 677 "MCAST_UNBLOCK_SOURCE" : 678 "MCAST_BLOCK_SOURCE"; 679 } else { 680 /* Source-specific mode socket membership. */ 681 optname = (*cmd == 't') ? 682 MCAST_JOIN_SOURCE_GROUP : 683 MCAST_LEAVE_SOURCE_GROUP; 684 toptname = (*cmd == 't') ? 685 "MCAST_JOIN_SOURCE_GROUP": 686 "MCAST_LEAVE_SOURCE_GROUP"; 687 } 688 optval = (void *)&mr.gr; 689 optlen = sizeof(mr.gr); 690 if (setsockopt(s6, level, optname, optval, 691 optlen) == 0) { 692 printf("ok\n"); 693 break; 694 } else { 695 warn("setsockopt %s", toptname); 696 } 697 } 698 #endif /* INET6 */ 699 /* FALLTHROUGH */ 700 printf("-1\n"); 701 } break; 702 703 case 'g': { 704 sockunion_t sources[MAX_ADDRS]; 705 char addrbuf[NI_MAXHOST]; 706 int nreqsrc, nsrc; 707 708 if ((sscanf(line, "%s %s %d", str1, str2, &nreqsrc)) != 3) { 709 printf("-1\n"); 710 break; 711 } 712 ifindex = parse_cmd_args(&su, NULL, str1, str2, NULL); 713 if (ifindex == 0 || (n < 0 || n > MAX_ADDRS)) { 714 printf("-1\n"); 715 break; 716 } 717 718 af = su.sa.sa_family; 719 if (af2sock(af, s, s6) == -1) { 720 warnc(EPROTONOSUPPORT, "getsourcefilter"); 721 break; 722 } 723 nsrc = nreqsrc; 724 if (getsourcefilter(af2sock(af, s, s6), ifindex, &su.sa, 725 su.sa.sa_len, &fmode, &nsrc, &sources[0].ss) != 0) { 726 warn("getsourcefilter"); 727 printf("-1\n"); 728 break; 729 } 730 printf("%s\n", (fmode == MCAST_INCLUDE) ? "include" : 731 "exclude"); 732 printf("%d\n", nsrc); 733 734 nsrc = MIN(nreqsrc, nsrc); 735 fprintf(stderr, "hexdump of sources:\n"); 736 uint8_t *bp = (uint8_t *)&sources[0]; 737 for (i = 0; i < (nsrc * sizeof(sources[0])); i++) { 738 fprintf(stderr, "%02x", bp[i]); 739 } 740 fprintf(stderr, "\nend hexdump\n"); 741 742 qsort(sources, nsrc, af2socklen(af), su_cmp); 743 for (i = 0; i < nsrc; i++) { 744 sockunion_t *psu = (sockunion_t *)&sources[i]; 745 addrbuf[0] = '\0'; 746 error = getnameinfo(&psu->sa, psu->sa.sa_len, 747 addrbuf, sizeof(addrbuf), NULL, 0, 748 NI_NUMERICHOST); 749 if (error) 750 warnx("getnameinfo: %s", gai_strerror(error)); 751 else 752 printf("%s\n", addrbuf); 753 } 754 printf("ok\n"); 755 } break; 756 757 /* link-layer stuff follows. */ 758 759 case 'a': 760 case 'd': { 761 struct sockaddr_dl *dlp; 762 struct ether_addr *ep; 763 764 memset(&ifr, 0, sizeof(struct ifreq)); 765 dlp = (struct sockaddr_dl *)&ifr.ifr_addr; 766 dlp->sdl_len = sizeof(struct sockaddr_dl); 767 dlp->sdl_family = AF_LINK; 768 dlp->sdl_index = 0; 769 dlp->sdl_nlen = 0; 770 dlp->sdl_alen = ETHER_ADDR_LEN; 771 dlp->sdl_slen = 0; 772 if (sscanf(line, "%s %s", str1, str2) != 2) { 773 warnc(EINVAL, "sscanf"); 774 break; 775 } 776 ep = ether_aton(str2); 777 if (ep == NULL) { 778 warnc(EINVAL, "ether_aton"); 779 break; 780 } 781 strlcpy(ifr.ifr_name, str1, IF_NAMESIZE); 782 memcpy(LLADDR(dlp), ep, ETHER_ADDR_LEN); 783 if (ioctl(s, (*cmd == 'a') ? SIOCADDMULTI : SIOCDELMULTI, 784 &ifr) == -1) { 785 warn("ioctl SIOCADDMULTI/SIOCDELMULTI"); 786 printf("-1\n"); 787 } else 788 printf("ok\n"); 789 break; 790 } 791 792 case 'm': 793 fprintf(stderr, 794 "warning: IFF_ALLMULTI cannot be set from userland " 795 "in FreeBSD; command ignored.\n"); 796 printf("-1\n"); 797 break; 798 799 case 'p': 800 if (sscanf(line, "%s %u", ifr.ifr_name, &f) != 2) { 801 printf("-1\n"); 802 break; 803 } 804 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) { 805 warn("ioctl SIOCGIFFLAGS"); 806 break; 807 } 808 flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16); 809 if (f == 0) { 810 flags &= ~IFF_PPROMISC; 811 } else { 812 flags |= IFF_PPROMISC; 813 } 814 ifr.ifr_flags = flags & 0xffff; 815 ifr.ifr_flagshigh = flags >> 16; 816 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) 817 warn("ioctl SIOCGIFFLAGS"); 818 else 819 printf( "changed to 0x%08x\n", flags ); 820 break; 821 822 case '\n': 823 break; 824 default: 825 printf("invalid command\n"); 826 break; 827 } 828 } 829 830 static void 831 usage(void) 832 { 833 834 printf("j mcast-addr ifname [src-addr] - join IP multicast group\n"); 835 printf("l mcast-addr ifname [src-addr] - leave IP multicast group\n"); 836 printf( 837 "i mcast-addr ifname n - set n include mode src filter\n"); 838 printf( 839 "e mcast-addr ifname n - set n exclude mode src filter\n"); 840 printf("t mcast-addr ifname src-addr - allow traffic from src\n"); 841 printf("b mcast-addr ifname src-addr - block traffic from src\n"); 842 printf("g mcast-addr ifname n - get and show n src filters\n"); 843 printf("a ifname mac-addr - add link multicast filter\n"); 844 printf("d ifname mac-addr - delete link multicast filter\n"); 845 printf("m ifname 1/0 - set/clear ether allmulti flag\n"); 846 printf("p ifname 1/0 - set/clear ether promisc flag\n"); 847 printf("f filename - read command(s) from file\n"); 848 printf("s seconds - sleep for some time\n"); 849 printf("q - quit\n"); 850 } 851 852