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 | /* linux/net/inet/arp.c * * Copyright (C) 1994 by Florian La Roche * * This module implements the Address Resolution Protocol ARP (RFC 826), * which is used to convert IP addresses (or in the future maybe other * high-level addresses into a low-level hardware address (like an Ethernet * address). * * FIXME: * Experiment with better retransmit timers * Clean up the timer deletions * If you create a proxy entry set your interface address to the address * and then delete it, proxies may get out of sync with reality - check this * * 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. * * * Fixes: * Alan Cox : Removed the ethernet assumptions in Florian's code * Alan Cox : Fixed some small errors in the ARP logic * Alan Cox : Allow >4K in /proc * Alan Cox : Make ARP add its own protocol entry * * Ross Martin : Rewrote arp_rcv() and arp_get_info() * Stephen Henson : Add AX25 support to arp_get_info() * Alan Cox : Drop data when a device is downed. * Alan Cox : Use init_timer(). * Alan Cox : Double lock fixes. * Martin Seine : Move the arphdr structure * to if_arp.h for compatibility. * with BSD based programs. * Andrew Tridgell : Added ARP netmask code and * re-arranged proxy handling. * Alan Cox : Changed to use notifiers. * Niibe Yutaka : Reply for this device or proxies only. * Alan Cox : Don't proxy across hardware types! * Jonathan Naylor : Added support for NET/ROM. * Mike Shaver : RFC1122 checks. * Jonathan Naylor : Only lookup the hardware address for * the correct hardware type. * Germano Caronni : Assorted subtle races. * Craig Schlenter : Don't modify permanent entry * during arp_rcv. * Russ Nelson : Tidied up a few bits. */ /* RFC1122 Status: 2.3.2.1 (ARP Cache Validation): MUST provide mechanism to flush stale cache entries (OK) SHOULD be able to configure cache timeout (NOT YET) MUST throttle ARP retransmits (OK) 2.3.2.2 (ARP Packet Queue): SHOULD save at least one packet from each "conversation" with an unresolved IP address. (OK) 950727 -- MS */ #include <linux/types.h> #include <linux/string.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/config.h> #include <linux/socket.h> #include <linux/sockios.h> #include <linux/errno.h> #include <linux/if_arp.h> #include <linux/in.h> #include <linux/mm.h> #include <asm/system.h> #include <asm/segment.h> #include <stdarg.h> #include <linux/inet.h> #include <linux/netdevice.h> #include <linux/etherdevice.h> #include <linux/trdevice.h> #include <net/ip.h> #include <net/route.h> #include <net/protocol.h> #include <net/tcp.h> #include <linux/skbuff.h> #include <net/sock.h> #include <net/arp.h> #ifdef CONFIG_AX25 #include <net/ax25.h> #ifdef CONFIG_NETROM #include <net/netrom.h> #endif #endif #include <linux/proc_fs.h> #include <linux/stat.h> /* * This structure defines the ARP mapping cache. As long as we make changes * in this structure, we keep interrupts off. But normally we can copy the * hardware address and the device pointer in a local variable and then * make any "long calls" to send a packet out. */ struct arp_table { struct arp_table *next; /* Linked entry list */ unsigned long last_used; /* For expiry */ unsigned int flags; /* Control status */ u32 ip; /* ip address of entry */ u32 mask; /* netmask - used for generalised proxy arps (tridge) */ unsigned char ha[MAX_ADDR_LEN]; /* Hardware address */ unsigned char hlen; /* Length of hardware address */ unsigned short htype; /* Type of hardware in use */ struct device *dev; /* Device the entry is tied to */ /* * The following entries are only used for unresolved hw addresses. */ struct timer_list timer; /* expire timer */ int retries; /* remaining retries */ struct sk_buff_head skb; /* list of queued packets */ }; /* * Configurable Parameters (don't touch unless you know what you are doing */ /* * If an arp request is send, ARP_RES_TIME is the timeout value until the * next request is send. */ /* RFC1122: OK. Throttles ARPing, as per 2.3.2.1. (MUST) */ /* The recommended minimum timeout is 1 second per destination. */ /* Is this a per-destination timeout? -- MS [YES AC]*/ #define ARP_RES_TIME (250*(HZ/10)) /* * The number of times an arp request is send, until the host is * considered unreachable. */ #define ARP_MAX_TRIES 3 /* * After that time, an unused entry is deleted from the arp table. */ #define ARP_TIMEOUT (600*HZ) /* * How often is the function 'arp_check_retries' called. * An entry is invalidated in the time between ARP_TIMEOUT and * (ARP_TIMEOUT+ARP_CHECK_INTERVAL). */ #define ARP_CHECK_INTERVAL (60 * HZ) enum proxy { PROXY_EXACT=0, PROXY_ANY, PROXY_NONE, }; /* Forward declarations. */ static void arp_check_expire (unsigned long); static struct arp_table *arp_lookup(u32 paddr, enum proxy proxy, unsigned short type); static struct timer_list arp_timer = { NULL, NULL, ARP_CHECK_INTERVAL, 0L, &arp_check_expire }; /* * The default arp netmask is just 255.255.255.255 which means it's * a single machine entry. Only proxy entries can have other netmasks * */ #define DEF_ARP_NETMASK (~0) /* * The size of the hash table. Must be a power of two. * Maybe we should remove hashing in the future for arp and concentrate * on Patrick Schaaf's Host-Cache-Lookup... */ #define ARP_TABLE_SIZE 16 /* The ugly +1 here is to cater for proxy entries. They are put in their own list for efficiency of lookup. If you don't want to find a proxy entry then don't look in the last entry, otherwise do */ #define FULL_ARP_TABLE_SIZE (ARP_TABLE_SIZE+1) struct arp_table *arp_tables[FULL_ARP_TABLE_SIZE] = { NULL, }; unsigned long arp_cache_stamp; /* * The last bits in the IP address are used for the cache lookup. * A special entry is used for proxy arp entries */ #define HASH(paddr) (htonl(paddr) & (ARP_TABLE_SIZE - 1)) #define PROXY_HASH ARP_TABLE_SIZE /* * Check if there are too old entries and remove them. If the ATF_PERM * flag is set, they are always left in the arp cache (permanent entry). * Note: Only fully resolved entries, which don't have any packets in * the queue, can be deleted, since ARP_TIMEOUT is much greater than * ARP_MAX_TRIES*ARP_RES_TIME. */ /* RFC1122: Looks good. Prevents stale ARP entries, as per 2.3.2.1. (MUST) */ static void arp_check_expire(unsigned long dummy) { int i; unsigned long now = jiffies; unsigned long flags; save_flags(flags); cli(); for (i = 0; i < FULL_ARP_TABLE_SIZE; i++) { struct arp_table *entry; struct arp_table **pentry = &arp_tables[i]; while ((entry = *pentry) != NULL) { if ((now - entry->last_used) > ARP_TIMEOUT && !(entry->flags & ATF_PERM)) { *pentry = entry->next; /* remove from list */ arp_cache_stamp++; del_timer(&entry->timer); /* Paranoia */ kfree_s(entry, sizeof(struct arp_table)); /* Don't have to remove packets in entry->skb. */ /* See comments above. */ } else pentry = &entry->next; /* go to next entry */ } } restore_flags(flags); /* * Set the timer again. */ del_timer(&arp_timer); arp_timer.expires = jiffies + ARP_CHECK_INTERVAL; add_timer(&arp_timer); } /* * Release all linked skb's and the memory for this entry. */ static void arp_release_entry(struct arp_table *entry) { struct sk_buff *skb; unsigned long flags; save_flags(flags); cli(); /* Release the list of `skb' pointers. */ while ((skb = skb_dequeue(&entry->skb)) != NULL) { skb_device_lock(skb); restore_flags(flags); dev_kfree_skb(skb, FREE_WRITE); cli(); } restore_flags(flags); del_timer(&entry->timer); kfree_s(entry, sizeof(struct arp_table)); return; } /* * Purge a device from the ARP queue */ int arp_device_event(struct notifier_block *this, unsigned long event, void *ptr) { struct device *dev=ptr; int i; unsigned long flags; if(event!=NETDEV_DOWN) return NOTIFY_DONE; /* * This is a bit OTT - maybe we need some arp semaphores instead. */ save_flags(flags); cli(); for (i = 0; i < FULL_ARP_TABLE_SIZE; i++) { struct arp_table *entry; struct arp_table **pentry = &arp_tables[i]; while ((entry = *pentry) != NULL) { if(entry->dev==dev) { *pentry = entry->next; /* remove from list */ del_timer(&entry->timer); /* Paranoia */ kfree_s(entry, sizeof(struct arp_table)); } else pentry = &entry->next; /* go to next entry */ } } arp_cache_stamp++; restore_flags(flags); return NOTIFY_DONE; } /* * Create and send an arp packet. If (dest_hw == NULL), we create a broadcast * message. */ void arp_send(int type, int ptype, u32 dest_ip, struct device *dev, u32 src_ip, unsigned char *dest_hw, unsigned char *src_hw) { struct sk_buff *skb; struct arphdr *arp; unsigned char *arp_ptr; /* * No arp on this interface. */ if(dev->flags&IFF_NOARP) return; /* * Allocate a buffer */ skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4) + dev->hard_header_len, GFP_ATOMIC); if (skb == NULL) { printk("ARP: no memory to send an arp packet\n"); return; } skb_reserve(skb, dev->hard_header_len); arp = (struct arphdr *) skb_put(skb,sizeof(struct arphdr) + 2*(dev->addr_len+4)); skb->arp = 1; skb->dev = dev; skb->free = 1; /* * Fill the device header for the ARP frame */ dev->hard_header(skb,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len); /* Fill out the arp protocol part. */ arp->ar_hrd = htons(dev->type); #ifdef CONFIG_AX25 #ifdef CONFIG_NETROM arp->ar_pro = (dev->type == ARPHRD_AX25 || dev->type == ARPHRD_NETROM) ? htons(AX25_P_IP) : htons(ETH_P_IP); #else arp->ar_pro = (dev->type != ARPHRD_AX25)? htons(ETH_P_IP) : htons(AX25_P_IP); #endif #else arp->ar_pro = htons(ETH_P_IP); #endif arp->ar_hln = dev->addr_len; arp->ar_pln = 4; arp->ar_op = htons(type); arp_ptr=(unsigned char *)(arp+1); memcpy(arp_ptr, src_hw, dev->addr_len); arp_ptr+=dev->addr_len; memcpy(arp_ptr, &src_ip,4); arp_ptr+=4; if (dest_hw != NULL) memcpy(arp_ptr, dest_hw, dev->addr_len); else memset(arp_ptr, 0, dev->addr_len); arp_ptr+=dev->addr_len; memcpy(arp_ptr, &dest_ip, 4); dev_queue_xmit(skb, dev, 0); } /* * This function is called, if an entry is not resolved in ARP_RES_TIME. * Either resend a request, or give it up and free the entry. */ static void arp_expire_request (unsigned long arg) { struct arp_table *entry = (struct arp_table *) arg; struct arp_table **pentry; unsigned long hash; unsigned long flags; save_flags(flags); cli(); /* * Since all timeouts are handled with interrupts enabled, there is a * small chance, that this entry has just been resolved by an incoming * packet. This is the only race condition, but it is handled... */ if (entry->flags & ATF_COM) { restore_flags(flags); return; } if (--entry->retries > 0) { u32 ip = entry->ip; struct device *dev = entry->dev; /* Set new timer. */ del_timer(&entry->timer); entry->timer.expires = jiffies + ARP_RES_TIME; add_timer(&entry->timer); restore_flags(flags); arp_send(ARPOP_REQUEST, ETH_P_ARP, ip, dev, dev->pa_addr, NULL, dev->dev_addr); return; } /* * Arp request timed out. Delete entry and all waiting packets. * If we give each entry a pointer to itself, we don't have to * loop through everything again. Maybe hash is good enough, but * I will look at it later. */ hash = HASH(entry->ip); /* proxy entries shouldn't really time out so this is really only here for completeness */ /* RFC1122: They *can* be timed out, according to 2.3.2.1. */ /* They recommend a minute. -- MS */ /* The world doesn't work this way -- AC */ if (entry->flags & ATF_PUBL) pentry = &arp_tables[PROXY_HASH]; else pentry = &arp_tables[hash]; while (*pentry != NULL) { if (*pentry == entry) { *pentry = entry->next; /* delete from linked list */ del_timer(&entry->timer); restore_flags(flags); arp_release_entry(entry); arp_cache_stamp++; return; } pentry = &(*pentry)->next; } restore_flags(flags); printk("Possible ARP queue corruption.\n"); /* * We should never arrive here. */ /* Should we perhaps flush the ARP table (except the ones we're */ /* publishing, if we can trust the queue that much) at this */ /* point? -- MS */ } /* * This will try to retransmit everything on the queue. */ static void arp_send_q(struct arp_table *entry, unsigned char *hw_dest) { struct sk_buff *skb; unsigned long flags; /* * Empty the entire queue, building its data up ready to send */ if(!(entry->flags&ATF_COM)) { printk("arp_send_q: incomplete entry for %s\n", in_ntoa(entry->ip)); /* Can't flush the skb, because RFC1122 says to hang on to */ /* at least one from any unresolved entry. --MS */ /* Whats happened is that someone has 'unresolved' the entry as we got to use it - this 'can't happen' -- AC */ return; } save_flags(flags); cli(); while((skb = skb_dequeue(&entry->skb)) != NULL) { IS_SKB(skb); skb_device_lock(skb); restore_flags(flags); if(!skb->dev->rebuild_header(skb->data,skb->dev,skb->raddr,skb)) { skb->arp = 1; if(skb->sk==NULL) dev_queue_xmit(skb, skb->dev, 0); else dev_queue_xmit(skb,skb->dev,skb->sk->priority); } } restore_flags(flags); } /* * Delete an ARP mapping entry in the cache. */ void arp_destroy(u32 ip_addr, int force) { int checked_proxies = 0; struct arp_table *entry; struct arp_table **pentry; unsigned long hash = HASH(ip_addr); ugly: cli(); pentry = &arp_tables[hash]; if (! *pentry) /* also check proxy entries */ pentry = &arp_tables[PROXY_HASH]; while ((entry = *pentry) != NULL) { if (entry->ip == ip_addr) { if ((entry->flags & ATF_PERM) && !force) { sti(); return; } *pentry = entry->next; del_timer(&entry->timer); sti(); arp_release_entry(entry); /* this would have to be cleaned up */ goto ugly; /* perhaps like this ? cli(); entry = *pentry; */ } pentry = &entry->next; if (!checked_proxies && ! *pentry) { /* ugly. we have to make sure we check proxy entries as well */ checked_proxies = 1; pentry = &arp_tables[PROXY_HASH]; } } sti(); } /* * Receive an arp request by the device layer. Maybe I rewrite it, to * use the incoming packet for the reply. The time for the current * "overhead" isn't that high... */ int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt) { /* * We shouldn't use this type conversion. Check later. */ struct arphdr *arp = (struct arphdr *)skb->h.raw; unsigned char *arp_ptr= (unsigned char *)(arp+1); struct arp_table *entry; struct arp_table *proxy_entry; int hlen,htype; unsigned long hash; unsigned char ha[MAX_ADDR_LEN]; /* So we can enable ints again. */ unsigned char *sha,*tha; u32 sip,tip; /* * The hardware length of the packet should match the hardware length * of the device. Similarly, the hardware types should match. The * device should be ARP-able. Also, if pln is not 4, then the lookup * is not from an IP number. We can't currently handle this, so toss * it. */ if (arp->ar_hln != dev->addr_len || dev->type != ntohs(arp->ar_hrd) || dev->flags & IFF_NOARP || arp->ar_pln != 4) { kfree_skb(skb, FREE_READ); return 0; /* Should this be an error/printk? Seems like something */ /* you'd want to know about. Unless it's just !IFF_NOARP. -- MS */ } /* * Another test. * The logic here is that the protocol being looked up by arp should * match the protocol the device speaks. If it doesn't, there is a * problem, so toss the packet. */ /* Again, should this be an error/printk? -- MS */ switch(dev->type) { #ifdef CONFIG_AX25 case ARPHRD_AX25: if(arp->ar_pro != htons(AX25_P_IP)) { kfree_skb(skb, FREE_READ); return 0; } break; #endif #ifdef CONFIG_NETROM case ARPHRD_NETROM: if(arp->ar_pro != htons(AX25_P_IP)) { kfree_skb(skb, FREE_READ); return 0; } break; #endif case ARPHRD_ETHER: case ARPHRD_ARCNET: if(arp->ar_pro != htons(ETH_P_IP)) { kfree_skb(skb, FREE_READ); return 0; } break; case ARPHRD_IEEE802: if(arp->ar_pro != htons(ETH_P_IP)) { kfree_skb(skb, FREE_READ); return 0; } break; default: printk("ARP: dev->type mangled!\n"); kfree_skb(skb, FREE_READ); return 0; } /* * Extract fields */ hlen = dev->addr_len; htype = dev->type; sha=arp_ptr; arp_ptr+=hlen; memcpy(&sip,arp_ptr,4); arp_ptr+=4; tha=arp_ptr; arp_ptr+=hlen; memcpy(&tip,arp_ptr,4); /* * Check for bad requests for 127.x.x.x and requests for multicast * addresses. If this is one such, delete it. */ if (LOOPBACK(tip) || MULTICAST(tip)) { kfree_skb(skb, FREE_READ); return 0; } /* * Process entry. The idea here is we want to send a reply if it is a * request for us or if it is a request for someone else that we hold * a proxy for. We want to add an entry to our cache if it is a reply * to us or if it is a request for our address. * (The assumption for this last is that if someone is requesting our * address, they are probably intending to talk to us, so it saves time * if we cache their address. Their address is also probably not in * our cache, since ours is not in their cache.) * * Putting this another way, we only care about replies if they are to * us, in which case we add them to the cache. For requests, we care * about those for us and those for our proxies. We reply to both, * and in the case of requests for us we add the requester to the arp * cache. */ if(arp->ar_op == htons(ARPOP_REQUEST)) { /* * Only reply for the real device address or when it's in our proxy tables */ if(tip!=dev->pa_addr) { /* * To get in here, it is a request for someone else. We need to * check if that someone else is one of our proxies. If it isn't, * we can toss it. */ cli(); for(proxy_entry=arp_tables[PROXY_HASH]; proxy_entry; proxy_entry = proxy_entry->next) { /* we will respond to a proxy arp request if the masked arp table ip matches the masked tip. This allows a single proxy arp table entry to be used on a gateway machine to handle all requests for a whole network, rather than having to use a huge number of proxy arp entries and having to keep them uptodate. */ if (proxy_entry->dev != dev && proxy_entry->htype == htype && !((proxy_entry->ip^tip)&proxy_entry->mask)) break; } if (proxy_entry) { memcpy(ha, proxy_entry->ha, hlen); sti(); arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,ha); kfree_skb(skb, FREE_READ); return 0; } else { sti(); kfree_skb(skb, FREE_READ); return 0; } } else { /* * To get here, it must be an arp request for us. We need to reply. */ arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr); } } /* * It is now an arp reply. */ if(ip_chk_addr(tip)!=IS_MYADDR) { /* * Replies to other machines get tossed. */ kfree_skb(skb, FREE_READ); return 0; } /* * Now all replies are handled. Next, anything that falls through to here * needs to be added to the arp cache, or have its entry updated if it is * there. */ hash = HASH(sip); cli(); for(entry=arp_tables[hash];entry;entry=entry->next) if(entry->ip==sip && entry->htype==htype) break; if(entry) { /* * Entry found; update it only if it is not a permanent entry. */ if (!(entry->flags & ATF_PERM)) { memcpy(entry->ha, sha, hlen); entry->hlen = hlen; entry->last_used = jiffies; } if (!(entry->flags & ATF_COM)) { /* * This entry was incomplete. Delete the retransmit timer * and switch to complete status. */ del_timer(&entry->timer); entry->flags |= ATF_COM; sti(); /* * Send out waiting packets. We might have problems, if someone is * manually removing entries right now -- entry might become invalid * underneath us. */ arp_send_q(entry, sha); } else { sti(); } } else { /* * No entry found. Need to add a new entry to the arp table. */ entry = (struct arp_table *)kmalloc(sizeof(struct arp_table),GFP_ATOMIC); if(entry == NULL) { sti(); printk("ARP: no memory for new arp entry\n"); kfree_skb(skb, FREE_READ); return 0; } entry->mask = DEF_ARP_NETMASK; entry->ip = sip; entry->hlen = hlen; entry->htype = htype; entry->flags = ATF_COM; init_timer(&entry->timer); memcpy(entry->ha, sha, hlen); entry->last_used = jiffies; entry->dev = skb->dev; skb_queue_head_init(&entry->skb); entry->next = arp_tables[hash]; arp_tables[hash] = entry; sti(); } /* * Replies have been sent, and entries have been added. All done. */ kfree_skb(skb, FREE_READ); return 0; } /* * Find an arp mapping in the cache. If not found, return false. */ int arp_query(unsigned char *haddr, u32 paddr, unsigned short type) { struct arp_table *entry; unsigned long hash = HASH(paddr); /* * Find an entry */ cli(); for (entry = arp_tables[hash]; entry != NULL; entry = entry->next) if (entry->ip == paddr && entry->htype == type) break; if (entry != NULL) { /* * Update the record */ entry->last_used = jiffies; memcpy(haddr, entry->ha, entry->hlen); sti(); return 1; } sti(); return 0; } /* * Find an arp mapping in the cache. If not found, post a request. */ int arp_find(unsigned char *haddr, u32 paddr, struct device *dev, u32 saddr, struct sk_buff *skb) { struct arp_table *entry; unsigned long hash; #ifdef CONFIG_IP_MULTICAST u32 taddr; #endif switch (ip_chk_addr(paddr)) { case IS_MYADDR: printk("ARP: arp called for own IP address\n"); memcpy(haddr, dev->dev_addr, dev->addr_len); skb->arp = 1; return 0; #ifdef CONFIG_IP_MULTICAST case IS_MULTICAST: if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802) { /* What exactly does this do? -- MS */ haddr[0]=0x01; haddr[1]=0x00; haddr[2]=0x5e; taddr=ntohl(paddr); haddr[5]=taddr&0xff; taddr=taddr>>8; haddr[4]=taddr&0xff; taddr=taddr>>8; haddr[3]=taddr&0x7f; return 0; } /* * If a device does not support multicast broadcast the stuff (eg AX.25 for now) */ #endif case IS_BROADCAST: memcpy(haddr, dev->broadcast, dev->addr_len); skb->arp = 1; return 0; } hash = HASH(paddr); cli(); /* * Find an entry */ entry = arp_lookup(paddr, PROXY_NONE, dev->type); if (entry != NULL) /* It exists */ { if (!(entry->flags & ATF_COM)) { /* * A request was already send, but no reply yet. Thus * queue the packet with the previous attempt */ if (skb != NULL) { skb_queue_tail(&entry->skb, skb); skb_device_unlock(skb); } sti(); return 1; } /* * Update the record */ entry->last_used = jiffies; memcpy(haddr, entry->ha, dev->addr_len); if (skb) skb->arp = 1; sti(); return 0; } /* * Create a new unresolved entry. */ entry = (struct arp_table *) kmalloc(sizeof(struct arp_table), GFP_ATOMIC); if (entry != NULL) { entry->next = arp_tables[hash]; entry->last_used = jiffies; entry->flags = 0; entry->ip = paddr; entry->mask = DEF_ARP_NETMASK; memset(entry->ha, 0, dev->addr_len); entry->hlen = dev->addr_len; entry->htype = dev->type; entry->dev = dev; init_timer(&entry->timer); entry->timer.function = arp_expire_request; entry->timer.data = (unsigned long)entry; entry->timer.expires = jiffies + ARP_RES_TIME; arp_tables[hash] = entry; add_timer(&entry->timer); entry->retries = ARP_MAX_TRIES; skb_queue_head_init(&entry->skb); if (skb != NULL) { skb_queue_tail(&entry->skb, skb); skb_device_unlock(skb); } } else { if (skb != NULL && skb->free) kfree_skb(skb, FREE_WRITE); } sti(); /* * If we didn't find an entry, we will try to send an ARP packet. */ arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL, dev->dev_addr); return 1; } /* * Write the contents of the ARP cache to a PROCfs file. */ #define HBUFFERLEN 30 int arp_get_info(char *buffer, char **start, off_t offset, int length, int dummy) { int len=0; off_t begin=0; off_t pos=0; int size; struct arp_table *entry; char hbuffer[HBUFFERLEN]; int i,j,k; const char hexbuf[] = "0123456789ABCDEF"; size = sprintf(buffer,"IP address HW type Flags HW address Mask\n"); pos+=size; len+=size; cli(); for(i=0; i<FULL_ARP_TABLE_SIZE; i++) { for(entry=arp_tables[i]; entry!=NULL; entry=entry->next) { /* * Convert hardware address to XX:XX:XX:XX ... form. */ #ifdef CONFIG_AX25 #ifdef CONFIG_NETROM if (entry->htype == ARPHRD_AX25 || entry->htype == ARPHRD_NETROM) strcpy(hbuffer,ax2asc((ax25_address *)entry->ha)); else { #else if(entry->htype==ARPHRD_AX25) strcpy(hbuffer,ax2asc((ax25_address *)entry->ha)); else { #endif #endif for(k=0,j=0;k<HBUFFERLEN-3 && j<entry->hlen;j++) { hbuffer[k++]=hexbuf[ (entry->ha[j]>>4)&15 ]; hbuffer[k++]=hexbuf[ entry->ha[j]&15 ]; hbuffer[k++]=':'; } hbuffer[--k]=0; #ifdef CONFIG_AX25 } #endif size = sprintf(buffer+len, "%-17s0x%-10x0x%-10x%s", in_ntoa(entry->ip), (unsigned int)entry->htype, entry->flags, hbuffer); size += sprintf(buffer+len+size, " %-17s\n", entry->mask==DEF_ARP_NETMASK? "*":in_ntoa(entry->mask)); len+=size; pos=begin+len; if(pos<offset) { len=0; begin=pos; } if(pos>offset+length) break; } } sti(); *start=buffer+(offset-begin); /* Start of wanted data */ len-=(offset-begin); /* Start slop */ if(len>length) len=length; /* Ending slop */ return len; } /* * This will find an entry in the ARP table by looking at the IP address. * If proxy is PROXY_EXACT then only exact IP matches will be allowed * for proxy entries, otherwise the netmask will be used */ static struct arp_table *arp_lookup(u32 paddr, enum proxy proxy, unsigned short type) { struct arp_table *entry; unsigned long hash = HASH(paddr); for (entry = arp_tables[hash]; entry != NULL; entry = entry->next) if (entry->ip == paddr && entry->htype == type) break; /* it's possibly a proxy entry (with a netmask) */ if (!entry && proxy != PROXY_NONE) for (entry=arp_tables[PROXY_HASH]; entry != NULL; entry = entry->next) if ((proxy==PROXY_EXACT) ? (entry->ip==paddr) : !((entry->ip^paddr)&entry->mask)) break; return entry; } int arp_find_cache(unsigned char *dp, u32 daddr, struct device *dev) { /* * We need the broadcast/multicast awareness here and the find routine split up. */ struct arp_table *entry; #ifdef CONFIG_IP_MULTICAST u32 taddr; #endif switch (ip_chk_addr(daddr)) { case IS_MYADDR: printk("ARP: arp called for own IP address\n"); memcpy(dp, dev->dev_addr, dev->addr_len); return 1; #ifdef CONFIG_IP_MULTICAST case IS_MULTICAST: if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802) { dp[0]=0x01; dp[1]=0x00; dp[2]=0x5e; taddr=ntohl(daddr); dp[5]=taddr&0xff; taddr=taddr>>8; dp[4]=taddr&0xff; taddr=taddr>>8; dp[3]=taddr&0x7f; return 1; } /* * If a device does not support multicast broadcast the stuff (eg AX.25 for now) */ #endif case IS_BROADCAST: memcpy(dp, dev->broadcast, dev->addr_len); return 1; default: entry=arp_lookup(daddr, PROXY_NONE, dev->type); if(entry) { memcpy(dp,entry->ha, ETH_ALEN); return 1; } } return 0; } /* * Set (create) an ARP cache entry. */ static int arp_req_set(struct arpreq *req) { struct arpreq r; struct arp_table *entry; struct sockaddr_in *si; int htype, hlen; struct rtable *rt; u32 ip; memcpy_fromfs(&r, req, sizeof(r)); /* We only understand about IP addresses... */ if (r.arp_pa.sa_family != AF_INET) return -EPFNOSUPPORT; /* * Find out about the hardware type. * We have to be compatible with BSD UNIX, so we have to * assume that a "not set" value (i.e. 0) means Ethernet. */ switch (r.arp_ha.sa_family) { case ARPHRD_ETHER: htype = ARPHRD_ETHER; hlen = ETH_ALEN; break; case ARPHRD_ARCNET: htype = ARPHRD_ARCNET; hlen = 1; /* length of arcnet addresses */ break; #ifdef CONFIG_AX25 case ARPHRD_AX25: htype = ARPHRD_AX25; hlen = AX25_ADDR_LEN; break; #endif #ifdef CONFIG_NETROM case ARPHRD_NETROM: htype = ARPHRD_NETROM; hlen = AX25_ADDR_LEN; break; #endif case ARPHRD_IEEE802: htype = ARPHRD_IEEE802; hlen = TR_ALEN; break; default: return -EPFNOSUPPORT; } si = (struct sockaddr_in *) &r.arp_pa; ip = si->sin_addr.s_addr; if (ip == 0) { printk("ARP: SETARP: requested PA is 0.0.0.0 !\n"); return -EINVAL; } /* * Is it reachable directly ? */ rt = ip_rt_route(ip, NULL, NULL); if (rt == NULL) return -ENETUNREACH; /* * Is there an existing entry for this address? */ cli(); /* * Find the entry */ entry = arp_lookup(ip, PROXY_EXACT, htype); if (entry && (entry->flags & ATF_PUBL) != (r.arp_flags & ATF_PUBL)) { sti(); arp_destroy(ip,1); cli(); entry = NULL; } /* * Do we need to create a new entry */ if (entry == NULL) { unsigned long hash = HASH(ip); if (r.arp_flags & ATF_PUBL) hash = PROXY_HASH; entry = (struct arp_table *) kmalloc(sizeof(struct arp_table), GFP_ATOMIC); if (entry == NULL) { sti(); return -ENOMEM; } entry->ip = ip; entry->hlen = hlen; entry->htype = htype; init_timer(&entry->timer); entry->next = arp_tables[hash]; arp_tables[hash] = entry; skb_queue_head_init(&entry->skb); } /* * We now have a pointer to an ARP entry. Update it! */ memcpy(&entry->ha, &r.arp_ha.sa_data, hlen); entry->last_used = jiffies; entry->flags = r.arp_flags | ATF_COM; if ((entry->flags & ATF_PUBL) && (entry->flags & ATF_NETMASK)) { si = (struct sockaddr_in *) &r.arp_netmask; entry->mask = si->sin_addr.s_addr; } else entry->mask = DEF_ARP_NETMASK; entry->dev = rt->rt_dev; arp_cache_stamp++; sti(); return 0; } /* * Get an ARP cache entry. */ static int arp_req_get(struct arpreq *req) { struct arpreq r; struct arp_table *entry; struct sockaddr_in *si; /* * We only understand about IP addresses... */ memcpy_fromfs(&r, req, sizeof(r)); if (r.arp_pa.sa_family != AF_INET) return -EPFNOSUPPORT; /* * Is there an existing entry for this address? */ si = (struct sockaddr_in *) &r.arp_pa; cli(); entry = arp_lookup(si->sin_addr.s_addr, PROXY_ANY, r.arp_ha.sa_family); if (entry == NULL) { sti(); return -ENXIO; } /* * We found it; copy into structure. */ memcpy(r.arp_ha.sa_data, &entry->ha, entry->hlen); r.arp_ha.sa_family = entry->htype; r.arp_flags = entry->flags; sti(); /* * Copy the information back */ memcpy_tofs(req, &r, sizeof(r)); return 0; } /* * Handle an ARP layer I/O control request. */ int arp_ioctl(unsigned int cmd, void *arg) { struct arpreq r; struct sockaddr_in *si; int err; switch(cmd) { case SIOCDARP: if (!suser()) return -EPERM; err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq)); if(err) return err; memcpy_fromfs(&r, arg, sizeof(r)); if (r.arp_pa.sa_family != AF_INET) return -EPFNOSUPPORT; si = (struct sockaddr_in *) &r.arp_pa; arp_destroy(si->sin_addr.s_addr, 1); return 0; case SIOCGARP: err = verify_area(VERIFY_WRITE, arg, sizeof(struct arpreq)); if(err) return err; return arp_req_get((struct arpreq *)arg); case SIOCSARP: if (!suser()) return -EPERM; err = verify_area(VERIFY_READ, arg, sizeof(struct arpreq)); if(err) return err; return arp_req_set((struct arpreq *)arg); default: return -EINVAL; } /*NOTREACHED*/ return 0; } /* * Called once on startup. */ static struct packet_type arp_packet_type = { 0, /* Should be: __constant_htons(ETH_P_ARP) - but this _doesn't_ come out constant! */ NULL, /* All devices */ arp_rcv, NULL, NULL }; static struct notifier_block arp_dev_notifier={ arp_device_event, NULL, 0 }; void arp_init (void) { /* Register the packet type */ arp_packet_type.type=htons(ETH_P_ARP); dev_add_pack(&arp_packet_type); /* Start with the regular checks for expired arp entries. */ add_timer(&arp_timer); /* Register for device down reports */ register_netdevice_notifier(&arp_dev_notifier); proc_net_register(&(struct proc_dir_entry) { PROC_NET_ARP, 3, "arp", S_IFREG | S_IRUGO, 1, 0, 0, 0, &proc_net_inode_operations, arp_get_info }); } |