sys/netinet/udp_usrreq.c
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 | /* * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved. * Copyright (c) 2004 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Jeffrey M. Hsu. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 * 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 the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $ */ #include "opt_inet6.h" #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> #include <sys/malloc.h> #include <sys/mbuf.h> #include <sys/domain.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/protosw.h> #include <sys/socket.h> #include <sys/socketvar.h> #include <sys/sysctl.h> #include <sys/syslog.h> #include <sys/in_cksum.h> #include <sys/ktr.h> #include <sys/jail.h> #include <sys/socketvar2.h> #include <sys/serialize.h> #include <machine/stdarg.h> #include <net/if.h> #include <net/route.h> #include <net/netmsg2.h> #include <net/netisr2.h> #include <netinet/in.h> #include <netinet/in_systm.h> #include <netinet/ip.h> #ifdef INET6 #include <netinet/ip6.h> #endif #include <netinet/in_pcb.h> #include <netinet/in_var.h> #include <netinet/ip_var.h> #ifdef INET6 #include <netinet6/ip6_var.h> #endif #include <netinet/ip_icmp.h> #include <netinet/icmp_var.h> #include <netinet/udp.h> #include <netinet/udp_var.h> #define MSGF_UDP_SEND MSGF_PROTO1 #define INP_DIRECT_DETACH INP_FLAG_PROTO2 #define UDP_KTR_STRING "inp=%p" #define UDP_KTR_ARGS struct inpcb *inp #ifndef KTR_UDP #define KTR_UDP KTR_ALL #endif KTR_INFO_MASTER(udp); KTR_INFO(KTR_UDP, udp, send_beg, 0, UDP_KTR_STRING, UDP_KTR_ARGS); KTR_INFO(KTR_UDP, udp, send_end, 1, UDP_KTR_STRING, UDP_KTR_ARGS); KTR_INFO(KTR_UDP, udp, send_ipout, 2, UDP_KTR_STRING, UDP_KTR_ARGS); KTR_INFO(KTR_UDP, udp, redisp_ipout_beg, 3, UDP_KTR_STRING, UDP_KTR_ARGS); KTR_INFO(KTR_UDP, udp, redisp_ipout_end, 4, UDP_KTR_STRING, UDP_KTR_ARGS); KTR_INFO(KTR_UDP, udp, send_redisp, 5, UDP_KTR_STRING, UDP_KTR_ARGS); KTR_INFO(KTR_UDP, udp, send_inswildcard, 6, UDP_KTR_STRING, UDP_KTR_ARGS); #define logudp(name, inp) KTR_LOG(udp_##name, inp) /* * UDP protocol implementation. * Per RFC 768, August, 1980. */ #ifndef COMPAT_42 static int udpcksum = 1; #else static int udpcksum = 0; /* XXX */ #endif SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udpcksum, 0, "Enable checksumming of UDP packets"); int log_in_vain = 0; SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, &log_in_vain, 0, "Log all incoming UDP packets"); static int blackhole = 0; SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &blackhole, 0, "Do not send port unreachables for refused connects"); static int strict_mcast_mship = 1; SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW, &strict_mcast_mship, 0, "Only send multicast to member sockets"); int udp_sosend_async = 1; SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW, &udp_sosend_async, 0, "UDP asynchronized pru_send"); int udp_sosend_prepend = 1; SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW, &udp_sosend_prepend, 0, "Prepend enough space for proto and link header in pru_send"); static int udp_reuseport_ext = 1; SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW, &udp_reuseport_ext, 0, "SO_REUSEPORT extension"); struct inpcbinfo udbinfo[MAXCPU]; #ifndef UDBHASHSIZE #define UDBHASHSIZE 16 #endif CTASSERT(powerof2(UDBHASHSIZE)); struct udpstat udpstat_percpu[MAXCPU] __cachealign; static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, struct sockaddr_in *udp_in); static int udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin, struct sockaddr_in *if_sin, uint16_t hash); static boolean_t udp_inswildcardhash(struct inpcb *inp, struct netmsg_base *msg, int error); static void udp_remwildcardhash(struct inpcb *inp); static __inline int udp_lportcpu(short lport) { return (ntohs(lport) % netisr_ncpus); } void udp_init(void) { struct inpcbportinfo *portinfo; int cpu; portinfo = kmalloc(sizeof(*portinfo) * netisr_ncpus, M_PCB, M_WAITOK | M_CACHEALIGN); for (cpu = 0; cpu < netisr_ncpus; cpu++) { struct inpcbinfo *uicb = &udbinfo[cpu]; /* * NOTE: * UDP pcb list, wildcard hash table and localgroup hash * table are shared. */ in_pcbinfo_init(uicb, cpu, TRUE); uicb->hashbase = hashinit(UDBHASHSIZE, M_PCB, &uicb->hashmask); in_pcbportinfo_init(&portinfo[cpu], UDBHASHSIZE, cpu); in_pcbportinfo_set(uicb, portinfo, netisr_ncpus); uicb->wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB, &uicb->wildcardhashmask); uicb->localgrphashbase = hashinit(UDBHASHSIZE, M_PCB, &uicb->localgrphashmask); uicb->ipi_size = sizeof(struct inpcb); } /* * Initialize UDP statistics counters for each CPU. */ for (cpu = 0; cpu < netisr_ncpus; ++cpu) bzero(&udpstat_percpu[cpu], sizeof(struct udpstat)); } static int sysctl_udpstat(SYSCTL_HANDLER_ARGS) { int cpu, error = 0; for (cpu = 0; cpu < netisr_ncpus; ++cpu) { if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu], sizeof(struct udpstat)))) break; if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu], sizeof(struct udpstat)))) break; } return (error); } SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW), 0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics"); void udp_ctloutput(netmsg_t msg) { struct socket *so = msg->base.nm_so; struct sockopt *sopt = msg->ctloutput.nm_sopt; struct inpcb *inp = so->so_pcb; if (inp == NULL) { lwkt_replymsg(&msg->lmsg, EINVAL); return; } if (sopt->sopt_level == IPPROTO_IP && sopt->sopt_dir == SOPT_SET) { switch (sopt->sopt_name) { case IP_MULTICAST_IF: case IP_MULTICAST_VIF: case IP_MULTICAST_TTL: case IP_MULTICAST_LOOP: case IP_ADD_MEMBERSHIP: case IP_DROP_MEMBERSHIP: /* * This pr_ctloutput msg will be forwarded * to netisr0 to run; we can't do direct * detaching anymore. * * NOTE: * Don't optimize for the sockets whose * current so_port is netisr0's msgport. * These sockets could be connect(2)'ed * later and the so_port will be changed. */ inp->inp_flags &= ~INP_DIRECT_DETACH; break; } } return ip_ctloutput(msg); } /* * Check multicast packets to make sure they are only sent to sockets with * multicast memberships for the packet's destination address and arrival * interface. Multicast packets to multicast-unaware sockets are also * disallowed. * * Returns 0 if the packet is acceptable, -1 if it is not. */ static __inline int check_multicast_membership(const struct ip *ip, const struct inpcb *inp, const struct mbuf *m) { const struct ip_moptions *mopt; int mshipno; if (strict_mcast_mship == 0 || !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { return (0); } ASSERT_NETISR0; mopt = inp->inp_moptions; if (mopt == NULL) return (-1); for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) { const struct in_multi *maddr = mopt->imo_membership[mshipno]; if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr && m->m_pkthdr.rcvif == maddr->inm_ifp) { return (0); } } return (-1); } struct udp_mcast_arg { struct inpcb *inp; struct inpcb *last; struct ip *ip; struct mbuf *m; int iphlen; struct sockaddr_in *udp_in; }; static int udp_mcast_input(struct udp_mcast_arg *arg) { struct inpcb *inp = arg->inp; struct inpcb *last = arg->last; struct ip *ip = arg->ip; struct mbuf *m = arg->m; if (check_multicast_membership(ip, inp, m) < 0) return ERESTART; /* caller continue */ if (last != NULL) { struct mbuf *n; if ((n = m_copypacket(m, M_NOWAIT)) != NULL) udp_append(last, ip, n, arg->iphlen + sizeof(struct udphdr), arg->udp_in); } arg->last = last = inp; /* * Don't look for additional matches if this one does * not have either the SO_REUSEPORT or SO_REUSEADDR * socket options set. This heuristic avoids searching * through all pcbs in the common case of a non-shared * port. It * assumes that an application will never * clear these options after setting them. */ if (!(last->inp_socket->so_options & (SO_REUSEPORT | SO_REUSEADDR))) return EJUSTRETURN; /* caller stop */ return 0; } int udp_input(struct mbuf **mp, int *offp, int proto) { struct sockaddr_in udp_in = { sizeof udp_in, AF_INET }; int iphlen; struct ip *ip; struct udphdr *uh; struct inpcb *inp; struct mbuf *m; struct mbuf *opts = NULL; int ip_len, len, off; struct ip save_ip; struct inpcbinfo *pcbinfo = &udbinfo[mycpuid]; off = *offp; m = *mp; *mp = NULL; iphlen = off; udp_stat.udps_ipackets++; /* * Strip IP options, if any; should skip this, * make available to user, and use on returned packets, * but we don't yet have a way to check the checksum * with options still present. */ if (iphlen > sizeof(struct ip)) { ip_stripoptions(m); iphlen = sizeof(struct ip); } /* * IP and UDP headers are together in first mbuf. * Already checked and pulled up in ip_demux(). */ KASSERT(m->m_len >= iphlen + sizeof(struct udphdr), ("UDP header not in one mbuf")); ip = mtod(m, struct ip *); uh = (struct udphdr *)((caddr_t)ip + iphlen); /* destination port of 0 is illegal, based on RFC768. */ if (uh->uh_dport == 0) goto bad; /* * Make mbuf data length reflect UDP length. * If not enough data to reflect UDP length, drop. */ len = ntohs((u_short)uh->uh_ulen); ip_len = ntohs(ip->ip_len) - iphlen; if (ip_len != len) { if (len > ip_len || len < sizeof(struct udphdr)) { udp_stat.udps_badlen++; goto bad; } m_adj(m, len - ip_len); /* ip->ip_len = htons(len); */ } /* * Save a copy of the IP header in case we want restore it * for sending an ICMP error message in response. */ save_ip = *ip; /* * Checksum extended UDP header and data. */ if (uh->uh_sum) { if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) uh->uh_sum = m->m_pkthdr.csum_data; else uh->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, htonl((u_short)len + m->m_pkthdr.csum_data + IPPROTO_UDP)); uh->uh_sum ^= 0xffff; } else { char b[9]; bcopy(((struct ipovly *)ip)->ih_x1, b, 9); bzero(((struct ipovly *)ip)->ih_x1, 9); ((struct ipovly *)ip)->ih_len = uh->uh_ulen; uh->uh_sum = in_cksum(m, len + sizeof(struct ip)); bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); } if (uh->uh_sum) { udp_stat.udps_badsum++; m_freem(m); return(IPPROTO_DONE); } } else { udp_stat.udps_nosum++; } if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { struct inpcbhead *connhead; struct inpcontainer *ic, *ic_marker; struct inpcontainerhead *ichead; struct udp_mcast_arg arg; struct inpcb *last; int error; /* * Deliver a multicast or broadcast datagram to *all* sockets * for which the local and remote addresses and ports match * those of the incoming datagram. This allows more than * one process to receive multi/broadcasts on the same port. * (This really ought to be done for unicast datagrams as * well, but that would cause problems with existing * applications that open both address-specific sockets and * a wildcard socket listening to the same port -- they would * end up receiving duplicates of every unicast datagram. * Those applications open the multiple sockets to overcome an * inadequacy of the UDP socket interface, but for backwards * compatibility we avoid the problem here rather than * fixing the interface. Maybe 4.5BSD will remedy this?) */ /* * Construct sockaddr format source address. */ udp_in.sin_port = uh->uh_sport; udp_in.sin_addr = ip->ip_src; arg.udp_in = &udp_in; /* * Locate pcb(s) for datagram. * (Algorithm copied from raw_intr().) */ last = NULL; arg.iphlen = iphlen; connhead = &pcbinfo->hashbase[ INP_PCBCONNHASH(ip->ip_src.s_addr, uh->uh_sport, ip->ip_dst.s_addr, uh->uh_dport, pcbinfo->hashmask)]; LIST_FOREACH(inp, connhead, inp_hash) { #ifdef INET6 if (!INP_ISIPV4(inp)) continue; #endif if (!in_hosteq(inp->inp_faddr, ip->ip_src) || !in_hosteq(inp->inp_laddr, ip->ip_dst) || inp->inp_fport != uh->uh_sport || inp->inp_lport != uh->uh_dport) continue; arg.inp = inp; arg.last = last; arg.ip = ip; arg.m = m; error = udp_mcast_input(&arg); if (error == ERESTART) continue; last = arg.last; if (error == EJUSTRETURN) goto done; } ichead = &pcbinfo->wildcardhashbase[ INP_PCBWILDCARDHASH(uh->uh_dport, pcbinfo->wildcardhashmask)]; ic_marker = in_pcbcontainer_marker(); GET_PCBINFO_TOKEN(pcbinfo); LIST_INSERT_HEAD(ichead, ic_marker, ic_list); while ((ic = LIST_NEXT(ic_marker, ic_list)) != NULL) { LIST_REMOVE(ic_marker, ic_list); LIST_INSERT_AFTER(ic, ic_marker, ic_list); inp = ic->ic_inp; if (inp->inp_flags & INP_PLACEMARKER) continue; #ifdef INET6 if (!INP_ISIPV4(inp)) continue; #endif if (inp->inp_lport != uh->uh_dport) continue; if (inp->inp_laddr.s_addr != INADDR_ANY && inp->inp_laddr.s_addr != ip->ip_dst.s_addr) continue; arg.inp = inp; arg.last = last; arg.ip = ip; arg.m = m; error = udp_mcast_input(&arg); if (error == ERESTART) continue; last = arg.last; if (error == EJUSTRETURN) break; } LIST_REMOVE(ic_marker, ic_list); REL_PCBINFO_TOKEN(pcbinfo); done: if (last == NULL) { /* * No matching pcb found; discard datagram. * (No need to send an ICMP Port Unreachable * for a broadcast or multicast datgram.) */ udp_stat.udps_noportbcast++; goto bad; } udp_append(last, ip, m, iphlen + sizeof(struct udphdr), &udp_in); return(IPPROTO_DONE); } /* * Locate pcb for datagram. */ inp = in_pcblookup_pkthash(pcbinfo, ip->ip_src, uh->uh_sport, ip->ip_dst, uh->uh_dport, TRUE, m->m_pkthdr.rcvif, udp_reuseport_ext ? m : NULL); if (inp == NULL) { if (log_in_vain) { char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN]; log(LOG_INFO, "Connection attempt to UDP %s:%d from %s:%d\n", kinet_ntoa(ip->ip_dst, dst), ntohs(uh->uh_dport), kinet_ntoa(ip->ip_src, src), ntohs(uh->uh_sport)); } udp_stat.udps_noport++; if (m->m_flags & (M_BCAST | M_MCAST)) { udp_stat.udps_noportbcast++; goto bad; } if (blackhole) goto bad; #ifdef ICMP_BANDLIM if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) goto bad; #endif *ip = save_ip; icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); return(IPPROTO_DONE); } KASSERT(INP_ISIPV4(inp), ("not inet inpcb")); /* * Check the minimum TTL for socket. */ if (ip->ip_ttl < inp->inp_ip_minttl) goto bad; /* * Construct sockaddr format source address. * Stuff source address and datagram in user buffer. */ udp_in.sin_port = uh->uh_sport; udp_in.sin_addr = ip->ip_src; if ((inp->inp_flags & INP_CONTROLOPTS) || (inp->inp_socket->so_options & SO_TIMESTAMP)) ip_savecontrol(inp, &opts, ip, m); m_adj(m, iphlen + sizeof(struct udphdr)); lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token); if (ssb_appendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m, opts) == 0) { lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token); udp_stat.udps_fullsock++; soroverflow(inp->inp_socket); goto bad; } lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token); sorwakeup(inp->inp_socket); return(IPPROTO_DONE); bad: m_freem(m); if (opts) m_freem(opts); return(IPPROTO_DONE); } /* * subroutine of udp_input(), mainly for source code readability. * caller must properly init udp_ip6 and udp_in6 beforehand. */ static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, struct sockaddr_in *udp_in) { struct mbuf *opts = NULL; int ret; KASSERT(INP_ISIPV4(last), ("not inet inpcb")); if (last->inp_flags & INP_CONTROLOPTS || last->inp_socket->so_options & SO_TIMESTAMP) ip_savecontrol(last, &opts, ip, n); m_adj(n, off); lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token); ret = ssb_appendaddr(&last->inp_socket->so_rcv, (struct sockaddr *)udp_in, n, opts); lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token); if (ret == 0) { m_freem(n); if (opts) m_freem(opts); udp_stat.udps_fullsock++; } else { sorwakeup(last->inp_socket); } } /* * Notify a udp user of an asynchronous error; * just wake up so that he can collect error status. */ void udp_notify(struct inpcb *inp, int error) { inp->inp_socket->so_error = error; sorwakeup(inp->inp_socket); sowwakeup(inp->inp_socket); } struct netmsg_udp_notify { struct netmsg_base base; inp_notify_t nm_notify; struct in_addr nm_faddr; int nm_arg; }; static void udp_notifyall_oncpu(netmsg_t msg) { struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg; int nextcpu, cpu = mycpuid; ASSERT_NETISR_NCPUS(cpu); in_pcbnotifyall(&udbinfo[cpu], nm->nm_faddr, nm->nm_arg, nm->nm_notify); nextcpu = cpu + 1; if (nextcpu < netisr_ncpus) lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg); else lwkt_replymsg(&nm->base.lmsg, 0); } inp_notify_t udp_get_inpnotify(int cmd, const struct sockaddr *sa, struct ip **ip0, int *cpuid) { struct in_addr faddr; struct ip *ip = *ip0; inp_notify_t notify = udp_notify; faddr = ((const struct sockaddr_in *)sa)->sin_addr; if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) return NULL; if (PRC_IS_REDIRECT(cmd)) { ip = NULL; notify = in_rtchange; } else if (cmd == PRC_HOSTDEAD) { ip = NULL; } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) { return NULL; } if (cpuid != NULL) { if (ip == NULL) { /* Go through all effective netisr CPUs. */ *cpuid = netisr_ncpus; } else { const struct udphdr *uh; uh = (const struct udphdr *) ((caddr_t)ip + (ip->ip_hl << 2)); *cpuid = udp_addrcpu(faddr.s_addr, uh->uh_dport, ip->ip_src.s_addr, uh->uh_sport); } } *ip0 = ip; return notify; } void udp_ctlinput(netmsg_t msg) { struct sockaddr *sa = msg->ctlinput.nm_arg; struct ip *ip = msg->ctlinput.nm_extra; int cmd = msg->ctlinput.nm_cmd, cpuid; inp_notify_t notify; struct in_addr faddr; ASSERT_NETISR_NCPUS(mycpuid); notify = udp_get_inpnotify(cmd, sa, &ip, &cpuid); if (notify == NULL) goto done; faddr = ((struct sockaddr_in *)sa)->sin_addr; if (ip) { const struct udphdr *uh; struct inpcb *inp; if (cpuid != mycpuid) goto done; uh = (const struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); inp = in_pcblookup_hash(&udbinfo[mycpuid], faddr, uh->uh_dport, ip->ip_src, uh->uh_sport, 0, NULL); if (inp != NULL && inp->inp_socket != NULL) notify(inp, inetctlerrmap[cmd]); } else if (msg->ctlinput.nm_direct) { if (cpuid != netisr_ncpus && cpuid != mycpuid) goto done; in_pcbnotifyall(&udbinfo[mycpuid], faddr, inetctlerrmap[cmd], notify); } else { struct netmsg_udp_notify *nm; ASSERT_NETISR0; nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT); netmsg_init(&nm->base, NULL, &netisr_afree_rport, 0, udp_notifyall_oncpu); nm->nm_faddr = faddr; nm->nm_arg = inetctlerrmap[cmd]; nm->nm_notify = notify; lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg); } done: lwkt_replymsg(&msg->lmsg, 0); } SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, udbinfo, 0, in_pcblist_ncpus, "S,xinpcb", "List of active UDP sockets"); static int udp_getcred(SYSCTL_HANDLER_ARGS) { struct sockaddr_in addrs[2]; struct ucred cred0, *cred = NULL; struct inpcb *inp; int error, cpu, origcpu; error = caps_priv_check_td(req->td, SYSCAP_RESTRICTEDROOT); if (error) return (error); error = SYSCTL_IN(req, addrs, sizeof addrs); if (error) return (error); origcpu = mycpuid; cpu = udp_addrcpu(addrs[1].sin_addr.s_addr, addrs[1].sin_port, addrs[0].sin_addr.s_addr, addrs[0].sin_port); lwkt_migratecpu(cpu); inp = in_pcblookup_hash(&udbinfo[cpu], addrs[1].sin_addr, addrs[1].sin_port, addrs[0].sin_addr, addrs[0].sin_port, TRUE, NULL); if (inp == NULL || inp->inp_socket == NULL) { error = ENOENT; } else if (inp->inp_socket->so_cred != NULL) { cred0 = *(inp->inp_socket->so_cred); cred = &cred0; } lwkt_migratecpu(origcpu); if (error) return error; return SYSCTL_OUT(req, cred, sizeof(struct ucred)); } SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); static void udp_send_redispatch(netmsg_t msg) { struct mbuf *m = msg->send.nm_m; int pru_flags = msg->send.nm_flags; struct inpcb *inp = msg->send.base.nm_so->so_pcb; struct mbuf *m_opt = msg->send.nm_control; /* XXX save ipopt */ int flags = msg->send.nm_priv; /* ip_output flags */ int error; logudp(redisp_ipout_beg, inp); /* * - Don't use inp route cache. It should only be used in the * inp owner netisr. * - Access to inp_moptions should be safe, since multicast UDP * datagrams are redispatched to netisr0 and inp_moptions is * changed only in netisr0. */ error = ip_output(m, m_opt, NULL, flags, inp->inp_moptions, inp); if ((pru_flags & PRUS_NOREPLY) == 0) lwkt_replymsg(&msg->send.base.lmsg, error); if (m_opt != NULL) { /* Free saved ip options, if any */ m_freem(m_opt); } logudp(redisp_ipout_end, inp); } static void udp_send(netmsg_t msg) { struct socket *so = msg->send.base.nm_so; struct mbuf *m = msg->send.nm_m; struct sockaddr *dstaddr = msg->send.nm_addr; int pru_flags = msg->send.nm_flags; struct inpcb *inp = so->so_pcb; struct thread *td = msg->send.nm_td; struct mbuf *control = msg->send.nm_control; uint16_t hash; int flags; struct udpiphdr *ui; int len = m->m_pkthdr.len; struct sockaddr_in *sin; /* really is initialized before use */ int error = 0, cpu; struct sockaddr_in src; struct in_addr laddr; u_char tos; logudp(send_beg, inp); if (inp == NULL) { error = EINVAL; goto release; } if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { error = EMSGSIZE; goto release; } if (inp->inp_lport == 0) { /* unbound socket */ boolean_t forwarded; error = in_pcbbind(inp, NULL, td); if (error) goto release; /* * Need to call udp_send again, after this inpcb is * inserted into wildcard hash table. */ msg->send.base.lmsg.ms_flags |= MSGF_UDP_SEND; forwarded = udp_inswildcardhash(inp, &msg->send.base, 0); if (forwarded) { /* * The message is further forwarded, so we are * done here. */ logudp(send_inswildcard, inp); return; } } tos = inp->inp_ip_tos; src.sin_family = 0; if (control != NULL) { struct cmsghdr *cm; /* * XXX: Currently, we assume all the optional information is * stored in a single mbuf. */ if (control->m_next) { error = EINVAL; goto release; } for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len), control->m_len -= CMSG_ALIGN(cm->cmsg_len)) { cm = mtod(control, struct cmsghdr *); if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 || cm->cmsg_len > control->m_len) { error = EINVAL; goto release; } if (cm->cmsg_level != IPPROTO_IP) continue; switch (cm->cmsg_type) { case IP_SENDSRCADDR: if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_addr))) { error = EINVAL; goto release; } src.sin_family = AF_INET; src.sin_len = sizeof(src); src.sin_addr = *(struct in_addr *)CMSG_DATA(cm); break; case IP_TOS: if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) { error = EINVAL; goto release; } tos = *(u_char *)CMSG_DATA(cm); break; default: error = ENOPROTOOPT; goto release; } } KKASSERT(error == 0); m_freem(control); control = NULL; } /* * If the IP_SENDSRCADDR control message was specified, override the * source address for this datagram. Its use is invalidated if the * address thus specified is incomplete or clobbers other inpcbs. */ laddr = inp->inp_laddr; if (src.sin_family == AF_INET) { error = in_pcbsrcaddr_check(inp, &src, &laddr, td); if (error) goto release; } if (dstaddr != NULL) { /* destination address specified */ if (inp->inp_faddr.s_addr != INADDR_ANY) { /* already connected */ error = EISCONN; goto release; } sin = (struct sockaddr_in *)dstaddr; } else { if (inp->inp_faddr.s_addr == INADDR_ANY) { /* no destination specified and not already connected */ error = ENOTCONN; goto release; } sin = NULL; } /* * Calculate data length and get a mbuf * for UDP and IP headers. */ M_PREPEND(m, sizeof(struct udpiphdr), M_NOWAIT); if (m == NULL) { error = ENOBUFS; goto release; } /* * Fill in mbuf with extended UDP header * and addresses and length put into network format. */ ui = mtod(m, struct udpiphdr *); bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */ ui->ui_pr = IPPROTO_UDP; /* * Set destination address. */ if (dstaddr != NULL) { /* use specified destination */ ui->ui_dst = sin->sin_addr; ui->ui_dport = sin->sin_port; } else { /* use connected destination */ ui->ui_dst = inp->inp_faddr; ui->ui_dport = inp->inp_fport; } /* * Set source address. */ if (laddr.s_addr == INADDR_ANY || IN_MULTICAST(ntohl(laddr.s_addr))) { struct sockaddr_in *if_sin; if (dstaddr == NULL) { /* * connect() had (or should have) failed because * the interface had no IP address, but the * application proceeded to call send() anyways. */ error = ENOTCONN; goto release; } /* Look up outgoing interface. */ error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1); if (error) goto release; ui->ui_src = if_sin->sin_addr; /* use address of interface */ } else { ui->ui_src = laddr; /* use non-null bound address */ } ui->ui_sport = inp->inp_lport; KASSERT(inp->inp_lport != 0, ("inp lport should have been bound")); /* * Release the original thread, since it is no longer used */ if (pru_flags & PRUS_HELDTD) { lwkt_rele(td); pru_flags &= ~PRUS_HELDTD; } /* * Free the dest address, since it is no longer needed */ if (pru_flags & PRUS_FREEADDR) { kfree(dstaddr, M_SONAME); pru_flags &= ~PRUS_FREEADDR; } ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); /* * Set up checksum and output datagram. */ if (udpcksum) { ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); m->m_pkthdr.csum_flags = CSUM_UDP; m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); m->m_pkthdr.csum_thlen = sizeof(struct udphdr); } else { ui->ui_sum = 0; } ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len); ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ ((struct ip *)ui)->ip_tos = tos; udp_stat.udps_opackets++; flags = IP_DEBUGROUTE | (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)); if (pru_flags & PRUS_DONTROUTE) flags |= SO_DONTROUTE; /* * NOTE: * For multicast bound and connected socket, source address of * the datagram is selected at the time of the sending, so the * datagram will have to be hashed. */ if ((inp->inp_flags & INP_CONNECTED) && ui->ui_src.s_addr == inp->inp_laddr.s_addr) { /* * For connected socket, this datagram has already * been in the correct netisr; no need to rehash. */ KASSERT(inp->inp_flags & INP_HASH, ("inpcb has no hash")); m_sethash(m, inp->inp_hashval); goto sendit; } hash = udp_addrhash(ui->ui_dst.s_addr, ui->ui_dport, ui->ui_src.s_addr, ui->ui_sport); m_sethash(m, hash); cpu = netisr_hashcpu(hash); if (cpu != mycpuid) { struct mbuf *m_opt = NULL; struct netmsg_pru_send *smsg; struct lwkt_port *port = netisr_cpuport(cpu); if (inp->inp_flags & INP_DIRECT_DETACH) { /* * No longer direct detachable, i.e. datagram has * to be forwarded to other CPUs for multicast * bound and connected socket. */ inp->inp_flags &= ~INP_DIRECT_DETACH; } /* * Not on the CPU that matches this UDP datagram hash; * redispatch to the correct CPU to do the ip_output(). */ if (inp->inp_options != NULL) { /* * If there are ip options, then save a copy, * since accessing inp_options on other CPUs' * is not safe. * * XXX optimize this? */ m_opt = m_copym(inp->inp_options, 0, M_COPYALL, M_WAITOK); } if ((pru_flags & PRUS_NOREPLY) == 0) { /* * Change some parts of the original netmsg and * forward it to the target netisr. * * NOTE: so_port MUST NOT be checked in the target * netisr. */ smsg = &msg->send; smsg->nm_priv = flags; /* ip_output flags */ smsg->nm_m = m; smsg->nm_control = m_opt; /* XXX save ipopt */ smsg->base.lmsg.ms_flags |= MSGF_IGNSOPORT; smsg->base.nm_dispatch = udp_send_redispatch; lwkt_forwardmsg(port, &smsg->base.lmsg); } else { /* * Recreate the netmsg, since the original mbuf * could have been changed. And send it to the * target netisr. * * NOTE: so_port MUST NOT be checked in the target * netisr. */ smsg = &m->m_hdr.mh_sndmsg; netmsg_init(&smsg->base, so, &netisr_apanic_rport, MSGF_IGNSOPORT, udp_send_redispatch); smsg->nm_priv = flags; /* ip_output flags */ smsg->nm_flags = pru_flags; smsg->nm_m = m; smsg->nm_control = m_opt; /* XXX save ipopt */ lwkt_sendmsg(port, &smsg->base.lmsg); } /* This UDP datagram is redispatched; done */ logudp(send_redisp, inp); return; } sendit: logudp(send_ipout, inp); error = ip_output(m, inp->inp_options, &inp->inp_route, flags, inp->inp_moptions, inp); m = NULL; release: if (m != NULL) m_freem(m); if (control != NULL) m_freem(control); if (pru_flags & PRUS_HELDTD) lwkt_rele(td); if (pru_flags & PRUS_FREEADDR) kfree(dstaddr, M_SONAME); if ((pru_flags & PRUS_NOREPLY) == 0) lwkt_replymsg(&msg->send.base.lmsg, error); logudp(send_end, inp); } u_long udp_sendspace = 9216; /* really max datagram size */ /* 40 1K datagrams */ SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); u_long udp_recvspace = 40 * (1024 + #ifdef INET6 sizeof(struct sockaddr_in6) #else sizeof(struct sockaddr_in) #endif ); SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, &udp_recvspace, 0, "Maximum incoming UDP datagram size"); /* * This should never happen, since UDP socket does not support * connection acception (SO_ACCEPTCONN, i.e. listen(2)). */ static void udp_abort(netmsg_t msg __unused) { panic("udp_abort is called"); } static int udp_preattach(struct socket *so, int proto __unused, struct pru_attach_info *ai) { return soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit); } static void udp_attach(netmsg_t msg) { struct socket *so = msg->attach.base.nm_so; struct pru_attach_info *ai = msg->attach.nm_ai; struct inpcb *inp; int error; KASSERT(so->so_pcb == NULL, ("udp socket attached")); if (ai != NULL) { error = udp_preattach(so, 0 /* don't care */, ai); if (error) goto out; } else { /* Post attach; do nothing */ } error = in_pcballoc(so, &udbinfo[mycpuid]); if (error) goto out; inp = so->so_pcb; inp->inp_flags |= INP_DIRECT_DETACH; inp->inp_ip_ttl = ip_defttl; error = 0; out: lwkt_replymsg(&msg->attach.base.lmsg, error); } static void udp_inswildcard_replymsg(netmsg_t msg) { lwkt_msg_t lmsg = &msg->lmsg; if (lmsg->ms_flags & MSGF_UDP_SEND) { udp_send(msg); /* msg is replied by udp_send() */ } else { lwkt_replymsg(lmsg, lmsg->ms_error); } } static void udp_soreuseport_dispatch(netmsg_t msg) { /* This inpcb has already been in the wildcard hash. */ in_pcblink_flags(msg->base.nm_so->so_pcb, &udbinfo[mycpuid], 0); udp_inswildcard_replymsg(msg); } static void udp_sosetport(struct lwkt_msg *msg, lwkt_port_t port) { sosetport(((struct netmsg_base *)msg)->nm_so, port); } static boolean_t udp_inswildcardhash_oncpu(struct inpcb *inp, struct netmsg_base *msg) { int cpu; KASSERT(inp->inp_pcbinfo == &udbinfo[mycpuid], ("not on owner cpu")); in_pcbinswildcardhash(inp); for (cpu = 0; cpu < netisr_ncpus; ++cpu) { if (cpu == mycpuid) { /* * This inpcb has been inserted by the above * in_pcbinswildcardhash(). */ continue; } in_pcbinswildcardhash_oncpu(inp, &udbinfo[cpu]); } /* NOTE: inp_lgrpindex is _not_ assigned in jail. */ if ((inp->inp_socket->so_options & SO_REUSEPORT) && inp->inp_lgrpindex >= 0) { /* * For SO_REUSEPORT socket, redistribute it based on its * local group index. */ cpu = inp->inp_lgrpindex % netisr_ncpus; if (cpu != mycpuid) { struct lwkt_port *port = netisr_cpuport(cpu); lwkt_msg_t lmsg = &msg->lmsg; /* * We are moving the protocol processing port the * socket is on, we have to unlink here and re-link * on the target cpu (this inpcb is still left in * the wildcard hash). */ in_pcbunlink_flags(inp, &udbinfo[mycpuid], 0); msg->nm_dispatch = udp_soreuseport_dispatch; /* * See the related comment in tcp_usrreq.c * tcp_connect() */ lwkt_setmsg_receipt(lmsg, udp_sosetport); lwkt_forwardmsg(port, lmsg); return TRUE; /* forwarded */ } } return FALSE; } static void udp_inswildcardhash_dispatch(netmsg_t msg) { struct inpcb *inp = msg->base.nm_so->so_pcb; boolean_t forwarded; KASSERT(inp->inp_lport != 0, ("local port not set yet")); KASSERT(udp_lportcpu(inp->inp_lport) == mycpuid, ("not target cpu")); in_pcblink(inp, &udbinfo[mycpuid]); forwarded = udp_inswildcardhash_oncpu(inp, &msg->base); if (forwarded) { /* The message is further forwarded, so we are done here. */ return; } udp_inswildcard_replymsg(msg); } static boolean_t udp_inswildcardhash(struct inpcb *inp, struct netmsg_base *msg, int error) { lwkt_msg_t lmsg = &msg->lmsg; int cpu; ASSERT_INP_NOTINHASH(inp); /* This inpcb could no longer be directly detached */ inp->inp_flags &= ~INP_DIRECT_DETACH; /* * Always clear the route cache, so we don't need to * worry about any owner CPU changes later. */ in_pcbresetroute(inp); KASSERT(inp->inp_lport != 0, ("local port not set yet")); cpu = udp_lportcpu(inp->inp_lport); lmsg->ms_error = error; if (cpu != mycpuid) { struct lwkt_port *port = netisr_cpuport(cpu); /* * We are moving the protocol processing port the socket * is on, we have to unlink here and re-link on the * target cpu. */ in_pcbunlink(inp, &udbinfo[mycpuid]); msg->nm_dispatch = udp_inswildcardhash_dispatch; /* See the related comment in tcp_usrreq.c tcp_connect() */ lwkt_setmsg_receipt(lmsg, udp_sosetport); lwkt_forwardmsg(port, lmsg); return TRUE; /* forwarded */ } return udp_inswildcardhash_oncpu(inp, msg); } static void udp_bind(netmsg_t msg) { struct socket *so = msg->bind.base.nm_so; struct inpcb *inp; int error; inp = so->so_pcb; if (inp) { struct sockaddr *nam = msg->bind.nm_nam; struct thread *td = msg->bind.nm_td; struct sockaddr_in *sin; lwkt_port_t port; int cpu; /* * Check "already bound" here (in_pcbbind() does the same * check though), so we don't forward a connected/bound * socket randomly which would panic in the following * in_pcbunlink(). */ if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY) { error = EINVAL; /* already bound */ goto done; } if (nam->sa_len != sizeof(*sin)) { error = EINVAL; goto done; } sin = (struct sockaddr_in *)nam; cpu = udp_lportcpu(sin->sin_port); port = netisr_cpuport(cpu); /* * See the related comment in tcp_usrreq.c tcp_usr_bind(). * The exception is that we use local port based netisr * to serialize in_pcbbind(). */ if (&curthread->td_msgport != port) { lwkt_msg_t lmsg = &msg->bind.base.lmsg; KASSERT((msg->bind.nm_flags & PRUB_RELINK) == 0, ("already asked to relink")); in_pcbunlink(so->so_pcb, &udbinfo[mycpuid]); msg->bind.nm_flags |= PRUB_RELINK; /* * See the related comment in tcp_usrreq.c * tcp_connect(). */ lwkt_setmsg_receipt(lmsg, udp_sosetport); lwkt_forwardmsg(port, lmsg); /* msg invalid now */ return; } KASSERT(so->so_port == port, ("so_port is not netisr%d", cpu)); if (msg->bind.nm_flags & PRUB_RELINK) { msg->bind.nm_flags &= ~PRUB_RELINK; in_pcblink(so->so_pcb, &udbinfo[mycpuid]); } KASSERT(inp->inp_pcbinfo == &udbinfo[cpu], ("pcbinfo is not udbinfo%d", cpu)); error = in_pcbbind(inp, nam, td); if (error == 0) { boolean_t forwarded; if (sin->sin_addr.s_addr != INADDR_ANY) inp->inp_flags |= INP_WASBOUND_NOTANY; forwarded = udp_inswildcardhash(inp, &msg->bind.base, 0); if (forwarded) { /* * The message is further forwarded, so * we are done here. */ return; } } } else { error = EINVAL; } done: lwkt_replymsg(&msg->bind.base.lmsg, error); } static int udp_preconnect(struct socket *so, const struct sockaddr *nam __unused, struct thread *td __unused) { sosetstate(so, SS_ISCONNECTED); /* XXX */ return 0; } static void udp_connect(netmsg_t msg) { struct socket *so = msg->connect.base.nm_so; struct sockaddr *nam = msg->connect.nm_nam; struct thread *td = msg->connect.nm_td; struct inpcb *inp; struct sockaddr_in *sin = (struct sockaddr_in *)nam; struct sockaddr_in *if_sin; struct lwkt_port *port; uint16_t hash; int error; KKASSERT(msg->connect.nm_m == NULL); inp = so->so_pcb; if (inp == NULL) { error = EINVAL; goto out; } if (msg->connect.nm_flags & PRUC_RECONNECT) { msg->connect.nm_flags &= ~PRUC_RECONNECT; in_pcblink(inp, &udbinfo[mycpuid]); } if (inp->inp_faddr.s_addr != INADDR_ANY) { error = EISCONN; goto out; } error = 0; /* * Bind if we have to */ if (inp->inp_lport == 0) { error = in_pcbbind(inp, NULL, td); if (error) goto out; } /* * Calculate the correct protocol processing thread. The connect * operation must run there. */ error = in_pcbladdr(inp, nam, &if_sin, td); if (error) goto out; hash = udp_addrhash(sin->sin_addr.s_addr, sin->sin_port, inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr, inp->inp_lport); port = netisr_hashport(hash); if (port != &curthread->td_msgport) { lwkt_msg_t lmsg = &msg->connect.base.lmsg; int nm_flags = PRUC_RECONNECT; /* * in_pcbladdr() may have allocated a route entry for us * on the current CPU, but we need a route entry on the * inpcb's owner CPU, so free it here. */ in_pcbresetroute(inp); if (inp->inp_flags & INP_WILDCARD) { /* * Remove this inpcb from the wildcard hash before * the socket's msgport changes. */ udp_remwildcardhash(inp); } if (so->so_orig_port == NULL) { /* * First time change protocol processing port. * Save the current port for synchronization upon * udp_detach. */ so->so_orig_port = &curthread->td_msgport; } else { /* * We have changed protocol processing port more * than once. We could not do direct detach * anymore, because we lose the track of the * original protocol processing ports to perform * synchronization upon udp_detach. This should * be rare though. */ inp->inp_flags &= ~INP_DIRECT_DETACH; } /* * We are moving the protocol processing port the socket * is on, we have to unlink here and re-link on the * target cpu. */ in_pcbunlink(inp, &udbinfo[mycpuid]); msg->connect.nm_flags |= nm_flags; /* See the related comment in tcp_usrreq.c tcp_connect() */ lwkt_setmsg_receipt(lmsg, udp_sosetport); lwkt_forwardmsg(port, lmsg); /* msg invalid now */ return; } error = udp_connect_oncpu(inp, sin, if_sin, hash); out: if (msg->connect.nm_flags & PRUC_HELDTD) lwkt_rele(td); if (error && (msg->connect.nm_flags & PRUC_ASYNC)) { if (inp->inp_lport == 0) { /* * As long as we have the local port, it is fine * for connect to fail, e.g. disconnect. */ so->so_error = error; } soclrstate(so, SS_ISCONNECTED); /* * Wake up callers blocked on this socket to make sure * that they can see this error. * * NOTE: * sodisconnected() can't be used here, which bricks * sending and receiving. */ wakeup(&so->so_timeo); sowwakeup(so); sorwakeup(so); } if (error && inp != NULL && inp->inp_lport != 0 && (inp->inp_flags & INP_WILDCARD) == 0) { boolean_t forwarded; /* Connect failed; put it to wildcard hash. */ forwarded = udp_inswildcardhash(inp, &msg->connect.base, error); if (forwarded) { /* * The message is further forwarded, so we are done * here. */ return; } } lwkt_replymsg(&msg->connect.base.lmsg, error); } static void udp_remwildcardhash(struct inpcb *inp) { int cpu; KASSERT(inp->inp_pcbinfo == &udbinfo[mycpuid], ("not on owner cpu")); for (cpu = 0; cpu < netisr_ncpus; ++cpu) { if (cpu == mycpuid) { /* * This inpcb will be removed by the later * in_pcbremwildcardhash(). */ continue; } in_pcbremwildcardhash_oncpu(inp, &udbinfo[cpu]); } in_pcbremwildcardhash(inp); } static int udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin, struct sockaddr_in *if_sin, uint16_t hash) { struct socket *so = inp->inp_socket; struct inpcb *oinp; oinp = in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port, inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr : if_sin->sin_addr, inp->inp_lport, FALSE, NULL); if (oinp != NULL) return EADDRINUSE; /* * No more errors can occur, finish adjusting the socket * and change the processing port to reflect the connected * socket. Once set we can no longer safely mess with the * socket. */ if (inp->inp_flags & INP_WILDCARD) udp_remwildcardhash(inp); if (inp->inp_laddr.s_addr == INADDR_ANY) inp->inp_laddr = if_sin->sin_addr; inp->inp_faddr = sin->sin_addr; inp->inp_fport = sin->sin_port; in_pcbinsconnhash(inp); inp->inp_flags |= INP_HASH; inp->inp_hashval = hash; soisconnected(so); return 0; } static void udp_detach2(struct socket *so) { in_pcbdetach(so->so_pcb); sodiscard(so); sofree(so); } static void udp_detach_final_dispatch(netmsg_t msg) { udp_detach2(msg->base.nm_so); } static void udp_detach_oncpu_dispatch(netmsg_t msg) { struct netmsg_base *clomsg = &msg->base; struct socket *so = clomsg->nm_so; struct inpcb *inp = so->so_pcb; struct thread *td = curthread; int nextcpu, cpuid = mycpuid; KASSERT(td->td_type == TD_TYPE_NETISR, ("not in netisr")); if (inp->inp_flags & INP_WILDCARD) { /* * This inp will be removed on the inp's * owner CPU later, so don't do it now. */ if (&td->td_msgport != so->so_port) in_pcbremwildcardhash_oncpu(inp, &udbinfo[cpuid]); } if (cpuid == 0) { /* * Free and clear multicast socket option, * which is only accessed in netisr0. */ ip_freemoptions(inp->inp_moptions); inp->inp_moptions = NULL; } nextcpu = cpuid + 1; if (nextcpu < netisr_ncpus) { lwkt_forwardmsg(netisr_cpuport(nextcpu), &clomsg->lmsg); } else { /* * No one could see this inpcb now; destroy this * inpcb in its owner netisr. */ netmsg_init(clomsg, so, &netisr_apanic_rport, 0, udp_detach_final_dispatch); lwkt_sendmsg(so->so_port, &clomsg->lmsg); } } static void udp_detach_syncorig_dispatch(netmsg_t msg) { struct netmsg_base *clomsg = &msg->base; struct socket *so = clomsg->nm_so; /* * Original protocol processing port is synchronized; * destroy this inpcb in its owner netisr. */ netmsg_init(clomsg, so, &netisr_apanic_rport, 0, udp_detach_final_dispatch); lwkt_sendmsg(so->so_port, &clomsg->lmsg); } static void udp_detach(netmsg_t msg) { struct socket *so = msg->detach.base.nm_so; struct netmsg_base *clomsg; struct inpcb *inp; inp = so->so_pcb; if (inp == NULL) { lwkt_replymsg(&msg->detach.base.lmsg, EINVAL); return; } /* * Reply EJUSTRETURN ASAP, we will call sodiscard() and * sofree() later. */ lwkt_replymsg(&msg->detach.base.lmsg, EJUSTRETURN); if (netisr_ncpus == 1) { /* Only one CPU, detach the inpcb directly. */ udp_detach2(so); return; } /* * Remove this inpcb from the inpcb list first, so that * no one could find this inpcb from the inpcb list. */ in_pcbofflist(inp); /* * Remove this inpcb from the local port hash directly * here, so that its bound local port could be recycled * timely. */ in_pcbremporthash(inp); if (inp->inp_flags & INP_DIRECT_DETACH) { /* * Direct detaching is allowed */ KASSERT((inp->inp_flags & INP_WILDCARD) == 0, ("in the wildcardhash")); KASSERT(inp->inp_moptions == NULL, ("has mcast options")); if (so->so_orig_port == NULL) { udp_detach2(so); } else { /* * Protocol processing port changed once, so * we need to make sure that there are nothing * left on the original protocol processing * port before we destroy this socket and inpcb. * This is more lightweight than going through * all UDP processing netisrs. */ clomsg = &so->so_clomsg; netmsg_init(clomsg, so, &netisr_apanic_rport, MSGF_IGNSOPORT, udp_detach_syncorig_dispatch); lwkt_sendmsg(so->so_orig_port, &clomsg->lmsg); } return; } /* * Go through netisrs which process UDP to make sure * no one could find this inpcb anymore. */ clomsg = &so->so_clomsg; netmsg_init(clomsg, so, &netisr_apanic_rport, MSGF_IGNSOPORT, udp_detach_oncpu_dispatch); lwkt_sendmsg(netisr_cpuport(0), &clomsg->lmsg); } static void udp_disconnect(netmsg_t msg) { struct socket *so = msg->disconnect.base.nm_so; struct inpcb *inp; boolean_t forwarded; int error = 0; inp = so->so_pcb; if (inp == NULL) { error = EINVAL; goto out; } if (inp->inp_faddr.s_addr == INADDR_ANY) { error = ENOTCONN; goto out; } soclrstate(so, SS_ISCONNECTED); /* XXX */ in_pcbdisconnect(inp); inp->inp_flags &= ~INP_HASH; /* * Follow traditional BSD behavior and retain the local port * binding. But, fix the old misbehavior of overwriting any * previously bound local address. */ if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) inp->inp_laddr.s_addr = INADDR_ANY; if (so->so_state & SS_ISCLOSING) { /* * If this socket is being closed, there is no need * to put this socket back into wildcard hash table. */ error = 0; goto out; } forwarded = udp_inswildcardhash(inp, &msg->disconnect.base, 0); if (forwarded) { /* * The message is further forwarded, so we are done * here. */ return; } out: lwkt_replymsg(&msg->disconnect.base.lmsg, error); } void udp_shutdown(netmsg_t msg) { struct socket *so = msg->shutdown.base.nm_so; struct inpcb *inp; int error; inp = so->so_pcb; if (inp) { socantsendmore(so); error = 0; } else { error = EINVAL; } lwkt_replymsg(&msg->shutdown.base.lmsg, error); } struct pr_usrreqs udp_usrreqs = { .pru_abort = udp_abort, .pru_accept = pr_generic_notsupp, .pru_attach = udp_attach, .pru_bind = udp_bind, .pru_connect = udp_connect, .pru_connect2 = pr_generic_notsupp, .pru_control = in_control_dispatch, .pru_detach = udp_detach, .pru_disconnect = udp_disconnect, .pru_listen = pr_generic_notsupp, .pru_peeraddr = in_setpeeraddr_dispatch, .pru_rcvd = pr_generic_notsupp, .pru_rcvoob = pr_generic_notsupp, .pru_send = udp_send, .pru_sense = pru_sense_null, .pru_shutdown = udp_shutdown, .pru_sockaddr = in_setsockaddr_dispatch, .pru_sosend = sosendudp, .pru_soreceive = soreceive, .pru_preconnect = udp_preconnect, .pru_preattach = udp_preattach }; |