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