1 /* 2 * Copyright (c) 2010 Advanced Computing Technologies 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 * $FreeBSD$ 44 */ 45 46 #include <unistd.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <sys/types.h> 51 #include <sys/sysctl.h> 52 #include <sys/socket.h> 53 #include <netinet/in.h> 54 #include <arpa/inet.h> 55 #include <net/if_arp.h> 56 57 #define MSG_SIZE 1024 58 #define PORT 6969 59 60 int 61 main(int argc, char **argv) 62 { 63 64 int sock; 65 int maxhold; 66 int wait; 67 size_t size = sizeof(maxhold); 68 struct sockaddr_in dest; 69 char message[MSG_SIZE]; 70 struct arpstat arpstat; 71 size_t len = sizeof(arpstat); 72 unsigned long dropped = 0; 73 74 memset(&message, 1, sizeof(message)); 75 76 if (sysctlbyname("net.link.ether.inet.maxhold", &maxhold, &size, 77 NULL, 0) < 0) { 78 perror("not ok 1 - sysctlbyname failed"); 79 exit(1); 80 } 81 82 #ifdef DEBUG 83 printf("maxhold is %d\n", maxhold); 84 #endif /* DEBUG */ 85 86 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 87 perror("not ok 1 - could not open socket"); 88 exit(1); 89 } 90 91 bzero(&dest, sizeof(dest)); 92 if (inet_pton(AF_INET, argv[1], &dest.sin_addr.s_addr) != 1) { 93 perror("not ok 1 - could not parse address"); 94 exit(1); 95 } 96 dest.sin_len = sizeof(dest); 97 dest.sin_family = AF_INET; 98 dest.sin_port = htons(PORT); 99 100 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 101 NULL, 0) < 0) { 102 perror("not ok 1 - could not get initial arp stats"); 103 exit(1); 104 } 105 106 dropped = arpstat.dropped; 107 #ifdef DEBUG 108 printf("dropped before %ld\n", dropped); 109 #endif /* DEBUG */ 110 111 /* 112 * Load up the queue in the ARP entry to the maximum. 113 * We should not drop any packets at this point. 114 */ 115 116 while (maxhold > 0) { 117 if (sendto(sock, message, sizeof(message), 0, 118 (struct sockaddr *)&dest, sizeof(dest)) < 0) { 119 perror("not ok 1 - could not send packet"); 120 exit(1); 121 } 122 maxhold--; 123 } 124 125 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 126 NULL, 0) < 0) { 127 perror("not ok 1 - could not get new arp stats"); 128 exit(1); 129 } 130 131 #ifdef DEBUG 132 printf("dropped after %ld\n", arpstat.dropped); 133 #endif /* DEBUG */ 134 135 if (arpstat.dropped != dropped) { 136 printf("not ok 1 - Failed, drops changed:" 137 "before %ld after %ld\n", dropped, arpstat.dropped); 138 exit(1); 139 } 140 141 dropped = arpstat.dropped; 142 143 /* Now add one extra and make sure it is dropped. */ 144 if (sendto(sock, message, sizeof(message), 0, 145 (struct sockaddr *)&dest, sizeof(dest)) < 0) { 146 perror("not ok 1 - could not send packet"); 147 exit(1); 148 } 149 150 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len, 151 NULL, 0) < 0) { 152 perror("not ok 1 - could not get new arp stats"); 153 exit(1); 154 } 155 156 if (arpstat.dropped != (dropped + 1)) { 157 printf("not ok 1 - Failed to drop one packet: before" 158 " %ld after %ld\n", dropped, arpstat.dropped); 159 exit(1); 160 } 161 162 printf("ok\n"); 163 return (0); 164 } 165