1 /* 2 * Copyright (c) 2010 Hudson River Trading LLC 3 * Written by George Neville-Neil gnn@freebsd.org 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * Description: The following is a test of the arp entry packet queues 28 * which replaced the single packet hold entry that existed in the BSDs 29 * since time immemorial. The test process is: 30 * 31 * 1) Find out the current system limit (maxhold) 32 * 2) Using an IP address for which we do not yet have an entry 33 * load up an ARP entry packet queue with exactly that many packets. 34 * 3) Check the arp dropped stat to make sure that we have not dropped 35 * any packets as yet. 36 * 4) Add one more packet to the queue. 37 * 5) Make sure that only one packet was dropped. 38 * 39 * CAVEAT: The ARP timer will flush the queue after 1 second so it is 40 * important not to run this code in a fast loop or the test will 41 * fail. 42 */ 43 44 #include <unistd.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <sys/types.h> 49 #include <sys/sysctl.h> 50 #include <sys/socket.h> 51 #include <netinet/in.h> 52 #include <arpa/inet.h> 53 #include <net/if_arp.h> 54 55 #define MSG_SIZE 1024 56 #define PORT 6969 57 58 int 59 main(int argc, char **argv) 60 { 61 62 int sock; 63 int maxhold; 64 size_t size = sizeof(maxhold); 65 struct sockaddr_in dest; 66 char message[MSG_SIZE]; 67 struct arpstat arpstat; 68 size_t len = sizeof(arpstat); 69 unsigned long dropped = 0; 70 71 memset(&message, 1, sizeof(message)); 72 73 if (sysctlbyname("net.link.ether.inet.maxhold", &maxhold, &size, 74 NULL, 0) < 0) { 75 perror("not ok 1 - sysctlbyname failed"); 76 exit(1); 77 } 78 79 #ifdef DEBUG 80 printf("maxhold is %d\n", maxhold); 81 #endif /* DEBUG */ 82 83 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 84 perror("not ok 1 - could not open socket"); 85 exit(1); 86 } 87 88 bzero(&dest, sizeof(dest)); 89 if (inet_pton(AF_INET, argv[1], &dest.sin_addr.s_addr) != 1) { 90 perror("not ok 1 - could not parse address"); 91 exit(1); 92 } 93 dest.sin_len = sizeof(dest); 94 dest.sin_family = AF_INET; 95 dest.sin_port = htons(PORT); 96 97 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 98 NULL, 0) < 0) { 99 perror("not ok 1 - could not get initial arp stats"); 100 exit(1); 101 } 102 103 dropped = arpstat.dropped; 104 #ifdef DEBUG 105 printf("dropped before %ld\n", dropped); 106 #endif /* DEBUG */ 107 108 /* 109 * Load up the queue in the ARP entry to the maximum. 110 * We should not drop any packets at this point. 111 */ 112 113 while (maxhold > 0) { 114 if (sendto(sock, message, sizeof(message), 0, 115 (struct sockaddr *)&dest, sizeof(dest)) < 0) { 116 perror("not ok 1 - could not send packet"); 117 exit(1); 118 } 119 maxhold--; 120 } 121 122 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 123 NULL, 0) < 0) { 124 perror("not ok 1 - could not get new arp stats"); 125 exit(1); 126 } 127 128 #ifdef DEBUG 129 printf("dropped after %ld\n", arpstat.dropped); 130 #endif /* DEBUG */ 131 132 if (arpstat.dropped != dropped) { 133 printf("not ok 1 - Failed, drops changed:" 134 "before %ld after %ld\n", dropped, arpstat.dropped); 135 exit(1); 136 } 137 138 dropped = arpstat.dropped; 139 140 /* Now add one extra and make sure it is dropped. */ 141 if (sendto(sock, message, sizeof(message), 0, 142 (struct sockaddr *)&dest, sizeof(dest)) < 0) { 143 perror("not ok 1 - could not send packet"); 144 exit(1); 145 } 146 147 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 148 NULL, 0) < 0) { 149 perror("not ok 1 - could not get new arp stats"); 150 exit(1); 151 } 152 153 if (arpstat.dropped != (dropped + 1)) { 154 printf("not ok 1 - Failed to drop one packet: before" 155 " %ld after %ld\n", dropped, arpstat.dropped); 156 exit(1); 157 } 158 159 printf("ok\n"); 160 return (0); 161 } 162