Loading...
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 | /* * DDP: An implementation of the Appletalk DDP protocol for * ethernet 'ELAP'. * * Alan Cox <Alan.Cox@linux.org> * * With more than a little assistance from * * Wesley Craig <netatalk@umich.edu> * * Fixes: * Michael Callahan : Made routing work * Wesley Craig : Fix probing to listen to a * passed node id. * Alan Cox : Added send/recvmsg support * Alan Cox : Moved at. to protinfo in * socket. * Alan Cox : Added firewall hooks. * Alan Cox : Supports new ARPHRD_LOOPBACK * Christer Weinigel : Routing and /proc fixes. * Bradford Johnson : Localtalk. * Tom Dyas : Module support. * Alan Cox : Hooks for PPP (based on the * localtalk hook). * Alan Cox : Posix bits * Alan Cox/Mike Freeman : Possible fix to NBP problems * Bradford Johnson : IP-over-DDP (experimental) * Jay Schulist : Moved IP-over-DDP to its own * driver file. (ipddp.c & ipddp.h) * Jay Schulist : Made work as module with * Appletalk drivers, cleaned it. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * */ #include <linux/config.h> #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE) #include <linux/module.h> #include <asm/uaccess.h> #include <asm/system.h> #include <asm/bitops.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/string.h> #include <linux/mm.h> #include <linux/socket.h> #include <linux/sockios.h> #include <linux/in.h> #include <linux/errno.h> #include <linux/interrupt.h> #include <linux/if_ether.h> #include <linux/notifier.h> #include <linux/netdevice.h> #include <linux/inetdevice.h> #include <linux/route.h> #include <linux/inet.h> #include <linux/etherdevice.h> #include <linux/if_arp.h> #include <linux/skbuff.h> #include <linux/termios.h> /* For TIOCOUTQ/INQ */ #include <net/datalink.h> #include <net/p8022.h> #include <net/psnap.h> #include <net/sock.h> #include <linux/ip.h> #include <net/route.h> #include <linux/atalk.h> #include <linux/proc_fs.h> #include <linux/stat.h> #include <linux/firewall.h> #include <linux/init.h> #undef APPLETALK_DEBUG #ifdef APPLETALK_DEBUG #define DPRINT(x) print(x) #else #define DPRINT(x) #endif /* APPLETALK_DEBUG */ #ifdef CONFIG_SYSCTL extern inline void atalk_register_sysctl(void); extern inline void atalk_unregister_sysctl(void); #endif /* CONFIG_SYSCTL */ struct datalink_proto *ddp_dl, *aarp_dl; static struct proto_ops atalk_dgram_ops; #define min(a,b) (((a)<(b))?(a):(b)) /**************************************************************************\ * * * Handlers for the socket list. * * * \**************************************************************************/ static struct sock *atalk_socket_list = NULL; /* * Note: Sockets may not be removed _during_ an interrupt or inet_bh * handler using this technique. They can be added although we do not * use this facility. */ extern inline void atalk_remove_socket(struct sock *sk) { sklist_remove_socket(&atalk_socket_list,sk); } extern inline void atalk_insert_socket(struct sock *sk) { sklist_insert_socket(&atalk_socket_list,sk); } static struct sock *atalk_search_socket(struct sockaddr_at *to, struct atalk_iface *atif) { struct sock *s; for(s = atalk_socket_list; s != NULL; s = s->next) { if(to->sat_port != s->protinfo.af_at.src_port) { continue; } if(to->sat_addr.s_net == 0 && to->sat_addr.s_node == ATADDR_BCAST && s->protinfo.af_at.src_net == atif->address.s_net) { break; } if(to->sat_addr.s_net == s->protinfo.af_at.src_net && (to->sat_addr.s_node == s->protinfo.af_at.src_node || to->sat_addr.s_node == ATADDR_BCAST || to->sat_addr.s_node == ATADDR_ANYNODE)) { break; } /* XXXX.0 */ } return (s); } /* * Find a socket in the list. */ static struct sock *atalk_find_socket(struct sockaddr_at *sat) { struct sock *s; for(s = atalk_socket_list; s != NULL; s = s->next) { if(s->protinfo.af_at.src_net != sat->sat_addr.s_net) { continue; } if(s->protinfo.af_at.src_node != sat->sat_addr.s_node) { continue; } if(s->protinfo.af_at.src_port != sat->sat_port) { continue; } break; } return (s); } extern inline void atalk_destroy_socket(struct sock *sk) { sklist_destroy_socket(&atalk_socket_list, sk); MOD_DEC_USE_COUNT; } /* * Called from proc fs */ int atalk_get_info(char *buffer, char **start, off_t offset, int length, int dummy) { struct sock *s; int len=0; off_t pos=0; off_t begin=0; /* * Output the appletalk data for the /proc virtual fs. */ len += sprintf(buffer,"Type local_addr remote_addr tx_queue rx_queue st uid\n"); for(s = atalk_socket_list; s != NULL; s = s->next) { len += sprintf(buffer+len,"%02X ", s->type); len += sprintf(buffer+len,"%04X:%02X:%02X ", ntohs(s->protinfo.af_at.src_net), s->protinfo.af_at.src_node, s->protinfo.af_at.src_port); len += sprintf(buffer+len,"%04X:%02X:%02X ", ntohs(s->protinfo.af_at.dest_net), s->protinfo.af_at.dest_node, s->protinfo.af_at.dest_port); len += sprintf(buffer+len,"%08X:%08X ", atomic_read(&s->wmem_alloc), atomic_read(&s->rmem_alloc)); len += sprintf(buffer+len,"%02X %d\n", s->state, SOCK_INODE(s->socket)->i_uid); /* Are we still dumping unwanted data then discard the record */ pos = begin + len; if(pos < offset) { len = 0; /* Keep dumping into the buffer start */ begin = pos; } if(pos > offset + length) /* We have dumped enough */ break; } /* The data in question runs from begin to begin+len */ *start = buffer + (offset - begin); /* Start of wanted data */ len -= (offset - begin); /* Remove unwanted header data from length */ if(len > length) len = length; /* Remove unwanted tail data from length */ return (len); } /**************************************************************************\ * * * Routing tables for the Appletalk socket layer. * * * \**************************************************************************/ static struct atalk_route *atalk_router_list = NULL; static struct atalk_iface *atalk_iface_list = NULL; static struct atalk_route atrtr_default; /* For probing devices or in a routerless network */ /* * Appletalk interface control */ /* * Drop a device. Doesn't drop any of its routes - that is the the callers * problem. Called when we down the interface or delete the address. */ static void atif_drop_device(struct device *dev) { struct atalk_iface **iface = &atalk_iface_list; struct atalk_iface *tmp; while((tmp = *iface) != NULL) { if(tmp->dev == dev) { *iface = tmp->next; kfree_s(tmp, sizeof(struct atalk_iface)); dev->atalk_ptr=NULL; MOD_DEC_USE_COUNT; } else iface = &tmp->next; } } static struct atalk_iface *atif_add_device(struct device *dev, struct at_addr *sa) { struct atalk_iface *iface = (struct atalk_iface *) kmalloc(sizeof(*iface), GFP_KERNEL); unsigned long flags; if(iface==NULL) return (NULL); iface->dev=dev; dev->atalk_ptr=iface; iface->address= *sa; iface->status=0; save_flags(flags); cli(); iface->next=atalk_iface_list; atalk_iface_list=iface; restore_flags(flags); MOD_INC_USE_COUNT; return (iface); } /* * Probe a Phase 1 device or a device that requires its Net:Node to * be set via an ioctl. */ void atif_send_probe_phase1(struct atalk_iface *iface) { struct ifreq atreq; struct sockaddr_at *sa = (struct sockaddr_at *)&atreq.ifr_addr; sa->sat_addr.s_node = iface->address.s_node; sa->sat_addr.s_net = ntohs(iface->address.s_net); /* We pass the Net:Node to the drivers/cards by a Device ioctl. */ if(!(iface->dev->do_ioctl(iface->dev, &atreq, SIOCSIFADDR))) { (void)iface->dev->do_ioctl(iface->dev, &atreq, SIOCGIFADDR); if((iface->address.s_net != htons(sa->sat_addr.s_net)) || (iface->address.s_node != sa->sat_addr.s_node)) iface->status |= ATIF_PROBE_FAIL; iface->address.s_net = htons(sa->sat_addr.s_net); iface->address.s_node = sa->sat_addr.s_node; } return; } /* * Perform phase 2 AARP probing on our tentative address. */ static int atif_probe_device(struct atalk_iface *atif) { int netrange=ntohs(atif->nets.nr_lastnet)-ntohs(atif->nets.nr_firstnet)+1; int probe_net=ntohs(atif->address.s_net); int probe_node=atif->address.s_node; int ct, netct, nodect; /* * Offset the network we start probing with. */ if(probe_net == ATADDR_ANYNET) { if(!netrange) probe_net = ntohs(atif->nets.nr_firstnet); else probe_net = ntohs(atif->nets.nr_firstnet) + (jiffies%netrange); } if(probe_node == ATADDR_ANYNODE) probe_node = jiffies&0xFF; /* * Scan the networks. */ for(netct = 0; netct <= netrange; netct++) { /* * Sweep the available nodes from a given start. */ atif->address.s_net = htons(probe_net); for(nodect = 0; nodect < 256; nodect++) { atif->address.s_node = ((nodect+probe_node) & 0xFF); if(atif->address.s_node > 0 && atif->address.s_node<254) { /* * Probe a proposed address. */ if(atif->dev->type == ARPHRD_LOCALTLK || atif->dev->type == ARPHRD_PPP) atif_send_probe_phase1(atif); else { for(ct = 0; ct < AARP_RETRANSMIT_LIMIT; ct++) { aarp_send_probe(atif->dev, &atif->address); /* * Defer 1/10th */ current->timeout = jiffies + (HZ/10); current->state = TASK_INTERRUPTIBLE; schedule(); if(atif->status & ATIF_PROBE_FAIL) break; } } if(!(atif->status & ATIF_PROBE_FAIL)) return (0); } atif->status &= ~ATIF_PROBE_FAIL; } probe_net++; if(probe_net > ntohs(atif->nets.nr_lastnet)) probe_net = ntohs(atif->nets.nr_firstnet); } return (-EADDRINUSE); /* Network is full... */ } struct at_addr *atalk_find_dev_addr(struct device *dev) { struct atalk_iface *iface=dev->atalk_ptr; if(iface) return (&iface->address); return (NULL); } static struct at_addr *atalk_find_primary(void) { struct atalk_iface *iface; struct atalk_iface *fiface = NULL; /* * Return a point-to-point interface only if * there is no non-ptp interface available. */ for(iface=atalk_iface_list; iface != NULL; iface=iface->next) { if(!fiface && !(iface->dev->flags & IFF_LOOPBACK)) fiface=iface; if(!(iface->dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT))) return (&iface->address); } if(fiface) return (&fiface->address); if(atalk_iface_list != NULL) return (&atalk_iface_list->address); else return (NULL); } /* * Find a match for 'any network' - ie any of our interfaces with that * node number will do just nicely. */ static struct atalk_iface *atalk_find_anynet(int node, struct device *dev) { struct atalk_iface *iface=dev->atalk_ptr; if(iface==NULL || (iface->status & ATIF_PROBE)) return (NULL); if(node == ATADDR_BCAST || iface->address.s_node == node || node == ATADDR_ANYNODE) return (iface); return (NULL); } /* * Find a match for a specific network:node pair */ static struct atalk_iface *atalk_find_interface(int net, int node) { struct atalk_iface *iface; for(iface=atalk_iface_list; iface != NULL; iface=iface->next) { if((node==ATADDR_BCAST || node==ATADDR_ANYNODE || iface->address.s_node==node) && iface->address.s_net==net && !(iface->status & ATIF_PROBE)) return (iface); } return (NULL); } /* * Find a route for an appletalk packet. This ought to get cached in * the socket (later on...). We know about host routes and the fact * that a route must be direct to broadcast. */ static struct atalk_route *atrtr_find(struct at_addr *target) { struct atalk_route *r; for(r=atalk_router_list; r != NULL; r=r->next) { if(!(r->flags & RTF_UP)) continue; if(r->target.s_net == target->s_net) { if(!(r->flags&RTF_HOST) || r->target.s_node == target->s_node) return (r); } } if(atrtr_default.dev) return (&atrtr_default); return (NULL); } /* * Given an appletalk network find the device to use. This can be * a simple lookup. */ struct device *atrtr_get_dev(struct at_addr *sa) { struct atalk_route *atr=atrtr_find(sa); if(atr == NULL) return (NULL); else return (atr->dev); } /* * Set up a default router. */ static void atrtr_set_default(struct device *dev) { atrtr_default.dev = dev; atrtr_default.flags = RTF_UP; atrtr_default.gateway.s_net = htons(0); atrtr_default.gateway.s_node = 0; } /* * Add a router. Basically make sure it looks valid and stuff the * entry in the list. While it uses netranges we always set them to one * entry to work like netatalk. */ static int atrtr_create(struct rtentry *r, struct device *devhint) { struct sockaddr_at *ta=(struct sockaddr_at *)&r->rt_dst; struct sockaddr_at *ga=(struct sockaddr_at *)&r->rt_gateway; struct atalk_route *rt; struct atalk_iface *iface, *riface; unsigned long flags; save_flags(flags); /* * Fixme: Raise/Lower a routing change semaphore for these * operations. */ /* * Validate the request */ if(ta->sat_family != AF_APPLETALK) return (-EINVAL); if(devhint == NULL && ga->sat_family != AF_APPLETALK) return (-EINVAL); /* * Now walk the routing table and make our decisions. */ for(rt=atalk_router_list; rt!=NULL; rt=rt->next) { if(r->rt_flags != rt->flags) continue; if(ta->sat_addr.s_net == rt->target.s_net) { if(!(rt->flags & RTF_HOST)) break; if(ta->sat_addr.s_node == rt->target.s_node) break; } } if(devhint == NULL) { for(riface = NULL, iface = atalk_iface_list; iface; iface = iface->next) { if(riface == NULL && ntohs(ga->sat_addr.s_net) >= ntohs(iface->nets.nr_firstnet) && ntohs(ga->sat_addr.s_net) <= ntohs(iface->nets.nr_lastnet)) { riface = iface; } if(ga->sat_addr.s_net == iface->address.s_net && ga->sat_addr.s_node == iface->address.s_node) riface = iface; } if(riface == NULL) return (-ENETUNREACH); devhint = riface->dev; } if(rt == NULL) { rt = (struct atalk_route *)kmalloc(sizeof(struct atalk_route), GFP_KERNEL); if(rt == NULL) return (-ENOBUFS); cli(); rt->next = atalk_router_list; atalk_router_list = rt; } /* * Fill in the routing entry. */ rt->target = ta->sat_addr; rt->dev = devhint; rt->flags = r->rt_flags; rt->gateway = ga->sat_addr; restore_flags(flags); return (0); } /* * Delete a route. Find it and discard it. */ static int atrtr_delete( struct at_addr *addr ) { struct atalk_route **r = &atalk_router_list; struct atalk_route *tmp; while((tmp = *r) != NULL) { if(tmp->target.s_net == addr->s_net && (!(tmp->flags&RTF_GATEWAY) || tmp->target.s_node == addr->s_node)) { *r = tmp->next; kfree_s(tmp, sizeof(struct atalk_route)); return (0); } r = &tmp->next; } return (-ENOENT); } /* * Called when a device is downed. Just throw away any routes * via it. */ void atrtr_device_down(struct device *dev) { struct atalk_route **r = &atalk_router_list; struct atalk_route *tmp; while((tmp = *r) != NULL) { if(tmp->dev == dev) { *r = tmp->next; kfree_s(tmp, sizeof(struct atalk_route)); } else r = &tmp->next; } if(atrtr_default.dev == dev) atrtr_set_default(NULL); } /* * Actually down the interface. */ static inline void atalk_dev_down(struct device *dev) { atrtr_device_down(dev); /* Remove all routes for the device */ aarp_device_down(dev); /* Remove AARP entries for the device */ atif_drop_device(dev); /* Remove the device */ } /* * A device event has occurred. Watch for devices going down and * delete our use of them (iface and route). */ static int ddp_device_event(struct notifier_block *this, unsigned long event, void *ptr) { if(event == NETDEV_DOWN) { /* Discard any use of this */ atalk_dev_down((struct device *) ptr); } return (NOTIFY_DONE); } /* * ioctl calls. Shouldn't even need touching. */ /* * Device configuration ioctl calls. */ int atif_ioctl(int cmd, void *arg) { struct ifreq atreq; static char aarp_mcast[6] = {0x09, 0x00, 0x00, 0xFF, 0xFF, 0xFF}; struct netrange *nr; struct sockaddr_at *sa; struct device *dev; struct atalk_iface *atif; int ct; int limit; struct rtentry rtdef; if(copy_from_user(&atreq,arg,sizeof(atreq))) return (-EFAULT); if((dev = dev_get(atreq.ifr_name)) == NULL) return (-ENODEV); sa=(struct sockaddr_at*)&atreq.ifr_addr; atif=atalk_find_dev(dev); switch(cmd) { case SIOCSIFADDR: if(!suser()) return (-EPERM); if(sa->sat_family != AF_APPLETALK) return (-EINVAL); if(dev->type != ARPHRD_ETHER && dev->type != ARPHRD_LOOPBACK && dev->type != ARPHRD_LOCALTLK && dev->type != ARPHRD_PPP) return (-EPROTONOSUPPORT); nr=(struct netrange *)&sa->sat_zero[0]; /* * Phase 1 is fine on Localtalk but we don't do * Ethertalk phase 1. Anyone wanting to add it go ahead. */ if(dev->type == ARPHRD_ETHER && nr->nr_phase != 2) return (-EPROTONOSUPPORT); if(sa->sat_addr.s_node == ATADDR_BCAST || sa->sat_addr.s_node == 254) return (-EINVAL); if(atif) { /* * Already setting address. */ if(atif->status&ATIF_PROBE) return (-EBUSY); atif->address.s_net = sa->sat_addr.s_net; atif->address.s_node = sa->sat_addr.s_node; atrtr_device_down(dev); /* Flush old routes */ } else { atif=atif_add_device(dev, &sa->sat_addr); if (atif == NULL) return (-ENOMEM); } atif->nets= *nr; /* * Check if the chosen address is used. If so we * error and atalkd will try another. */ if(!(dev->flags & IFF_LOOPBACK) && atif_probe_device(atif) < 0) { atif_drop_device(dev); return (-EADDRINUSE); } /* * Hey it worked - add the direct routes. */ sa = (struct sockaddr_at *)&rtdef.rt_gateway; sa->sat_family = AF_APPLETALK; sa->sat_addr.s_net = atif->address.s_net; sa->sat_addr.s_node = atif->address.s_node; sa = (struct sockaddr_at *)&rtdef.rt_dst; rtdef.rt_flags = RTF_UP; sa->sat_family = AF_APPLETALK; sa->sat_addr.s_node = ATADDR_ANYNODE; if(dev->flags & IFF_LOOPBACK) rtdef.rt_flags |= RTF_HOST; /* * Routerless initial state. */ if(nr->nr_firstnet == htons(0) && nr->nr_lastnet == htons(0xFFFE)) { sa->sat_addr.s_net = atif->address.s_net; atrtr_create(&rtdef, dev); atrtr_set_default(dev); } else { limit = ntohs(nr->nr_lastnet); if(limit - ntohs(nr->nr_firstnet) > 256) { printk(KERN_WARNING "Too many routes/iface.\n"); return (-EINVAL); } for(ct=ntohs(nr->nr_firstnet);ct<=limit;ct++) { sa->sat_addr.s_net = htons(ct); atrtr_create(&rtdef, dev); } } dev_mc_add(dev, aarp_mcast, 6, 1); return (0); case SIOCGIFADDR: if(atif == NULL) return (-EADDRNOTAVAIL); ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK; ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr=atif->address; break; case SIOCGIFBRDADDR: if(atif == NULL) return (-EADDRNOTAVAIL); ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_family=AF_APPLETALK; ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_net=atif->address.s_net; ((struct sockaddr_at *)(&atreq.ifr_addr))->sat_addr.s_node=ATADDR_BCAST; break; case SIOCATALKDIFADDR: case SIOCDIFADDR: if(!suser()) return (-EPERM); if(sa->sat_family != AF_APPLETALK) return (-EINVAL); atalk_dev_down(dev); break; } if(copy_to_user(arg, &atreq, sizeof(atreq))) return (-EFAULT); return (0); } /* * Routing ioctl() calls */ static int atrtr_ioctl(unsigned int cmd, void *arg) { struct rtentry rt; if(copy_from_user(&rt, arg, sizeof(rt))) return (-EFAULT); switch(cmd) { case SIOCDELRT: if(rt.rt_dst.sa_family != AF_APPLETALK) return (-EINVAL); return (atrtr_delete(&((struct sockaddr_at *)&rt.rt_dst)->sat_addr)); case SIOCADDRT: return (atrtr_create(&rt, NULL)); default: return (-EINVAL); } } /* Called from proc fs - just make it print the ifaces neatly */ int atalk_if_get_info(char *buffer, char **start, off_t offset, int length, int dummy) { struct atalk_iface *iface; int len=0; off_t pos=0; off_t begin=0; len += sprintf(buffer,"Interface Address Networks Status\n"); for(iface = atalk_iface_list; iface != NULL; iface = iface->next) { len += sprintf(buffer+len,"%-16s %04X:%02X %04X-%04X %d\n", iface->dev->name, ntohs(iface->address.s_net), iface->address.s_node, ntohs(iface->nets.nr_firstnet), ntohs(iface->nets.nr_lastnet), iface->status); pos = begin + len; if(pos < offset) { len = 0; begin = pos; } if(pos > offset + length) break; } *start = buffer + (offset - begin); len -= (offset - begin); if(len > length) len = length; return (len); } /* Called from proc fs - just make it print the routes neatly */ int atalk_rt_get_info(char *buffer, char **start, off_t offset, int length, int dummy) { struct atalk_route *rt; int len=0; off_t pos=0; off_t begin=0; len += sprintf(buffer,"Target Router Flags Dev\n"); if(atrtr_default.dev) { rt = &atrtr_default; len += sprintf(buffer+len,"Default %04X:%02X %-4d %s\n", ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags, rt->dev->name); } for(rt = atalk_router_list; rt != NULL; rt = rt->next) { len += sprintf(buffer+len,"%04X:%02X %04X:%02X %-4d %s\n", ntohs(rt->target.s_net),rt->target.s_node, ntohs(rt->gateway.s_net), rt->gateway.s_node, rt->flags, rt->dev->name); pos = begin + len; if(pos < offset) { len = 0; begin = pos; } if(pos > offset + length) break; } *start = buffer + (offset - begin); len -= (offset - begin); if(len > length) len = length; return (len); } /**************************************************************************\ * * * Handling for system calls applied via the various interfaces to an * * Appletalk socket object. * * * \**************************************************************************/ /* * Checksum: This is 'optional'. It's quite likely also a good * candidate for assembler hackery 8) */ unsigned short atalk_checksum(struct ddpehdr *ddp, int len) { unsigned long sum=0; /* Assume unsigned long is >16 bits */ unsigned char *data=(unsigned char *)ddp; len -= 4; /* skip header 4 bytes */ data += 4; /* This ought to be unwrapped neatly. I'll trust gcc for now */ while(len--) { sum += *data; sum <<= 1; if(sum & 0x10000) { sum++; sum &= 0xFFFF; } data++; } if(sum) return htons((unsigned short)sum); return 0xFFFF; /* Use 0xFFFF for 0. 0 itself means none */ } /* * Create a socket. Initialise the socket, blank the addresses * set the state. */ static int atalk_create(struct socket *sock, int protocol) { struct sock *sk; sk = sk_alloc(AF_APPLETALK, GFP_KERNEL); if(sk == NULL) return (-ENOMEM); switch(sock->type) { /* * We permit SOCK_DGRAM and RAW is an extension. It is * trivial to do and gives you the full ELAP frame. * Should be handy for CAP 8) */ case SOCK_RAW: case SOCK_DGRAM: sock->ops = &atalk_dgram_ops; break; default: sk_free((void *)sk); return (-ESOCKTNOSUPPORT); } MOD_INC_USE_COUNT; sock_init_data(sock,sk); sk->destruct = NULL; /* Checksums on by default */ sk->mtu = DDP_MAXSZ; sk->zapped = 1; return (0); } /* * Free a socket. No work needed */ static int atalk_release(struct socket *sock, struct socket *peer) { struct sock *sk=sock->sk; if(sk == NULL) return (0); if(!sk->dead) sk->state_change(sk); sk->dead = 1; sock->sk = NULL; atalk_destroy_socket(sk); return (0); } /* * Pick a source address if one is not given. Just return * an error if not supportable. */ static int atalk_pick_port(struct sockaddr_at *sat) { for(sat->sat_port = ATPORT_RESERVED; sat->sat_port < ATPORT_LAST; sat->sat_port++) { if(atalk_find_socket(sat) == NULL) return sat->sat_port; } return (-EBUSY); } static int atalk_autobind(struct sock *sk) { struct at_addr *ap = atalk_find_primary(); struct sockaddr_at sat; int n; if(ap == NULL || ap->s_net == htons(ATADDR_ANYNET)) return (-EADDRNOTAVAIL); sk->protinfo.af_at.src_net = sat.sat_addr.s_net = ap->s_net; sk->protinfo.af_at.src_node = sat.sat_addr.s_node = ap->s_node; if((n = atalk_pick_port(&sat)) < 0) return (n); sk->protinfo.af_at.src_port = n; atalk_insert_socket(sk); sk->zapped = 0; return (0); } /* * Set the address 'our end' of the connection. */ static int atalk_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) { struct sock *sk; struct sockaddr_at *addr=(struct sockaddr_at *)uaddr; sk = sock->sk; if(sk->zapped == 0) return (-EINVAL); if(addr_len != sizeof(struct sockaddr_at)) return (-EINVAL); if(addr->sat_family != AF_APPLETALK) return (-EAFNOSUPPORT); if(addr->sat_addr.s_net == htons(ATADDR_ANYNET)) { struct at_addr *ap=atalk_find_primary(); if(ap == NULL) return (-EADDRNOTAVAIL); sk->protinfo.af_at.src_net = addr->sat_addr.s_net = ap->s_net; sk->protinfo.af_at.src_node = addr->sat_addr.s_node= ap->s_node; } else { if(atalk_find_interface(addr->sat_addr.s_net, addr->sat_addr.s_node) == NULL) return (-EADDRNOTAVAIL); sk->protinfo.af_at.src_net = addr->sat_addr.s_net; sk->protinfo.af_at.src_node = addr->sat_addr.s_node; } if(addr->sat_port == ATADDR_ANYPORT) { int n = atalk_pick_port(addr); if(n < 0) return (n); sk->protinfo.af_at.src_port = addr->sat_port = n; } else sk->protinfo.af_at.src_port = addr->sat_port; if(atalk_find_socket(addr) != NULL) return (-EADDRINUSE); atalk_insert_socket(sk); sk->zapped = 0; return (0); } /* * Set the address we talk to. */ static int atalk_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags) { struct sock *sk=sock->sk; struct sockaddr_at *addr; sk->state = TCP_CLOSE; sock->state = SS_UNCONNECTED; if(addr_len != sizeof(*addr)) return (-EINVAL); addr = (struct sockaddr_at *)uaddr; if(addr->sat_family != AF_APPLETALK) return (-EAFNOSUPPORT); if(addr->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast) { #if 1 printk(KERN_WARNING "%s is broken and did not set SO_BROADCAST. It will break when 2.2 is released.\n", current->comm); #else return (-EACCES); #endif } if(sk->zapped) { if(atalk_autobind(sk) < 0) return (-EBUSY); } if(atrtr_get_dev(&addr->sat_addr) == NULL) return (-ENETUNREACH); sk->protinfo.af_at.dest_port = addr->sat_port; sk->protinfo.af_at.dest_net = addr->sat_addr.s_net; sk->protinfo.af_at.dest_node = addr->sat_addr.s_node; sock->state = SS_CONNECTED; sk->state = TCP_ESTABLISHED; return (0); } /* * Not relevant */ static int atalk_accept(struct socket *sock, struct socket *newsock, int flags) { if(newsock->sk) { sk_free(newsock->sk); MOD_DEC_USE_COUNT; } return (-EOPNOTSUPP); } /* * Find the name of an appletalk socket. Just copy the right * fields into the sockaddr. */ static int atalk_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer) { struct sockaddr_at sat; struct sock *sk; sk = sock->sk; if(sk->zapped) { if(atalk_autobind(sk) < 0) return (-ENOBUFS); } *uaddr_len = sizeof(struct sockaddr_at); if(peer) { if(sk->state != TCP_ESTABLISHED) return (-ENOTCONN); sat.sat_addr.s_net = sk->protinfo.af_at.dest_net; sat.sat_addr.s_node = sk->protinfo.af_at.dest_node; sat.sat_port = sk->protinfo.af_at.dest_port; } else { sat.sat_addr.s_net = sk->protinfo.af_at.src_net; sat.sat_addr.s_node = sk->protinfo.af_at.src_node; sat.sat_port = sk->protinfo.af_at.src_port; } sat.sat_family = AF_APPLETALK; memcpy(uaddr, &sat, sizeof(sat)); return (0); } /* * Receive a packet (in skb) from device dev. This has come from the SNAP * decoder, and on entry skb->h.raw is the DDP header, skb->len is the DDP * header, skb->len is the DDP length. The physical headers have been * extracted. PPP should probably pass frames marked as for this layer. * [ie ARPHRD_ETHERTALK] */ static int atalk_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt) { struct sock *sock; struct ddpehdr *ddp=(void *)skb->h.raw; struct atalk_iface *atif; struct sockaddr_at tosat; int origlen; /* Size check */ if(skb->len < sizeof(*ddp)) { kfree_skb(skb,FREE_READ); return (0); } /* * Fix up the length field [Ok this is horrible but otherwise * I end up with unions of bit fields and messy bit field order * compiler/endian dependencies..] * * FIXME: This is a write to a shared object. Granted it * happens to be safe BUT.. (Its safe as user space will not * run until we put it back) */ *((__u16 *)ddp) = ntohs(*((__u16 *)ddp)); /* * Trim buffer in case of stray trailing data */ origlen = skb->len; skb_trim(skb, min(skb->len, ddp->deh_len)); /* * Size check to see if ddp->deh_len was crap * (Otherwise we'll detonate most spectacularly * in the middle of recvmsg()). */ if(skb->len < sizeof(*ddp)) { kfree_skb(skb,FREE_READ); return (0); } /* * Any checksums. Note we don't do htons() on this == is assumed to be * valid for net byte orders all over the networking code... */ if(ddp->deh_sum && atalk_checksum(ddp, ddp->deh_len) != ddp->deh_sum) { /* Not a valid appletalk frame - dustbin time */ kfree_skb(skb, FREE_READ); return (0); } if(call_in_firewall(AF_APPLETALK, skb->dev, ddp, NULL,&skb)!=FW_ACCEPT) { kfree_skb(skb, FREE_READ); return (0); } /* Check the packet is aimed at us */ if(ddp->deh_dnet == 0) /* Net 0 is 'this network' */ atif = atalk_find_anynet(ddp->deh_dnode, dev); else atif = atalk_find_interface(ddp->deh_dnet, ddp->deh_dnode); /* * Not ours, so we route the packet via the correct Appletalk interface. */ if(atif == NULL) { struct atalk_route *rt; struct at_addr ta; /* * Don't route multicast, etc., packets, or packets * sent to "this network" */ if (skb->pkt_type != PACKET_HOST || ddp->deh_dnet == 0) { kfree_skb(skb, FREE_READ); return (0); } /* * Check firewall allows this routing */ if(call_fw_firewall(AF_APPLETALK, skb->dev, ddp, NULL, &skb) != FW_ACCEPT) { kfree_skb(skb, FREE_READ); return (0); } ta.s_net = ddp->deh_dnet; ta.s_node = ddp->deh_dnode; /* Route the packet */ rt = atrtr_find(&ta); if(rt == NULL || ddp->deh_hops == DDP_MAXHOPS) { kfree_skb(skb, FREE_READ); return (0); } ddp->deh_hops++; /* * Route goes through another gateway, so * set the target to the gateway instead. */ if(rt->flags & RTF_GATEWAY) { ta.s_net = rt->gateway.s_net; ta.s_node = rt->gateway.s_node; } /* Fix up skb->len field */ skb_trim(skb, min(origlen, rt->dev->hard_header_len + ddp_dl->header_length + ddp->deh_len)); /* Mend the byte order */ *((__u16 *)ddp) = ntohs(*((__u16 *)ddp)); /* * Send the buffer onwards * * Now we must always be careful. If it's come from * localtalk to ethertalk it might not fit * * Order matters here: If a packet has to be copied * to make a new headroom (rare hopefully) then it * won't need unsharing. * * Note. ddp-> becomes invalid at the realloc. */ if(skb_headroom(skb) < 22) /* 22 bytes - 12 ether, 2 len, 3 802.2 5 snap */ skb = skb_realloc_headroom(skb, 32); else skb = skb_unshare(skb, GFP_ATOMIC, FREE_READ); /* * If the buffer didn't vanish into the lack of * space bitbucket we can send it. */ if(skb) { if(aarp_send_ddp(rt->dev, skb, &ta, NULL) == -1) kfree_skb(skb, FREE_READ); } return (0); } /* * Which socket - atalk_search_socket() looks for a *full match* * of the <net,node,port> tuple. */ tosat.sat_addr.s_net = ddp->deh_dnet; tosat.sat_addr.s_node = ddp->deh_dnode; tosat.sat_port = ddp->deh_dport; sock = atalk_search_socket(&tosat, atif); if(sock == NULL) /* But not one of our sockets */ { kfree_skb(skb, FREE_READ); return (0); } #ifdef CONFIG_IPDDP /* * Check if IP-over-DDP */ if(skb->data[12] == 22) { struct device *dev; struct net_device_stats *estats; if((dev = dev_get("ipddp0")) == NULL) return (-ENODEV); estats = (struct net_device_stats *) dev->priv; skb->protocol = htons(ETH_P_IP); skb_pull(skb, 13); skb->dev = dev; skb->h.raw = skb->data; skb->nh.raw = skb->data; /* printk("passing up ipddp, 0x%02x better be 45\n",skb->data[0]); * printk("tot_len %d, skb->len %d\n", * ntohs(skb->h.iph->tot_len),skb->len); */ estats->rx_packets++; estats->rx_bytes += skb->len + 13; netif_rx(skb); /* Send the SKB up to a higher place. */ return (0); } #endif /* CONFIG_IPDDP */ /* * Queue packet (standard) */ skb->sk = sock; if(sock_queue_rcv_skb(sock, skb) < 0) { skb->sk = NULL; kfree_skb(skb, FREE_WRITE); } return (0); } /* * Receive a localtalk frame. We make some demands on the caller here. * Caller must provide enough headroom on the packet to pull the short * header and append a long one. */ static int ltalk_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt) { struct ddpehdr *ddp; struct at_addr *ap; /* * Expand any short form frames. */ if(skb->mac.raw[2] == 1) { /* * Find our address. */ ap = atalk_find_dev_addr(dev); if(ap == NULL || skb->len < sizeof(struct ddpshdr)) { kfree_skb(skb, FREE_READ); return (0); } /* * The push leaves us with a ddephdr not an shdr, and * handily the port bytes in the right place preset. */ skb_push(skb, sizeof(*ddp) - 4); ddp = (struct ddpehdr *)skb->data; /* * Now fill in the long header. */ /* * These two first. The mac overlays the new source/dest * network information so we MUST copy these before * we write the network numbers ! */ ddp->deh_dnode = skb->mac.raw[0]; /* From physical header */ ddp->deh_snode = skb->mac.raw[1]; /* From physical header */ ddp->deh_dnet = ap->s_net; /* Network number */ ddp->deh_snet = ap->s_net; ddp->deh_sum = 0; /* No checksum */ /* * Not sure about this bit... */ ddp->deh_len = skb->len; ddp->deh_hops = DDP_MAXHOPS; /* Non routable, so force a drop if we slip up later */ /* Mend the byte order */ *((__u16 *)ddp) = htons(*((__u16 *)ddp)); } skb->h.raw = skb->data; return (atalk_rcv(skb, dev, pt)); } static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm) { struct sock *sk=sock->sk; struct sockaddr_at *usat=(struct sockaddr_at *)msg->msg_name; struct sockaddr_at local_satalk, gsat; struct sk_buff *skb; struct device *dev; struct ddpehdr *ddp; int size; struct atalk_route *rt; int loopback=0; int err; int flags = msg->msg_flags; if(flags & ~MSG_DONTWAIT) return (-EINVAL); if(len > DDP_MAXSZ) return (-EMSGSIZE); if(usat) { if(sk->zapped) { if(atalk_autobind(sk) < 0) return (-EBUSY); } if(msg->msg_namelen < sizeof(*usat)) return (-EINVAL); if(usat->sat_family != AF_APPLETALK) return (-EINVAL); /* netatalk doesn't implement this check */ if(usat->sat_addr.s_node == ATADDR_BCAST && !sk->broadcast) { printk(KERN_INFO "SO_BROADCAST: Fix your netatalk as it will break before 2.2\n"); #if 0 return (-EPERM); #endif } } else { if(sk->state != TCP_ESTABLISHED) return (-ENOTCONN); usat =& local_satalk; usat->sat_family = AF_APPLETALK; usat->sat_port = sk->protinfo.af_at.dest_port; usat->sat_addr.s_node = sk->protinfo.af_at.dest_node; usat->sat_addr.s_net = sk->protinfo.af_at.dest_net; } /* Build a packet */ SOCK_DEBUG(sk, "SK %p: Got address.\n",sk); /* For headers */ size = sizeof(struct ddpehdr) + len + ddp_dl->header_length; if(usat->sat_addr.s_net != 0 || usat->sat_addr.s_node == ATADDR_ANYNODE) { rt = atrtr_find(&usat->sat_addr); if(rt == NULL) return (-ENETUNREACH); dev = rt->dev; } else { struct at_addr at_hint; at_hint.s_node = 0; at_hint.s_net = sk->protinfo.af_at.src_net; rt = atrtr_find(&at_hint); if(rt == NULL) return (-ENETUNREACH); dev = rt->dev; } SOCK_DEBUG(sk, "SK %p: Size needed %d, device %s\n", sk, size, dev->name); size += dev->hard_header_len; skb = sock_alloc_send_skb(sk, size, 0, flags&MSG_DONTWAIT, &err); if(skb == NULL) return (err); skb->sk = sk; skb_reserve(skb, ddp_dl->header_length); skb_reserve(skb, dev->hard_header_len); skb->dev = dev; SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk); ddp = (struct ddpehdr *)skb_put(skb,sizeof(struct ddpehdr)); ddp->deh_pad = 0; ddp->deh_hops = 0; ddp->deh_len = len + sizeof(*ddp); /* * Fix up the length field [Ok this is horrible but otherwise * I end up with unions of bit fields and messy bit field order * compiler/endian dependencies.. */ *((__u16 *)ddp) = ntohs(*((__u16 *)ddp)); ddp->deh_dnet = usat->sat_addr.s_net; ddp->deh_snet = sk->protinfo.af_at.src_net; ddp->deh_dnode = usat->sat_addr.s_node; ddp->deh_snode = sk->protinfo.af_at.src_node; ddp->deh_dport = usat->sat_port; ddp->deh_sport = sk->protinfo.af_at.src_port; SOCK_DEBUG(sk, "SK %p: Copy user data (%d bytes).\n", sk, len); err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); if(err) { kfree_skb(skb, FREE_WRITE); return (-EFAULT); } if(sk->no_check == 1) ddp->deh_sum = 0; else ddp->deh_sum = atalk_checksum(ddp, len + sizeof(*ddp)); if(call_out_firewall(AF_APPLETALK, skb->dev, ddp, NULL, &skb) != FW_ACCEPT) { kfree_skb(skb, FREE_WRITE); return (-EPERM); } /* * Loopback broadcast packets to non gateway targets (ie routes * to group we are in) */ if(ddp->deh_dnode == ATADDR_BCAST) { if((!(rt->flags&RTF_GATEWAY)) && (!(dev->flags&IFF_LOOPBACK))) { struct sk_buff *skb2 = skb_copy(skb, GFP_KERNEL); if(skb2) { loopback = 1; SOCK_DEBUG(sk, "SK %p: send out(copy).\n", sk); if(aarp_send_ddp(dev, skb2, &usat->sat_addr, NULL) == -1) kfree_skb(skb2, FREE_WRITE); /* else queued/sent above in the aarp queue */ } } } if((dev->flags & IFF_LOOPBACK) || loopback) { SOCK_DEBUG(sk, "SK %p: Loop back.\n", sk); /* loop back */ skb_orphan(skb); ddp_dl->datalink_header(ddp_dl, skb, dev->dev_addr); skb->mac.raw = skb->data; skb->h.raw = skb->data + ddp_dl->header_length + dev->hard_header_len; skb_pull(skb,dev->hard_header_len); skb_pull(skb,ddp_dl->header_length); atalk_rcv(skb, dev, NULL); } else { SOCK_DEBUG(sk, "SK %p: send out.\n", sk); if (rt->flags & RTF_GATEWAY) { gsat.sat_addr = rt->gateway; usat = &gsat; } if(aarp_send_ddp(dev, skb, &usat->sat_addr, NULL) == -1) kfree_skb(skb, FREE_WRITE); /* else queued/sent above in the aarp queue */ } SOCK_DEBUG(sk, "SK %p: Done write (%d).\n", sk, len); return (len); } static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm) { struct sock *sk=sock->sk; struct sockaddr_at *sat=(struct sockaddr_at *)msg->msg_name; struct ddpehdr *ddp = NULL; int copied = 0; struct sk_buff *skb; int err = 0; skb = skb_recv_datagram(sk,flags&~MSG_DONTWAIT,flags&MSG_DONTWAIT,&err); if(skb == NULL) return (err); ddp = (struct ddpehdr *)(skb->h.raw); if(sk->type == SOCK_RAW) { copied = ddp->deh_len; if(copied > size) { copied = size; msg->msg_flags |= MSG_TRUNC; } err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); } else { copied = ddp->deh_len - sizeof(*ddp); if(copied > size) { copied = size; msg->msg_flags |= MSG_TRUNC; } err = skb_copy_datagram_iovec(skb,sizeof(*ddp),msg->msg_iov,copied); } if(!err) { if(sat) { sat->sat_family = AF_APPLETALK; sat->sat_port = ddp->deh_sport; sat->sat_addr.s_node = ddp->deh_snode; sat->sat_addr.s_net = ddp->deh_snet; } msg->msg_namelen = sizeof(*sat); } skb_free_datagram(sk, skb); /* Free the datagram. */ return (err ? err : (copied)); } static int atalk_shutdown(struct socket *sk,int how) { return (-EOPNOTSUPP); } /* * Appletalk ioctl calls. */ static int atalk_ioctl(struct socket *sock,unsigned int cmd, unsigned long arg) { long amount=0; struct sock *sk=sock->sk; switch(cmd) { /* * Protocol layer */ case TIOCOUTQ: amount = sk->sndbuf - atomic_read(&sk->wmem_alloc); if(amount < 0) amount = 0; break; case TIOCINQ: { struct sk_buff *skb; /* These two are safe on a single CPU system as only user tasks fiddle here */ if((skb = skb_peek(&sk->receive_queue)) != NULL) amount = skb->len-sizeof(struct ddpehdr); break; } case SIOCGSTAMP: if(sk) { if(sk->stamp.tv_sec == 0) return -ENOENT; return (copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval)) ? -EFAULT : 0); } return (-EINVAL); /* * Routing */ case SIOCADDRT: case SIOCDELRT: if(!suser()) return -EPERM; return (atrtr_ioctl(cmd,(void *)arg)); /* * Interface */ case SIOCGIFADDR: case SIOCSIFADDR: case SIOCGIFBRDADDR: case SIOCATALKDIFADDR: case SIOCDIFADDR: return (atif_ioctl(cmd,(void *)arg)); /* * Physical layer ioctl calls */ case SIOCSIFLINK: case SIOCGIFHWADDR: case SIOCSIFHWADDR: case SIOCGIFFLAGS: case SIOCSIFFLAGS: case SIOCGIFMTU: case SIOCGIFCONF: case SIOCADDMULTI: case SIOCDELMULTI: case SIOCGIFCOUNT: case SIOCGIFINDEX: case SIOCGIFNAME: return ((dev_ioctl(cmd,(void *) arg))); case SIOCSIFMETRIC: case SIOCSIFBRDADDR: case SIOCGIFNETMASK: case SIOCSIFNETMASK: case SIOCGIFMEM: case SIOCSIFMEM: case SIOCGIFDSTADDR: case SIOCSIFDSTADDR: return (-EINVAL); default: return (-EINVAL); } return (put_user(amount, (int *)arg)); } static struct net_proto_family atalk_family_ops= { AF_APPLETALK, atalk_create }; static struct proto_ops atalk_dgram_ops= { AF_APPLETALK, sock_no_dup, atalk_release, atalk_bind, atalk_connect, sock_no_socketpair, atalk_accept, atalk_getname, datagram_poll, atalk_ioctl, sock_no_listen, atalk_shutdown, sock_no_setsockopt, sock_no_getsockopt, sock_no_fcntl, atalk_sendmsg, atalk_recvmsg }; static struct notifier_block ddp_notifier= { ddp_device_event, NULL, 0 }; struct packet_type ltalk_packet_type= { 0, NULL, ltalk_rcv, NULL, NULL }; struct packet_type ppptalk_packet_type= { 0, NULL, atalk_rcv, NULL, NULL }; static char ddp_snap_id[] = {0x08, 0x00, 0x07, 0x80, 0x9B}; /* * Export symbols for use by drivers when Appletalk is a module. */ EXPORT_SYMBOL(aarp_send_ddp); EXPORT_SYMBOL(atrtr_get_dev); EXPORT_SYMBOL(atalk_find_dev_addr); #ifdef CONFIG_PROC_FS static struct proc_dir_entry proc_appletalk= { PROC_NET_ATALK, 9, "appletalk", S_IFREG | S_IRUGO, 1, 0, 0, 0, &proc_net_inode_operations, atalk_get_info }; static struct proc_dir_entry proc_atalk_route= { PROC_NET_AT_ROUTE, 11,"atalk_route", S_IFREG | S_IRUGO, 1, 0, 0, 0, &proc_net_inode_operations, atalk_rt_get_info }; static struct proc_dir_entry proc_atalk_iface= { PROC_NET_ATIF, 11,"atalk_iface", S_IFREG | S_IRUGO, 1, 0, 0, 0, &proc_net_inode_operations, atalk_if_get_info }; #endif /* CONFIG_PROC_FS */ /* Called by proto.c on kernel start up */ __initfunc(void atalk_proto_init(struct net_proto *pro)) { (void) sock_register(&atalk_family_ops); if((ddp_dl = register_snap_client(ddp_snap_id, atalk_rcv)) == NULL) printk(KERN_CRIT "Unable to register DDP with SNAP.\n"); ltalk_packet_type.type = htons(ETH_P_LOCALTALK); dev_add_pack(<alk_packet_type); ppptalk_packet_type.type = htons(ETH_P_PPPTALK); dev_add_pack(&ppptalk_packet_type); register_netdevice_notifier(&ddp_notifier); aarp_proto_init(); #ifdef CONFIG_PROC_FS proc_net_register(&proc_appletalk); proc_net_register(&proc_atalk_route); proc_net_register(&proc_atalk_iface); #endif /* CONFIG_PROC_FS */ #ifdef CONFIG_SYSCTL atalk_register_sysctl(); #endif /* CONFIG_SYSCTL */ printk(KERN_INFO "Appletalk 0.18 for Linux NET3.037\n"); } #ifdef MODULE int init_module(void) { atalk_proto_init(NULL); return (0); } /* * Note on MOD_{INC,DEC}_USE_COUNT: * * Use counts are incremented/decremented when * sockets are created/deleted. * * Appletalk interfaces are not incremented untill atalkd is run * and are only decremented when they are downed. * * Ergo, before the appletalk module can be removed, all Appletalk * sockets be closed from user space. */ void cleanup_module(void) { #ifdef CONFIG_SYSCTL atalk_unregister_sysctl(); #endif /* CONFIG_SYSCTL */ #ifdef CONFIG_PROC_FS proc_net_unregister(PROC_NET_ATALK); proc_net_unregister(PROC_NET_AT_ROUTE); proc_net_unregister(PROC_NET_ATIF); #endif /* CONFIG_PROC_FS */ aarp_cleanup_module(); /* General aarp clean-up. */ unregister_netdevice_notifier(&ddp_notifier); dev_remove_pack(<alk_packet_type); dev_remove_pack(&ppptalk_packet_type); unregister_snap_client(ddp_snap_id); sock_unregister(atalk_family_ops.family); return; } #endif /* MODULE */ #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */ |