|
root / base / usr / src / cmd / cmd-inet / usr.sbin / traceroute
traceroute Plain Text 3761 lines 99.6 KB
   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
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#  Copyright (c) 1988, 1989, 1990, 1991, 1992, 1995, 1996, 1997
#	The Regents of the University of California.  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that: (1) source code distributions
#  retain the above copyright notice and this paragraph in its entirety, (2)
#  distributions including binary code include the above copyright notice and
#  this paragraph in its entirety in the documentation or other materials
#  provided with the distribution, and (3) all advertising materials mentioning
#  features or use of this software display the following acknowledgement:
#  ``This product includes software developed by the University of California,
#  Lawrence Berkeley Laboratory and its contributors.'' 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

PROG=		traceroute
OBJS=		traceroute.o traceroute_aux.o traceroute_aux6.o

include	../../../Makefile.cmd

$(ROOTUSRSBIN)/traceroute	:	FILEMODE= 04555

# Traceroute uses the ancillary data feature which is available only through
# UNIX 98 standards version of Socket interface. This interface is supposed to
# be accessed by -lxnet. In addition -lsocket is used to
# capture new not-yet-standard interfaces. Someday -lxnet alone should be enough
# when IPv6 inspired new interfaces are part of standards.
LDLIBS +=	-lxnet -lsocket -linetutil

# These #defines are required to use UNIX 98 interfaces
CPPFLAGS += -D_XOPEN_SOURCE=500 -D__EXTENSIONS__

CERRWARN +=	$(CNOWARN_UNINIT)
CERRWARN +=	-Wno-clobbered

# Hammerhead: Suppress socklen_t type mismatch warnings in legacy code
CERRWARN +=	-Wno-incompatible-pointer-types

.KEEP_STATE:

all:	$(PROG)

$(PROG): $(OBJS)
	$(LINK.c) -o $@ $(OBJS) $(LDLIBS)
	$(POST_PROCESS)

install: all $(ROOTUSRSBINPROG)

clean:
	$(RM) $(OBJS)

include ../../../Makefile.targ
 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
TRACEROUTE COMMAND SOFTWARE
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2017, Joyent, Inc.
 */

/*
 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
 */

#include <sys/param.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/sysmacros.h>

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>

#include <arpa/inet.h>

#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <memory.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
#include <signal.h>
#include <setjmp.h>
#include <limits.h>
#include <zone.h>
#include <thread.h>
#include <synch.h>

#include <priv_utils.h>

#include <libinetutil.h>
#include "traceroute.h"

#define	MAX_SEQ			65535	/* max sequence value for ICMP */
#define	MAX_TRAFFIC_CLASS	255	/* max traffic class for IPv6 */
#define	MAX_FLOW_LABEL		0xFFFFF	/* max flow label for IPv6 */
#define	MAX_TOS			255	/* max type-of-service for IPv4 */
#define	STR_LEN			30

/* store the information about a host */
struct hostinfo {
	char *name;		/* hostname */
	int family;		/* address family of the IP addresses */
	int num_addr;			/* number of IP addresses */
	union any_in_addr *addrs;	/* list of IP addresses */
};

/* used to store a bunch of protocol specific values */
struct pr_set {
	int family;		/* AF_INET or AF_INET6 */
	char name[STR_LEN];	/* "IPv4" or "IPv6" */
	char icmp[STR_LEN];	/* "icmp" or "ipv6-icmp" */
	int icmp_minlen;
	int addr_len;
	int ip_hdr_len;
	int packlen;
	int sock_size;		/* size of sockaddr_in or sockaddr_in6 */
	struct sockaddr *to;
	struct sockaddr *from;
	void *from_sin_addr;
	union any_in_addr *gwIPlist;
	/* pointers to v4/v6 functions */
	struct ip *(*set_buffers_fn) (int);
	int (*check_reply_fn)(struct msghdr *, int, int, uchar_t *, uchar_t *);
	boolean_t (*print_icmp_other_fn)(uchar_t, uchar_t);
	void (*print_addr_fn)(uchar_t *, int, struct sockaddr *);

};

/*
 * LBNL bug fixed: in LBNL traceroute 'uchar_t packet[512];'
 * Not sufficient to hold the complete packet for ECHO REPLY of a big probe.
 * Packet size is reported incorrectly in such a case.
 * Also this buffer needs to be 32 bit aligned. In the future the alignment
 * requirement will be increased to 64 bit. So, let's use 64 bit alignment now.
 */
static uint64_t packet[(IP_MAXPACKET + 1)/8];	/* received packet */

static struct ip *outip4;	/* output buffer to send as an IPv4 datagram */
static struct ip *outip6;	/* output buffer to send as an IPv6 datagram */

/* Used to store the ancillary data that comes with the received packets */
static uint64_t ancillary_data[(IP_MAXPACKET + 1)/8];

/* first get the gw names, later you'll resolve them based on the family */
static char *gwlist[MAXMAX_GWS];		/* gateway names list */
static union any_in_addr gwIPlist[MAX_GWS];	/* gateway IPv4 address list */
static union any_in_addr gwIP6list[MAX_GWS6];	/* gateway IPv6 address list */

static int family_input = AF_UNSPEC;	/* User supplied protocol family */
static int rcvsock4;		/* receive (icmp) socket file descriptor */
static int sndsock4;		/* send (udp/icmp) socket file descriptor */
static int rcvsock6;		/* receive (icmp6) socket file descriptor */
static int sndsock6;		/* send (udp6/icmp6) socket file descriptor */
int gw_count = 0;		/* number of gateways */
static struct sockaddr_in whereto;	/* Who to try to reach */
static struct sockaddr_in6 whereto6;
static struct sockaddr_in wherefrom;	/* Who we are */
static struct sockaddr_in6 wherefrom6;
static int packlen_input = 0;		/* user input for packlen */

char *prog;
static char *source_input = NULL; /* this is user arg. source, doesn't change */
static char *source = NULL;	/* this gets modified after name lookup */
char *hostname;
static char *device = NULL;   	/* interface name */
static struct pr_set *pr4;	/* protocol info for IPv4 */
static struct pr_set *pr6;	/* protocol info for IPv6 */
static struct ifaddrlist *al4;	/* list of interfaces */
static struct ifaddrlist *al6;	/* list of interfaces */
static uint_t if_index = 0;	/* interface index */
static int num_v4 = 0;		/* count of IPv4 addresses */
static int num_v6 = 0;		/* count of IPv6 addresses */
static int num_ifs4 = 0;	/* count of local IPv4 interfaces */
static int num_ifs6 = 0;	/* count of local IPv6 interfaces */

static int nprobes = 3;		/* number of probes */
static int max_ttl = 30;	/* max number of hops */
static int first_ttl = 1;	/* initial number of hops */
ushort_t ident;			/* used to authenticate replies */
ushort_t port = 32768 + 666;	/* start udp dest port # for probe packets */

static int options = 0;		/* socket options */
boolean_t verbose = _B_FALSE;	/* verbose output */
static int waittime = 5;	/* time to wait for response (in seconds) */
static struct timeval delay = {0, 0}; /* delay between consecutive probe */
boolean_t nflag = _B_FALSE;	/* print addresses numerically */
static boolean_t showttl = _B_FALSE; /* print the ttl(hop limit) of recvd pkt */
boolean_t useicmp = _B_FALSE;  	/* use icmp echo instead of udp packets */
boolean_t docksum = _B_TRUE;	/* calculate checksums */
static boolean_t collect_stat = _B_FALSE;	/* print statistics */
boolean_t settos = _B_FALSE;   	/* set type-of-service field */
int dontfrag = 0;		/* IP*_DONTFRAG */
static int max_timeout = 5;	/* quit after this consecutive timeouts */
static boolean_t probe_all = _B_FALSE;	/* probe all the IFs of the target */
static boolean_t pick_src = _B_FALSE;	/* traceroute picks the src address */

/*
 * flow and class are specific to IPv6, tos and off are specific to IPv4.
 * Each protocol uses the ones that are specific to itself, and ignores
 * others.
 */
static uint_t flow = 0;		/* IPv6 flow info */
static uint_t class = 0;	/* IPv6 class */
uchar_t tos = 0;		/* IPv4 type-of-service */
ushort_t off = 0;		/* set DF bit */

static jmp_buf env;		/* stack environment for longjmp() */
boolean_t raw_req;		/* if sndsock for IPv4 must be raw */

/*
 * Name service lookup related data.
 */
static mutex_t tr_nslock = ERRORCHECKMUTEX;
static boolean_t tr_nsactive = _B_FALSE;	/* Lookup ongoing */
static hrtime_t tr_nsstarttime;			/* Start time */
static int tr_nssleeptime = 2;			/* Interval between checks */
static int tr_nswarntime = 2;			/* Interval to warn after */

/* Forwards */
static uint_t calc_packetlen(int, struct pr_set *);
extern int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
extern int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *);
static double deltaT(struct timeval *, struct timeval *);
static char *device_name(struct ifaddrlist *, int, union any_in_addr *,
    struct pr_set *);
extern void *find_ancillary_data(struct msghdr *, int, int);
static boolean_t has_addr(struct addrinfo *, union any_in_addr *);
static struct ifaddrlist *find_device(struct ifaddrlist *, int, char *);
static struct ifaddrlist *find_ifaddr(struct ifaddrlist *, int,
    union any_in_addr *, int);
static void get_gwaddrs(char **, int, union any_in_addr *,
    union any_in_addr *, int *, int *);
static void get_hostinfo(char *, int, struct addrinfo **);
char *inet_name(union any_in_addr *, int);
ushort_t in_cksum(ushort_t *, int);
extern int ip_hdr_length_v6(ip6_t *, int, uint8_t *);
extern char *pr_type(uchar_t);
extern char *pr_type6(uchar_t);
extern void print_addr(uchar_t *, int, struct sockaddr *);
extern void print_addr6(uchar_t *, int, struct sockaddr *);
extern boolean_t print_icmp_other(uchar_t, uchar_t);
extern boolean_t print_icmp_other6(uchar_t, uchar_t);
static void print_stats(int, int, double, double, double, double);
static void print_unknown_host_msg(const char *, const char *);
static void record_stats(double, int *, double *, double *, double *, double *);
static void resolve_nodes(int *, struct addrinfo **);
static void select_src_addr(union any_in_addr *, union any_in_addr *, int);
extern void send_probe(int, struct sockaddr *, struct ip *, int, int,
    struct timeval *, int);
extern void send_probe6(int, struct msghdr *, struct ip *, int, int,
    struct timeval *, int);
extern void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int,
    uint_t);
extern struct ip *set_buffers(int);
extern struct ip *set_buffers6(int);
extern void set_IPv4opt_sourcerouting(int, union any_in_addr *,
    union any_in_addr *);
static void set_sin(struct sockaddr *, union any_in_addr *, int);
static int set_src_addr(struct pr_set *, struct ifaddrlist **);
static void setup_protocol(struct pr_set *, int);
static void setup_socket(struct pr_set *, int);
static void sig_handler(int);
static int str2int(const char *, const char *, int, int);
static double str2dbl(const char *, const char *, double, double);
static void trace_it(struct addrinfo *);
static void traceroute(union any_in_addr *, struct msghdr *, struct pr_set *,
    int, struct ifaddrlist *);
static void tv_sub(struct timeval *, struct timeval *);
static void usage(void);
static int wait_for_reply(int, struct msghdr *, struct timeval *);
static double xsqrt(double);
static void *ns_warning_thr(void *);

/*
 * main
 */
int
main(int argc, char **argv)
{
	struct addrinfo *ai_dst = NULL;		/* destination host */
	/*
	 * "probing_successful" indicates if we could successfully send probes,
	 * not necessarily received reply from the target (this behavior is from
	 * the original traceroute). It's _B_FALSE if packlen is invalid, or no
	 * interfaces found.
	 */
	boolean_t probing_successful = _B_FALSE;
	int longjmp_return;			/* return value from longjump */
	int i = 0;
	char *cp;
	int op;
	char *ep;
	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
	double pause;

	/*
	 * A raw socket will be used for IPv4 if there is sufficient
	 * privilege.
	 */
	raw_req = priv_ineffect(PRIV_NET_RAWACCESS);

	/*
	 * We'll need the privilege only when we open the sockets; that's
	 * when we'll fail if the program has insufficient privileges.
	 */
	(void) __init_suid_priv(PU_CLEARLIMITSET, PRIV_NET_ICMPACCESS,
	    raw_req ? PRIV_NET_RAWACCESS : NULL, NULL);

	(void) setlinebuf(stdout);

	if ((cp = strrchr(argv[0], '/')) != NULL)
		prog = cp + 1;
	else
		prog = argv[0];

	opterr = 0;
	while ((op = getopt(argc, argv, "adFIlnrSvxA:c:f:g:i:L:m:P:p:Q:q:s:"
	    "t:w:")) != EOF) {
		switch (op) {
		case 'A':
			if (strcmp(optarg, "inet") == 0) {
				family_input = AF_INET;
			} else if (strcmp(optarg, "inet6") == 0) {
				family_input = AF_INET6;
			} else {
				Fprintf(stderr,
				    "%s: unknown address family %s\n",
				    prog, optarg);
				exit(EXIT_FAILURE);
			}
			break;

		case 'a':
			probe_all = _B_TRUE;
			break;

		case 'c':
			class = str2int(optarg, "traffic class", 0,
			    MAX_TRAFFIC_CLASS);
			break;

		case 'd':
			options |= SO_DEBUG;
			break;

		case 'f':
			first_ttl = str2int(optarg, "first ttl", 1, MAXTTL);
			break;

		case 'F':
			off = IP_DF;
			dontfrag = 1;
			break;

		case 'g':
			if (!raw_req) {
				Fprintf(stderr,
				    "%s: privilege to specify a loose source "
				    "route gateway is unavailable\n",
				    prog);
				exit(EXIT_FAILURE);
			}
			if (gw_count >= MAXMAX_GWS) {
				Fprintf(stderr,
				    "%s: Too many gateways\n", prog);
				exit(EXIT_FAILURE);
			}
			gwlist[gw_count] = strdup(optarg);
			if (gwlist[gw_count] == NULL) {
				Fprintf(stderr, "%s: strdup %s\n", prog,
				    strerror(errno));
				exit(EXIT_FAILURE);
			}

			++gw_count;
			break;

		case 'l':
			showttl = _B_TRUE;
			break;

		case 'i':
			/* this can be IF name or IF index */
			if_index = (uint_t)strtol(optarg, &ep, 10);

			/* convert IF index <-->  IF name */
			if (errno != 0 || *ep != '\0') {
				device = optarg;
				if_index = if_nametoindex((const char *)device);

				/*
				 * In case it fails, check to see if the problem
				 * is other than "IF not found".
				 */
				if (if_index == 0 && errno != ENXIO) {
					Fprintf(stderr, "%s: if_nametoindex:"
					    "%s\n", prog, strerror(errno));
					exit(EXIT_FAILURE);
				}
			} else {
				device = (char *)malloc(LIFNAMSIZ + 1);
				if (device == NULL) {
					Fprintf(stderr, "%s: malloc: %s\n",
					    prog, strerror(errno));
					exit(EXIT_FAILURE);
				}

				device = if_indextoname(if_index, device);
				if (device != NULL) {
					device[LIFNAMSIZ] = '\0';
				} else if (errno != ENXIO) {
					/*
					 * The problem was other than "index
					 * not found".
					 */
					Fprintf(stderr, "%s: if_indextoname:"
					    "%s\n", prog, strerror(errno));
					exit(EXIT_FAILURE);
				}
			}

			if (device == NULL || if_index == 0) {
				Fprintf(stderr, "%s: interface %s "
				    "doesn't match any actual interfaces\n",
				    prog, optarg);
				exit(EXIT_FAILURE);
			}
			break;

		case 'I':
			useicmp = _B_TRUE;
			break;

		case 'L':
			flow = str2int(optarg, "flow label", 0, MAX_FLOW_LABEL);
			break;

		case 'm':
			max_ttl = str2int(optarg, "max ttl(hop limit)", 1,
			    MAXTTL);
			break;

		case 'n':
			nflag = _B_TRUE;
			break;

		case 'P':
			pause = str2dbl(optarg, "pause", 0, INT_MAX);
			delay.tv_sec = (time_t)pause;
			delay.tv_usec = (suseconds_t)((pause - delay.tv_sec) *
			    1000000);
			break;

		case 'p':
			port = str2int(optarg, "port", 1, MAX_PORT);
			break;

		case 'Q':
			max_timeout = str2int(optarg, "max timeout", 1, -1);
			break;

		case 'q':
			nprobes = str2int(optarg, "nprobes", 1, -1);
			break;

		case 'r':
			options |= SO_DONTROUTE;
			break;

		case 'S':
			collect_stat = _B_TRUE;
			break;

		case 's':
			/*
			 * set the ip source address of the outbound
			 * probe (e.g., on a multi-homed host).
			 */
			source_input = optarg;
			break;

		case 't':
			tos = (uchar_t)str2int(optarg, "tos", 0, MAX_TOS);
			settos = _B_TRUE;
			break;

		case 'v':
			verbose = _B_TRUE;
			break;

		case 'x':
			docksum = _B_FALSE;
			break;

		case 'w':
			waittime = str2int(optarg, "wait time", 2, -1);
			break;

		default:
			usage();
			break;
		}
	}

	/*
	 * If it's probe_all, SIGQUIT makes traceroute exit(). But we set the
	 * address to jump back to in traceroute(). Until then, we'll need to
	 * temporarily specify one.
	 */
	if (probe_all) {
		if ((longjmp_return = setjmp(env)) != 0) {
			if (longjmp_return == SIGQUIT) {
				Printf("(exiting)\n");
				exit(EXIT_SUCCESS);
			} else {		/* should never happen */
				exit(EXIT_FAILURE);
			}
		}
		(void) signal(SIGQUIT, sig_handler);
	}

	if ((gw_count > 0) && (options & SO_DONTROUTE)) {
		Fprintf(stderr, "%s: loose source route gateways (-g)"
		    " cannot be specified when probe packets are sent"
		    " directly to a host on an attached network (-r)\n",
		    prog);
		exit(EXIT_FAILURE);
	}

	i = argc - optind;
	if (i == 1 || i == 2) {
		hostname = argv[optind];

		if (i == 2) {
			/* accept any length now, we'll check it later */
			packlen_input = str2int(argv[optind + 1],
			    "packet length", 0, -1);
		}
	} else {
		usage();
	}

	if (first_ttl > max_ttl) {
		Fprintf(stderr,
		    "%s: first ttl(hop limit) (%d) may not be greater"
		    " than max ttl(hop limit) (%d)\n",
		    prog, first_ttl, max_ttl);
		exit(EXIT_FAILURE);
	}

	/*
	 * Start up the name services warning thread.
	 */
	if (thr_create(NULL, 0, ns_warning_thr, NULL,
	    THR_DETACHED | THR_DAEMON, NULL) != 0) {
		Fprintf(stderr, "%s: failed to create name services "
		    "thread: %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}


	/* resolve hostnames */
	resolve_nodes(&family_input, &ai_dst);
	if (ai_dst == NULL) {
		exit(EXIT_FAILURE);
	}

	/*
	 * If it's probe_all, SIGINT makes traceroute skip to probing next IP
	 * address of the target. The new interrupt handler is assigned in
	 * traceroute() function. Until then let's ignore the signal.
	 */
	if (probe_all)
		(void) signal(SIGINT, SIG_IGN);

	ident = (getpid() & 0xffff) | 0x8000;

	/*
	 * We KNOW that probe_all == TRUE if family is AF_UNSPEC,
	 * since family is set to the specific AF found unless it's
	 * probe_all. So if family == AF_UNSPEC, we need to init pr4 and pr6.
	 */
	switch (family_input) {
	case AF_UNSPEC:
		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
		if (pr4 == NULL) {
			Fprintf(stderr,
			    "%s: malloc %s\n", prog, strerror(errno));
			exit(EXIT_FAILURE);
		}
		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
		if (pr6 == NULL) {
			Fprintf(stderr,
			    "%s: malloc %s\n", prog, strerror(errno));
			exit(EXIT_FAILURE);
		}
		setup_protocol(pr6, AF_INET6);
		setup_protocol(pr4, AF_INET);
		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
		setup_socket(pr6, pr6->packlen);

		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
		setup_socket(pr4, pr4->packlen);
		num_ifs6 = set_src_addr(pr6, &al6);
		num_ifs4 = set_src_addr(pr4, &al4);
		break;
	case AF_INET6:
		pr6 = (struct pr_set *)malloc(sizeof (struct pr_set));
		if (pr6 == NULL) {
			Fprintf(stderr,
			    "%s: malloc %s\n", prog, strerror(errno));
			exit(EXIT_FAILURE);
		}
		setup_protocol(pr6, AF_INET6);
		outip6 = (*pr6->set_buffers_fn)(pr6->packlen);
		setup_socket(pr6, pr6->packlen);
		num_ifs6 = set_src_addr(pr6, &al6);
		break;
	case AF_INET:
		pr4 = (struct pr_set *)malloc(sizeof (struct pr_set));
		if (pr4 == NULL) {
			Fprintf(stderr,
			    "%s: malloc %s\n", prog, strerror(errno));
			exit(EXIT_FAILURE);
		}
		setup_protocol(pr4, AF_INET);
		outip4 = (*pr4->set_buffers_fn)(pr4->packlen);
		setup_socket(pr4, pr4->packlen);
		num_ifs4 = set_src_addr(pr4, &al4);
		break;
	default:
		Fprintf(stderr, "%s: unknow address family.\n", prog);
		exit(EXIT_FAILURE);
	}

	if (num_v4 + num_v6 > 1 && !probe_all) {
		if (ai_dst->ai_family == AF_INET) {
			Fprintf(stderr,
			    "%s: Warning: %s has multiple addresses;"
			    " using %s\n", prog, hostname,
			    inet_ntop(AF_INET,
			    /* LINTED E_BAD_PTR_CAST_ALIGN */
			    (void *)&((struct sockaddr_in *)
			    ai_dst->ai_addr)->sin_addr,
			    temp_buf, sizeof (temp_buf)));
		} else {
			Fprintf(stderr,
			    "%s: Warning: %s has multiple addresses;"
			    " using %s\n", prog, hostname,
			    inet_ntop(AF_INET6,
			    /* LINTED E_BAD_PTR_CAST_ALIGN */
			    (void *)&((struct sockaddr_in6 *)
			    ai_dst->ai_addr)->sin6_addr,
			    temp_buf, sizeof (temp_buf)));
		}
	}

	if (num_ifs4 + num_ifs6 > 0) {
		trace_it(ai_dst);
		probing_successful = _B_TRUE;
	}

	(void) close(rcvsock4);
	(void) close(sndsock4);
	(void) close(rcvsock6);
	(void) close(sndsock6);

	/*
	 * if we could probe any of the IP addresses of the target, that means
	 * this was a successful operation
	 */
	if (probing_successful)
		return (EXIT_SUCCESS);
	else
		return (EXIT_FAILURE);
}

/*
 * print "unknown host" message
 */
static void
print_unknown_host_msg(const char *protocol, const char *host)
{
	Fprintf(stderr, "%s: unknown%s host %s\n", prog, protocol, host);
}

/*
 * resolve destination host and gateways
 */
static void
resolve_nodes(int *family, struct addrinfo **ai_dstp)
{
	struct addrinfo *ai_dst = NULL;
	struct addrinfo *aip = NULL;
	int num_resolved_gw = 0;
	int num_resolved_gw6 = 0;

	get_hostinfo(hostname, *family, &ai_dst);
	if (ai_dst == NULL) {
		print_unknown_host_msg("", hostname);
		exit(EXIT_FAILURE);
	}
	/* Get a count of the v4 & v6 addresses */
	for (aip = ai_dst; aip != NULL; aip = aip->ai_next) {
		switch (aip->ai_family) {
		case AF_INET:
			num_v4++;
			break;
		case AF_INET6:
			num_v6++;
			break;
		}
	}

	if (*family == AF_UNSPEC && !probe_all) {
		*family = ai_dst->ai_family;
	}

	/* resolve gateways */
	if (gw_count > 0) {
		get_gwaddrs(gwlist, *family, gwIPlist, gwIP6list,
		    &num_resolved_gw, &num_resolved_gw6);

		/* we couldn't resolve a gateway as an IPv6 host */
		if (num_resolved_gw6 != gw_count && num_v6 != 0) {
			if (*family == AF_INET6 || *family == AF_UNSPEC)
				print_unknown_host_msg(" IPv6",
				    gwlist[num_resolved_gw6]);
			num_v6 = 0;
		}

		/* we couldn't resolve a gateway as an IPv4 host */
		if (num_resolved_gw != gw_count && num_v4 != 0) {
			if (*family == AF_INET || *family == AF_UNSPEC)
				print_unknown_host_msg(" IPv4",
				    gwlist[num_resolved_gw]);
			num_v4 = 0;
		}
	}

	*ai_dstp = (num_v4 + num_v6 > 0) ? ai_dst : NULL;
}

/*
 * Given IP address or hostname, return v4 and v6 hostinfo lists.
 * Assumes that hostinfo ** ptrs are non-null.
 */
static void
get_hostinfo(char *host, int family, struct addrinfo **aipp)
{
	struct addrinfo hints, *ai;
	struct in6_addr addr6;
	struct in_addr addr;
	char abuf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */
	int rc;

	/*
	 * Take care of v4-mapped addresses. It should run same as v4, after
	 * chopping off the prefix, leaving the IPv4 address
	 */
	if ((inet_pton(AF_INET6, host, &addr6) > 0) &&
	    IN6_IS_ADDR_V4MAPPED(&addr6)) {
		/* peel off the "mapping" stuff, leaving 32 bit IPv4 address */
		IN6_V4MAPPED_TO_INADDR(&addr6, &addr);

		/* convert it back to a string */
		(void) inet_ntop(AF_INET, &addr, abuf, sizeof (abuf));

		/* now the host is an IPv4 address */
		(void) strcpy(host, abuf);

		/*
		 * If it's a mapped address, we convert it into IPv4
		 * address because traceroute will send and receive IPv4
		 * packets for that address. Therefore, it's a failure case to
		 * ask get_hostinfo() to treat a mapped address as an IPv6
		 * address.
		 */
		if (family == AF_INET6) {
			return;
		}
	}

	(void) memset(&hints, 0, sizeof (hints));
	hints.ai_family = family;
	hints.ai_flags = AI_ADDRCONFIG | AI_CANONNAME;
	rc = getaddrinfo(host, NULL, &hints, &ai);
	if (rc != 0) {
		if (rc != EAI_NONAME)
			Fprintf(stderr, "%s: getaddrinfo: %s\n", prog,
			    gai_strerror(rc));
		*aipp = NULL;
		return;
	}
	*aipp = ai;
}

/*
 * Calculate the packet length to be used, and check against the valid range.
 * Returns -1 if range check fails.
 */
static uint_t
calc_packetlen(int plen_input, struct pr_set *pr)
{
	int minpacket;			/* min ip packet size */
	int optlen;			/* length of ip options */
	int plen;

	/*
	 * LBNL bug fixed: miscalculation of optlen
	 */
	if (gw_count > 0) {
		/*
		 * IPv4:
		 * ----
		 * 5 (NO OPs) + 3 (code, len, ptr) + gateways
		 * IP options field can hold up to 9 gateways. But the API
		 * allows you to specify only 8, because the last one is the
		 * destination host. When this packet is sent, on the wire
		 * you see one gateway replaced by 4 NO OPs. The other 1 NO
		 * OP is for alignment
		 *
		 * IPv6:
		 * ----
		 * Well, formula is different, but the result is same.
		 * 8 byte fixed part for Type 0 Routing header, followed by
		 * gateway addresses
		 */
		optlen = 8 + gw_count * pr->addr_len;
	} else {
		optlen = 0;
	}

	/* take care of the packet length calculations and checks */
	minpacket = pr->ip_hdr_len + sizeof (struct outdata) + optlen;
	if (useicmp)
		minpacket += pr->icmp_minlen;	/* minimum ICMP header size */
	else
		minpacket += sizeof (struct udphdr);
	plen = plen_input;
	if (plen == 0) {
		plen = minpacket;		/* minimum sized packet */
	} else if (minpacket > plen || plen > IP_MAXPACKET) {
		Fprintf(stderr, "%s: %s packet size must be >= %d and <= %d\n",
		    prog, pr->name, minpacket, IP_MAXPACKET);
		return (0);
	}

	return (plen);
}

/*
 * Sets the source address by resolving -i and -s arguments, or if -i and -s
 * don't dictate any, it sets the pick_src to make sure traceroute uses the
 * kernel's pick of the source address.
 * Returns number of interfaces configured on the source host, 0 on error or
 * there's no interface which is up amd not a loopback.
 */
static int
set_src_addr(struct pr_set *pr, struct ifaddrlist **alp)
{
	union any_in_addr *ap;
	struct ifaddrlist *al = NULL;
	struct ifaddrlist *tmp1_al = NULL;
	struct ifaddrlist *tmp2_al = NULL;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	struct sockaddr_in *sin_from = (struct sockaddr_in *)pr->from;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	struct sockaddr_in6 *sin6_from = (struct sockaddr_in6 *)pr->from;
	struct addrinfo *aip;
	char errbuf[ERRBUFSIZE];
	char abuf[INET6_ADDRSTRLEN];		/* use for inet_ntop() */
	int num_ifs;				/* all the interfaces  */
	int num_src_ifs;			/* exclude loopback and down */
	int i;
	uint_t ifaddrflags = 0;

	source = source_input;

	if (device != NULL)
		ifaddrflags |= LIFC_UNDER_IPMP;

	/* get the interface address list */
	num_ifs = ifaddrlist(&al, pr->family, ifaddrflags, errbuf);
	if (num_ifs < 0) {
		Fprintf(stderr, "%s: ifaddrlist: %s\n", prog, errbuf);
		exit(EXIT_FAILURE);
	}

	num_src_ifs = 0;
	for (i = 0; i < num_ifs; i++) {
		if (!(al[i].flags & IFF_LOOPBACK) && (al[i].flags & IFF_UP))
			num_src_ifs++;
	}

	if (num_src_ifs == 0) {
		Fprintf(stderr, "%s: can't find any %s network interfaces\n",
		    prog, pr->name);
		return (0);
	}

	/* verify the device */
	if (device != NULL) {
		tmp1_al = find_device(al, num_ifs, device);

		if (tmp1_al == NULL) {
			Fprintf(stderr, "%s: %s (index %d) is an invalid %s"
			    " interface\n", prog, device, if_index, pr->name);
			free(al);
			return (0);
		}
	}

	/* verify the source address */
	if (source != NULL) {
		get_hostinfo(source, pr->family, &aip);
		if (aip == NULL) {
			Fprintf(stderr,
			    "%s: %s is an invalid %s source address\n",
			    prog, source, pr->name);

			free(al);
			return (0);
		}

		source = aip->ai_canonname;

		if (pr->family == AF_INET)
			ap = (union any_in_addr *)
			    /* LINTED E_BAD_PTR_CAST_ALIGN */
			    &((struct sockaddr_in *)aip->ai_addr)->sin_addr;
		else
			ap = (union any_in_addr *)
			    /* LINTED E_BAD_PTR_CAST_ALIGN */
			    &((struct sockaddr_in6 *)aip->ai_addr)->sin6_addr;

		/*
		 * LBNL bug fixed: used to accept any src address
		 */
		tmp2_al = find_ifaddr(al, num_ifs, ap, pr->family);
		if (tmp2_al == NULL) {
			(void) inet_ntop(pr->family, ap, abuf, sizeof (abuf));
			Fprintf(stderr, "%s: %s is not a local %s address\n",
			    prog, abuf, pr->name);
			free(al);
			freeaddrinfo(aip);
			return (0);
		}
	}

	pick_src = _B_FALSE;

	if (source == NULL) {			/* no -s used */
		if (device == NULL) {		/* no -i used, no -s used */
			pick_src = _B_TRUE;
		} else {			/* -i used, no -s used */
			/*
			 * -i used, but not -s, and it's IPv4: set the source
			 * address to whatever the interface has configured on
			 * it.
			 */
			if (pr->family == AF_INET)
				set_sin(pr->from, &(tmp1_al->addr), pr->family);
			else
				pick_src = _B_TRUE;
		}
	} else {				/* -s used */
		if (device == NULL) {		/* no -i used, -s used */
			set_sin(pr->from, ap, pr->family);

			if (aip->ai_next != NULL) {
				(void) inet_ntop(pr->family, pr->from_sin_addr,
				    abuf, sizeof (abuf));
				Fprintf(stderr, "%s: Warning: %s has multiple "
				    "addresses; using %s\n", prog, source,
				    abuf);
			}
		} else {			/* -i and -s used */
			/*
			 * Make sure the source specified matches the
			 * interface address. You only care about this for IPv4
			 * IPv6 can handle IF not matching src address
			 */
			if (pr->family == AF_INET) {
				if (!has_addr(aip, &tmp1_al->addr)) {
					Fprintf(stderr,
					    "%s: %s is not on interface %s\n",
					    prog, source, device);
					exit(EXIT_FAILURE);
				}
				/*
				 * make sure we use the one matching the
				 * interface's address
				 */
				*ap = tmp1_al->addr;
			}

			set_sin(pr->from, ap, pr->family);
		}
	}

	/*
	 * Binding at this point will set the source address to be used
	 * for both IPv4 (when raw IP datagrams are not required) and
	 * IPv6.  If the address being bound to is zero, then the kernel
	 * will end up choosing the source address when the datagram is
	 * sent.
	 *
	 * For raw IPv4 datagrams, the source address is initialized
	 * within traceroute() along with the outbound destination
	 * address.
	 */
	if (pr->family == AF_INET && !raw_req) {
		sin_from->sin_family = AF_INET;
		sin_from->sin_port = htons(ident);
		if (bind(sndsock4, (struct sockaddr *)pr->from,
			sizeof (struct sockaddr_in)) < 0) {
			Fprintf(stderr, "%s: bind: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	} else if (pr->family == AF_INET6) {
		sin6_from->sin6_family = AF_INET6;
		sin6_from->sin6_port = htons(ident);
		if (bind(sndsock6, (struct sockaddr *)pr->from,
			sizeof (struct sockaddr_in6)) < 0) {
			Fprintf(stderr, "%s: bind: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}

		whereto6.sin6_flowinfo = htonl((class << 20) | flow);
	}
	*alp = al;
	return (num_ifs);
}

/*
 * Returns the complete ifaddrlist structure matching the desired interface
 * address. Ignores interfaces which are either down or loopback.
 */
static struct ifaddrlist *
find_ifaddr(struct ifaddrlist *al, int len, union any_in_addr *addr,
    int family)
{
	struct ifaddrlist *tmp_al = al;
	int i;
	size_t addr_len = (family == AF_INET) ? sizeof (struct in_addr) :
	    sizeof (struct in6_addr);

	for (i = 0; i < len; i++, tmp_al++) {
		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
		    (tmp_al->flags & IFF_UP)) &&
		    (memcmp(&tmp_al->addr, addr, addr_len) == 0))
			break;
	}

	if (i < len) {
		return (tmp_al);
	} else {
		return (NULL);
	}
}

/*
 * Returns the complete ifaddrlist structure matching the desired interface name
 * Ignores interfaces which are either down or loopback.
 */
static struct ifaddrlist *
find_device(struct ifaddrlist *al, int len, char *device)
{
	struct ifaddrlist *tmp_al = al;
	int i;

	for (i = 0; i < len; i++, tmp_al++) {
		if ((!(tmp_al->flags & IFF_LOOPBACK) &&
		    (tmp_al->flags & IFF_UP)) &&
		    (strcmp(tmp_al->device, device) == 0))
			break;
	}

	if (i < len) {
		return (tmp_al);
	} else {
		return (NULL);
	}
}

/*
 * returns _B_TRUE if given hostinfo contains the given address
 */
static boolean_t
has_addr(struct addrinfo *ai, union any_in_addr *addr)
{
	struct addrinfo *ai_tmp = NULL;
	union any_in_addr *ap;

	for (ai_tmp = ai; ai_tmp != NULL; ai_tmp = ai_tmp->ai_next) {
		if (ai_tmp->ai_family == AF_INET6)
			continue;
		ap = (union any_in_addr *)
		    /* LINTED E_BAD_PTR_CAST_ALIGN */
		    &((struct sockaddr_in *)ai_tmp->ai_addr)->sin_addr;
		if (memcmp(ap, addr, sizeof (struct in_addr)) == 0)
			break;
	}

	if (ai_tmp != NULL) {
		return (_B_TRUE);
	} else {
		return (_B_FALSE);
	}
}

/*
 * Resolve the gateway names, splitting results into v4 and v6 lists.
 * Gateway addresses are added to the appropriate passed-in array; the
 * number of resolved gateways for each af is returned in resolved[6].
 * Assumes that passed-in arrays are large enough for MAX_GWS[6] addrs
 * and resolved[6] ptrs are non-null; ignores array and counter if the
 * address family param makes them irrelevant.
 */
static void
get_gwaddrs(char **gwlist, int family, union any_in_addr *gwIPlist,
    union any_in_addr *gwIPlist6, int *resolved, int *resolved6)
{
	int i;
	boolean_t check_v4 = _B_TRUE, check_v6 = _B_TRUE;
	struct addrinfo *ai = NULL;
	struct addrinfo *aip = NULL;

	*resolved = *resolved6 = 0;
	switch (family) {
	case AF_UNSPEC:
		break;
	case AF_INET:
		check_v6 = _B_FALSE;
		break;
	case AF_INET6:
		check_v4 = _B_FALSE;
		break;
	default:
		return;
	}

	if (check_v4 && gw_count >= MAX_GWS) {
		check_v4 = _B_FALSE;
		Fprintf(stderr, "%s: too many IPv4 gateways\n", prog);
		num_v4 = 0;
	}
	if (check_v6 && gw_count >= MAX_GWS6) {
		check_v6 = _B_FALSE;
		Fprintf(stderr, "%s: too many IPv6 gateways\n", prog);
		num_v6 = 0;
	}

	for (i = 0; i < gw_count; i++) {
		if (!check_v4 && !check_v6)
			return;
		get_hostinfo(gwlist[i], family, &ai);
		if (ai == NULL)
			return;
		if (check_v4 && num_v4 != 0) {
			check_v4 = _B_FALSE;
			for (aip = ai; aip != NULL; aip = aip->ai_next) {
				if (aip->ai_family == AF_INET) {
					/* LINTED E_BAD_PTR_CAST_ALIGN */
					bcopy(&((struct sockaddr_in *)
					    aip->ai_addr)->sin_addr,
					    &gwIPlist[i].addr,
					    aip->ai_addrlen);
					(*resolved)++;
					check_v4 = _B_TRUE;
					break;
				}
			}
		} else if (check_v4) {
			check_v4 = _B_FALSE;
		}
		if (check_v6 && num_v6 != 0) {
			check_v6 = _B_FALSE;
			for (aip = ai; aip != NULL; aip = aip->ai_next) {
				if (aip->ai_family == AF_INET6) {
					/* LINTED E_BAD_PTR_CAST_ALIGN */
					bcopy(&((struct sockaddr_in6 *)
					    aip->ai_addr)->sin6_addr,
					    &gwIPlist6[i].addr6,
					    aip->ai_addrlen);
					(*resolved6)++;
					check_v6 = _B_TRUE;
					break;
				}
			}
		} else if (check_v6) {
			check_v6 = _B_FALSE;
		}
	}
	freeaddrinfo(ai);
}

/*
 * set protocol specific values here
 */
static void
setup_protocol(struct pr_set *pr, int family)
{
	/*
	 * Set the global variables for each AF. This is going to save us lots
	 * of "if (family == AF_INET)... else .."
	 */
	pr->family = family;

	if (family == AF_INET) {
		if (!docksum) {
			Fprintf(stderr,
			    "%s: Warning: checksums disabled\n", prog);
		}
		(void) strcpy(pr->name, "IPv4");
		(void) strcpy(pr->icmp, "icmp");
		pr->icmp_minlen = ICMP_MINLEN;
		pr->addr_len = sizeof (struct in_addr);
		pr->ip_hdr_len = sizeof (struct ip);
		pr->sock_size = sizeof (struct sockaddr_in);
		pr->to = (struct sockaddr *)&whereto;
		pr->from = (struct sockaddr *)&wherefrom;
		pr->from_sin_addr = (void *)&wherefrom.sin_addr;
		pr->gwIPlist = gwIPlist;
		pr->set_buffers_fn = set_buffers;
		pr->check_reply_fn = check_reply;
		pr->print_icmp_other_fn = print_icmp_other;
		pr->print_addr_fn = print_addr;
		pr->packlen = calc_packetlen(packlen_input, pr);
	} else {
		(void) strcpy(pr->name, "IPv6");
		(void) strcpy(pr->icmp, "ipv6-icmp");
		pr->icmp_minlen = ICMP6_MINLEN;
		pr->addr_len = sizeof (struct in6_addr);
		pr->ip_hdr_len = sizeof (struct ip6_hdr);
		pr->sock_size = sizeof (struct sockaddr_in6);
		pr->to = (struct sockaddr *)&whereto6;
		pr->from = (struct sockaddr *)&wherefrom6;
		pr->from_sin_addr = (void *)&wherefrom6.sin6_addr;
		pr->gwIPlist = gwIP6list;
		pr->set_buffers_fn = set_buffers6;
		pr->check_reply_fn = check_reply6;
		pr->print_icmp_other_fn = print_icmp_other6;
		pr->print_addr_fn = print_addr6;
		pr->packlen = calc_packetlen(packlen_input, pr);
	}
	if (pr->packlen == 0)
		exit(EXIT_FAILURE);
}

/*
 * setup the sockets for the given protocol's address family
 */
static void
setup_socket(struct pr_set *pr, int packet_len)
{
	int on = 1;
	struct protoent *pe;
	int type;
	int proto;
	int int_op;
	int rsock;
	int ssock;

	if ((pe = getprotobyname(pr->icmp)) == NULL) {
		Fprintf(stderr, "%s: unknown protocol %s\n", prog, pr->icmp);
		exit(EXIT_FAILURE);
	}

	/* privilege bracketing */
	(void) __priv_bracket(PRIV_ON);

	if ((rsock = socket(pr->family, SOCK_RAW, pe->p_proto)) < 0) {
		Fprintf(stderr, "%s: icmp socket: %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (options & SO_DEBUG) {
		if (setsockopt(rsock, SOL_SOCKET, SO_DEBUG, (char *)&on,
		    sizeof (on)) < 0) {
			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}
	if (options & SO_DONTROUTE) {
		if (setsockopt(rsock, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
		    sizeof (on)) < 0) {
			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	if (pr->family == AF_INET6) {
		/* Enable receipt of destination address info */
		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVPKTINFO,
		    (char *)&on, sizeof (on)) < 0) {
			Fprintf(stderr, "%s: IPV6_RECVPKTINFO: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
		/* Enable receipt of hoplimit info */
		if (setsockopt(rsock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
		    (char *)&on, sizeof (on)) < 0) {
			Fprintf(stderr, "%s: IPV6_RECVHOPLIMIT: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}

	}

	/*
	 * Initialize the socket type and protocol based on the address
	 * family, whether or not a raw IP socket is required (for IPv4)
	 * or whether ICMP will be used instead of UDP.
	 *
	 * For historical reasons, the datagrams sent out by
	 * traceroute(8) do not have the "don't fragment" flag set.  For
	 * this reason as well as the ability to set the Loose Source and
	 * Record Route (LSRR) option, a raw IP socket will be used for
	 * IPv4 when run in the global zone.  Otherwise, the actual
	 * datagram that will be sent will be a regular UDP or ICMP echo
	 * request packet.  However for convenience and for future options
	 * when other IP header information may be specified using
	 * traceroute, the buffer including the raw IP and UDP or ICMP
	 * header is always filled in.  When the probe is actually sent,
	 * the size of the request and the start of the packet is set
	 * according to the type of datagram to send.
	 */
	if (pr->family == AF_INET && raw_req) {
		type = SOCK_RAW;
		proto = IPPROTO_RAW;
	} else if (useicmp) {
		type = SOCK_RAW;
		if (pr->family == AF_INET)
			proto = IPPROTO_ICMP;
		else
			proto = IPPROTO_ICMPV6;
	} else {
		type = SOCK_DGRAM;
		proto = IPPROTO_UDP;
	}
	ssock = socket(pr->family, type, proto);

	if (ssock < 0) {
		if (proto == IPPROTO_RAW) {
			Fprintf(stderr, "%s: raw socket: %s\n", prog,
			    strerror(errno));
		} else if (proto == IPPROTO_UDP) {
			Fprintf(stderr, "%s: udp socket: %s\n", prog,
			    strerror(errno));
		} else {
			Fprintf(stderr, "%s: icmp socket: %s\n", prog,
			    strerror(errno));
		}
		exit(EXIT_FAILURE);
	}

	if (setsockopt(ssock, SOL_SOCKET, SO_SNDBUF, (char *)&packet_len,
	    sizeof (packet_len)) < 0) {
		Fprintf(stderr, "%s: SO_SNDBUF: %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (pr->family == AF_INET && raw_req) {
		if (setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
		    sizeof (on)) < 0) {
			Fprintf(stderr, "%s: IP_HDRINCL: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	if (options & SO_DEBUG) {
		if (setsockopt(ssock, SOL_SOCKET, SO_DEBUG, (char *)&on,
		    sizeof (on)) < 0) {
			Fprintf(stderr, "%s: SO_DEBUG: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}
	if (options & SO_DONTROUTE) {
		if (setsockopt(ssock, SOL_SOCKET, SO_DONTROUTE,
		    (char *)&on, sizeof (on)) < 0) {
			Fprintf(stderr, "%s: SO_DONTROUTE: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	/*
	 * If a raw IPv4 packet is going to be sent, the Type of Service
	 * field in the packet will be initialized in set_buffers().
	 * Otherwise, it is initialized here using the IPPROTO_IP level
	 * socket option.
	 */
	if (settos && !raw_req) {
		int_op = tos;
		if (setsockopt(ssock, IPPROTO_IP, IP_TOS, (char *)&int_op,
		    sizeof (int_op)) < 0) {
			Fprintf(stderr, "%s: IP_TOS: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	/* We enable or disable to not depend on the kernel default */
	if (pr->family == AF_INET) {
		if (setsockopt(ssock, IPPROTO_IP, IP_DONTFRAG,
		    (char *)&dontfrag, sizeof (dontfrag)) == -1) {
			Fprintf(stderr, "%s: IP_DONTFRAG %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	} else {
		if (setsockopt(ssock, IPPROTO_IPV6, IPV6_DONTFRAG,
		    (char *)&dontfrag, sizeof (dontfrag)) == -1) {
			Fprintf(stderr, "%s: IPV6_DONTFRAG %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	if (pr->family == AF_INET) {
		rcvsock4 = rsock;
		sndsock4 = ssock;
	} else {
		rcvsock6 = rsock;
		sndsock6 = ssock;
	}
	/* Revert to non-privileged user after configuring sockets */
	(void) __priv_bracket(PRIV_OFF);
}

/*
 * If we are "probing all", this function calls traceroute() for each IP address
 * of the target, otherwise calls only once. Returns _B_FALSE if traceroute()
 * fails.
 */
static void
trace_it(struct addrinfo *ai_dst)
{
	struct msghdr msg6;
	int num_dst_IPaddrs;
	struct addrinfo *aip;
	int i;

	if (!probe_all)
		num_dst_IPaddrs = 1;
	else
		num_dst_IPaddrs = num_v4 + num_v6;

	/*
	 * Initialize the msg6 structure using the hoplimit for the first
	 * probe packet, gateway addresses and the outgoing interface index.
	 */
	if (ai_dst->ai_family == AF_INET6 || (probe_all && num_v6)) {
		msg6.msg_control = NULL;
		msg6.msg_controllen = 0;
		set_ancillary_data(&msg6, first_ttl, pr6->gwIPlist, gw_count,
		    if_index);
	}

	/* run traceroute for all the IP addresses of the multihomed dest */
	for (aip = ai_dst, i = 0; i < num_dst_IPaddrs && aip != NULL; i++) {
		union any_in_addr *addrp;
		if (aip->ai_family == AF_INET) {
			addrp = (union any_in_addr *)
			    /* LINTED E_BAD_PTR_CAST_ALIGN */
			    &((struct sockaddr_in *)
			    aip->ai_addr)->sin_addr;
			set_sin((struct sockaddr *)pr4->to, addrp,
			    aip->ai_family);
			traceroute(addrp, &msg6, pr4, num_ifs4, al4);
		} else {
			addrp = (union any_in_addr *)
			    /* LINTED E_BAD_PTR_CAST_ALIGN */
			    &((struct sockaddr_in6 *)
			    aip->ai_addr)->sin6_addr;
			set_sin((struct sockaddr *)pr6->to, addrp,
			    aip->ai_family);
			traceroute(addrp, &msg6, pr6, num_ifs6, al6);
		}
		aip = aip->ai_next;
		if (i < (num_dst_IPaddrs - 1))
			(void) putchar('\n');
	}
}

/*
 * set the IP address in a sockaddr struct
 */
static void
set_sin(struct sockaddr *sock, union any_in_addr *addr, int family)
{
	sock->sa_family = family;

	if (family == AF_INET)
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		((struct sockaddr_in *)sock)->sin_addr = addr->addr;
	else
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		((struct sockaddr_in6 *)sock)->sin6_addr = addr->addr6;
}

/*
 * returns the IF name on which the given IP address is configured
 */
static char *
device_name(struct ifaddrlist *al, int len, union any_in_addr *ip_addr,
    struct pr_set *pr)
{
	int i;
	struct ifaddrlist *tmp_al;

	tmp_al = al;

	for (i = 0; i < len; i++, tmp_al++) {
		if (memcmp(&tmp_al->addr, ip_addr, pr->addr_len) == 0) {
			return (tmp_al->device);
		}
	}

	return (NULL);
}

/*
 * Trace the route to the host with given IP address.
 */
static void
traceroute(union any_in_addr *ip_addr, struct msghdr *msg6, struct pr_set *pr,
    int num_ifs, struct ifaddrlist *al)
{
	int ttl;
	int probe;
	uchar_t type;				/* icmp type */
	uchar_t code;				/* icmp code */
	int reply;
	int seq = 0;
	char abuf[INET6_ADDRSTRLEN];		/* use for inet_ntop() */
	int longjmp_return;			/* return value from longjump */
	struct ip *ip = (struct ip *)packet;
	boolean_t got_there = _B_FALSE;		/* we hit the destination */
	static boolean_t first_pkt = _B_TRUE;
	int hoplimit;				/* hoplimit for IPv6 packets */
	struct in6_addr addr6;
	int num_src_ifs;			/* excludes down and loopback */
	struct msghdr in_msg;
	struct iovec iov;
	int *intp;
	int sndsock;
	int rcvsock;

	msg6->msg_name = pr->to;
	msg6->msg_namelen = sizeof (struct sockaddr_in6);
	sndsock =  (pr->family == AF_INET) ? sndsock4 : sndsock6;
	rcvsock =  (pr->family == AF_INET) ? rcvsock4 : rcvsock6;

	/* carry out the source address selection */
	if (pick_src) {
		union any_in_addr src_addr;
		char *dev_name;
		int i;

		/*
		 * If there's a gateway, a routing header as a consequence, our
		 * kernel picks the source address based on the first hop
		 * address, rather than final destination address.
		 */
		if (gw_count > 0) {
			(void) select_src_addr(pr->gwIPlist, &src_addr,
			    pr->family);
		} else {
			(void) select_src_addr(ip_addr, &src_addr, pr->family);
		}
		set_sin(pr->from, &src_addr, pr->family);

		/* filter out down and loopback interfaces */
		num_src_ifs = 0;
		for (i = 0; i < num_ifs; i++) {
			if (!(al[i].flags & IFF_LOOPBACK) &&
			    (al[i].flags & IFF_UP))
				num_src_ifs++;
		}

		if (num_src_ifs > 1) {
			dev_name = device_name(al, num_ifs, &src_addr, pr);
			if (dev_name == NULL)
				dev_name = "?";

			(void) inet_ntop(pr->family, pr->from_sin_addr, abuf,
			    sizeof (abuf));
			Fprintf(stderr,
			    "%s: Warning: Multiple interfaces found;"
			    " using %s @ %s\n", prog, abuf, dev_name);
		}
	}

	if (pr->family == AF_INET) {
		outip4->ip_src = *(struct in_addr *)pr->from_sin_addr;
		outip4->ip_dst = ip_addr->addr;
	}

	/*
	 * If the hostname is an IPv6 literal address, let's not print it twice.
	 */
	if (pr->family == AF_INET6 &&
	    inet_pton(AF_INET6, hostname, &addr6) > 0) {
		Fprintf(stderr, "%s to %s", prog, hostname);
	} else {
		Fprintf(stderr, "%s to %s (%s)", prog, hostname,
		    inet_ntop(pr->family, ip_addr, abuf, sizeof (abuf)));
	}

	if (source)
		Fprintf(stderr, " from %s", source);
	Fprintf(stderr, ", %d hops max, %d byte packets\n", max_ttl,
	    pr->packlen);
	(void) fflush(stderr);

	/*
	 * Setup the source routing for IPv4. For IPv6, we did the required
	 * setup in the caller function, trace_it(), because it's independent
	 * from the IP address of target.
	 */
	if (pr->family == AF_INET && gw_count > 0)
		set_IPv4opt_sourcerouting(sndsock, ip_addr, pr->gwIPlist);

	if (probe_all) {
		/* interrupt handler sig_handler() jumps back to here */
		if ((longjmp_return = setjmp(env)) != 0) {
			switch (longjmp_return) {
			case SIGINT:
				Printf("(skipping)\n");
				return;
			case SIGQUIT:
				Printf("(exiting)\n");
				exit(EXIT_SUCCESS);
			default:	/* should never happen */
				exit(EXIT_FAILURE);
			}
		}
		(void) signal(SIGINT, sig_handler);
	}

	for (ttl = first_ttl; ttl <= max_ttl; ++ttl) {
		union any_in_addr lastaddr;
		int timeouts = 0;
		double rtt;		/* for statistics */
		int nreceived = 0;
		double rttmin, rttmax;
		double rttsum, rttssq;
		int unreachable;

		got_there = _B_FALSE;
		unreachable = 0;

		/*
		 * The following line clears both IPv4 and IPv6 address stored
		 * in the union.
		 */
		lastaddr.addr6 = in6addr_any;

		if ((ttl == (first_ttl + 1)) && (options & SO_DONTROUTE)) {
			Fprintf(stderr,
			    "%s: host %s is not on a directly-attached"
			    " network\n", prog, hostname);
			break;
		}

		Printf("%2d ", ttl);
		(void) fflush(stdout);

		for (probe = 0; (probe < nprobes) && (timeouts < max_timeout);
		    ++probe) {
			int cc;
			struct timeval t1, t2;

			/*
			 * Put a delay before sending this probe packet. Don't
			 * delay it if it's the very first packet.
			 */
			if (!first_pkt) {
				if (delay.tv_sec > 0)
					(void) sleep((uint_t)delay.tv_sec);
				if (delay.tv_usec > 0)
					(void) usleep(delay.tv_usec);
			} else {
				first_pkt = _B_FALSE;
			}

			(void) gettimeofday(&t1, NULL);

			if (pr->family == AF_INET) {
				send_probe(sndsock, pr->to, outip4, seq, ttl,
				    &t1, pr->packlen);
			} else {
				send_probe6(sndsock, msg6, outip6, seq, ttl,
				    &t1, pr->packlen);
			}

			/* prepare msghdr for recvmsg() */
			in_msg.msg_name = pr->from;
			in_msg.msg_namelen = pr->sock_size;

			iov.iov_base = (char *)packet;
			iov.iov_len = sizeof (packet);

			in_msg.msg_iov = &iov;
			in_msg.msg_iovlen = 1;

			in_msg.msg_control = ancillary_data;
			in_msg.msg_controllen = sizeof (ancillary_data);

			while ((cc = wait_for_reply(rcvsock, &in_msg,
			    &t1)) != 0) {
				(void) gettimeofday(&t2, NULL);

				reply = (*pr->check_reply_fn) (&in_msg, cc, seq,
				    &type, &code);

				in_msg.msg_controllen =
				    sizeof (ancillary_data);
				/* Skip short packet */
				if (reply == REPLY_SHORT_PKT) {
					continue;
				}

				timeouts = 0;

				/*
				 * if reply comes from a different host, print
				 * the hostname
				 */
				if (memcmp(pr->from_sin_addr, &lastaddr,
				    pr->addr_len) != 0) {
					(*pr->print_addr_fn) ((uchar_t *)packet,
					    cc, pr->from);
					/* store the address response */
					(void) memcpy(&lastaddr,
					    pr->from_sin_addr, pr->addr_len);
				}

				rtt = deltaT(&t1, &t2);
				if (collect_stat) {
					record_stats(rtt, &nreceived, &rttmin,
					    &rttmax, &rttsum, &rttssq);
				} else {
					Printf("  %.3f ms", rtt);
				}

				if (pr->family == AF_INET6) {
					intp = find_ancillary_data(&in_msg,
					    IPPROTO_IPV6, IPV6_HOPLIMIT);
					if (intp == NULL) {
						Fprintf(stderr,
						    "%s: can't find "
						    "IPV6_HOPLIMIT ancillary "
						    "data\n", prog);
						exit(EXIT_FAILURE);
					}
					hoplimit = *intp;
				}

				if (reply == REPLY_GOT_TARGET) {
					got_there = _B_TRUE;

					if (((pr->family == AF_INET) &&
					    (ip->ip_ttl <= 1)) ||
					    ((pr->family == AF_INET6) &&
					    (hoplimit <= 1)))
						Printf(" !");
				}

				if (!collect_stat && showttl) {
					if (pr->family == AF_INET) {
						Printf(" (ttl=%d)",
						    (int)ip->ip_ttl);
					} else if (hoplimit != -1) {
						Printf(" (hop limit=%d)",
						    hoplimit);
					}
				}

				if (reply == REPLY_GOT_OTHER) {
					if ((*pr->print_icmp_other_fn)
					    (type, code)) {
						unreachable++;
					}
				}

				/* special case */
				if (pr->family == AF_INET &&
				    type == ICMP_UNREACH &&
				    code == ICMP_UNREACH_PROTOCOL)
					got_there = _B_TRUE;

				break;
			}

			seq = (seq + 1) % (MAX_SEQ + 1);

			if (cc == 0) {
				Printf(" *");
				timeouts++;
			}

			(void) fflush(stdout);
		}

		if (collect_stat) {
			print_stats(probe, nreceived, rttmin, rttmax, rttsum,
			    rttssq);
		}

		(void) putchar('\n');

		/* either we hit the target or received too many unreachables */
		if (got_there ||
		    (unreachable > 0 && unreachable >= nprobes - 1))
			break;
	}

	/* Ignore the SIGINT between traceroute() runs */
	if (probe_all)
		(void) signal(SIGINT, SIG_IGN);
}

/*
 * for a given destination address and address family, it finds out what
 * source address kernel is going to pick
 */
static void
select_src_addr(union any_in_addr *dst_addr, union any_in_addr *src_addr,
    int family)
{
	int tmp_fd;
	struct sockaddr *sock;
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;
	size_t sock_len;

	sock = (struct sockaddr *)malloc(sizeof (struct sockaddr_in6));
	if (sock == NULL) {
		Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}
	(void) bzero(sock, sizeof (struct sockaddr_in6));

	if (family == AF_INET) {
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		sin = (struct sockaddr_in *)sock;
		sin->sin_family = AF_INET;
		sin->sin_addr = dst_addr->addr;
		sin->sin_port = IPPORT_ECHO;	/* port shouldn't be 0 */
		sock_len = sizeof (struct sockaddr_in);
	} else {
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		sin6 = (struct sockaddr_in6 *)sock;
		sin6->sin6_family = AF_INET6;
		sin6->sin6_addr = dst_addr->addr6;
		sin6->sin6_port = IPPORT_ECHO;	/* port shouldn't be 0 */
		sock_len = sizeof (struct sockaddr_in6);
	}

	/* open a UDP socket */
	if ((tmp_fd = socket(family, SOCK_DGRAM, 0)) < 0) {
		Fprintf(stderr, "%s: udp socket: %s\n", prog,
		    strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* connect it */
	if (connect(tmp_fd, sock, sock_len) < 0) {
		/*
		 * If there's no route to the destination, this connect() call
		 * fails. We just return all-zero (wildcard) as the source
		 * address, so that user can get to see "no route to dest"
		 * message, as it'll try to send the probe packet out and will
		 * receive ICMP unreachable.
		 */
		if (family == AF_INET)
			src_addr->addr.s_addr = INADDR_ANY;
		else
			src_addr->addr6 = in6addr_any;
		free(sock);
		return;
	}

	/* get the local sock info */
	if (getsockname(tmp_fd, sock, &sock_len) < 0) {
		Fprintf(stderr, "%s: getsockname: %s\n", prog,
		    strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (family == AF_INET) {
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		sin = (struct sockaddr_in *)sock;
		src_addr->addr = sin->sin_addr;
	} else {
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		sin6 = (struct sockaddr_in6 *)sock;
		src_addr->addr6 = sin6->sin6_addr;
	}

	free(sock);
	(void) close(tmp_fd);
}

/*
 * Checksum routine for Internet Protocol family headers (C Version)
 */
ushort_t
in_cksum(ushort_t *addr, int len)
{
	int nleft = len;
	ushort_t *w = addr;
	ushort_t answer;
	int sum = 0;

	/*
	 *  Our algorithm is simple, using a 32 bit accumulator (sum),
	 *  we add sequential 16 bit words to it, and at the end, fold
	 *  back all the carry bits from the top 16 bits into the lower
	 *  16 bits.
	 */
	while (nleft > 1)  {
		sum += *w++;
		nleft -= 2;
	}

	/* mop up an odd byte, if necessary */
	if (nleft == 1)
		sum += *(uchar_t *)w;

	/* add back carry outs from top 16 bits to low 16 bits */
	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
	sum += (sum >> 16);			/* add carry */
	answer = ~sum;				/* truncate to 16 bits */
	return (answer);
}

/*
 * Wait until a reply arrives or timeout occurs. If packet arrived, read it
 * return the size of the packet read.
 */
static int
wait_for_reply(int sock, struct msghdr *msg, struct timeval *tp)
{
	fd_set fds;
	struct timeval now, wait;
	int cc = 0;
	int result;

	(void) FD_ZERO(&fds);
	FD_SET(sock, &fds);

	wait.tv_sec = tp->tv_sec + waittime;
	wait.tv_usec = tp->tv_usec;
	(void) gettimeofday(&now, NULL);
	tv_sub(&wait, &now);

	if (wait.tv_sec < 0 || wait.tv_usec < 0)
		return (0);

	result = select(sock + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &wait);

	if (result == -1) {
		if (errno != EINTR) {
			Fprintf(stderr, "%s: select: %s\n", prog,
			    strerror(errno));
		}
	} else if (result > 0)
		cc = recvmsg(sock, msg, 0);

	return (cc);
}

/*
 * Construct an Internet address representation. If the nflag has been supplied,
 * give numeric value, otherwise try for symbolic name.
 */
char *
inet_name(union any_in_addr *in, int family)
{
	char *cp;
	static boolean_t first = _B_TRUE;
	static char domain[NI_MAXHOST + 1];
	static char line[NI_MAXHOST + 1];	/* assuming		*/
				/* (NI_MAXHOST + 1) >= INET6_ADDRSTRLEN */
	char hbuf[NI_MAXHOST];
	socklen_t slen;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
	struct sockaddr *sa;
	int flags;

	switch (family) {
	case AF_INET:
		slen = sizeof (struct sockaddr_in);
		sin.sin_addr = in->addr;
		sin.sin_port = 0;
		sa = (struct sockaddr *)&sin;
		break;
	case AF_INET6:
		slen = sizeof (struct sockaddr_in6);
		sin6.sin6_addr = in->addr6;
		sin6.sin6_port = 0;
		sin6.sin6_scope_id = 0;
		sa = (struct sockaddr *)&sin6;
		break;
	default:
		(void) snprintf(line, sizeof (line),
		    "<invalid address family>");
		return (line);
	}
	sa->sa_family = family;

	if (first && !nflag) {
		/* find out the domain name */
		first = _B_FALSE;
		mutex_enter(&tr_nslock);
		tr_nsactive = _B_TRUE;
		tr_nsstarttime = gethrtime();
		mutex_exit(&tr_nslock);
		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
		    (cp = strchr(domain, '.')) != NULL) {
			(void) strncpy(domain, cp + 1, sizeof (domain) - 1);
			domain[sizeof (domain) - 1] = '\0';
		} else {
			domain[0] = '\0';
		}
		mutex_enter(&tr_nslock);
		tr_nsactive = _B_FALSE;
		mutex_exit(&tr_nslock);
	}

	flags = (nflag) ? NI_NUMERICHOST : NI_NAMEREQD;
	mutex_enter(&tr_nslock);
	tr_nsactive = _B_TRUE;
	tr_nsstarttime = gethrtime();
	mutex_exit(&tr_nslock);
	if (getnameinfo(sa, slen, hbuf, sizeof (hbuf), NULL, 0, flags) != 0) {
		if (inet_ntop(family, (const void *)&in->addr6,
		    hbuf, sizeof (hbuf)) == NULL)
			hbuf[0] = 0;
	} else if (!nflag && (cp = strchr(hbuf, '.')) != NULL &&
	    strcmp(cp + 1, domain) == 0) {
		*cp = '\0';
	}
	mutex_enter(&tr_nslock);
	tr_nsactive = _B_FALSE;
	mutex_exit(&tr_nslock);
	(void) strlcpy(line, hbuf, sizeof (line));

	return (line);
}

/*
 * return the difference (in msec) between two time values
 */
static double
deltaT(struct timeval *t1p, struct timeval *t2p)
{
	double dt;

	dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
	    (double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
	return (dt);
}

/*
 * Subtract 2 timeval structs:  out = out - in.
 * Out is assumed to be >= in.
 */
static void
tv_sub(struct timeval *out, struct timeval *in)
{
	if ((out->tv_usec -= in->tv_usec) < 0)   {
		--out->tv_sec;
		out->tv_usec += 1000000;
	}
	out->tv_sec -= in->tv_sec;
}

/*
 * record statistics
 */
static void
record_stats(double rtt, int *nreceived, double *rttmin, double *rttmax,
    double *rttsum, double *rttssq)
{
	if (*nreceived == 0) {
		*rttmin = rtt;
		*rttmax = rtt;
		*rttsum = rtt;
		*rttssq = rtt * rtt;
	} else {
		if (rtt < *rttmin)
			*rttmin = rtt;

		if (rtt > *rttmax)
			*rttmax = rtt;

		*rttsum += rtt;
		*rttssq += rtt * rtt;
	}

	(*nreceived)++;
}

/*
 * display statistics
 */
static void
print_stats(int ntransmitted, int nreceived, double rttmin, double rttmax,
    double rttsum, double rttssq)
{
	double rttavg;			/* average round-trip time */
	double rttstd;			/* rtt standard deviation */

	if (ntransmitted > 0 && ntransmitted >= nreceived) {
		int missed = ntransmitted - nreceived;
		double loss = 100 * (double)missed / (double)ntransmitted;

		if (nreceived > 0) {
			rttavg = rttsum / nreceived;
			rttstd = rttssq - (rttavg * rttsum);
			rttstd = xsqrt(rttstd / nreceived);

			Printf("  %.3f", rttmin);
			Printf("/%.3f", rttavg);
			Printf("/%.3f", rttmax);

			Printf(" (%.3f) ms ", rttstd);
		}

		Printf(" %d/%d pkts", nreceived, ntransmitted);

		if (nreceived == 0)
			Printf(" (100%% loss)");
		else
			Printf(" (%.2g%% loss)", loss);
	}
}

/*
 * square root function
 */
double
xsqrt(double y)
{
	double t, x;

	if (y <= 0) {
		return (0.0);
	}

	x = (y < 1.0) ? 1.0 : y;
	do {
		t = x;
		x = (t + (y/t))/2.0;
	} while (0 < x && x < t);

	return (x);
}

/*
 * String to double with optional min and max.
 */
static double
str2dbl(const char *str, const char *what, double mi, double ma)
{
	double val;
	char *ep;

	errno = 0;

	val = strtod(str, &ep);
	if (errno != 0 || *ep != '\0') {
		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
		    prog, str, what);
		exit(EXIT_FAILURE);
	}
	if (val < mi && mi >= 0) {
		Fprintf(stderr, "%s: %s must be >= %f\n", prog, what, mi);
		exit(EXIT_FAILURE);
	}
	if (val > ma && ma >= 0) {
		Fprintf(stderr, "%s: %s must be <= %f\n", prog, what, ma);
		exit(EXIT_FAILURE);
	}
	return (val);
}

/*
 * String to int with optional min and max. Handles decimal and hex.
 */
static int
str2int(const char *str, const char *what, int mi, int ma)
{
	const char *cp;
	int val;
	char *ep;

	errno = 0;

	if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
		cp = str + 2;
		val = (int)strtol(cp, &ep, 16);
	} else {
		val = (int)strtol(str, &ep, 10);
	}
	if (errno != 0 || *ep != '\0') {
		Fprintf(stderr, "%s: \"%s\" bad value for %s \n",
		    prog, str, what);
		exit(EXIT_FAILURE);
	}
	if (val < mi && mi >= 0) {
		if (mi == 0) {
			Fprintf(stderr, "%s: %s must be >= %d\n",
			    prog, what, mi);
		} else {
			Fprintf(stderr, "%s: %s must be > %d\n",
			    prog, what, mi - 1);
		}
		exit(EXIT_FAILURE);
	}
	if (val > ma && ma >= 0) {
		Fprintf(stderr, "%s: %s must be <= %d\n", prog, what, ma);
		exit(EXIT_FAILURE);
	}
	return (val);
}

/*
 * This is the interrupt handler for SIGINT and SIGQUIT. It's completely handled
 * where it jumps to.
 */
static void
sig_handler(int sig)
{
	longjmp(env, sig);
}

/*
 * display the usage of traceroute
 */
static void
usage(void)
{
	Fprintf(stderr, "Usage: %s [-adFIlnSvx] [-A address_family] "
	    "[-c traffic_class]\n"
	    "\t[-f first_hop] [-g gateway [-g gateway ...]| -r] [-i iface]\n"
	    "\t[-L flow_label] [-m max_hop] [-P pause_sec] [-p port] "
	    "[-Q max_timeout]\n"
	    "\t[-q nqueries] [-s src_addr] [-t tos] [-w wait_time] host "
	    "[packetlen]\n", prog);
	exit(EXIT_FAILURE);
}

/* ARGSUSED */
static void *
ns_warning_thr(void *unused)
{
	for (;;) {
		hrtime_t now;

		(void) sleep(tr_nssleeptime);

		now = gethrtime();
		mutex_enter(&tr_nslock);
		if (tr_nsactive && now - tr_nsstarttime >=
		    tr_nswarntime * NANOSEC) {
			Fprintf(stderr, "%s: warning: responses "
			    "received, but name service lookups are "
			    "taking a while. Use %s -n to disable "
			    "name service lookups.\n",
			    prog, prog);
			mutex_exit(&tr_nslock);
			return (NULL);
		}
		mutex_exit(&tr_nslock);
	}

	/* LINTED: E_STMT_NOT_REACHED */
	return (NULL);
}
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
 */

#ifndef _TRACEROUTE_H
#define	_TRACEROUTE_H

#ifdef __cplusplus
extern "C" {
#endif

#define	MAX_PORT	65535		/* max port value for UDP */

#define	REPLY_SHORT_PKT		0	/* check_reply() has a short packet */
#define	REPLY_GOT_GATEWAY	1	/* ... rcvd a reply from an inter. gw */
#define	REPLY_GOT_TARGET	2	/* ... rcvd the reply from the target */
#define	REPLY_GOT_OTHER		3	/* ... received other */

/*
 * this is the max it can be, yet another factor is PMTU, which is ignored
 * here
 */
#define	MAX_GWS6	127

/*
 * Maximum number of gateways (include room for one noop).
 * 'in_addr_t' is 32 bits, size of IPv4 address.
 * Note that the actual number of gateways that can be used for source
 * routing is one less than the value below. This is because the API requires
 * the last gateway to be the target address.
 */
#define	MAX_GWS		9

/* maximum of max_gws */
#define	MAXMAX_GWS	MAX(MAX_GWS, MAX_GWS6)

#define	A_CNT(ARRAY)	(sizeof (ARRAY) / sizeof ((ARRAY)[0]))

#define	Fprintf		(void)fprintf
#define	Printf		(void)printf

struct icmptype_table {
	int type;		/* ICMP type */
	char *message;		/* corresponding string message */
};

/* Data section of the probe packet */
struct outdata {
	uchar_t seq;		/* sequence number of this packet */
	uchar_t ttl;		/* ttl packet left with */
	struct timeval tv;	/* time packet left */
};

extern boolean_t docksum;	/* do checksum (IPv4 only) */
extern int gw_count;		/* number of LSRR gateways */
extern char *hostname;
extern ushort_t ident;		/* identity of this traceroute run */
extern boolean_t nflag;		/* numeric flag */
extern ushort_t off;		/* set DF bit (IPv4 only) */
extern int packlen;		/* packet length */
extern ushort_t port;		/* seed of destination port */
extern char *prog;		/* program name */
extern boolean_t raw_req;	/* if sndsock for IPv4 must be raw */
extern boolean_t settos;	/* set type-of-service (IPv4 only) */
extern unsigned char tos;	/* value of tos to set */
extern boolean_t useicmp;	/* use ICMP or UDP */
extern boolean_t verbose;

#ifdef __cplusplus
}
#endif

#endif /* _TRACEROUTE_H */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
 */

#include <sys/socket.h>

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <libintl.h>
#include <errno.h>

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>

#include <arpa/inet.h>
#include <netdb.h>

#include <libinetutil.h>
#include "traceroute.h"

/*
 * IPv4 source routing option.
 * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
 * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
 */
struct ip_sourceroute {
	uint8_t ipsr_code;
	uint8_t ipsr_len;
	uint8_t ipsr_ptr;
	/* up to 9 IPv4 addresses */
	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
};

int check_reply(struct msghdr *, int, int, uchar_t *, uchar_t *);
extern ushort_t in_cksum(ushort_t *, int);
extern char *inet_name(union any_in_addr *, int);
static char *pr_type(uchar_t);
void print_addr(uchar_t *, int, struct sockaddr *);
boolean_t print_icmp_other(uchar_t, uchar_t);
void send_probe(int, struct sockaddr *, struct ip *, int, int,
    struct timeval *, int);
struct ip *set_buffers(int);
void set_IPv4opt_sourcerouting(int, union any_in_addr *, union any_in_addr *);

/*
 * prepares the buffer to be sent as an IP datagram
 */
struct ip *
set_buffers(int plen)
{
	struct ip *outip;
	uchar_t *outp;		/* packet following the IP header (UDP/ICMP) */
	struct udphdr *outudp;
	struct icmp *outicmp;
	int optlen = 0;

	outip = (struct ip *)malloc((size_t)plen);
	if (outip == NULL) {
		Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (gw_count > 0) {
		/* 8 = 5 (NO OPs) + 3 (code, len, ptr) */
		optlen = 8 + gw_count * sizeof (struct in_addr);
	}

	(void) memset((char *)outip, 0, (size_t)plen);
	outp = (uchar_t *)(outip + 1);

	outip->ip_v = IPVERSION;
	if (settos)
		outip->ip_tos = tos;

	/*
	 * LBNL bug fixed: missing '- optlen' before, causing optlen
	 * added twice
	 *
	 * BSD bug: BSD touches the header fields 'len' and 'ip_off'
	 * even when HDRINCL is set. It applies htons() on these
	 * fields. It should send the header untouched when HDRINCL
	 * is set.
	 */
	outip->ip_len = htons(plen - optlen);
	outip->ip_off = htons(off);
	outip->ip_hl = (outp - (uchar_t *)outip) >> 2;

	/* setup ICMP or UDP */
	if (useicmp) {
		outip->ip_p = IPPROTO_ICMP;

		/* LINTED E_BAD_PTR_CAST_ALIGN */
		outicmp = (struct icmp *)outp;
		outicmp->icmp_type = ICMP_ECHO;
		outicmp->icmp_id = htons(ident);
	} else {
		outip->ip_p = IPPROTO_UDP;

		/* LINTED E_BAD_PTR_CAST_ALIGN */
		outudp = (struct udphdr *)outp;
		outudp->uh_sport = htons(ident);
		outudp->uh_ulen =
		    htons((ushort_t)(plen - (sizeof (struct ip) + optlen)));
	}

	return (outip);
}

/*
 * Setup the source routing for IPv4.
 */
void
set_IPv4opt_sourcerouting(int sndsock, union any_in_addr *ip_addr,
    union any_in_addr *gwIPlist)
{
	struct protoent *pe;
	struct ip_sourceroute *srp;
	uchar_t optlist[MAX_IPOPTLEN];
	int i;
	int gwV4_count;

	if ((pe = getprotobyname("ip")) == NULL) {
		Fprintf(stderr, "%s: unknown protocol ip\n", prog);
		exit(EXIT_FAILURE);
	}

	gwV4_count = (gw_count < MAX_GWS) ? gw_count : MAX_GWS - 1;
	/* final hop */
	gwIPlist[gwV4_count].addr = ip_addr->addr;

	/*
	 * the option length passed to setsockopt() needs to be a multiple of
	 * 32 bits. Therefore we need to use a 1-byte padding (source routing
	 * information takes 4x+3 bytes).
	 */
	optlist[0] = IPOPT_NOP;

	srp = (struct ip_sourceroute *)&optlist[1];
	srp->ipsr_code = IPOPT_LSRR;
	/* 3 = 1 (code) + 1 (len) + 1 (ptr) */
	srp->ipsr_len = 3 + (gwV4_count + 1) * sizeof (gwIPlist[0].addr);
	srp->ipsr_ptr = IPOPT_MINOFF;

	for (i = 0; i <= gwV4_count; i++) {
		(void) bcopy((char *)&gwIPlist[i].addr, &srp->ipsr_addrs[i],
		    sizeof (struct in_addr));
	}

	if (setsockopt(sndsock, pe->p_proto, IP_OPTIONS, (const char *)optlist,
	    srp->ipsr_len + 1) < 0) {
		Fprintf(stderr, "%s: IP_OPTIONS: %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}
}

/*
 * send a probe packet to the destination
 */
void
send_probe(int sndsock, struct sockaddr *to, struct ip *outip,
    int seq, int ttl, struct timeval *tp, int packlen)
{
	int cc;
	struct udpiphdr *ui;
	uchar_t *outp;		/* packet following the IP header (UDP/ICMP) */
	struct udphdr *outudp;
	struct icmp *outicmp;
	struct outdata *outdata;
	struct ip tip;
	int optlen = 0;
	int send_size;

	/* initialize buffer pointers */
	outp = (uchar_t *)(outip + 1);
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	outudp =  (struct udphdr *)outp;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	outicmp = (struct icmp *)outp;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	outdata = (struct outdata *)(outp + ICMP_MINLEN);

	if (gw_count > 0) {
		/* 8 = 5 (NO OPs) + 3 (code, len, ptr) */
		optlen = 8 + gw_count * sizeof (struct in_addr);
	}

	if (raw_req) {
		send_size = packlen - optlen;
	} else if (useicmp) {
		send_size = packlen - optlen - sizeof (struct ip);
	} else {
		send_size = packlen - optlen - sizeof (struct ip) -
		    sizeof (struct udphdr);
	}

	outip->ip_ttl = ttl;
	outip->ip_id = htons(ident + seq);

	/*
	 * If a raw IPv4 packet is going to be sent, the Time to Live
	 * field in the packet was initialized above.  Otherwise, it is
	 * initialized here using the IPPROTO_IP level socket option.
	 */
	if (!raw_req) {
		if (setsockopt(sndsock, IPPROTO_IP, IP_TTL, (char *)&ttl,
		    sizeof (ttl)) < 0) {
			Fprintf(stderr, "%s: IP_TTL: %s\n", prog,
			    strerror(errno));
			exit(EXIT_FAILURE);
		}
	}

	/*
	 * In most cases, the kernel will recalculate the ip checksum.
	 * But we must do it anyway so that the udp checksum comes out
	 * right.
	 */
	if (docksum) {
		outip->ip_sum =
		    in_cksum((ushort_t *)outip, sizeof (*outip) + optlen);
		if (outip->ip_sum == 0)
			outip->ip_sum = 0xffff;
	}

	/* Payload */
	outdata->seq = seq;
	outdata->ttl = ttl;
	outdata->tv = *tp;

	if (useicmp) {
		outicmp->icmp_seq = htons(seq);
	} else {
		outudp->uh_dport  = htons((port + seq) % (MAX_PORT + 1));
	}

	if (!raw_req)
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		((struct sockaddr_in *)to)->sin_port = outudp->uh_dport;

	/* (We can only do the checksum if we know our ip address) */
	if (docksum) {
		if (useicmp) {
			outicmp->icmp_cksum = 0;
			outicmp->icmp_cksum = in_cksum((ushort_t *)outicmp,
			    packlen - (sizeof (struct ip) + optlen));
			if (outicmp->icmp_cksum == 0)
				outicmp->icmp_cksum = 0xffff;
		} else {
			/* Checksum (must save and restore ip header) */
			tip = *outip;
			ui = (struct udpiphdr *)outip;
			ui->ui_next = 0;
			ui->ui_prev = 0;
			ui->ui_x1 = 0;
			ui->ui_len = outudp->uh_ulen;
			outudp->uh_sum = 0;
			outudp->uh_sum = in_cksum((ushort_t *)ui, packlen);
			if (outudp->uh_sum == 0)
				outudp->uh_sum = 0xffff;
			*outip = tip;
		}
	}

	if (raw_req) {
		cc = sendto(sndsock, (char *)outip, send_size, 0, to,
		    sizeof (struct sockaddr_in));
	} else if (useicmp) {
		cc = sendto(sndsock, (char *)outicmp, send_size, 0, to,
		    sizeof (struct sockaddr_in));
	} else {
		cc = sendto(sndsock, (char *)outp, send_size, 0, to,
		    sizeof (struct sockaddr_in));
	}

	if (cc < 0 || cc != send_size)  {
		if (cc < 0) {
			Fprintf(stderr, "%s: sendto: %s\n", prog,
			    strerror(errno));
		}
		Printf("%s: wrote %s %d chars, ret=%d\n",
		    prog, hostname, send_size, cc);
		(void) fflush(stdout);
	}
}

/*
 * Check out the reply packet to see if it's what we were expecting.
 * Returns REPLY_GOT_TARGET if the reply comes from the target
 *         REPLY_GOT_GATEWAY if an intermediate gateway sends TIME_EXCEEDED
 *         REPLY_GOT_OTHER for other kinds of unreachables indicating none of
 *	   the above two cases
 *
 * It also sets the icmp type and icmp code values
 */
int
check_reply(struct msghdr *msg, int cc, int seq, uchar_t *type, uchar_t *code)
{
	uchar_t *buf = msg->msg_iov->iov_base;
	struct sockaddr_in *from_in = (struct sockaddr_in *)msg->msg_name;
	struct icmp *icp;
	int hlen;
	int save_cc = cc;
	struct ip *ip;

	/* LINTED E_BAD_PTR_CAST_ALIGN */
	ip = (struct ip *)buf;
	hlen = ip->ip_hl << 2;
	if (cc < hlen + ICMP_MINLEN) {
		if (verbose) {
			Printf("packet too short (%d bytes) from %s\n",
			    cc, inet_ntoa(from_in->sin_addr));
		}
		return (REPLY_SHORT_PKT);
	}
	cc -= hlen;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	icp = (struct icmp *)(buf + hlen);

	*type = icp->icmp_type;
	*code = icp->icmp_code;

	/*
	 * traceroute interpretes only ICMP_TIMXCEED_INTRANS, ICMP_UNREACH and
	 * ICMP_ECHOREPLY, ignores others
	 */
	if ((*type == ICMP_TIMXCEED && *code == ICMP_TIMXCEED_INTRANS) ||
	    *type == ICMP_UNREACH || *type == ICMP_ECHOREPLY) {
		struct ip *hip;
		struct udphdr *up;
		struct icmp *hicmp;

		cc -= ICMP_MINLEN;
		hip = &icp->icmp_ip;
		hlen = hip->ip_hl << 2;
		cc -= hlen;
		if (useicmp) {
			if (*type == ICMP_ECHOREPLY &&
			    icp->icmp_id == htons(ident) &&
			    icp->icmp_seq == htons(seq))
				return (REPLY_GOT_TARGET);

			/* LINTED E_BAD_PTR_CAST_ALIGN */
			hicmp = (struct icmp *)((uchar_t *)hip + hlen);

			if (ICMP_MINLEN <= cc &&
			    hip->ip_p == IPPROTO_ICMP &&
			    hicmp->icmp_id == htons(ident) &&
			    hicmp->icmp_seq == htons(seq)) {
				return ((*type == ICMP_TIMXCEED) ?
				    REPLY_GOT_GATEWAY : REPLY_GOT_OTHER);
			}
		} else {
			/* LINTED E_BAD_PTR_CAST_ALIGN */
			up = (struct udphdr *)((uchar_t *)hip + hlen);
			/*
			 * at least 4 bytes of UDP header is required for this
			 * check
			 */
			if (4 <= cc &&
			    hip->ip_p == IPPROTO_UDP &&
			    up->uh_sport == htons(ident) &&
			    up->uh_dport == htons((port + seq) %
				(MAX_PORT + 1))) {
				if (*type == ICMP_UNREACH &&
				    *code == ICMP_UNREACH_PORT) {
					return (REPLY_GOT_TARGET);
				} else if (*type == ICMP_TIMXCEED) {
					return (REPLY_GOT_GATEWAY);
				} else {
					return (REPLY_GOT_OTHER);
				}
			}
		}
	}

	if (verbose) {
		int i, j;
		uchar_t *lp = (uchar_t *)ip;

		cc = save_cc;
		Printf("\n%d bytes from %s to ", cc,
		    inet_ntoa(from_in->sin_addr));
		Printf("%s: icmp type %d (%s) code %d\n",
		    inet_ntoa(ip->ip_dst), *type, pr_type(*type), *code);
		for (i = 0; i < cc; i += 4) {
			Printf("%2d: x", i);
			for (j = 0; ((j < 4) && ((i + j) < cc)); j++)
				Printf("%2.2x", *lp++);
			(void) putchar('\n');
		}
	}

	return (REPLY_SHORT_PKT);
}

/*
 * convert an ICMP "type" field to a printable string.
 */
static char *
pr_type(uchar_t type)
{
	static struct icmptype_table ttab[] = {
		{ICMP_ECHOREPLY,	"Echo Reply"},
		{1,			"ICMP 1"},
		{2,			"ICMP 2"},
		{ICMP_UNREACH,		"Dest Unreachable"},
		{ICMP_SOURCEQUENCH,	"Source Quench"},
		{ICMP_REDIRECT,		"Redirect"},
		{6,			"ICMP 6"},
		{7,			"ICMP 7"},
		{ICMP_ECHO,		"Echo"},
		{ICMP_ROUTERADVERT,	"Router Advertisement"},
		{ICMP_ROUTERSOLICIT,	"Router Solicitation"},
		{ICMP_TIMXCEED,		"Time Exceeded"},
		{ICMP_PARAMPROB,	"Param Problem"},
		{ICMP_TSTAMP,		"Timestamp"},
		{ICMP_TSTAMPREPLY,	"Timestamp Reply"},
		{ICMP_IREQ,		"Info Request"},
		{ICMP_IREQREPLY,	"Info Reply"},
		{ICMP_MASKREQ,		"Netmask Request"},
		{ICMP_MASKREPLY,	"Netmask Reply"}
	};
	int i = 0;

	for (i = 0; i < A_CNT(ttab); i++) {
		if (ttab[i].type == type)
			return (ttab[i].message);
	}

	return ("OUT-OF-RANGE");
}

/*
 * print the IPv4 src address of the reply packet
 */
void
print_addr(uchar_t *buf, int cc, struct sockaddr *from)
{
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	struct sockaddr_in *from_in = (struct sockaddr_in *)from;
	struct ip *ip;
	union any_in_addr ip_addr;

	ip_addr.addr = from_in->sin_addr;

	/* LINTED E_BAD_PTR_CAST_ALIGN */
	ip = (struct ip *)buf;

	if (nflag) {
		Printf(" %s", inet_ntoa(from_in->sin_addr));
	} else {
		Printf(" %s (%s)", inet_name(&ip_addr, AF_INET),
		    inet_ntoa(from_in->sin_addr));
	}

	if (verbose)
		Printf(" %d bytes to %s", cc, inet_ntoa(ip->ip_dst));
}

/*
 * ICMP messages which doesn't mean we got the target, or we got a gateway, are
 * processed here. It returns _B_TRUE if it's some sort of 'unreachable'.
 */
boolean_t
print_icmp_other(uchar_t type, uchar_t code)
{
	boolean_t unreach = _B_FALSE;

	/*
	 * this function only prints '!*' for ICMP unreachable messages,
	 * ignores others.
	 */
	if (type != ICMP_UNREACH) {
		return (_B_FALSE);
	}

	switch (code) {
	case ICMP_UNREACH_PORT:
		break;

	case ICMP_UNREACH_NET_UNKNOWN:
	case ICMP_UNREACH_NET:
		unreach = _B_TRUE;
		Printf(" !N");
		break;

	case ICMP_UNREACH_HOST_UNKNOWN:
	case ICMP_UNREACH_HOST:
		unreach = _B_TRUE;
		Printf(" !H");
		break;

	case ICMP_UNREACH_PROTOCOL:
		Printf(" !P");
		break;

	case ICMP_UNREACH_NEEDFRAG:
		unreach = _B_TRUE;
		Printf(" !F");
		break;

	case ICMP_UNREACH_SRCFAIL:
		unreach = _B_TRUE;
		Printf(" !S");
		break;

	case ICMP_UNREACH_FILTER_PROHIB:
	case ICMP_UNREACH_NET_PROHIB:
	case ICMP_UNREACH_HOST_PROHIB:
		unreach = _B_TRUE;
		Printf(" !X");
		break;

	case ICMP_UNREACH_TOSNET:
	case ICMP_UNREACH_TOSHOST:
		unreach = _B_TRUE;
		Printf(" !T");
		break;

	case ICMP_UNREACH_ISOLATED:
	case ICMP_UNREACH_HOST_PRECEDENCE:
	case ICMP_UNREACH_PRECEDENCE_CUTOFF:
		unreach = _B_TRUE;
		Printf(" !U");
		break;

	default:
		unreach = _B_TRUE;
		Printf(" !<%d>", code);
		break;
	}

	return (unreach);
}
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


/*
 * Copyright (c) 1988, 1989, 1991, 1994, 1995, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * @(#)$Header: traceroute.c,v 1.49 97/06/13 02:30:23 leres Exp $ (LBL)
 */

#include <sys/socket.h>

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include <libintl.h>
#include <errno.h>
#include <netdb.h>

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>

#include <arpa/inet.h>

#include <libinetutil.h>
#include "traceroute.h"

int check_reply6(struct msghdr *, int, int, uchar_t *, uchar_t *);
void *find_ancillary_data(struct msghdr *, int, int);
extern char *inet_name(union any_in_addr *, int);
static int IPv6_hdrlen(ip6_t *, int, uint8_t *);
static char *pr_type6(uchar_t);
void print_addr6(uchar_t *, int, struct sockaddr *);
boolean_t print_icmp_other6(uchar_t, uchar_t);
void send_probe6(int, struct msghdr *, struct ip *, int, int,
    struct timeval *, int);
void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int, uint_t);
struct ip *set_buffers6(int);
static boolean_t update_hoplimit_ancillary_data(struct msghdr *, int);

/*
 * prepares the buffer to be sent as an IP datagram
 */
struct ip *
set_buffers6(int plen)
{
	struct ip *outip;
	uchar_t *outp;
	struct udphdr *outudp;
	struct icmp *outicmp;
	int optlen = 0;

	outip = (struct ip *)malloc((size_t)plen);
	if (outip == NULL) {
		Fprintf(stderr, "%s: malloc: %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if (gw_count > 0) {
		/* ip6_rthdr0 structure includes one gateway address */
		optlen = sizeof (struct ip6_rthdr0) +
		    gw_count * sizeof (struct in6_addr);
	}

	(void) memset((char *)outip, 0, (size_t)plen);
	outp = (uchar_t *)(outip + 1);

	if (useicmp) {
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		outicmp = (struct icmp *)outp;
		outicmp->icmp_type = ICMP6_ECHO_REQUEST;
		outicmp->icmp_id = htons(ident);
	} else {
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		outudp = (struct udphdr *)outp;
		/*
		 * "source port" is set at bind() call, so we don't do it
		 * again
		 */
		outudp->uh_ulen = htons((ushort_t)(plen -
		    (sizeof (struct ip6_hdr) + optlen)));
	}

	return (outip);
}

/*
 * Initialize the msghdr for specifying hoplimit, outgoing interface and routing
 * header for the probe packets.
 */
void
set_ancillary_data(struct msghdr *msgp, int hoplimit,
    union any_in_addr *gwIPlist, int gw_cnt, uint_t if_index)
{
	size_t hoplimit_space;
	size_t rthdr_space;
	size_t pktinfo_space;
	size_t bufspace;
	struct cmsghdr *cmsgp;
	uchar_t *cmsg_datap;
	int i;

	msgp->msg_control = NULL;
	msgp->msg_controllen = 0;

	/*
	 * Need to figure out size of buffer needed for ancillary data
	 * containing routing header and packet info options.
	 *
	 * Portable heuristic to compute upper bound on space needed for
	 * N ancillary data options. It assumes up to _MAX_ALIGNMENT padding
	 * after both header and data as the worst possible upper bound on space
	 * consumed by padding.
	 * It also adds one extra "sizeof (struct cmsghdr)" for the last option.
	 * This is needed because we would like to use CMSG_NXTHDR() while
	 * composing the buffer. The CMSG_NXTHDR() macro is designed better for
	 * parsing than composing the buffer. It requires the pointer it returns
	 * to leave space in buffer for addressing a cmsghdr and we want to make
	 * sure it works for us while we skip beyond the last ancillary data
	 * option.
	 *
	 * bufspace[i]  = sizeof(struct cmsghdr) + <pad after header> +
	 *		<option[i] content length> + <pad after data>;
	 *
	 * total_bufspace = bufspace[0] + bufspace[1] + ...
	 *		    ... + bufspace[N-1] + sizeof (struct cmsghdr);
	 */

	rthdr_space = 0;
	pktinfo_space = 0;
	/* We'll always set the hoplimit of the outgoing packets */
	hoplimit_space = sizeof (int);
	bufspace = sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
	    hoplimit_space + _MAX_ALIGNMENT;

	if (gw_cnt > 0) {
		rthdr_space = inet6_rth_space(IPV6_RTHDR_TYPE_0, gw_cnt);
		bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
		    rthdr_space + _MAX_ALIGNMENT;
	}

	if (if_index != 0) {
		pktinfo_space = sizeof (struct in6_pktinfo);
		bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
		    pktinfo_space + _MAX_ALIGNMENT;
	}

	/*
	 * We need to temporarily set the msgp->msg_controllen to bufspace
	 * (we will later trim it to actual length used). This is needed because
	 * CMSG_NXTHDR() uses it to check we have not exceeded the bounds.
	 */
	bufspace += sizeof (struct cmsghdr);
	msgp->msg_controllen = bufspace;

	msgp->msg_control = (struct cmsghdr *)malloc(bufspace);
	if (msgp->msg_control == NULL) {
		Fprintf(stderr, "%s: malloc %s\n", prog, strerror(errno));
		exit(EXIT_FAILURE);
	}
	cmsgp = CMSG_FIRSTHDR(msgp);

	/*
	 * Fill ancillary data. First hoplimit, then rthdr and pktinfo if
	 * needed.
	 */

	/* set hoplimit ancillary data */
	cmsgp->cmsg_level = IPPROTO_IPV6;
	cmsgp->cmsg_type = IPV6_HOPLIMIT;
	cmsg_datap = CMSG_DATA(cmsgp);
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	*(int *)cmsg_datap = hoplimit;
	cmsgp->cmsg_len = cmsg_datap + hoplimit_space - (uchar_t *)cmsgp;
	cmsgp = CMSG_NXTHDR(msgp, cmsgp);

	/* set rthdr ancillary data if needed */
	if (gw_cnt > 0) {
		struct ip6_rthdr0 *rthdr0p;

		cmsgp->cmsg_level = IPPROTO_IPV6;
		cmsgp->cmsg_type = IPV6_RTHDR;
		cmsg_datap = CMSG_DATA(cmsgp);

		/*
		 * Initialize rthdr structure
		 */
		/* LINTED E_BAD_PTR_CAST_ALIGN */
		rthdr0p = (struct ip6_rthdr0 *)cmsg_datap;
		if (inet6_rth_init(rthdr0p, rthdr_space,
		    IPV6_RTHDR_TYPE_0, gw_cnt) == NULL) {
			Fprintf(stderr, "%s: inet6_rth_init failed\n",
			    prog);
			exit(EXIT_FAILURE);
		}

		/*
		 * Stuff in gateway addresses
		 */
		for (i = 0; i < gw_cnt; i++) {
			if (inet6_rth_add(rthdr0p,
			    &gwIPlist[i].addr6) == -1) {
				Fprintf(stderr,
				    "%s: inet6_rth_add\n", prog);
				exit(EXIT_FAILURE);
			}
		}

		cmsgp->cmsg_len = cmsg_datap + rthdr_space - (uchar_t *)cmsgp;
		cmsgp = CMSG_NXTHDR(msgp, cmsgp);
	}

	/* set pktinfo ancillary data if needed */
	if (if_index != 0) {
		struct in6_pktinfo *pktinfop;

		cmsgp->cmsg_level = IPPROTO_IPV6;
		cmsgp->cmsg_type = IPV6_PKTINFO;
		cmsg_datap = CMSG_DATA(cmsgp);

		/* LINTED E_BAD_PTR_CAST_ALIGN */
		pktinfop = (struct in6_pktinfo *)cmsg_datap;
		/*
		 * We don't know if pktinfop->ipi6_addr is aligned properly,
		 * therefore let's use bcopy, instead of assignment.
		 */
		(void) bcopy(&in6addr_any, &pktinfop->ipi6_addr,
		sizeof (struct in6_addr));

		/*
		 *  We can assume pktinfop->ipi6_ifindex is 32 bit aligned.
		 */
		pktinfop->ipi6_ifindex = if_index;
		cmsgp->cmsg_len = cmsg_datap + pktinfo_space - (uchar_t *)cmsgp;
		cmsgp = CMSG_NXTHDR(msgp, cmsgp);
	}

	msgp->msg_controllen = (char *)cmsgp - (char *)msgp->msg_control;
}

/*
 * Parses the given msg->msg_control to find the IPV6_HOPLIMIT ancillary data
 * and update the hoplimit.
 * Returns _B_FALSE if it can't find IPV6_HOPLIMIT ancillary data, _B_TRUE
 * otherwise.
 */
static boolean_t
update_hoplimit_ancillary_data(struct msghdr *msg, int hoplimit)
{
	struct cmsghdr *cmsg;
	int *intp;

	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
		if (cmsg->cmsg_level == IPPROTO_IPV6 &&
		    cmsg->cmsg_type == IPV6_HOPLIMIT) {
			/* LINTED E_BAD_PTR_CAST_ALIGN */
			intp = (int *)(CMSG_DATA(cmsg));
			*intp = hoplimit;
			return (_B_TRUE);
		}
	}

	return (_B_FALSE);
}

/*
 * send a probe packet to the destination
 */
void
send_probe6(int sndsock, struct msghdr *msg6, struct ip *outip, int seq,
    int ttl, struct timeval *tp, int packlen)
{
	uchar_t *outp;
	struct icmp *outicmp;
	struct outdata *outdata;
	struct iovec iov;
	int cc;
	int optlen = 0;
	int send_size;
	struct sockaddr_in6 *to6;

	if (gw_count > 0) {
		/* ip6_rthdr0 structure includes one gateway address */
		optlen = sizeof (struct ip6_rthdr0) +
		    gw_count * sizeof (struct in6_addr);
	}

	send_size = packlen - sizeof (struct ip6_hdr) - optlen;

	/* if using UDP, further discount UDP header size */
	if (!useicmp)
		send_size -= sizeof (struct udphdr);

	/* initialize buffer pointers */
	outp = (uchar_t *)(outip + 1);
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	outicmp = (struct icmp *)outp;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	outdata = (struct outdata *)(outp + ICMP6_MINLEN);

	if (!update_hoplimit_ancillary_data(msg6, ttl)) {
		Fprintf(stderr,
		    "%s: can't find IPV6_HOPLIMIT ancillary data\n", prog);
		exit(EXIT_FAILURE);
	}

	/* Payload */
	outdata->seq = seq;
	outdata->ttl = ttl;
	outdata->tv = *tp;

	if (useicmp) {
		outicmp->icmp_seq = htons(seq);
	} else {
		to6 = (struct sockaddr_in6 *)msg6->msg_name;
		to6->sin6_port =  htons((port + seq) % (MAX_PORT + 1));
	}

	iov.iov_base = outp;
	iov.iov_len = send_size;

	msg6->msg_iov = &iov;
	msg6->msg_iovlen = 1;

	cc = sendmsg(sndsock, msg6, 0);

	if (cc < 0 || cc != send_size)  {
		if (cc < 0) {
			Fprintf(stderr, "%s: sendmsg: %s\n", prog,
			    strerror(errno));
		}
		Printf("%s: wrote %s %d chars, ret=%d\n",
		    prog, hostname, send_size, cc);
		(void) fflush(stdout);
	}
}

/*
 * Return a pointer to the ancillary data for the given cmsg_level and
 * cmsg_type.
 * If not found return NULL.
 */
void *
find_ancillary_data(struct msghdr *msg, int cmsg_level, int cmsg_type)
{
	struct cmsghdr *cmsg;

	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
		if (cmsg->cmsg_level == cmsg_level &&
		    cmsg->cmsg_type == cmsg_type) {
			return (CMSG_DATA(cmsg));
		}
	}
	return (NULL);
}

/*
 * Check out the reply packet to see if it's what we were expecting.
 * Returns REPLY_GOT_TARGET if the reply comes from the target
 *         REPLY_GOT_GATEWAY if an intermediate gateway sends TIME_EXCEEDED
 *         REPLY_GOT_OTHER for other kinds of unreachables indicating none of
 *	   the above two cases
 *
 * It also sets the icmp type and icmp code values
 */
int
check_reply6(struct msghdr *msg, int cc, int seq, uchar_t *type, uchar_t *code)
{
	uchar_t *buf = msg->msg_iov->iov_base;
	struct sockaddr_in6 *from_in6 = (struct sockaddr_in6 *)msg->msg_name;
	icmp6_t *icp6;
	ulong_t ip6hdr_len;
	uint8_t last_hdr;
	int save_cc = cc;
	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */

	/* Ignore packets > 64k or control buffers that don't fit */
	if (msg->msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
		if (verbose) {
			Printf("Truncated message: msg_flags 0x%x from %s\n",
			    msg->msg_flags,
			    inet_ntop(AF_INET6,
			    (void *)&(from_in6->sin6_addr),
			    temp_buf, sizeof (temp_buf)));
		}
		return (REPLY_SHORT_PKT);
	}
	if (cc < ICMP6_MINLEN) {
		if (verbose) {
			Printf("packet too short (%d bytes) from %s\n",
			    cc,
			    inet_ntop(AF_INET6,
			    (void *)&(from_in6->sin6_addr),
			    temp_buf, sizeof (temp_buf)));
		}
		return (REPLY_SHORT_PKT);
	}
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	icp6 = (icmp6_t *)buf;
	*type = icp6->icmp6_type;
	*code = icp6->icmp6_code;

	/*
	 * traceroute interprets only ICMP6_TIME_EXCEED_TRANSIT,
	 * ICMP6_DST_UNREACH, ICMP6_ECHO_REPLY, ICMP6_PACKET_TOO_BIG and
	 * ICMP6_PARAMPROB_NEXTHEADER, ignores others
	 */
	if ((*type == ICMP6_TIME_EXCEEDED &&
	    *code == ICMP6_TIME_EXCEED_TRANSIT) ||
	    *type == ICMP6_DST_UNREACH || *type == ICMP6_ECHO_REPLY ||
	    *type == ICMP6_PACKET_TOO_BIG ||
	    (*type == ICMP6_PARAM_PROB &&
	    *code == ICMP6_PARAMPROB_NEXTHEADER)) {
		ip6_t *hip6;
		struct udphdr *up;
		icmp6_t *hicmp6;

		cc -= ICMP6_MINLEN;
		hip6 = (ip6_t *)&(icp6->icmp6_data32[1]);
		last_hdr = hip6->ip6_nxt;
		ip6hdr_len = IPv6_hdrlen(hip6, cc, &last_hdr);

		cc -= ip6hdr_len;
		if (useicmp) {
			if (*type == ICMP6_ECHO_REPLY &&
			    icp6->icmp6_id == htons(ident) &&
			    icp6->icmp6_seq == htons(seq)) {
				return (REPLY_GOT_TARGET);
			}

			/* LINTED E_BAD_PTR_CAST_ALIGN */
			hicmp6 = (icmp6_t *)((uchar_t *)hip6 + ip6hdr_len);

			if (ICMP6_MINLEN <= cc &&
			    last_hdr == IPPROTO_ICMPV6 &&
			    hicmp6->icmp6_id == htons(ident) &&
			    hicmp6->icmp6_seq == htons(seq)) {
				if (*type == ICMP6_TIME_EXCEEDED) {
					return (REPLY_GOT_GATEWAY);
				} else {
					return (REPLY_GOT_OTHER);
				}
			}
		} else {
			/* LINTED E_BAD_PTR_CAST_ALIGN */
			up = (struct udphdr *)((uchar_t *)hip6 + ip6hdr_len);
			/*
			 * at least 4 bytes of UDP header is required for this
			 * check
			 */
			if (4 <= cc &&
			    last_hdr == IPPROTO_UDP &&
			    up->uh_sport == htons(ident) &&
			    up->uh_dport == htons((port + seq) %
			    (MAX_PORT + 1))) {
				if (*type == ICMP6_DST_UNREACH &&
				    *code == ICMP6_DST_UNREACH_NOPORT) {
					return (REPLY_GOT_TARGET);
				} else if (*type == ICMP6_TIME_EXCEEDED) {
					return (REPLY_GOT_GATEWAY);
				} else {
					return (REPLY_GOT_OTHER);
				}
			}
		}
	}

	if (verbose) {
		int i, j;
		uchar_t *lp = (uchar_t *)icp6;
		struct in6_addr *dst;
		struct in6_pktinfo *pkti;

		pkti = (struct in6_pktinfo *)find_ancillary_data(msg,
		    IPPROTO_IPV6, IPV6_PKTINFO);
		if (pkti == NULL) {
			Fprintf(stderr,
			    "%s: can't find IPV6_PKTINFO ancillary data\n",
			    prog);
			exit(EXIT_FAILURE);
		}
		dst = &pkti->ipi6_addr;
		cc = save_cc;
		Printf("\n%d bytes from %s to ", cc,
		    inet_ntop(AF_INET6, (const void *)&(from_in6->sin6_addr),
			temp_buf, sizeof (temp_buf)));
		Printf("%s: icmp type %d (%s) code %d\n",
		    inet_ntop(AF_INET6, (const void *)dst,
			temp_buf, sizeof (temp_buf)),
		    *type, pr_type6(*type), *code);
		for (i = 0; i < cc; i += 4) {
			Printf("%2d: x", i);
			for (j = 0; ((j < 4) && ((i + j) < cc)); j++)
				Printf("%2.2x", *lp++);
			(void) putchar('\n');
		}
	}

	return (REPLY_SHORT_PKT);
}

/*
 * Return the length of the IPv6 related headers (including extension headers)
 */
static int
IPv6_hdrlen(ip6_t *ip6h, int pkt_len, uint8_t *last_hdr_rtrn)
{
	int length;
	int exthdrlength;
	uint8_t nexthdr;
	uint8_t *whereptr;
	ip6_hbh_t *hbhhdr;
	ip6_dest_t *desthdr;
	ip6_rthdr_t *rthdr;
	ip6_frag_t *fraghdr;
	uint8_t	*endptr;

	length = sizeof (ip6_t);

	whereptr = ((uint8_t *)&ip6h[1]); 	/* point to next hdr */
	endptr = ((uint8_t *)ip6h) + pkt_len;

	nexthdr = ip6h->ip6_nxt;
	*last_hdr_rtrn = IPPROTO_NONE;

	if (whereptr >= endptr)
		return (length);

	while (whereptr < endptr) {
		*last_hdr_rtrn = nexthdr;
		switch (nexthdr) {
		case IPPROTO_HOPOPTS:
			hbhhdr = (ip6_hbh_t *)whereptr;
			exthdrlength = 8 * (hbhhdr->ip6h_len + 1);
			if ((uchar_t *)hbhhdr + exthdrlength > endptr)
				return (length);
			nexthdr = hbhhdr->ip6h_nxt;
			length += exthdrlength;
			break;

		case IPPROTO_DSTOPTS:
			desthdr = (ip6_dest_t *)whereptr;
			exthdrlength = 8 * (desthdr->ip6d_len + 1);
			if ((uchar_t *)desthdr + exthdrlength > endptr)
				return (length);
			nexthdr = desthdr->ip6d_nxt;
			length += exthdrlength;
			break;

		case IPPROTO_ROUTING:
			rthdr = (ip6_rthdr_t *)whereptr;
			exthdrlength = 8 * (rthdr->ip6r_len + 1);
			if ((uchar_t *)rthdr + exthdrlength > endptr)
				return (length);
			nexthdr = rthdr->ip6r_nxt;
			length += exthdrlength;
			break;

		case IPPROTO_FRAGMENT:
			/* LINTED E_BAD_PTR_CAST_ALIGN */
			fraghdr = (ip6_frag_t *)whereptr;
			if ((uchar_t *)&fraghdr[1] > endptr)
				return (length);
			nexthdr = fraghdr->ip6f_nxt;
			length += sizeof (struct ip6_frag);
			break;

		case IPPROTO_NONE:
		default:
			return (length);
		}
		whereptr = (uint8_t *)ip6h + length;
	}
	*last_hdr_rtrn = nexthdr;

	return (length);
}

/*
 * convert an ICMP6 "type" field to a printable string.
 */
static char *
pr_type6(uchar_t type)
{
	static struct icmptype_table ttab6[] = {
		{ICMP6_DST_UNREACH,		"Dest Unreachable"},
		{ICMP6_PACKET_TOO_BIG,		"Packet Too Big"},
		{ICMP6_TIME_EXCEEDED,		"Time Exceeded"},
		{ICMP6_PARAM_PROB,		"Param Problem"},
		{ICMP6_ECHO_REQUEST,		"Echo Request"},
		{ICMP6_ECHO_REPLY,		"Echo Reply"},
		{MLD_LISTENER_QUERY,		"Multicast Listener Query"},
		{MLD_LISTENER_REPORT,		"Multicast Listener Report"},
		{MLD_LISTENER_REDUCTION,	"Multicast Listener Done"},
		{ND_ROUTER_SOLICIT,		"Router Solicitation"},
		{ND_ROUTER_ADVERT,		"Router Advertisement"},
		{ND_NEIGHBOR_SOLICIT,		"Neighbor Solicitation"},
		{ND_NEIGHBOR_ADVERT,		"Neighbor Advertisement"},
		{ND_REDIRECT,			"Redirect Message"}
	};
	int i = 0;

	for (i = 0; i < A_CNT(ttab6); i++) {
		if (ttab6[i].type == type)
			return (ttab6[i].message);
	}

	return ("OUT-OF-RANGE");
}


/*
 * print the IPv6 src address of the reply packet
 */
void
print_addr6(uchar_t *buf, int cc, struct sockaddr *from)
{
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	struct sockaddr_in6 *from_in6 = (struct sockaddr_in6 *)from;
	ip6_t *ip;
	union any_in_addr ip_addr;
	char *resolved_name;
	char temp_buf[INET6_ADDRSTRLEN];	/* use for inet_ntop() */

	ip_addr.addr6 = from_in6->sin6_addr;

	/* LINTED E_BAD_PTR_CAST_ALIGN */
	ip = (ip6_t *)buf;

	(void) inet_ntop(AF_INET6, &(from_in6->sin6_addr), temp_buf,
	    sizeof (temp_buf));
	if (!nflag)
		resolved_name = inet_name(&ip_addr, AF_INET6);
	/*
	 * If the IPv6 address cannot be resolved to hostname, inet_name()
	 * returns the IPv6 address as a string. In that case, we choose not
	 * to print it twice. This saves us space on display.
	 */
	if (nflag || (strcmp(temp_buf, resolved_name) == 0))
		Printf(" %s", temp_buf);
	else
		Printf(" %s (%s)", resolved_name, temp_buf);

	if (verbose) {
		Printf(" %d bytes to %s", cc, inet_ntop(AF_INET6,
		    (const void *) &(ip->ip6_dst), temp_buf,
		    sizeof (temp_buf)));
	}
}

/*
 * ICMP6 messages which doesn't mean we got the target, or we got a gateway, are
 * processed here. It returns _B_TRUE if it's some sort of 'unreachable'.
 */
boolean_t
print_icmp_other6(uchar_t type, uchar_t code)
{
	boolean_t unreach = _B_FALSE;

	switch (type) {

	/* this corresponds to "ICMP_UNREACH_NEEDFRAG" in ICMP */
	case ICMP6_PACKET_TOO_BIG:
		unreach = _B_TRUE;
		Printf(" !B");
		break;

	case ICMP6_PARAM_PROB:
		/* this corresponds to "ICMP_UNREACH_PROTOCOL" in ICMP */
		if (code == ICMP6_PARAMPROB_NEXTHEADER) {
			unreach = _B_TRUE;
			Printf(" !R");
		}
		break;

	case ICMP6_DST_UNREACH:
		switch (code) {
		case ICMP6_DST_UNREACH_NOPORT:
			break;

		case ICMP6_DST_UNREACH_NOROUTE:
			unreach = _B_TRUE;
			Printf(" !H");
			break;

		case ICMP6_DST_UNREACH_ADMIN:
			unreach = _B_TRUE;
			Printf(" !X");
			break;

		case ICMP6_DST_UNREACH_ADDR:
			unreach = _B_TRUE;
			Printf(" !A");
			break;

		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
			unreach = _B_TRUE;
			Printf(" !E");
			break;

		default:
			unreach = _B_TRUE;
			Printf(" !<%d>", code);
			break;
		}
		break;
	default:
		break;
	}

	return (unreach);
}