1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
|
/*
*
* Modified for AF_INET6 by Pedro Roque
*
* <roque@di.fc.ul.pt>
*
* Original copyright notice included bellow
*/
/*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Mike Muuss.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* P I N G . C
*
* Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
* measure round-trip-delays and packet loss across network paths.
*
* Author -
* Mike Muuss
* U. S. Army Ballistic Research Laboratory
* December, 1983
*
* Status -
* Public Domain. Distribution Unlimited.
* Bugs -
* More statistics could always be gathered.
* If kernel does not support non-raw ICMP sockets or
* if -N option is used, this program has to run SUID to ROOT or
* with net_cap_raw enabled.
*/
#include "ping.h"
#if defined(ENABLE_PING6_RTHDR) && !defined(ENABLE_PING6_RTHDR_RFC3542)
#ifndef IPV6_SRCRT_TYPE_0
#define IPV6_SRCRT_TYPE_0 0
#endif
#endif
ping_func_set_st ping6_func_set = {
.send_probe = ping6_send_probe,
.receive_error_msg = ping6_receive_error_msg,
.parse_reply = ping6_parse_reply,
.install_filter = ping6_install_filter
};
#define BIT_CLEAR(nr, addr) do { ((__u32 *)(addr))[(nr) >> 5] &= ~(1U << ((nr) & 31)); } while(0)
#define BIT_SET(nr, addr) do { ((__u32 *)(addr))[(nr) >> 5] |= (1U << ((nr) & 31)); } while(0)
#define BIT_TEST(nr, addr) do { (__u32 *)(addr))[(nr) >> 5] & (1U << ((nr) & 31)); } while(0)
#ifndef SCOPE_DELIMITER
# define SCOPE_DELIMITER '%'
#endif
__u32 flowlabel;
__u32 tclass;
#ifdef ENABLE_PING6_RTHDR
struct cmsghdr *srcrt;
#endif
static struct sockaddr_in6 whereto;
static struct sockaddr_in6 firsthop;
static unsigned char cmsgbuf[4096];
static int cmsglen = 0;
static int pr_icmph(__u8 type, __u8 code, __u32 info);
void ping6_usage(unsigned) __attribute((noreturn));
struct sockaddr_in6 source6 = { .sin6_family = AF_INET6 };
char *device;
int pmtudisc=-1;
#if defined(USE_GCRYPT) || defined(USE_OPENSSL) || defined(USE_NETTLE)
#include "iputils_md5dig.h"
#define USE_CRYPTO
#endif
/* Node Information query */
int ni_query = -1;
int ni_flag = 0;
void *ni_subject = NULL;
int ni_subject_len = 0;
int ni_subject_type = -1;
char *ni_group;
static inline int ntohsp(__u16 *p)
{
__u16 v;
memcpy(&v, p, sizeof(v));
return ntohs(v);
}
#if defined(ENABLE_PING6_RTHDR) && !defined(ENABLE_PING6_RTHDR_RFC3542)
size_t inet6_srcrt_space(int type, int segments)
{
if (type != 0 || segments > 24)
return 0;
return (sizeof(struct cmsghdr) + sizeof(struct ip6_rthdr0) +
segments * sizeof(struct in6_addr));
}
extern struct cmsghdr * inet6_srcrt_init(void *bp, int type)
{
struct cmsghdr *cmsg;
if (type)
return NULL;
memset(bp, 0, sizeof(struct cmsghdr) + sizeof(struct ip6_rthdr0));
cmsg = (struct cmsghdr *) bp;
cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct ip6_rthdr0);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_RTHDR;
return cmsg;
}
int inet6_srcrt_add(struct cmsghdr *cmsg, const struct in6_addr *addr)
{
struct ip6_rthdr0 *hdr;
hdr = (struct ip6_rthdr0 *) CMSG_DATA(cmsg);
cmsg->cmsg_len += sizeof(struct in6_addr);
hdr->ip6r0_len += sizeof(struct in6_addr) / 8;
memcpy(&hdr->ip6r0_addr[hdr->ip6r0_segleft++], addr,
sizeof(struct in6_addr));
return 0;
}
#endif
unsigned int if_name2index(const char *ifname)
{
unsigned int i = if_nametoindex(ifname);
if (!i) {
fprintf(stderr, "ping: unknown iface %s\n", ifname);
exit(2);
}
return i;
}
struct niquery_option {
char *name;
int namelen;
int has_arg;
int data;
int (*handler)(int index, const char *arg);
};
#define NIQUERY_OPTION(_name, _has_arg, _data, _handler) \
{ \
.name = _name, \
.namelen = sizeof(_name) - 1, \
.has_arg = _has_arg, \
.data = _data, \
.handler = _handler \
}
static int niquery_option_name_handler(int index, const char *arg);
static int niquery_option_ipv6_handler(int index, const char *arg);
static int niquery_option_ipv6_flag_handler(int index, const char *arg);
static int niquery_option_ipv4_handler(int index, const char *arg);
static int niquery_option_ipv4_flag_handler(int index, const char *arg);
static int niquery_option_subject_addr_handler(int index, const char *arg);
static int niquery_option_subject_name_handler(int index, const char *arg);
static int niquery_option_help_handler(int index, const char *arg);
struct niquery_option niquery_options[] = {
NIQUERY_OPTION("name", 0, 0, niquery_option_name_handler),
NIQUERY_OPTION("fqdn", 0, 0, niquery_option_name_handler),
NIQUERY_OPTION("ipv6", 0, 0, niquery_option_ipv6_handler),
NIQUERY_OPTION("ipv6-all", 0, NI_IPV6ADDR_F_ALL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-compatible", 0, NI_IPV6ADDR_F_COMPAT, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-linklocal", 0, NI_IPV6ADDR_F_LINKLOCAL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-sitelocal", 0, NI_IPV6ADDR_F_SITELOCAL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv6-global", 0, NI_IPV6ADDR_F_GLOBAL, niquery_option_ipv6_flag_handler),
NIQUERY_OPTION("ipv4", 0, 0, niquery_option_ipv4_handler),
NIQUERY_OPTION("ipv4-all", 0, NI_IPV4ADDR_F_ALL, niquery_option_ipv4_flag_handler),
NIQUERY_OPTION("subject-ipv6", 1, NI_SUBJ_IPV6, niquery_option_subject_addr_handler),
NIQUERY_OPTION("subject-ipv4", 1, NI_SUBJ_IPV4, niquery_option_subject_addr_handler),
NIQUERY_OPTION("subject-name", 1, 0, niquery_option_subject_name_handler),
NIQUERY_OPTION("subject-fqdn", 1, -1, niquery_option_subject_name_handler),
NIQUERY_OPTION("help", 0, 0, niquery_option_help_handler),
{},
};
static inline int niquery_is_enabled(void)
{
return ni_query >= 0;
}
#if PING6_NONCE_MEMORY
__u8 *ni_nonce_ptr;
#else
struct {
struct timeval tv;
pid_t pid;
} ni_nonce_secret;
#endif
static void niquery_init_nonce(void)
{
#if PING6_NONCE_MEMORY
struct timeval tv;
unsigned long seed;
seed = (unsigned long)getpid();
if (!gettimeofday(&tv, NULL))
seed ^= tv.tv_usec;
srand(seed);
ni_nonce_ptr = calloc(NI_NONCE_SIZE, MAX_DUP_CHK);
if (!ni_nonce_ptr) {
perror("ping6: calloc");
exit(2);
}
ni_nonce_ptr[0] = ~0;
#else
gettimeofday(&ni_nonce_secret.tv, NULL);
ni_nonce_secret.pid = getpid();
#endif
}
#if !PING6_NONCE_MEMORY
static int niquery_nonce(__u8 *nonce, int fill)
{
# ifdef USE_CRYPTO
static __u8 digest[MD5_DIGEST_LENGTH];
static int seq = -1;
if (fill || seq != *(__u16 *)nonce || seq < 0) {
MD5_CTX ctxt;
MD5_Init(&ctxt);
MD5_Update(&ctxt, &ni_nonce_secret, sizeof(ni_nonce_secret));
MD5_Update(&ctxt, nonce, sizeof(__u16));
MD5_Final(digest, &ctxt);
seq = *(__u16 *)nonce;
}
if (fill) {
memcpy(nonce + sizeof(__u16), digest, NI_NONCE_SIZE - sizeof(__u16));
return 0;
} else {
if (memcmp(nonce + sizeof(__u16), digest, NI_NONCE_SIZE - sizeof(__u16)))
return -1;
return ntohsp((__u16 *)nonce);
}
# else
fprintf(stderr, "ping6: function not available; crypto disabled\n");
exit(3);
# endif
}
#endif
static inline void niquery_fill_nonce(__u16 seq, __u8 *nonce)
{
__u16 v = htons(seq);
#if PING6_NONCE_MEMORY
int i;
memcpy(&ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], &v, sizeof(v));
for (i = sizeof(v); i < NI_NONCE_SIZE; i++)
ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK) + i] = 0x100 * (rand() / (RAND_MAX + 1.0));
memcpy(nonce, &ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], NI_NONCE_SIZE);
#else
memcpy(nonce, &v, sizeof(v));
niquery_nonce(nonce, 1);
#endif
}
static inline int niquery_check_nonce(__u8 *nonce)
{
#if PING6_NONCE_MEMORY
__u16 seq = ntohsp((__u16 *)nonce);
if (memcmp(nonce, &ni_nonce_ptr[NI_NONCE_SIZE * (seq % MAX_DUP_CHK)], NI_NONCE_SIZE))
return -1;
return seq;
#else
return niquery_nonce(nonce, 0);
#endif
}
static int niquery_set_qtype(int type)
{
if (niquery_is_enabled() && ni_query != type) {
printf("Qtype conflict\n");
return -1;
}
ni_query = type;
return 0;
}
static int niquery_option_name_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_NAME) < 0)
return -1;
return 0;
}
static int niquery_option_ipv6_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV6ADDR) < 0)
return -1;
return 0;
}
static int niquery_option_ipv6_flag_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV6ADDR) < 0)
return -1;
ni_flag |= niquery_options[index].data;
return 0;
}
static int niquery_option_ipv4_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV4ADDR) < 0)
return -1;
return 0;
}
static int niquery_option_ipv4_flag_handler(int index, const char *arg)
{
if (niquery_set_qtype(NI_QTYPE_IPV4ADDR) < 0)
return -1;
ni_flag |= niquery_options[index].data;
return 0;
}
static inline int niquery_is_subject_valid(void)
{
return ni_subject_type >= 0 && ni_subject;
}
static int niquery_set_subject_type(int type)
{
if (niquery_is_subject_valid() && ni_subject_type != type) {
printf("Subject type conflict\n");
return -1;
}
ni_subject_type = type;
return 0;
}
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
#define OFFSET_OF(type,elem) ((size_t)&((type *)0)->elem)
static int niquery_option_subject_addr_handler(int index, const char *arg)
{
struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = getaddrinfo_flags };
struct addrinfo *result, *ai;
int status;
int offset;
if (niquery_set_subject_type(niquery_options[index].data) < 0)
return -1;
ni_subject_type = niquery_options[index].data;
switch (niquery_options[index].data) {
case NI_SUBJ_IPV6:
ni_subject_len = sizeof(struct in6_addr);
offset = OFFSET_OF(struct sockaddr_in6, sin6_addr);
hints.ai_family = AF_INET6;
break;
case NI_SUBJ_IPV4:
ni_subject_len = sizeof(struct in_addr);
offset = OFFSET_OF(struct sockaddr_in, sin_addr);
hints.ai_family = AF_INET;
break;
default:
/* should not happen. */
offset = -1;
}
status = getaddrinfo(arg, 0, &hints, &result);
if (status) {
fprintf(stderr, "ping6: %s: %s\n", arg, gai_strerror(status));
return -1;
}
for (ai = result; ai; ai = ai->ai_next) {
void *p = malloc(ni_subject_len);
if (!p)
continue;
memcpy(p, (__u8 *)ai->ai_addr + offset, ni_subject_len);
free(ni_subject);
ni_subject = p;
break;
}
freeaddrinfo(result);
return 0;
}
static int niquery_option_subject_name_handler(int index, const char *arg)
{
#ifdef USE_CRYPTO
static char nigroup_buf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ];
unsigned char *dnptrs[2], **dpp, **lastdnptr;
int n;
int i;
char *name, *p;
char *canonname = NULL, *idn = NULL;
unsigned char *buf = NULL;
size_t namelen;
size_t buflen;
int dots, fqdn = niquery_options[index].data;
MD5_CTX ctxt;
__u8 digest[MD5_DIGEST_LENGTH];
#ifdef USE_IDN
int rc;
#endif
if (niquery_set_subject_type(NI_SUBJ_NAME) < 0)
return -1;
#ifdef USE_IDN
name = stringprep_locale_to_utf8(arg);
if (!name) {
fprintf(stderr, "ping6: IDN support failed.\n");
exit(2);
}
#else
name = strdup(arg);
if (!name)
goto oomexit;
#endif
p = strchr(name, SCOPE_DELIMITER);
if (p) {
*p = '\0';
if (strlen(p + 1) >= IFNAMSIZ) {
fprintf(stderr, "ping6: too long scope name.\n");
exit(1);
}
}
#ifdef USE_IDN
rc = idna_to_ascii_8z(name, &idn, 0);
if (rc) {
fprintf(stderr, "ping6: IDN encoding error: %s\n",
idna_strerror(rc));
exit(2);
}
#else
idn = strdup(name);
if (!idn)
goto oomexit;
#endif
namelen = strlen(idn);
canonname = malloc(namelen + 1);
if (!canonname)
goto oomexit;
dots = 0;
for (i = 0; i < namelen + 1; i++) {
canonname[i] = isupper(idn[i]) ? tolower(idn[i]) : idn[i];
if (idn[i] == '.')
dots++;
}
if (fqdn == 0) {
/* guess if hostname is FQDN */
fqdn = dots ? 1 : -1;
}
buflen = namelen + 3 + 1; /* dn_comp() requrires strlen() + 3,
plus non-fqdn indicator. */
buf = malloc(buflen);
if (!buf) {
fprintf(stderr, "ping6: out of memory.\n");
goto errexit;
}
dpp = dnptrs;
lastdnptr = &dnptrs[ARRAY_SIZE(dnptrs)];
*dpp++ = (unsigned char *)buf;
*dpp++ = NULL;
n = dn_comp(canonname, (unsigned char *)buf, buflen, dnptrs, lastdnptr);
if (n < 0) {
fprintf(stderr, "ping6: Inappropriate subject name: %s\n", canonname);
goto errexit;
} else if (n >= buflen) {
fprintf(stderr, "ping6: dn_comp() returned too long result.\n");
goto errexit;
}
MD5_Init(&ctxt);
MD5_Update(&ctxt, buf, buf[0]);
MD5_Final(digest, &ctxt);
sprintf(nigroup_buf, "ff02::2:%02x%02x:%02x%02x%s%s",
digest[0], digest[1], digest[2], digest[3],
p ? "%" : "",
p ? p + 1 : "");
if (fqdn < 0)
buf[n] = 0;
free(ni_subject);
ni_group = nigroup_buf;
ni_subject = buf;
ni_subject_len = n + (fqdn < 0);
ni_group = nigroup_buf;
free(canonname);
free(idn);
free(name);
return 0;
oomexit:
fprintf(stderr, "ping6: out of memory.\n");
errexit:
free(buf);
free(canonname);
free(idn);
free(name);
exit(1);
#else
fprintf(stderr, "ping6: function not available; crypto disabled\n");
exit(3);
#endif
}
int niquery_option_help_handler(int index, const char *arg)
{
fprintf(stderr, "ping6 -N suboptions\n"
"\tHelp:\n"
"\t\thelp\n"
"\tQuery:\n"
"\t\tname,\n"
"\t\tipv6,ipv6-all,ipv6-compatible,ipv6-linklocal,ipv6-sitelocal,ipv6-global,\n"
"\t\tipv4,ipv4-all,\n"
"\tSubject:\n"
"\t\tsubject-ipv6=addr,subject-ipv4=addr,subject-name=name,subject-fqdn=name,\n"
);
exit(2);
}
int niquery_option_handler(const char *opt_arg)
{
struct niquery_option *p;
int i;
int ret = -1;
for (i = 0, p = niquery_options; p->name; i++, p++) {
if (strncmp(p->name, opt_arg, p->namelen))
continue;
if (!p->has_arg) {
if (opt_arg[p->namelen] == '\0') {
ret = p->handler(i, NULL);
if (ret >= 0)
break;
}
} else {
if (opt_arg[p->namelen] == '=') {
ret = p->handler(i, &opt_arg[p->namelen] + 1);
if (ret >= 0)
break;
}
}
}
if (!p->name)
ret = niquery_option_help_handler(0, NULL);
return ret;
}
int hextoui(const char *str)
{
unsigned long val;
char *ep;
errno = 0;
val = strtoul(str, &ep, 16);
if (*ep) {
if (!errno)
errno = EINVAL;
return -1;
}
if (val > UINT_MAX) {
errno = ERANGE;
return UINT_MAX;
}
return val;
}
int ping6_run(int argc, char **argv, struct addrinfo *ai, struct socket_st *sock)
{
static const struct addrinfo hints = { .ai_family = AF_INET6, .ai_flags = getaddrinfo_flags };
struct addrinfo *result = NULL;
int status;
int hold, packlen;
unsigned char *packet;
char *target;
struct icmp6_filter filter;
int err;
static uint32_t scope_id = 0;
#ifdef ENABLE_PING6_RTHDR
while (argc > 1) {
struct in6_addr *addr;
if (srcrt == NULL) {
int space;
fprintf(stderr, "ping6: Warning: "
"Source routing is deprecated by RFC5095.\n");
#ifdef ENABLE_PING6_RTHDR_RFC3542
space = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1);
#else
space = inet6_srcrt_space(IPV6_SRCRT_TYPE_0, argc - 1);
#endif
if (space == 0) {
fprintf(stderr, "srcrt_space failed\n");
exit(2);
}
#ifdef ENABLE_PING6_RTHDR_RFC3542
if (cmsglen + CMSG_SPACE(space) > sizeof(cmsgbuf)) {
fprintf(stderr, "no room for options\n");
exit(2);
}
#else
if (space + cmsglen > sizeof(cmsgbuf)) {
fprintf(stderr, "no room for options\n");
exit(2);
}
#endif
srcrt = (struct cmsghdr*)(cmsgbuf+cmsglen);
#ifdef ENABLE_PING6_RTHDR_RFC3542
memset(srcrt, 0, CMSG_SPACE(0));
srcrt->cmsg_len = CMSG_LEN(space);
srcrt->cmsg_level = IPPROTO_IPV6;
srcrt->cmsg_type = IPV6_RTHDR;
inet6_rth_init(CMSG_DATA(srcrt), space, IPV6_RTHDR_TYPE_0, argc - 1);
cmsglen += CMSG_SPACE(space);
#else
cmsglen += CMSG_ALIGN(space);
inet6_srcrt_init(srcrt, IPV6_SRCRT_TYPE_0);
#endif
}
target = *argv;
status = getaddrinfo(target, NULL, &hints, &result);
if (status) {
fprintf(stderr, "ping6: %s: %s\n", target, gai_strerror(status));
exit(2);
}
addr = &((struct sockaddr_in6 *)(result->ai_addr))->sin6_addr;
#ifdef ENABLE_PING6_RTHDR_RFC3542
inet6_rth_add(CMSG_DATA(srcrt), addr);
#else
inet6_srcrt_add(srcrt, addr);
#endif
if (IN6_IS_ADDR_UNSPECIFIED(&firsthop.sin6_addr)) {
memcpy(&firsthop.sin6_addr, addr, 16);
firsthop.sin6_scope_id = ((struct sockaddr_in6 *)(result->ai_addr))->sin6_scope_id;
/* Verify scope_id is the same as previous nodes */
if (firsthop.sin6_scope_id && scope_id && firsthop.sin6_scope_id != scope_id) {
fprintf(stderr, "scope discrepancy among the nodes\n");
exit(2);
} else if (!scope_id) {
scope_id = firsthop.sin6_scope_id;
}
}
freeaddrinfo(result);
argv++;
argc--;
}
#endif
if (niquery_is_enabled()) {
niquery_init_nonce();
if (!niquery_is_subject_valid()) {
ni_subject = &whereto.sin6_addr;
ni_subject_len = sizeof(whereto.sin6_addr);
ni_subject_type = NI_SUBJ_IPV6;
}
}
if (argc > 1) {
#ifndef ENABLE_PING6_RTHDR
fprintf(stderr, "ping6: Source routing is deprecated by RFC5095.\n");
#endif
ping6_usage(0);
} else if (argc == 1) {
target = *argv;
} else {
if (ni_query < 0 && ni_subject_type != NI_SUBJ_NAME)
ping6_usage(0);
target = ni_group;
}
if (!ai) {
status = getaddrinfo(target, NULL, &hints, &result);
if (status) {
fprintf(stderr, "ping6: %s: %s\n", target, gai_strerror(status));
exit(2);
}
ai = result;
}
memcpy(&whereto, ai->ai_addr, sizeof(whereto));
whereto.sin6_port = htons(IPPROTO_ICMPV6);
if (result)
freeaddrinfo(result);
if (memchr(target, ':', strlen(target)))
options |= F_NUMERIC;
if (IN6_IS_ADDR_UNSPECIFIED(&firsthop.sin6_addr)) {
memcpy(&firsthop.sin6_addr, &whereto.sin6_addr, 16);
firsthop.sin6_scope_id = whereto.sin6_scope_id;
/* Verify scope_id is the same as intermediate nodes */
if (firsthop.sin6_scope_id && scope_id && firsthop.sin6_scope_id != scope_id) {
fprintf(stderr, "scope discrepancy among the nodes\n");
exit(2);
} else if (!scope_id) {
scope_id = firsthop.sin6_scope_id;
}
}
hostname = target;
if (IN6_IS_ADDR_UNSPECIFIED(&source6.sin6_addr)) {
socklen_t alen;
int probe_fd = socket(AF_INET6, SOCK_DGRAM, 0);
if (probe_fd < 0) {
perror("socket");
exit(2);
}
if (device) {
unsigned int iface = if_name2index(device);
#ifdef IPV6_RECVPKTINFO
struct in6_pktinfo ipi;
memset(&ipi, 0, sizeof(ipi));
ipi.ipi6_ifindex = iface;
#endif
if (IN6_IS_ADDR_LINKLOCAL(&firsthop.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&firsthop.sin6_addr))
firsthop.sin6_scope_id = iface;
enable_capability_raw();
if (
#ifdef IPV6_RECVPKTINFO
setsockopt(probe_fd, IPPROTO_IPV6, IPV6_PKTINFO, &ipi, sizeof ipi) == -1 &&
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO, &ipi, sizeof ipi) == -1 &&
#endif
setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1 &&
setsockopt(sock->fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1) {
perror("setsockopt(SO_BINDTODEVICE)");
exit(2);
}
disable_capability_raw();
}
firsthop.sin6_family = AF_INET6;
firsthop.sin6_port = htons(1025);
if (connect(probe_fd, (struct sockaddr*)&firsthop, sizeof(firsthop)) == -1) {
perror("connect");
exit(2);
}
alen = sizeof source6;
if (getsockname(probe_fd, (struct sockaddr *) &source6, &alen) == -1) {
perror("getsockname");
exit(2);
}
source6.sin6_port = 0;
close(probe_fd);
#ifndef WITHOUT_IFADDRS
if (device) {
struct ifaddrs *ifa0, *ifa;
if (getifaddrs(&ifa0)) {
perror("getifaddrs");
exit(2);
}
for (ifa = ifa0; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET6)
continue;
if (!strncmp(ifa->ifa_name, device, sizeof(device) - 1) &&
IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
&source6.sin6_addr))
break;
}
if (!ifa)
fprintf(stderr, "ping6: Warning: source address might be selected on device other than %s.\n", device);
freeifaddrs(ifa0);
}
#endif
}
else if (device && (IN6_IS_ADDR_LINKLOCAL(&source6.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&source6.sin6_addr)))
source6.sin6_scope_id = if_name2index(device);
if (device) {
struct cmsghdr *cmsg;
struct in6_pktinfo *ipi;
cmsg = (struct cmsghdr*)(cmsgbuf+cmsglen);
cmsglen += CMSG_SPACE(sizeof(*ipi));
cmsg->cmsg_len = CMSG_LEN(sizeof(*ipi));
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
ipi = (struct in6_pktinfo*)CMSG_DATA(cmsg);
memset(ipi, 0, sizeof(*ipi));
ipi->ipi6_ifindex = if_name2index(device);
}
if ((whereto.sin6_addr.s6_addr16[0]&htons(0xff00)) == htons (0xff00)) {
if (uid) {
if (interval < 1000) {
fprintf(stderr, "ping: multicast ping with too short interval.\n");
exit(2);
}
if (pmtudisc >= 0 && pmtudisc != IPV6_PMTUDISC_DO) {
fprintf(stderr, "ping: multicast ping does not fragment.\n");
exit(2);
}
}
if (pmtudisc < 0)
pmtudisc = IPV6_PMTUDISC_DO;
}
if (pmtudisc >= 0) {
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &pmtudisc, sizeof pmtudisc) == -1) {
perror("ping: IPV6_MTU_DISCOVER");
exit(2);
}
}
if ((options&F_STRICTSOURCE) &&
bind(sock->fd, (struct sockaddr *) &source6, sizeof source6) == -1) {
perror("ping: bind icmp socket");
exit(2);
}
if (datalen >= sizeof(struct timeval) && (ni_query < 0)) {
/* can we time transfer */
timing = 1;
}
packlen = datalen + 8 + 4096 + 40 + 8; /* 4096 for rthdr */
if (!(packet = (unsigned char *)malloc((unsigned int)packlen))) {
fprintf(stderr, "ping: out of memory.\n");
exit(2);
}
sock->working_recverr = 1;
hold = 1;
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVERR, &hold, sizeof hold)) {
fprintf(stderr, "WARNING: your kernel is veeery old. No problems.\n");
sock->working_recverr = 0;
}
/* Estimate memory eaten by single packet. It is rough estimate.
* Actually, for small datalen's it depends on kernel side a lot. */
hold = datalen+8;
hold += ((hold+511)/512)*(40+16+64+160);
sock_setbufs(sock, hold);
#ifdef __linux__
if (sock->socktype == SOCK_RAW) {
int csum_offset = 2;
int sz_opt = sizeof(int);
err = setsockopt(sock->fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sz_opt);
if (err < 0) {
/* checksum should be enabled by default and setting this
* option might fail anyway.
*/
fprintf(stderr, "setsockopt(RAW_CHECKSUM) failed - try to continue.");
}
#endif
/*
* select icmp echo reply as icmp type to receive
*/
ICMP6_FILTER_SETBLOCKALL(&filter);
if (!sock->working_recverr) {
ICMP6_FILTER_SETPASS(ICMP6_DST_UNREACH, &filter);
ICMP6_FILTER_SETPASS(ICMP6_PACKET_TOO_BIG, &filter);
ICMP6_FILTER_SETPASS(ICMP6_TIME_EXCEEDED, &filter);
ICMP6_FILTER_SETPASS(ICMP6_PARAM_PROB, &filter);
}
if (niquery_is_enabled())
ICMP6_FILTER_SETPASS(ICMPV6_NI_REPLY, &filter);
else
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
err = setsockopt(sock->fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof filter);
if (err < 0) {
perror("setsockopt(ICMP6_FILTER)");
exit(2);
}
}
if (options & F_NOLOOP) {
int loop = 0;
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof loop) == -1) {
perror ("can't disable multicast loopback");
exit(2);
}
}
if (options & F_TTL) {
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof ttl) == -1) {
perror ("can't set multicast hop limit");
exit(2);
}
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof ttl) == -1) {
perror ("can't set unicast hop limit");
exit(2);
}
}
const int on = 1;
if (
#ifdef IPV6_RECVHOPLIMIT
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof on) == -1 &&
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_2292HOPLIMIT, &on, sizeof on) == -1
#else
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof on) == -1
#endif
){
perror ("can't receive hop limit");
exit(2);
}
if (options & F_TCLASS) {
#ifdef IPV6_TCLASS
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof tclass) == -1) {
perror ("setsockopt(IPV6_TCLASS)");
exit(2);
}
#else
fprintf(stderr, "Traffic class is not supported.\n");
#endif
}
if (options&F_FLOWINFO) {
#ifdef IPV6_FLOWLABEL_MGR
char freq_buf[CMSG_ALIGN(sizeof(struct in6_flowlabel_req)) + cmsglen];
struct in6_flowlabel_req *freq = (struct in6_flowlabel_req *)freq_buf;
int freq_len = sizeof(*freq);
#ifdef ENABLE_PING6_RTHDR
if (srcrt)
freq_len = CMSG_ALIGN(sizeof(*freq)) + srcrt->cmsg_len;
#endif
memset(freq, 0, sizeof(*freq));
freq->flr_label = htonl(flowlabel & IPV6_FLOWINFO_FLOWLABEL);
freq->flr_action = IPV6_FL_A_GET;
freq->flr_flags = IPV6_FL_F_CREATE;
freq->flr_share = IPV6_FL_S_EXCL;
memcpy(&freq->flr_dst, &whereto.sin6_addr, 16);
#ifdef ENABLE_PING6_RTHDR
if (srcrt)
memcpy(freq_buf + CMSG_ALIGN(sizeof(*freq)), srcrt, srcrt->cmsg_len);
#endif
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_FLOWLABEL_MGR, freq, freq_len) == -1) {
perror ("can't set flowlabel");
exit(2);
}
flowlabel = freq->flr_label;
#ifdef ENABLE_PING6_RTHDR
if (srcrt) {
cmsglen = (char*)srcrt - (char*)cmsgbuf;
srcrt = NULL;
}
#endif
#else
fprintf(stderr, "Flow labels are not supported.\n");
exit(2);
#endif
#ifdef IPV6_FLOWINFO_SEND
whereto.sin6_flowinfo = flowlabel;
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND, &on, sizeof on) == -1) {
perror ("can't send flowinfo");
exit(2);
}
#else
fprintf(stderr, "Flowinfo is not supported.\n");
exit(2);
#endif
}
printf("PING %s(%s) ", hostname, pr_addr(&whereto, sizeof whereto));
if (flowlabel)
printf(", flow 0x%05x, ", (unsigned)ntohl(flowlabel));
if (device || (options&F_STRICTSOURCE)) {
int saved_options = options;
options |= F_NUMERIC;
printf("from %s %s: ", pr_addr(&source6, sizeof source6), device ? : "");
options = saved_options;
}
printf("%d data bytes\n", datalen);
setup(sock);
drop_capabilities();
main_loop(&ping6_func_set, sock, packet, packlen);
}
int ping6_receive_error_msg(socket_st *sock)
{
int res;
char cbuf[512];
struct iovec iov;
struct msghdr msg;
struct cmsghdr *cmsg;
struct sock_extended_err *e;
struct icmp6_hdr icmph;
struct sockaddr_in6 target;
int net_errors = 0;
int local_errors = 0;
int saved_errno = errno;
iov.iov_base = &icmph;
iov.iov_len = sizeof(icmph);
msg.msg_name = (void*)⌖
msg.msg_namelen = sizeof(target);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_flags = 0;
msg.msg_control = cbuf;
msg.msg_controllen = sizeof(cbuf);
res = recvmsg(sock->fd, &msg, MSG_ERRQUEUE|MSG_DONTWAIT);
if (res < 0)
goto out;
e = NULL;
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
if (cmsg->cmsg_level == IPPROTO_IPV6) {
if (cmsg->cmsg_type == IPV6_RECVERR)
e = (struct sock_extended_err *)CMSG_DATA(cmsg);
}
}
if (e == NULL)
abort();
if (e->ee_origin == SO_EE_ORIGIN_LOCAL) {
local_errors++;
if (options & F_QUIET)
goto out;
if (options & F_FLOOD)
write_stdout("E", 1);
else if (e->ee_errno != EMSGSIZE)
fprintf(stderr, "ping: local error: %s\n", strerror(e->ee_errno));
else
fprintf(stderr, "ping: local error: Message too long, mtu=%u\n", e->ee_info);
nerrors++;
} else if (e->ee_origin == SO_EE_ORIGIN_ICMP6) {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)(e+1);
if (res < sizeof(icmph) ||
memcmp(&target.sin6_addr, &whereto.sin6_addr, 16) ||
icmph.icmp6_type != ICMP6_ECHO_REQUEST ||
!is_ours(sock, icmph.icmp6_id)) {
/* Not our error, not an error at all. Clear. */
saved_errno = 0;
goto out;
}
net_errors++;
nerrors++;
if (options & F_QUIET)
goto out;
if (options & F_FLOOD) {
write_stdout("\bE", 2);
} else {
print_timestamp();
printf("From %s icmp_seq=%u ", pr_addr(sin6, sizeof *sin6), ntohs(icmph.icmp6_seq));
pr_icmph(e->ee_type, e->ee_code, e->ee_info);
putchar('\n');
fflush(stdout);
}
}
out:
errno = saved_errno;
return net_errors ? : -local_errors;
}
/*
* pinger --
* Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
* will be added on by the kernel. The ID field is our UNIX process ID,
* and the sequence number is an ascending integer. The first 8 bytes
* of the data portion are used to hold a UNIX "timeval" struct in VAX
* byte-order, to compute the round-trip time.
*/
int build_echo(__u8 *_icmph, unsigned packet_size)
{
struct icmp6_hdr *icmph;
int cc;
icmph = (struct icmp6_hdr *)_icmph;
icmph->icmp6_type = ICMP6_ECHO_REQUEST;
icmph->icmp6_code = 0;
icmph->icmp6_cksum = 0;
icmph->icmp6_seq = htons(ntransmitted+1);
icmph->icmp6_id = ident;
if (timing)
gettimeofday((struct timeval *)&_icmph[8],
(struct timezone *)NULL);
cc = datalen + 8; /* skips ICMP portion */
return cc;
}
int build_niquery(__u8 *_nih, unsigned packet_size)
{
struct ni_hdr *nih;
int cc;
nih = (struct ni_hdr *)_nih;
nih->ni_cksum = 0;
nih->ni_type = ICMPV6_NI_QUERY;
cc = sizeof(*nih);
datalen = 0;
niquery_fill_nonce(ntransmitted + 1, nih->ni_nonce);
nih->ni_code = ni_subject_type;
nih->ni_qtype = htons(ni_query);
nih->ni_flags = ni_flag;
memcpy(nih + 1, ni_subject, ni_subject_len);
cc += ni_subject_len;
return cc;
}
int ping6_send_probe(socket_st *sock, void *packet, unsigned packet_size)
{
int len, cc;
rcvd_clear(ntransmitted + 1);
if (niquery_is_enabled())
len = build_niquery(packet, packet_size);
else
len = build_echo(packet, packet_size);
if (cmsglen == 0) {
cc = sendto(sock->fd, (char *)packet, len, confirm,
(struct sockaddr *) &whereto,
sizeof(struct sockaddr_in6));
} else {
struct msghdr mhdr;
struct iovec iov;
iov.iov_len = len;
iov.iov_base = packet;
memset(&mhdr, 0, sizeof(mhdr));
mhdr.msg_name = &whereto;
mhdr.msg_namelen = sizeof(struct sockaddr_in6);
mhdr.msg_iov = &iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = cmsgbuf;
mhdr.msg_controllen = cmsglen;
cc = sendmsg(sock->fd, &mhdr, confirm);
}
confirm = 0;
return (cc == len ? 0 : cc);
}
void pr_echo_reply(__u8 *_icmph, int cc)
{
struct icmp6_hdr *icmph = (struct icmp6_hdr *) _icmph;
printf(" icmp_seq=%u", ntohs(icmph->icmp6_seq));
};
static void putchar_safe(char c)
{
if (isprint(c))
putchar(c);
else
printf("\\%03o", c);
}
static
void pr_niquery_reply_name(struct ni_hdr *nih, int len)
{
__u8 *h = (__u8 *)(nih + 1);
__u8 *p = h + 4;
__u8 *end = (__u8 *)nih + len;
int continued = 0;
char buf[1024];
int ret;
len -= sizeof(struct ni_hdr) + 4;
if (len < 0) {
printf(" parse error (too short)");
return;
}
while (p < end) {
int fqdn = 1;
int i;
memset(buf, 0xff, sizeof(buf));
if (continued)
putchar(',');
ret = dn_expand(h, end, p, buf, sizeof(buf));
if (ret < 0) {
printf(" parse error (truncated)");
break;
}
if (p + ret < end && *(p + ret) == '\0')
fqdn = 0;
putchar(' ');
for (i = 0; i < strlen(buf); i++)
putchar_safe(buf[i]);
if (fqdn)
putchar('.');
p += ret + !fqdn;
continued = 1;
}
}
static
void pr_niquery_reply_addr(struct ni_hdr *nih, int len)
{
__u8 *h = (__u8 *)(nih + 1);
__u8 *p;
__u8 *end = (__u8 *)nih + len;
int af;
int aflen;
int continued = 0;
int truncated;
char buf[1024];
switch (ntohs(nih->ni_qtype)) {
case NI_QTYPE_IPV4ADDR:
af = AF_INET;
aflen = sizeof(struct in_addr);
truncated = nih->ni_flags & NI_IPV6ADDR_F_TRUNCATE;
break;
case NI_QTYPE_IPV6ADDR:
af = AF_INET6;
aflen = sizeof(struct in6_addr);
truncated = nih->ni_flags & NI_IPV4ADDR_F_TRUNCATE;
break;
default:
/* should not happen */
af = aflen = truncated = 0;
}
p = h;
if (len < 0) {
printf(" parse error (too short)");
return;
}
while (p < end) {
if (continued)
putchar(',');
if (p + sizeof(__u32) + aflen > end) {
printf(" parse error (truncated)");
break;
}
if (!inet_ntop(af, p + sizeof(__u32), buf, sizeof(buf)))
printf(" unexpeced error in inet_ntop(%s)",
strerror(errno));
else
printf(" %s", buf);
p += sizeof(__u32) + aflen;
continued = 1;
}
if (truncated)
printf(" (truncated)");
}
static
void pr_niquery_reply(__u8 *_nih, int len)
{
struct ni_hdr *nih = (struct ni_hdr *)_nih;
switch (nih->ni_code) {
case NI_SUCCESS:
switch (ntohs(nih->ni_qtype)) {
case NI_QTYPE_NAME:
pr_niquery_reply_name(nih, len);
break;
case NI_QTYPE_IPV4ADDR:
case NI_QTYPE_IPV6ADDR:
pr_niquery_reply_addr(nih, len);
break;
default:
printf(" unknown qtype(0x%02x)", ntohs(nih->ni_qtype));
}
break;
case NI_REFUSED:
printf(" refused");
break;
case NI_UNKNOWN:
printf(" unknown");
break;
default:
printf(" unknown code(%02x)", ntohs(nih->ni_code));
}
printf("; seq=%u;", ntohsp((__u16*)nih->ni_nonce));
}
/*
* parse_reply --
* Print out the packet, if it came from us. This logic is necessary
* because ALL readers of the ICMP socket get a copy of ALL ICMP packets
* which arrive ('tis only fair). This permits multiple copies of this
* program to be run without having intermingled output (or statistics!).
*/
int
ping6_parse_reply(socket_st *sock, struct msghdr *msg, int cc, void *addr, struct timeval *tv)
{
struct sockaddr_in6 *from = addr;
__u8 *buf = msg->msg_iov->iov_base;
struct cmsghdr *c;
struct icmp6_hdr *icmph;
int hops = -1;
for (c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) {
if (c->cmsg_level != IPPROTO_IPV6)
continue;
switch(c->cmsg_type) {
case IPV6_HOPLIMIT:
#ifdef IPV6_2292HOPLIMIT
case IPV6_2292HOPLIMIT:
#endif
if (c->cmsg_len < CMSG_LEN(sizeof(int)))
continue;
memcpy(&hops, CMSG_DATA(c), sizeof(hops));
}
}
/* Now the ICMP part */
icmph = (struct icmp6_hdr *) buf;
if (cc < 8) {
if (options & F_VERBOSE)
fprintf(stderr, "ping: packet too short (%d bytes)\n", cc);
return 1;
}
if (icmph->icmp6_type == ICMP6_ECHO_REPLY) {
if (!is_ours(sock, icmph->icmp6_id))
return 1;
if (gather_statistics((__u8*)icmph, sizeof(*icmph), cc,
ntohs(icmph->icmp6_seq),
hops, 0, tv, pr_addr(from, sizeof *from),
pr_echo_reply)) {
fflush(stdout);
return 0;
}
} else if (icmph->icmp6_type == ICMPV6_NI_REPLY) {
struct ni_hdr *nih = (struct ni_hdr *)icmph;
int seq = niquery_check_nonce(nih->ni_nonce);
if (seq < 0)
return 1;
if (gather_statistics((__u8*)icmph, sizeof(*icmph), cc,
seq,
hops, 0, tv, pr_addr(from, sizeof *from),
pr_niquery_reply))
return 0;
} else {
int nexthdr;
struct ip6_hdr *iph1 = (struct ip6_hdr*)(icmph+1);
struct icmp6_hdr *icmph1 = (struct icmp6_hdr *)(iph1+1);
/* We must not ever fall here. All the messages but
* echo reply are blocked by filter and error are
* received with IPV6_RECVERR. Ugly code is preserved
* however, just to remember what crap we avoided
* using RECVRERR. :-)
*/
if (cc < 8+sizeof(struct ip6_hdr)+8)
return 1;
if (memcmp(&iph1->ip6_dst, &whereto.sin6_addr, 16))
return 1;
nexthdr = iph1->ip6_nxt;
if (nexthdr == 44) {
nexthdr = *(__u8*)icmph1;
icmph1++;
}
if (nexthdr == IPPROTO_ICMPV6) {
if (icmph1->icmp6_type != ICMP6_ECHO_REQUEST ||
!is_ours(sock, icmph1->icmp6_id))
return 1;
acknowledge(ntohs(icmph1->icmp6_seq));
if (sock->working_recverr)
return 0;
nerrors++;
if (options & F_FLOOD) {
write_stdout("\bE", 2);
return 0;
}
print_timestamp();
printf("From %s: icmp_seq=%u ", pr_addr(from, sizeof *from), ntohs(icmph1->icmp6_seq));
} else {
/* We've got something other than an ECHOREPLY */
if (!(options & F_VERBOSE) || uid)
return 1;
print_timestamp();
printf("From %s: ", pr_addr(from, sizeof *from));
}
pr_icmph(icmph->icmp6_type, icmph->icmp6_code, ntohl(icmph->icmp6_mtu));
}
if (options & F_AUDIBLE) {
putchar('\a');
if(options & F_FLOOD)
fflush(stdout);
}
if (!(options & F_FLOOD)) {
putchar('\n');
fflush(stdout);
}
return 0;
}
int pr_icmph(__u8 type, __u8 code, __u32 info)
{
switch(type) {
case ICMP6_DST_UNREACH:
printf("Destination unreachable: ");
switch (code) {
case ICMP6_DST_UNREACH_NOROUTE:
printf("No route");
break;
case ICMP6_DST_UNREACH_ADMIN:
printf("Administratively prohibited");
break;
case ICMP6_DST_UNREACH_BEYONDSCOPE:
printf("Beyond scope of source address");
break;
case ICMP6_DST_UNREACH_ADDR:
printf("Address unreachable");
break;
case ICMP6_DST_UNREACH_NOPORT:
printf("Port unreachable");
break;
default:
printf("Unknown code %d", code);
break;
}
break;
case ICMP6_PACKET_TOO_BIG:
printf("Packet too big: mtu=%u", info);
if (code)
printf(", code=%d", code);
break;
case ICMP6_TIME_EXCEEDED:
printf("Time exceeded: ");
if (code == ICMP6_TIME_EXCEED_TRANSIT)
printf("Hop limit");
else if (code == ICMP6_TIME_EXCEED_REASSEMBLY)
printf("Defragmentation failure");
else
printf("code %d", code);
break;
case ICMP6_PARAM_PROB:
printf("Parameter problem: ");
if (code == ICMP6_PARAMPROB_HEADER)
printf("Wrong header field ");
else if (code == ICMP6_PARAMPROB_NEXTHEADER)
printf("Unknown header ");
else if (code == ICMP6_PARAMPROB_OPTION)
printf("Unknown option ");
else
printf("code %d ", code);
printf ("at %u", info);
break;
case ICMP6_ECHO_REQUEST:
printf("Echo request");
break;
case ICMP6_ECHO_REPLY:
printf("Echo reply");
break;
case MLD_LISTENER_QUERY:
printf("MLD Query");
break;
case MLD_LISTENER_REPORT:
printf("MLD Report");
break;
case MLD_LISTENER_REDUCTION:
printf("MLD Reduction");
break;
default:
printf("unknown icmp type: %u", type);
}
return 0;
}
void ping6_install_filter(socket_st *sock)
{
static int once;
static struct sock_filter insns[] = {
BPF_STMT(BPF_LD|BPF_H|BPF_ABS, 4), /* Load icmp echo ident */
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, 0xAAAA, 0, 1), /* Ours? */
BPF_STMT(BPF_RET|BPF_K, ~0U), /* Yes, it passes. */
BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 0), /* Load icmp type */
BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, ICMP6_ECHO_REPLY, 1, 0), /* Echo? */
BPF_STMT(BPF_RET|BPF_K, ~0U), /* No. It passes. This must not happen. */
BPF_STMT(BPF_RET|BPF_K, 0), /* Echo with wrong ident. Reject. */
};
static struct sock_fprog filter = {
sizeof insns / sizeof(insns[0]),
insns
};
if (once)
return;
once = 1;
/* Patch bpflet for current identifier. */
insns[1] = (struct sock_filter)BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, htons(ident), 0, 1);
if (setsockopt(sock->fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)))
perror("WARNING: failed to install socket filter\n");
}
#define USAGE_NEWLINE "\n "
void ping6_usage(unsigned from_ping)
{
const char *name;
if (from_ping)
name = "ping -6";
else
name = "ping6";
fprintf(stderr,
"Usage: %s"
" [-"
"aAbBdDfhLnOqrRUvV"
"]"
" [-c count]"
" [-i interval]"
" [-I interface]"
USAGE_NEWLINE
" [-l preload]"
" [-m mark]"
" [-M pmtudisc_option]"
USAGE_NEWLINE
" [-N nodeinfo_option]"
" [-p pattern]"
" [-Q tclass]"
" [-s packetsize]"
USAGE_NEWLINE
" [-S sndbuf]"
" [-t ttl]"
" [-T timestamp_option]"
" [-w deadline]"
USAGE_NEWLINE
" [-W timeout]"
#ifdef ENABLE_PING6_RTHDR
" [hop1 ...]"
#endif
" destination"
"\n", name
);
exit(2);
}
|