Webserver+3d print
cyclone_tcp/ipv6/ndp_cache.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file ndp_cache.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief Neighbor and destination cache management |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | //Switch to the appropriate trace level |
Sergunb | 0:8918a71cdbe9 | 30 | #define TRACE_LEVEL NDP_TRACE_LEVEL |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 33 | #include "core/net.h" |
Sergunb | 0:8918a71cdbe9 | 34 | #include "ipv6/icmpv6.h" |
Sergunb | 0:8918a71cdbe9 | 35 | #include "ipv6/ndp.h" |
Sergunb | 0:8918a71cdbe9 | 36 | #include "ipv6/ndp_cache.h" |
Sergunb | 0:8918a71cdbe9 | 37 | #include "ipv6/ndp_misc.h" |
Sergunb | 0:8918a71cdbe9 | 38 | #include "debug.h" |
Sergunb | 0:8918a71cdbe9 | 39 | |
Sergunb | 0:8918a71cdbe9 | 40 | //Check TCP/IP stack configuration |
Sergunb | 0:8918a71cdbe9 | 41 | #if (IPV6_SUPPORT == ENABLED && NDP_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 42 | |
Sergunb | 0:8918a71cdbe9 | 43 | |
Sergunb | 0:8918a71cdbe9 | 44 | /** |
Sergunb | 0:8918a71cdbe9 | 45 | * @brief Create a new entry in the Neighbor cache |
Sergunb | 0:8918a71cdbe9 | 46 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 47 | * @return Pointer to the newly created entry |
Sergunb | 0:8918a71cdbe9 | 48 | **/ |
Sergunb | 0:8918a71cdbe9 | 49 | |
Sergunb | 0:8918a71cdbe9 | 50 | NdpNeighborCacheEntry *ndpCreateNeighborCacheEntry(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 51 | { |
Sergunb | 0:8918a71cdbe9 | 52 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 53 | NdpNeighborCacheEntry *entry; |
Sergunb | 0:8918a71cdbe9 | 54 | NdpNeighborCacheEntry *oldestEntry; |
Sergunb | 0:8918a71cdbe9 | 55 | |
Sergunb | 0:8918a71cdbe9 | 56 | //Keep track of the oldest entry |
Sergunb | 0:8918a71cdbe9 | 57 | oldestEntry = &interface->ndpContext.neighborCache[0]; |
Sergunb | 0:8918a71cdbe9 | 58 | |
Sergunb | 0:8918a71cdbe9 | 59 | //Loop through Neighbor cache entries |
Sergunb | 0:8918a71cdbe9 | 60 | for(i = 0; i < NDP_NEIGHBOR_CACHE_SIZE; i++) |
Sergunb | 0:8918a71cdbe9 | 61 | { |
Sergunb | 0:8918a71cdbe9 | 62 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 63 | entry = &interface->ndpContext.neighborCache[i]; |
Sergunb | 0:8918a71cdbe9 | 64 | |
Sergunb | 0:8918a71cdbe9 | 65 | //Check whether the entry is currently in used or not |
Sergunb | 0:8918a71cdbe9 | 66 | if(entry->state == NDP_STATE_NONE) |
Sergunb | 0:8918a71cdbe9 | 67 | { |
Sergunb | 0:8918a71cdbe9 | 68 | //Erase contents |
Sergunb | 0:8918a71cdbe9 | 69 | memset(entry, 0, sizeof(NdpNeighborCacheEntry)); |
Sergunb | 0:8918a71cdbe9 | 70 | //Return a pointer to the Neighbor cache entry |
Sergunb | 0:8918a71cdbe9 | 71 | return entry; |
Sergunb | 0:8918a71cdbe9 | 72 | } |
Sergunb | 0:8918a71cdbe9 | 73 | |
Sergunb | 0:8918a71cdbe9 | 74 | //Keep track of the oldest entry in the table |
Sergunb | 0:8918a71cdbe9 | 75 | if(timeCompare(entry->timestamp, oldestEntry->timestamp) < 0) |
Sergunb | 0:8918a71cdbe9 | 76 | oldestEntry = entry; |
Sergunb | 0:8918a71cdbe9 | 77 | } |
Sergunb | 0:8918a71cdbe9 | 78 | |
Sergunb | 0:8918a71cdbe9 | 79 | //Drop any pending packets |
Sergunb | 0:8918a71cdbe9 | 80 | ndpFlushQueuedPackets(interface, oldestEntry); |
Sergunb | 0:8918a71cdbe9 | 81 | //The oldest entry is removed whenever the table runs out of space |
Sergunb | 0:8918a71cdbe9 | 82 | memset(oldestEntry, 0, sizeof(NdpNeighborCacheEntry)); |
Sergunb | 0:8918a71cdbe9 | 83 | |
Sergunb | 0:8918a71cdbe9 | 84 | //Return a pointer to the Neighbor cache entry |
Sergunb | 0:8918a71cdbe9 | 85 | return oldestEntry; |
Sergunb | 0:8918a71cdbe9 | 86 | } |
Sergunb | 0:8918a71cdbe9 | 87 | |
Sergunb | 0:8918a71cdbe9 | 88 | |
Sergunb | 0:8918a71cdbe9 | 89 | /** |
Sergunb | 0:8918a71cdbe9 | 90 | * @brief Search the Neighbor cache for a given IPv6 address |
Sergunb | 0:8918a71cdbe9 | 91 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 92 | * @param[in] ipAddr IPv6 address |
Sergunb | 0:8918a71cdbe9 | 93 | * @return A pointer to the matching entry is returned. NULL is returned if |
Sergunb | 0:8918a71cdbe9 | 94 | * the specified IPv6 address could not be found in the Neighbor cache |
Sergunb | 0:8918a71cdbe9 | 95 | **/ |
Sergunb | 0:8918a71cdbe9 | 96 | |
Sergunb | 0:8918a71cdbe9 | 97 | NdpNeighborCacheEntry *ndpFindNeighborCacheEntry(NetInterface *interface, const Ipv6Addr *ipAddr) |
Sergunb | 0:8918a71cdbe9 | 98 | { |
Sergunb | 0:8918a71cdbe9 | 99 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 100 | NdpNeighborCacheEntry *entry; |
Sergunb | 0:8918a71cdbe9 | 101 | |
Sergunb | 0:8918a71cdbe9 | 102 | //Loop through Neighbor cache entries |
Sergunb | 0:8918a71cdbe9 | 103 | for(i = 0; i < NDP_NEIGHBOR_CACHE_SIZE; i++) |
Sergunb | 0:8918a71cdbe9 | 104 | { |
Sergunb | 0:8918a71cdbe9 | 105 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 106 | entry = &interface->ndpContext.neighborCache[i]; |
Sergunb | 0:8918a71cdbe9 | 107 | |
Sergunb | 0:8918a71cdbe9 | 108 | //Check whether the entry is currently in used |
Sergunb | 0:8918a71cdbe9 | 109 | if(entry->state != NDP_STATE_NONE) |
Sergunb | 0:8918a71cdbe9 | 110 | { |
Sergunb | 0:8918a71cdbe9 | 111 | //Current entry matches the specified address? |
Sergunb | 0:8918a71cdbe9 | 112 | if(ipv6CompAddr(&entry->ipAddr, ipAddr)) |
Sergunb | 0:8918a71cdbe9 | 113 | return entry; |
Sergunb | 0:8918a71cdbe9 | 114 | } |
Sergunb | 0:8918a71cdbe9 | 115 | } |
Sergunb | 0:8918a71cdbe9 | 116 | |
Sergunb | 0:8918a71cdbe9 | 117 | //No matching entry in Neighbor cache... |
Sergunb | 0:8918a71cdbe9 | 118 | return NULL; |
Sergunb | 0:8918a71cdbe9 | 119 | } |
Sergunb | 0:8918a71cdbe9 | 120 | |
Sergunb | 0:8918a71cdbe9 | 121 | |
Sergunb | 0:8918a71cdbe9 | 122 | /** |
Sergunb | 0:8918a71cdbe9 | 123 | * @brief Periodically update Neighbor cache |
Sergunb | 0:8918a71cdbe9 | 124 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 125 | **/ |
Sergunb | 0:8918a71cdbe9 | 126 | |
Sergunb | 0:8918a71cdbe9 | 127 | void ndpUpdateNeighborCache(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 128 | { |
Sergunb | 0:8918a71cdbe9 | 129 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 130 | systime_t time; |
Sergunb | 0:8918a71cdbe9 | 131 | NdpNeighborCacheEntry *entry; |
Sergunb | 0:8918a71cdbe9 | 132 | |
Sergunb | 0:8918a71cdbe9 | 133 | //Get current time |
Sergunb | 0:8918a71cdbe9 | 134 | time = osGetSystemTime(); |
Sergunb | 0:8918a71cdbe9 | 135 | |
Sergunb | 0:8918a71cdbe9 | 136 | //Go through Neighbor cache |
Sergunb | 0:8918a71cdbe9 | 137 | for(i = 0; i < NDP_NEIGHBOR_CACHE_SIZE; i++) |
Sergunb | 0:8918a71cdbe9 | 138 | { |
Sergunb | 0:8918a71cdbe9 | 139 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 140 | entry = &interface->ndpContext.neighborCache[i]; |
Sergunb | 0:8918a71cdbe9 | 141 | |
Sergunb | 0:8918a71cdbe9 | 142 | //INCOMPLETE state? |
Sergunb | 0:8918a71cdbe9 | 143 | if(entry->state == NDP_STATE_INCOMPLETE) |
Sergunb | 0:8918a71cdbe9 | 144 | { |
Sergunb | 0:8918a71cdbe9 | 145 | //The Neighbor Solicitation timed out? |
Sergunb | 0:8918a71cdbe9 | 146 | if(timeCompare(time, entry->timestamp + entry->timeout) >= 0) |
Sergunb | 0:8918a71cdbe9 | 147 | { |
Sergunb | 0:8918a71cdbe9 | 148 | //Increment retransmission counter |
Sergunb | 0:8918a71cdbe9 | 149 | entry->retransmitCount++; |
Sergunb | 0:8918a71cdbe9 | 150 | |
Sergunb | 0:8918a71cdbe9 | 151 | //Check whether the maximum number of retransmissions has been exceeded |
Sergunb | 0:8918a71cdbe9 | 152 | if(entry->retransmitCount < NDP_MAX_MULTICAST_SOLICIT) |
Sergunb | 0:8918a71cdbe9 | 153 | { |
Sergunb | 0:8918a71cdbe9 | 154 | //Retransmit the multicast Neighbor Solicitation message |
Sergunb | 0:8918a71cdbe9 | 155 | ndpSendNeighborSol(interface, &entry->ipAddr, TRUE); |
Sergunb | 0:8918a71cdbe9 | 156 | |
Sergunb | 0:8918a71cdbe9 | 157 | //Save the time at which the message was sent |
Sergunb | 0:8918a71cdbe9 | 158 | entry->timestamp = time; |
Sergunb | 0:8918a71cdbe9 | 159 | //Set timeout value |
Sergunb | 0:8918a71cdbe9 | 160 | entry->timeout = interface->ndpContext.retransTimer; |
Sergunb | 0:8918a71cdbe9 | 161 | } |
Sergunb | 0:8918a71cdbe9 | 162 | else |
Sergunb | 0:8918a71cdbe9 | 163 | { |
Sergunb | 0:8918a71cdbe9 | 164 | //Drop packets that are waiting for address resolution |
Sergunb | 0:8918a71cdbe9 | 165 | ndpFlushQueuedPackets(interface, entry); |
Sergunb | 0:8918a71cdbe9 | 166 | //The entry should be deleted since address resolution has failed |
Sergunb | 0:8918a71cdbe9 | 167 | entry->state = NDP_STATE_NONE; |
Sergunb | 0:8918a71cdbe9 | 168 | } |
Sergunb | 0:8918a71cdbe9 | 169 | } |
Sergunb | 0:8918a71cdbe9 | 170 | } |
Sergunb | 0:8918a71cdbe9 | 171 | //REACHABLE state? |
Sergunb | 0:8918a71cdbe9 | 172 | else if(entry->state == NDP_STATE_REACHABLE) |
Sergunb | 0:8918a71cdbe9 | 173 | { |
Sergunb | 0:8918a71cdbe9 | 174 | //Periodically time out Neighbor cache entries |
Sergunb | 0:8918a71cdbe9 | 175 | if(timeCompare(time, entry->timestamp + entry->timeout) >= 0) |
Sergunb | 0:8918a71cdbe9 | 176 | { |
Sergunb | 0:8918a71cdbe9 | 177 | //Save current time |
Sergunb | 0:8918a71cdbe9 | 178 | entry->timestamp = osGetSystemTime(); |
Sergunb | 0:8918a71cdbe9 | 179 | //Enter STALE state |
Sergunb | 0:8918a71cdbe9 | 180 | entry->state = NDP_STATE_STALE; |
Sergunb | 0:8918a71cdbe9 | 181 | } |
Sergunb | 0:8918a71cdbe9 | 182 | } |
Sergunb | 0:8918a71cdbe9 | 183 | //STALE state? |
Sergunb | 0:8918a71cdbe9 | 184 | else if(entry->state == NDP_STATE_STALE) |
Sergunb | 0:8918a71cdbe9 | 185 | { |
Sergunb | 0:8918a71cdbe9 | 186 | //The neighbor is no longer known to be reachable but until traffic |
Sergunb | 0:8918a71cdbe9 | 187 | //is sent to the neighbor, no attempt should be made to verify its |
Sergunb | 0:8918a71cdbe9 | 188 | //reachability |
Sergunb | 0:8918a71cdbe9 | 189 | } |
Sergunb | 0:8918a71cdbe9 | 190 | //DELAY state? |
Sergunb | 0:8918a71cdbe9 | 191 | else if(entry->state == NDP_STATE_DELAY) |
Sergunb | 0:8918a71cdbe9 | 192 | { |
Sergunb | 0:8918a71cdbe9 | 193 | //Wait for the specified delay before sending the first probe |
Sergunb | 0:8918a71cdbe9 | 194 | if(timeCompare(time, entry->timestamp + entry->timeout) >= 0) |
Sergunb | 0:8918a71cdbe9 | 195 | { |
Sergunb | 0:8918a71cdbe9 | 196 | Ipv6Addr ipAddr; |
Sergunb | 0:8918a71cdbe9 | 197 | |
Sergunb | 0:8918a71cdbe9 | 198 | //Save the time at which the message was sent |
Sergunb | 0:8918a71cdbe9 | 199 | entry->timestamp = time; |
Sergunb | 0:8918a71cdbe9 | 200 | //Set timeout value |
Sergunb | 0:8918a71cdbe9 | 201 | entry->timeout = interface->ndpContext.retransTimer; |
Sergunb | 0:8918a71cdbe9 | 202 | //Switch to the PROBE state |
Sergunb | 0:8918a71cdbe9 | 203 | entry->state = NDP_STATE_PROBE; |
Sergunb | 0:8918a71cdbe9 | 204 | |
Sergunb | 0:8918a71cdbe9 | 205 | //Target address |
Sergunb | 0:8918a71cdbe9 | 206 | ipAddr = entry->ipAddr; |
Sergunb | 0:8918a71cdbe9 | 207 | |
Sergunb | 0:8918a71cdbe9 | 208 | //Send a unicast Neighbor Solicitation message |
Sergunb | 0:8918a71cdbe9 | 209 | ndpSendNeighborSol(interface, &ipAddr, FALSE); |
Sergunb | 0:8918a71cdbe9 | 210 | } |
Sergunb | 0:8918a71cdbe9 | 211 | } |
Sergunb | 0:8918a71cdbe9 | 212 | //PROBE state? |
Sergunb | 0:8918a71cdbe9 | 213 | else if(entry->state == NDP_STATE_PROBE) |
Sergunb | 0:8918a71cdbe9 | 214 | { |
Sergunb | 0:8918a71cdbe9 | 215 | //The request timed out? |
Sergunb | 0:8918a71cdbe9 | 216 | if(timeCompare(time, entry->timestamp + entry->timeout) >= 0) |
Sergunb | 0:8918a71cdbe9 | 217 | { |
Sergunb | 0:8918a71cdbe9 | 218 | //Increment retransmission counter |
Sergunb | 0:8918a71cdbe9 | 219 | entry->retransmitCount++; |
Sergunb | 0:8918a71cdbe9 | 220 | |
Sergunb | 0:8918a71cdbe9 | 221 | //Check whether the maximum number of retransmissions has been exceeded |
Sergunb | 0:8918a71cdbe9 | 222 | if(entry->retransmitCount < NDP_MAX_UNICAST_SOLICIT) |
Sergunb | 0:8918a71cdbe9 | 223 | { |
Sergunb | 0:8918a71cdbe9 | 224 | Ipv6Addr ipAddr; |
Sergunb | 0:8918a71cdbe9 | 225 | |
Sergunb | 0:8918a71cdbe9 | 226 | //Save the time at which the packet was sent |
Sergunb | 0:8918a71cdbe9 | 227 | entry->timestamp = time; |
Sergunb | 0:8918a71cdbe9 | 228 | //Set timeout value |
Sergunb | 0:8918a71cdbe9 | 229 | entry->timeout = interface->ndpContext.retransTimer; |
Sergunb | 0:8918a71cdbe9 | 230 | |
Sergunb | 0:8918a71cdbe9 | 231 | //Target address |
Sergunb | 0:8918a71cdbe9 | 232 | ipAddr = entry->ipAddr; |
Sergunb | 0:8918a71cdbe9 | 233 | |
Sergunb | 0:8918a71cdbe9 | 234 | //Send a unicast Neighbor Solicitation message |
Sergunb | 0:8918a71cdbe9 | 235 | ndpSendNeighborSol(interface, &ipAddr, FALSE); |
Sergunb | 0:8918a71cdbe9 | 236 | } |
Sergunb | 0:8918a71cdbe9 | 237 | else |
Sergunb | 0:8918a71cdbe9 | 238 | { |
Sergunb | 0:8918a71cdbe9 | 239 | //The entry should be deleted since the host is not reachable anymore |
Sergunb | 0:8918a71cdbe9 | 240 | entry->state = NDP_STATE_NONE; |
Sergunb | 0:8918a71cdbe9 | 241 | |
Sergunb | 0:8918a71cdbe9 | 242 | //If at some point communication ceases to proceed, as determined |
Sergunb | 0:8918a71cdbe9 | 243 | //by the Neighbor Unreachability Detection algorithm, next-hop |
Sergunb | 0:8918a71cdbe9 | 244 | //determination may need to be performed again... |
Sergunb | 0:8918a71cdbe9 | 245 | ndpUpdateNextHop(interface, &entry->ipAddr); |
Sergunb | 0:8918a71cdbe9 | 246 | } |
Sergunb | 0:8918a71cdbe9 | 247 | } |
Sergunb | 0:8918a71cdbe9 | 248 | } |
Sergunb | 0:8918a71cdbe9 | 249 | } |
Sergunb | 0:8918a71cdbe9 | 250 | } |
Sergunb | 0:8918a71cdbe9 | 251 | |
Sergunb | 0:8918a71cdbe9 | 252 | |
Sergunb | 0:8918a71cdbe9 | 253 | /** |
Sergunb | 0:8918a71cdbe9 | 254 | * @brief Flush Neighbor cache |
Sergunb | 0:8918a71cdbe9 | 255 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 256 | **/ |
Sergunb | 0:8918a71cdbe9 | 257 | |
Sergunb | 0:8918a71cdbe9 | 258 | void ndpFlushNeighborCache(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 259 | { |
Sergunb | 0:8918a71cdbe9 | 260 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 261 | NdpNeighborCacheEntry *entry; |
Sergunb | 0:8918a71cdbe9 | 262 | |
Sergunb | 0:8918a71cdbe9 | 263 | //Loop through Neighbor cache entries |
Sergunb | 0:8918a71cdbe9 | 264 | for(i = 0; i < NDP_NEIGHBOR_CACHE_SIZE; i++) |
Sergunb | 0:8918a71cdbe9 | 265 | { |
Sergunb | 0:8918a71cdbe9 | 266 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 267 | entry = &interface->ndpContext.neighborCache[i]; |
Sergunb | 0:8918a71cdbe9 | 268 | |
Sergunb | 0:8918a71cdbe9 | 269 | //Drop packets that are waiting for address resolution |
Sergunb | 0:8918a71cdbe9 | 270 | ndpFlushQueuedPackets(interface, entry); |
Sergunb | 0:8918a71cdbe9 | 271 | //Release Neighbor cache entry |
Sergunb | 0:8918a71cdbe9 | 272 | entry->state = NDP_STATE_NONE; |
Sergunb | 0:8918a71cdbe9 | 273 | } |
Sergunb | 0:8918a71cdbe9 | 274 | } |
Sergunb | 0:8918a71cdbe9 | 275 | |
Sergunb | 0:8918a71cdbe9 | 276 | |
Sergunb | 0:8918a71cdbe9 | 277 | /** |
Sergunb | 0:8918a71cdbe9 | 278 | * @brief Send packets that are waiting for address resolution |
Sergunb | 0:8918a71cdbe9 | 279 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 280 | * @param[in] entry Pointer to a Neighbor cache entry |
Sergunb | 0:8918a71cdbe9 | 281 | * @return The number of packets that have been sent |
Sergunb | 0:8918a71cdbe9 | 282 | **/ |
Sergunb | 0:8918a71cdbe9 | 283 | |
Sergunb | 0:8918a71cdbe9 | 284 | uint_t ndpSendQueuedPackets(NetInterface *interface, NdpNeighborCacheEntry *entry) |
Sergunb | 0:8918a71cdbe9 | 285 | { |
Sergunb | 0:8918a71cdbe9 | 286 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 287 | NdpQueueItem *item; |
Sergunb | 0:8918a71cdbe9 | 288 | |
Sergunb | 0:8918a71cdbe9 | 289 | //Reset packet counter |
Sergunb | 0:8918a71cdbe9 | 290 | i = 0; |
Sergunb | 0:8918a71cdbe9 | 291 | |
Sergunb | 0:8918a71cdbe9 | 292 | //Check current state |
Sergunb | 0:8918a71cdbe9 | 293 | if(entry->state == NDP_STATE_INCOMPLETE) |
Sergunb | 0:8918a71cdbe9 | 294 | { |
Sergunb | 0:8918a71cdbe9 | 295 | //Loop through the queued packets |
Sergunb | 0:8918a71cdbe9 | 296 | for(i = 0; i < entry->queueSize; i++) |
Sergunb | 0:8918a71cdbe9 | 297 | { |
Sergunb | 0:8918a71cdbe9 | 298 | //Point to the current queue item |
Sergunb | 0:8918a71cdbe9 | 299 | item = &entry->queue[i]; |
Sergunb | 0:8918a71cdbe9 | 300 | |
Sergunb | 0:8918a71cdbe9 | 301 | #if (ETH_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 302 | //Ethernet interface? |
Sergunb | 0:8918a71cdbe9 | 303 | if(interface->nicDriver->type == NIC_TYPE_ETHERNET) |
Sergunb | 0:8918a71cdbe9 | 304 | { |
Sergunb | 0:8918a71cdbe9 | 305 | //Send the IPv6 packet |
Sergunb | 0:8918a71cdbe9 | 306 | ethSendFrame(interface, &entry->macAddr, |
Sergunb | 0:8918a71cdbe9 | 307 | item->buffer, item->offset, ETH_TYPE_IPV6); |
Sergunb | 0:8918a71cdbe9 | 308 | } |
Sergunb | 0:8918a71cdbe9 | 309 | #endif |
Sergunb | 0:8918a71cdbe9 | 310 | //Release memory buffer |
Sergunb | 0:8918a71cdbe9 | 311 | netBufferFree(item->buffer); |
Sergunb | 0:8918a71cdbe9 | 312 | } |
Sergunb | 0:8918a71cdbe9 | 313 | } |
Sergunb | 0:8918a71cdbe9 | 314 | |
Sergunb | 0:8918a71cdbe9 | 315 | //The queue is now empty |
Sergunb | 0:8918a71cdbe9 | 316 | entry->queueSize = 0; |
Sergunb | 0:8918a71cdbe9 | 317 | |
Sergunb | 0:8918a71cdbe9 | 318 | //Return the number of packets that have been sent |
Sergunb | 0:8918a71cdbe9 | 319 | return i; |
Sergunb | 0:8918a71cdbe9 | 320 | } |
Sergunb | 0:8918a71cdbe9 | 321 | |
Sergunb | 0:8918a71cdbe9 | 322 | |
Sergunb | 0:8918a71cdbe9 | 323 | /** |
Sergunb | 0:8918a71cdbe9 | 324 | * @brief Flush packet queue |
Sergunb | 0:8918a71cdbe9 | 325 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 326 | * @param[in] entry Pointer to a Neighbor cache entry |
Sergunb | 0:8918a71cdbe9 | 327 | **/ |
Sergunb | 0:8918a71cdbe9 | 328 | |
Sergunb | 0:8918a71cdbe9 | 329 | void ndpFlushQueuedPackets(NetInterface *interface, NdpNeighborCacheEntry *entry) |
Sergunb | 0:8918a71cdbe9 | 330 | { |
Sergunb | 0:8918a71cdbe9 | 331 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 332 | NdpQueueItem *item; |
Sergunb | 0:8918a71cdbe9 | 333 | |
Sergunb | 0:8918a71cdbe9 | 334 | //Check current state |
Sergunb | 0:8918a71cdbe9 | 335 | if(entry->state == NDP_STATE_INCOMPLETE) |
Sergunb | 0:8918a71cdbe9 | 336 | { |
Sergunb | 0:8918a71cdbe9 | 337 | //Loop through the queued packets |
Sergunb | 0:8918a71cdbe9 | 338 | for(i = 0; i < entry->queueSize; i++) |
Sergunb | 0:8918a71cdbe9 | 339 | { |
Sergunb | 0:8918a71cdbe9 | 340 | //Point to the current queue item |
Sergunb | 0:8918a71cdbe9 | 341 | item = &entry->queue[i]; |
Sergunb | 0:8918a71cdbe9 | 342 | |
Sergunb | 0:8918a71cdbe9 | 343 | //Check whether the address resolution has failed |
Sergunb | 0:8918a71cdbe9 | 344 | if(entry->retransmitCount >= NDP_MAX_MULTICAST_SOLICIT) |
Sergunb | 0:8918a71cdbe9 | 345 | { |
Sergunb | 0:8918a71cdbe9 | 346 | //Check whether the packet has been forwarded |
Sergunb | 0:8918a71cdbe9 | 347 | if(item->srcInterface != NULL) |
Sergunb | 0:8918a71cdbe9 | 348 | { |
Sergunb | 0:8918a71cdbe9 | 349 | //A Destination Unreachable message should be generated by a |
Sergunb | 0:8918a71cdbe9 | 350 | //router in response to a packet that cannot be delivered |
Sergunb | 0:8918a71cdbe9 | 351 | icmpv6SendErrorMessage(item->srcInterface, ICMPV6_TYPE_DEST_UNREACHABLE, |
Sergunb | 0:8918a71cdbe9 | 352 | ICMPV6_CODE_ADDR_UNREACHABLE, 0, item->buffer, item->offset); |
Sergunb | 0:8918a71cdbe9 | 353 | } |
Sergunb | 0:8918a71cdbe9 | 354 | } |
Sergunb | 0:8918a71cdbe9 | 355 | |
Sergunb | 0:8918a71cdbe9 | 356 | //Release memory buffer |
Sergunb | 0:8918a71cdbe9 | 357 | netBufferFree(item->buffer); |
Sergunb | 0:8918a71cdbe9 | 358 | } |
Sergunb | 0:8918a71cdbe9 | 359 | } |
Sergunb | 0:8918a71cdbe9 | 360 | |
Sergunb | 0:8918a71cdbe9 | 361 | //The queue is now empty |
Sergunb | 0:8918a71cdbe9 | 362 | entry->queueSize = 0; |
Sergunb | 0:8918a71cdbe9 | 363 | } |
Sergunb | 0:8918a71cdbe9 | 364 | |
Sergunb | 0:8918a71cdbe9 | 365 | |
Sergunb | 0:8918a71cdbe9 | 366 | /** |
Sergunb | 0:8918a71cdbe9 | 367 | * @brief Create a new entry in the Destination Cache |
Sergunb | 0:8918a71cdbe9 | 368 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 369 | * @return Pointer to the newly created entry |
Sergunb | 0:8918a71cdbe9 | 370 | **/ |
Sergunb | 0:8918a71cdbe9 | 371 | |
Sergunb | 0:8918a71cdbe9 | 372 | NdpDestCacheEntry *ndpCreateDestCacheEntry(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 373 | { |
Sergunb | 0:8918a71cdbe9 | 374 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 375 | NdpDestCacheEntry *entry; |
Sergunb | 0:8918a71cdbe9 | 376 | NdpDestCacheEntry *oldestEntry; |
Sergunb | 0:8918a71cdbe9 | 377 | |
Sergunb | 0:8918a71cdbe9 | 378 | //Keep track of the oldest entry |
Sergunb | 0:8918a71cdbe9 | 379 | oldestEntry = &interface->ndpContext.destCache[0]; |
Sergunb | 0:8918a71cdbe9 | 380 | |
Sergunb | 0:8918a71cdbe9 | 381 | //Loop through Destination cache entries |
Sergunb | 0:8918a71cdbe9 | 382 | for(i = 0; i < NDP_DEST_CACHE_SIZE; i++) |
Sergunb | 0:8918a71cdbe9 | 383 | { |
Sergunb | 0:8918a71cdbe9 | 384 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 385 | entry = &interface->ndpContext.destCache[i]; |
Sergunb | 0:8918a71cdbe9 | 386 | |
Sergunb | 0:8918a71cdbe9 | 387 | //Check whether the entry is currently in used or not |
Sergunb | 0:8918a71cdbe9 | 388 | if(ipv6CompAddr(&entry->destAddr, &IPV6_UNSPECIFIED_ADDR)) |
Sergunb | 0:8918a71cdbe9 | 389 | { |
Sergunb | 0:8918a71cdbe9 | 390 | //Erase contents |
Sergunb | 0:8918a71cdbe9 | 391 | memset(entry, 0, sizeof(NdpDestCacheEntry)); |
Sergunb | 0:8918a71cdbe9 | 392 | //Return a pointer to the Destination cache entry |
Sergunb | 0:8918a71cdbe9 | 393 | return entry; |
Sergunb | 0:8918a71cdbe9 | 394 | } |
Sergunb | 0:8918a71cdbe9 | 395 | |
Sergunb | 0:8918a71cdbe9 | 396 | //Keep track of the oldest entry in the table |
Sergunb | 0:8918a71cdbe9 | 397 | if(timeCompare(entry->timestamp, oldestEntry->timestamp) < 0) |
Sergunb | 0:8918a71cdbe9 | 398 | oldestEntry = entry; |
Sergunb | 0:8918a71cdbe9 | 399 | } |
Sergunb | 0:8918a71cdbe9 | 400 | |
Sergunb | 0:8918a71cdbe9 | 401 | //The oldest entry is removed whenever the table runs out of space |
Sergunb | 0:8918a71cdbe9 | 402 | memset(oldestEntry, 0, sizeof(NdpDestCacheEntry)); |
Sergunb | 0:8918a71cdbe9 | 403 | |
Sergunb | 0:8918a71cdbe9 | 404 | //Return a pointer to the Destination cache entry |
Sergunb | 0:8918a71cdbe9 | 405 | return oldestEntry; |
Sergunb | 0:8918a71cdbe9 | 406 | } |
Sergunb | 0:8918a71cdbe9 | 407 | |
Sergunb | 0:8918a71cdbe9 | 408 | |
Sergunb | 0:8918a71cdbe9 | 409 | /** |
Sergunb | 0:8918a71cdbe9 | 410 | * @brief Search the Destination Cache for a given destination address |
Sergunb | 0:8918a71cdbe9 | 411 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 412 | * @param[in] destAddr Destination IPv6 address |
Sergunb | 0:8918a71cdbe9 | 413 | * @return A pointer to the matching entry is returned. NULL is returned if |
Sergunb | 0:8918a71cdbe9 | 414 | * the specified address could not be found in the Destination cache |
Sergunb | 0:8918a71cdbe9 | 415 | **/ |
Sergunb | 0:8918a71cdbe9 | 416 | |
Sergunb | 0:8918a71cdbe9 | 417 | NdpDestCacheEntry *ndpFindDestCacheEntry(NetInterface *interface, const Ipv6Addr *destAddr) |
Sergunb | 0:8918a71cdbe9 | 418 | { |
Sergunb | 0:8918a71cdbe9 | 419 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 420 | NdpDestCacheEntry *entry; |
Sergunb | 0:8918a71cdbe9 | 421 | |
Sergunb | 0:8918a71cdbe9 | 422 | //Loop through Destination Cache entries |
Sergunb | 0:8918a71cdbe9 | 423 | for(i = 0; i < NDP_DEST_CACHE_SIZE; i++) |
Sergunb | 0:8918a71cdbe9 | 424 | { |
Sergunb | 0:8918a71cdbe9 | 425 | //Point to the current entry |
Sergunb | 0:8918a71cdbe9 | 426 | entry = &interface->ndpContext.destCache[i]; |
Sergunb | 0:8918a71cdbe9 | 427 | |
Sergunb | 0:8918a71cdbe9 | 428 | //Current entry matches the specified destination address? |
Sergunb | 0:8918a71cdbe9 | 429 | if(ipv6CompAddr(&entry->destAddr, destAddr)) |
Sergunb | 0:8918a71cdbe9 | 430 | return entry; |
Sergunb | 0:8918a71cdbe9 | 431 | } |
Sergunb | 0:8918a71cdbe9 | 432 | |
Sergunb | 0:8918a71cdbe9 | 433 | //No matching entry in Destination Cache... |
Sergunb | 0:8918a71cdbe9 | 434 | return NULL; |
Sergunb | 0:8918a71cdbe9 | 435 | } |
Sergunb | 0:8918a71cdbe9 | 436 | |
Sergunb | 0:8918a71cdbe9 | 437 | |
Sergunb | 0:8918a71cdbe9 | 438 | /** |
Sergunb | 0:8918a71cdbe9 | 439 | * @brief Flush Destination Cache |
Sergunb | 0:8918a71cdbe9 | 440 | * @param[in] interface Underlying network interface |
Sergunb | 0:8918a71cdbe9 | 441 | **/ |
Sergunb | 0:8918a71cdbe9 | 442 | |
Sergunb | 0:8918a71cdbe9 | 443 | void ndpFlushDestCache(NetInterface *interface) |
Sergunb | 0:8918a71cdbe9 | 444 | { |
Sergunb | 0:8918a71cdbe9 | 445 | //Clear the Destination Cache |
Sergunb | 0:8918a71cdbe9 | 446 | memset(interface->ndpContext.destCache, 0, |
Sergunb | 0:8918a71cdbe9 | 447 | sizeof(interface->ndpContext.destCache)); |
Sergunb | 0:8918a71cdbe9 | 448 | } |
Sergunb | 0:8918a71cdbe9 | 449 | |
Sergunb | 0:8918a71cdbe9 | 450 | #endif |
Sergunb | 0:8918a71cdbe9 | 451 |