rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Chris Anderson |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include "minip-internal.h" |
| 25 | |
| 26 | #include <list.h> |
| 27 | #include <string.h> |
| 28 | #include <malloc.h> |
| 29 | #include <stdio.h> |
| 30 | #include <kernel/thread.h> |
| 31 | #include <kernel/mutex.h> |
| 32 | #include <trace.h> |
| 33 | |
| 34 | typedef union { |
| 35 | uint32_t u; |
| 36 | uint8_t b[4]; |
| 37 | } ipv4_t; |
| 38 | |
| 39 | #define LOCAL_TRACE 0 |
| 40 | static struct list_node arp_list; |
| 41 | typedef struct { |
| 42 | struct list_node node; |
| 43 | uint32_t addr; |
| 44 | uint8_t mac[6]; |
| 45 | } arp_entry_t; |
| 46 | |
| 47 | static mutex_t arp_mutex = MUTEX_INITIAL_VALUE(arp_mutex); |
| 48 | |
| 49 | void arp_cache_init(void) |
| 50 | { |
| 51 | list_initialize(&arp_list); |
| 52 | } |
| 53 | |
| 54 | static inline void mru_update(struct list_node *entry) |
| 55 | { |
| 56 | if (arp_list.next == entry) |
| 57 | return; |
| 58 | |
| 59 | list_delete(entry); |
| 60 | list_add_head(&arp_list, entry); |
| 61 | } |
| 62 | |
| 63 | void arp_cache_update(uint32_t addr, const uint8_t mac[6]) |
| 64 | { |
| 65 | arp_entry_t *arp; |
| 66 | ipv4_t ip; |
| 67 | bool found = false; |
| 68 | |
| 69 | ip.u = addr; |
| 70 | |
| 71 | // Ignore 0.0.0.0 or x.x.x.255 |
| 72 | if (ip.u == 0 || ip.b[3] == 0xFF) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | /* If the entry is in the cache update the address and move |
| 77 | * it to head */ |
| 78 | mutex_acquire(&arp_mutex); |
| 79 | list_for_every_entry(&arp_list, arp, arp_entry_t, node) { |
| 80 | if (arp->addr == addr) { |
| 81 | arp->addr = addr; |
| 82 | mru_update(&arp->node); |
| 83 | found = true; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (!found) { |
| 89 | LTRACEF("Adding %u.%u.%u.%u -> %02x:%02x:%02x:%02x:%02x:%02x to cache\n", |
| 90 | ip.b[0], ip.b[1], ip.b[2], ip.b[3], |
| 91 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
| 92 | arp = malloc(sizeof(arp_entry_t)); |
| 93 | if (arp == NULL) { |
| 94 | goto err; |
| 95 | } |
| 96 | |
| 97 | arp->addr = addr; |
| 98 | memcpy(arp->mac, mac, sizeof(arp->mac)); |
| 99 | list_add_head(&arp_list, &arp->node); |
| 100 | } |
| 101 | |
| 102 | err: |
| 103 | mutex_release(&arp_mutex); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | /* Looks up and returns a MAC address based on the provided ip addr */ |
| 108 | uint8_t *arp_cache_lookup(uint32_t addr) |
| 109 | { |
| 110 | arp_entry_t *arp = NULL; |
| 111 | uint8_t *ret = NULL; |
| 112 | |
| 113 | /* If the entry is in the cache update the address and move |
| 114 | * it to head */ |
| 115 | mutex_acquire(&arp_mutex); |
| 116 | list_for_every_entry(&arp_list, arp, arp_entry_t, node) { |
| 117 | if (arp->addr == addr) { |
| 118 | mru_update(&arp->node); |
| 119 | ret = arp->mac; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | mutex_release(&arp_mutex); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | void arp_cache_dump(void) |
| 129 | { |
| 130 | int i = 0; |
| 131 | arp_entry_t *arp; |
| 132 | |
| 133 | if (!list_is_empty(&arp_list)) { |
| 134 | list_for_every_entry(&arp_list, arp, arp_entry_t, node) { |
| 135 | ipv4_t ip; |
| 136 | ip.u = arp->addr; |
| 137 | printf("%2d: %u.%u.%u.%u -> %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 138 | i++, ip.b[0], ip.b[1], ip.b[2], ip.b[3], |
| 139 | arp->mac[0], arp->mac[1], arp->mac[2], arp->mac[3], arp->mac[4], arp->mac[5]); |
| 140 | } |
| 141 | } else { |
| 142 | printf("The arp table is empty\n"); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | int arp_send_request(uint32_t addr) |
| 147 | { |
| 148 | pktbuf_t *p; |
| 149 | struct eth_hdr *eth; |
| 150 | struct arp_pkt *arp; |
| 151 | |
| 152 | if ((p = pktbuf_alloc()) == NULL) { |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | eth = pktbuf_prepend(p, sizeof(struct eth_hdr)); |
| 157 | arp = pktbuf_append(p, sizeof(struct arp_pkt)); |
| 158 | minip_build_mac_hdr(eth, bcast_mac, ETH_TYPE_ARP); |
| 159 | |
| 160 | arp->htype = htons(0x0001); |
| 161 | arp->ptype = htons(0x0800); |
| 162 | arp->hlen = 6; |
| 163 | arp->plen = 4; |
| 164 | arp->oper = htons(ARP_OPER_REQUEST); |
| 165 | arp->spa = minip_get_ipaddr(); |
| 166 | arp->tpa = addr; |
| 167 | minip_get_macaddr(arp->sha); |
| 168 | mac_addr_copy(arp->tha, bcast_mac); |
| 169 | |
| 170 | minip_tx_handler(p); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void handle_arp_timeout_cb(void *arg) { |
| 175 | *(bool *)arg = true; |
| 176 | } |
| 177 | |
| 178 | const uint8_t *arp_get_dest_mac(uint32_t host) |
| 179 | { |
| 180 | const uint8_t *dst_mac = NULL; |
| 181 | bool arp_timeout = false; |
| 182 | net_timer_t arp_timeout_timer; |
| 183 | |
| 184 | if (host == IPV4_BCAST) { |
| 185 | return bcast_mac; |
| 186 | } |
| 187 | |
| 188 | dst_mac = arp_cache_lookup(host); |
| 189 | if (dst_mac == NULL) { |
| 190 | arp_send_request(host); |
| 191 | memset(&arp_timeout_timer, 0, sizeof(arp_timeout_timer)); |
| 192 | net_timer_set(&arp_timeout_timer, handle_arp_timeout_cb, &arp_timeout, 100); |
| 193 | while (!arp_timeout) { |
| 194 | dst_mac = arp_cache_lookup(host); |
| 195 | if (dst_mac) { |
| 196 | net_timer_cancel(&arp_timeout_timer); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return dst_mac; |
| 203 | } |
| 204 | |
| 205 | // vim: set ts=4 sw=4 expandtab: |
| 206 | |