1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * This test program processes all ARP requests and claims every IP address it |
---|
5 | * notices. Use with care in production networks. |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
10 | * |
---|
11 | * embedded brains GmbH |
---|
12 | * Dornierstr. 4 |
---|
13 | * 82178 Puchheim |
---|
14 | * Germany |
---|
15 | * <rtems@embedded-brains.de> |
---|
16 | * |
---|
17 | * Redistribution and use in source and binary forms, with or without |
---|
18 | * modification, are permitted provided that the following conditions |
---|
19 | * are met: |
---|
20 | * 1. Redistributions of source code must retain the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer. |
---|
22 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer in the |
---|
24 | * documentation and/or other materials provided with the distribution. |
---|
25 | * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
36 | * SUCH DAMAGE. |
---|
37 | */ |
---|
38 | |
---|
39 | #include <sys/types.h> |
---|
40 | |
---|
41 | #include <net/if.h> |
---|
42 | #include <net/if_arp.h> |
---|
43 | #include <net/ethernet.h> |
---|
44 | #include <arpa/inet.h> |
---|
45 | #include <netinet/in.h> |
---|
46 | |
---|
47 | #include <assert.h> |
---|
48 | #include <ifaddrs.h> |
---|
49 | #include <stdio.h> |
---|
50 | #include <stdlib.h> |
---|
51 | #include <string.h> |
---|
52 | #include <unistd.h> |
---|
53 | |
---|
54 | #include <rtems/bsd/util.h> |
---|
55 | |
---|
56 | #define TEST_NAME "LIBBSD ARP HOLE" |
---|
57 | |
---|
58 | typedef struct { |
---|
59 | struct ether_header eh; |
---|
60 | struct arphdr arh; |
---|
61 | uint8_t sha[ETHER_ADDR_LEN]; |
---|
62 | uint32_t spa; |
---|
63 | uint8_t tha[ETHER_ADDR_LEN]; |
---|
64 | uint32_t tpa; |
---|
65 | } __packed arp_request; |
---|
66 | |
---|
67 | static void |
---|
68 | arp_processor(void *arg, int fd, const uint8_t eaddr[ETHER_ADDR_LEN], |
---|
69 | const struct arphdr *ar, uint32_t spa, uint32_t tpa, |
---|
70 | const uint8_t *sha, const uint8_t *tha) |
---|
71 | { |
---|
72 | if (ar->ar_op == htons(ARPOP_REQUEST) && spa == htonl(INADDR_ANY) && |
---|
73 | tpa != htonl(INADDR_ANY)) { |
---|
74 | arp_request arr; |
---|
75 | |
---|
76 | memcpy(&arr.eh.ether_shost[0], &eaddr[0], ETHER_HDR_LEN); |
---|
77 | memcpy(&arr.eh.ether_dhost[0], sha, ETHER_HDR_LEN); |
---|
78 | arr.eh.ether_type = htons(ETHERTYPE_ARP); |
---|
79 | |
---|
80 | arr.arh.ar_hrd = htons(ARPHRD_ETHER); |
---|
81 | arr.arh.ar_pro = htons(ETHERTYPE_IP); |
---|
82 | arr.arh.ar_hln = ETHER_ADDR_LEN; |
---|
83 | arr.arh.ar_pln = sizeof(spa); |
---|
84 | arr.arh.ar_op = htons(ARPOP_REPLY); |
---|
85 | |
---|
86 | memcpy(&arr.sha[0], &eaddr[0], ETHER_ADDR_LEN); |
---|
87 | arr.spa = tpa; |
---|
88 | memcpy(&arr.tha[0], sha, ETHER_ADDR_LEN); |
---|
89 | arr.tpa = htonl(INADDR_ANY); |
---|
90 | |
---|
91 | write(fd, &arr, sizeof(arr)); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | static void |
---|
96 | test_main(void) |
---|
97 | { |
---|
98 | char ifnamebuf[IF_NAMESIZE]; |
---|
99 | char *ifname; |
---|
100 | rtems_bsd_arp_processor_context *ctx; |
---|
101 | |
---|
102 | ifname = if_indextoname(1, &ifnamebuf[0]); |
---|
103 | assert(ifname != NULL); |
---|
104 | |
---|
105 | ctx = rtems_bsd_arp_processor_create(ifname); |
---|
106 | assert(ctx != NULL); |
---|
107 | |
---|
108 | while (1) { |
---|
109 | rtems_bsd_arp_processor_process(ctx, arp_processor, NULL); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | #define DEFAULT_NETWORK_NO_STATIC_IFCONFIG |
---|
114 | |
---|
115 | #include <rtems/bsd/test/default-network-init.h> |
---|