User | Revision | Line number | New contents of line |
mbed714 |
0:98aadd37a5c5
|
1
|
/**
|
mbed714 |
0:98aadd37a5c5
|
2
|
* @file
|
mbed714 |
0:98aadd37a5c5
|
3
|
* Address Resolution Protocol module for IP over Ethernet
|
mbed714 |
0:98aadd37a5c5
|
4
|
*
|
mbed714 |
0:98aadd37a5c5
|
5
|
* Functionally, ARP is divided into two parts. The first maps an IP address
|
mbed714 |
0:98aadd37a5c5
|
6
|
* to a physical address when sending a packet, and the second part answers
|
mbed714 |
0:98aadd37a5c5
|
7
|
* requests from other machines for our physical address.
|
mbed714 |
0:98aadd37a5c5
|
8
|
*
|
mbed714 |
0:98aadd37a5c5
|
9
|
* This implementation complies with RFC 826 (Ethernet ARP). It supports
|
mbed714 |
0:98aadd37a5c5
|
10
|
* Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
|
mbed714 |
0:98aadd37a5c5
|
11
|
* if an interface calls etharp_gratuitous(our_netif) upon address change.
|
mbed714 |
0:98aadd37a5c5
|
12
|
*/
|
mbed714 |
0:98aadd37a5c5
|
13
|
|
mbed714 |
0:98aadd37a5c5
|
14
|
/*
|
mbed714 |
0:98aadd37a5c5
|
15
|
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
mbed714 |
0:98aadd37a5c5
|
16
|
* Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
|
mbed714 |
0:98aadd37a5c5
|
17
|
* Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
|
mbed714 |
0:98aadd37a5c5
|
18
|
* All rights reserved.
|
mbed714 |
0:98aadd37a5c5
|
19
|
*
|
mbed714 |
0:98aadd37a5c5
|
20
|
* Redistribution and use in source and binary forms, with or without modification,
|
mbed714 |
0:98aadd37a5c5
|
21
|
* are permitted provided that the following conditions are met:
|
mbed714 |
0:98aadd37a5c5
|
22
|
*
|
mbed714 |
0:98aadd37a5c5
|
23
|
* 1. Redistributions of source code must retain the above copyright notice,
|
mbed714 |
0:98aadd37a5c5
|
24
|
* this list of conditions and the following disclaimer.
|
mbed714 |
0:98aadd37a5c5
|
25
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
mbed714 |
0:98aadd37a5c5
|
26
|
* this list of conditions and the following disclaimer in the documentation
|
mbed714 |
0:98aadd37a5c5
|
27
|
* and/or other materials provided with the distribution.
|
mbed714 |
0:98aadd37a5c5
|
28
|
* 3. The name of the author may not be used to endorse or promote products
|
mbed714 |
0:98aadd37a5c5
|
29
|
* derived from this software without specific prior written permission.
|
mbed714 |
0:98aadd37a5c5
|
30
|
*
|
mbed714 |
0:98aadd37a5c5
|
31
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
mbed714 |
0:98aadd37a5c5
|
32
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
mbed714 |
0:98aadd37a5c5
|
33
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
mbed714 |
0:98aadd37a5c5
|
34
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mbed714 |
0:98aadd37a5c5
|
35
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
mbed714 |
0:98aadd37a5c5
|
36
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
mbed714 |
0:98aadd37a5c5
|
37
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
mbed714 |
0:98aadd37a5c5
|
38
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
mbed714 |
0:98aadd37a5c5
|
39
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
mbed714 |
0:98aadd37a5c5
|
40
|
* OF SUCH DAMAGE.
|
mbed714 |
0:98aadd37a5c5
|
41
|
*
|
mbed714 |
0:98aadd37a5c5
|
42
|
* This file is part of the lwIP TCP/IP stack.
|
mbed714 |
0:98aadd37a5c5
|
43
|
*
|
mbed714 |
0:98aadd37a5c5
|
44
|
*/
|
mbed714 |
0:98aadd37a5c5
|
45
|
|
mbed714 |
0:98aadd37a5c5
|
46
|
#include "lwip/opt.h"
|
mbed714 |
0:98aadd37a5c5
|
47
|
|
mbed714 |
0:98aadd37a5c5
|
48
|
#if LWIP_ARP || LWIP_ETHERNET
|
mbed714 |
0:98aadd37a5c5
|
49
|
|
mbed714 |
0:98aadd37a5c5
|
50
|
#include "lwip/ip_addr.h"
|
mbed714 |
0:98aadd37a5c5
|
51
|
#include "lwip/def.h"
|
mbed714 |
0:98aadd37a5c5
|
52
|
#include "lwip/ip.h"
|
mbed714 |
0:98aadd37a5c5
|
53
|
#include "lwip/stats.h"
|
mbed714 |
0:98aadd37a5c5
|
54
|
#include "lwip/snmp.h"
|
mbed714 |
0:98aadd37a5c5
|
55
|
#include "lwip/dhcp.h"
|
mbed714 |
0:98aadd37a5c5
|
56
|
#include "lwip/autoip.h"
|
mbed714 |
0:98aadd37a5c5
|
57
|
#include "netif/etharp.h"
|
mbed714 |
0:98aadd37a5c5
|
58
|
|
mbed714 |
0:98aadd37a5c5
|
59
|
#if PPPOE_SUPPORT
|
mbed714 |
0:98aadd37a5c5
|
60
|
#include "netif/ppp_oe.h"
|
mbed714 |
0:98aadd37a5c5
|
61
|
#endif /* PPPOE_SUPPORT */
|
mbed714 |
0:98aadd37a5c5
|
62
|
|
mbed714 |
0:98aadd37a5c5
|
63
|
#include <string.h>
|
mbed714 |
0:98aadd37a5c5
|
64
|
|
mbed714 |
0:98aadd37a5c5
|
65
|
const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
|
mbed714 |
0:98aadd37a5c5
|
66
|
const struct eth_addr ethzero = {{0,0,0,0,0,0}};
|
mbed714 |
0:98aadd37a5c5
|
67
|
|
mbed714 |
0:98aadd37a5c5
|
68
|
#if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
|
mbed714 |
0:98aadd37a5c5
|
69
|
|
mbed714 |
0:98aadd37a5c5
|
70
|
/** the time an ARP entry stays valid after its last update,
|
mbed714 |
0:98aadd37a5c5
|
71
|
* for ARP_TMR_INTERVAL = 5000, this is
|
mbed714 |
0:98aadd37a5c5
|
72
|
* (240 * 5) seconds = 20 minutes.
|
mbed714 |
0:98aadd37a5c5
|
73
|
*/
|
mbed714 |
0:98aadd37a5c5
|
74
|
#define ARP_MAXAGE 240
|
mbed714 |
0:98aadd37a5c5
|
75
|
/** the time an ARP entry stays pending after first request,
|
mbed714 |
0:98aadd37a5c5
|
76
|
* for ARP_TMR_INTERVAL = 5000, this is
|
mbed714 |
0:98aadd37a5c5
|
77
|
* (2 * 5) seconds = 10 seconds.
|
mbed714 |
0:98aadd37a5c5
|
78
|
*
|
mbed714 |
0:98aadd37a5c5
|
79
|
* @internal Keep this number at least 2, otherwise it might
|
mbed714 |
0:98aadd37a5c5
|
80
|
* run out instantly if the timeout occurs directly after a request.
|
mbed714 |
0:98aadd37a5c5
|
81
|
*/
|
mbed714 |
0:98aadd37a5c5
|
82
|
#define ARP_MAXPENDING 2
|
mbed714 |
0:98aadd37a5c5
|
83
|
|
mbed714 |
0:98aadd37a5c5
|
84
|
#define HWTYPE_ETHERNET 1
|
mbed714 |
0:98aadd37a5c5
|
85
|
|
mbed714 |
0:98aadd37a5c5
|
86
|
enum etharp_state {
|
mbed714 |
0:98aadd37a5c5
|
87
|
ETHARP_STATE_EMPTY = 0,
|
mbed714 |
0:98aadd37a5c5
|
88
|
ETHARP_STATE_PENDING,
|
mbed714 |
0:98aadd37a5c5
|
89
|
ETHARP_STATE_STABLE
|
mbed714 |
0:98aadd37a5c5
|
90
|
};
|
mbed714 |
0:98aadd37a5c5
|
91
|
|
mbed714 |
0:98aadd37a5c5
|
92
|
struct etharp_entry {
|
mbed714 |
0:98aadd37a5c5
|
93
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
94
|
/** Pointer to queue of pending outgoing packets on this ARP entry. */
|
mbed714 |
0:98aadd37a5c5
|
95
|
struct etharp_q_entry *q;
|
mbed714 |
0:98aadd37a5c5
|
96
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
97
|
ip_addr_t ipaddr;
|
mbed714 |
0:98aadd37a5c5
|
98
|
struct eth_addr ethaddr;
|
mbed714 |
0:98aadd37a5c5
|
99
|
#if LWIP_SNMP
|
mbed714 |
0:98aadd37a5c5
|
100
|
struct netif *netif;
|
mbed714 |
0:98aadd37a5c5
|
101
|
#endif /* LWIP_SNMP */
|
mbed714 |
0:98aadd37a5c5
|
102
|
u8_t state;
|
mbed714 |
0:98aadd37a5c5
|
103
|
u8_t ctime;
|
mbed714 |
0:98aadd37a5c5
|
104
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
105
|
u8_t static_entry;
|
mbed714 |
0:98aadd37a5c5
|
106
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
107
|
};
|
mbed714 |
0:98aadd37a5c5
|
108
|
|
mbed714 |
0:98aadd37a5c5
|
109
|
static struct etharp_entry arp_table[ARP_TABLE_SIZE];
|
mbed714 |
0:98aadd37a5c5
|
110
|
|
mbed714 |
0:98aadd37a5c5
|
111
|
#if !LWIP_NETIF_HWADDRHINT
|
mbed714 |
0:98aadd37a5c5
|
112
|
static u8_t etharp_cached_entry;
|
mbed714 |
0:98aadd37a5c5
|
113
|
#endif /* !LWIP_NETIF_HWADDRHINT */
|
mbed714 |
0:98aadd37a5c5
|
114
|
|
mbed714 |
0:98aadd37a5c5
|
115
|
/** Try hard to create a new entry - we want the IP address to appear in
|
mbed714 |
0:98aadd37a5c5
|
116
|
the cache (even if this means removing an active entry or so). */
|
mbed714 |
0:98aadd37a5c5
|
117
|
#define ETHARP_FLAG_TRY_HARD 1
|
mbed714 |
0:98aadd37a5c5
|
118
|
#define ETHARP_FLAG_FIND_ONLY 2
|
mbed714 |
0:98aadd37a5c5
|
119
|
#define ETHARP_FLAG_STATIC_ENTRY 4
|
mbed714 |
0:98aadd37a5c5
|
120
|
|
mbed714 |
0:98aadd37a5c5
|
121
|
#if LWIP_NETIF_HWADDRHINT
|
mbed714 |
0:98aadd37a5c5
|
122
|
#define ETHARP_SET_HINT(netif, hint) if (((netif) != NULL) && ((netif)->addr_hint != NULL)) \
|
mbed714 |
0:98aadd37a5c5
|
123
|
*((netif)->addr_hint) = (hint);
|
mbed714 |
0:98aadd37a5c5
|
124
|
#else /* LWIP_NETIF_HWADDRHINT */
|
mbed714 |
0:98aadd37a5c5
|
125
|
#define ETHARP_SET_HINT(netif, hint) (etharp_cached_entry = (hint))
|
mbed714 |
0:98aadd37a5c5
|
126
|
#endif /* LWIP_NETIF_HWADDRHINT */
|
mbed714 |
0:98aadd37a5c5
|
127
|
|
mbed714 |
0:98aadd37a5c5
|
128
|
static err_t update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags);
|
mbed714 |
0:98aadd37a5c5
|
129
|
|
mbed714 |
0:98aadd37a5c5
|
130
|
|
mbed714 |
0:98aadd37a5c5
|
131
|
/* Some checks, instead of etharp_init(): */
|
mbed714 |
0:98aadd37a5c5
|
132
|
#if (LWIP_ARP && (ARP_TABLE_SIZE > 0x7f))
|
mbed714 |
0:98aadd37a5c5
|
133
|
#error "ARP_TABLE_SIZE must fit in an s8_t, you have to reduce it in your lwipopts.h"
|
mbed714 |
0:98aadd37a5c5
|
134
|
#endif
|
mbed714 |
0:98aadd37a5c5
|
135
|
|
mbed714 |
0:98aadd37a5c5
|
136
|
|
mbed714 |
0:98aadd37a5c5
|
137
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
138
|
/**
|
mbed714 |
0:98aadd37a5c5
|
139
|
* Free a complete queue of etharp entries
|
mbed714 |
0:98aadd37a5c5
|
140
|
*
|
mbed714 |
0:98aadd37a5c5
|
141
|
* @param q a qeueue of etharp_q_entry's to free
|
mbed714 |
0:98aadd37a5c5
|
142
|
*/
|
mbed714 |
0:98aadd37a5c5
|
143
|
static void
|
mbed714 |
0:98aadd37a5c5
|
144
|
free_etharp_q(struct etharp_q_entry *q)
|
mbed714 |
0:98aadd37a5c5
|
145
|
{
|
mbed714 |
0:98aadd37a5c5
|
146
|
struct etharp_q_entry *r;
|
mbed714 |
0:98aadd37a5c5
|
147
|
LWIP_ASSERT("q != NULL", q != NULL);
|
mbed714 |
0:98aadd37a5c5
|
148
|
LWIP_ASSERT("q->p != NULL", q->p != NULL);
|
mbed714 |
0:98aadd37a5c5
|
149
|
while (q) {
|
mbed714 |
0:98aadd37a5c5
|
150
|
r = q;
|
mbed714 |
0:98aadd37a5c5
|
151
|
q = q->next;
|
mbed714 |
0:98aadd37a5c5
|
152
|
LWIP_ASSERT("r->p != NULL", (r->p != NULL));
|
mbed714 |
0:98aadd37a5c5
|
153
|
pbuf_free(r->p);
|
mbed714 |
0:98aadd37a5c5
|
154
|
memp_free(MEMP_ARP_QUEUE, r);
|
mbed714 |
0:98aadd37a5c5
|
155
|
}
|
mbed714 |
0:98aadd37a5c5
|
156
|
}
|
mbed714 |
0:98aadd37a5c5
|
157
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
158
|
|
mbed714 |
0:98aadd37a5c5
|
159
|
/** Clean up ARP table entries */
|
mbed714 |
0:98aadd37a5c5
|
160
|
static void
|
mbed714 |
0:98aadd37a5c5
|
161
|
free_entry(int i)
|
mbed714 |
0:98aadd37a5c5
|
162
|
{
|
mbed714 |
0:98aadd37a5c5
|
163
|
/* remove from SNMP ARP index tree */
|
mbed714 |
0:98aadd37a5c5
|
164
|
snmp_delete_arpidx_tree(arp_table[i].netif, &arp_table[i].ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
165
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
166
|
/* and empty packet queue */
|
mbed714 |
0:98aadd37a5c5
|
167
|
if (arp_table[i].q != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
168
|
/* remove all queued packets */
|
mbed714 |
0:98aadd37a5c5
|
169
|
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
|
mbed714 |
0:98aadd37a5c5
|
170
|
free_etharp_q(arp_table[i].q);
|
mbed714 |
0:98aadd37a5c5
|
171
|
arp_table[i].q = NULL;
|
mbed714 |
0:98aadd37a5c5
|
172
|
}
|
mbed714 |
0:98aadd37a5c5
|
173
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
174
|
/* recycle entry for re-use */
|
mbed714 |
0:98aadd37a5c5
|
175
|
arp_table[i].state = ETHARP_STATE_EMPTY;
|
mbed714 |
0:98aadd37a5c5
|
176
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
177
|
arp_table[i].static_entry = 0;
|
mbed714 |
0:98aadd37a5c5
|
178
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
179
|
#ifdef LWIP_DEBUG
|
mbed714 |
0:98aadd37a5c5
|
180
|
/* for debugging, clean out the complete entry */
|
mbed714 |
0:98aadd37a5c5
|
181
|
arp_table[i].ctime = 0;
|
mbed714 |
0:98aadd37a5c5
|
182
|
#if LWIP_SNMP
|
mbed714 |
0:98aadd37a5c5
|
183
|
arp_table[i].netif = NULL;
|
mbed714 |
0:98aadd37a5c5
|
184
|
#endif /* LWIP_SNMP */
|
mbed714 |
0:98aadd37a5c5
|
185
|
ip_addr_set_zero(&arp_table[i].ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
186
|
arp_table[i].ethaddr = ethzero;
|
mbed714 |
0:98aadd37a5c5
|
187
|
#endif /* LWIP_DEBUG */
|
mbed714 |
0:98aadd37a5c5
|
188
|
}
|
mbed714 |
0:98aadd37a5c5
|
189
|
|
mbed714 |
0:98aadd37a5c5
|
190
|
/**
|
mbed714 |
0:98aadd37a5c5
|
191
|
* Clears expired entries in the ARP table.
|
mbed714 |
0:98aadd37a5c5
|
192
|
*
|
mbed714 |
0:98aadd37a5c5
|
193
|
* This function should be called every ETHARP_TMR_INTERVAL milliseconds (5 seconds),
|
mbed714 |
0:98aadd37a5c5
|
194
|
* in order to expire entries in the ARP table.
|
mbed714 |
0:98aadd37a5c5
|
195
|
*/
|
mbed714 |
0:98aadd37a5c5
|
196
|
void
|
mbed714 |
0:98aadd37a5c5
|
197
|
etharp_tmr(void)
|
mbed714 |
0:98aadd37a5c5
|
198
|
{
|
mbed714 |
0:98aadd37a5c5
|
199
|
u8_t i;
|
mbed714 |
0:98aadd37a5c5
|
200
|
|
mbed714 |
0:98aadd37a5c5
|
201
|
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
|
mbed714 |
0:98aadd37a5c5
|
202
|
/* remove expired entries from the ARP table */
|
mbed714 |
0:98aadd37a5c5
|
203
|
for (i = 0; i < ARP_TABLE_SIZE; ++i) {
|
mbed714 |
0:98aadd37a5c5
|
204
|
u8_t state = arp_table[i].state;
|
mbed714 |
0:98aadd37a5c5
|
205
|
if (state != ETHARP_STATE_EMPTY
|
mbed714 |
0:98aadd37a5c5
|
206
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
207
|
&& (arp_table[i].static_entry == 0)
|
mbed714 |
0:98aadd37a5c5
|
208
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
209
|
) {
|
mbed714 |
0:98aadd37a5c5
|
210
|
arp_table[i].ctime++;
|
mbed714 |
0:98aadd37a5c5
|
211
|
if ((arp_table[i].ctime >= ARP_MAXAGE) ||
|
mbed714 |
0:98aadd37a5c5
|
212
|
((arp_table[i].state == ETHARP_STATE_PENDING) &&
|
mbed714 |
0:98aadd37a5c5
|
213
|
(arp_table[i].ctime >= ARP_MAXPENDING))) {
|
mbed714 |
0:98aadd37a5c5
|
214
|
/* pending or stable entry has become old! */
|
mbed714 |
0:98aadd37a5c5
|
215
|
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %"U16_F".\n",
|
mbed714 |
0:98aadd37a5c5
|
216
|
arp_table[i].state == ETHARP_STATE_STABLE ? "stable" : "pending", (u16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
217
|
/* clean up entries that have just been expired */
|
mbed714 |
0:98aadd37a5c5
|
218
|
free_entry(i);
|
mbed714 |
0:98aadd37a5c5
|
219
|
}
|
mbed714 |
0:98aadd37a5c5
|
220
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
221
|
/* still pending entry? (not expired) */
|
mbed714 |
0:98aadd37a5c5
|
222
|
if (arp_table[i].state == ETHARP_STATE_PENDING) {
|
mbed714 |
0:98aadd37a5c5
|
223
|
/* resend an ARP query here? */
|
mbed714 |
0:98aadd37a5c5
|
224
|
}
|
mbed714 |
0:98aadd37a5c5
|
225
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
226
|
}
|
mbed714 |
0:98aadd37a5c5
|
227
|
}
|
mbed714 |
0:98aadd37a5c5
|
228
|
}
|
mbed714 |
0:98aadd37a5c5
|
229
|
|
mbed714 |
0:98aadd37a5c5
|
230
|
/**
|
mbed714 |
0:98aadd37a5c5
|
231
|
* Search the ARP table for a matching or new entry.
|
mbed714 |
0:98aadd37a5c5
|
232
|
*
|
mbed714 |
0:98aadd37a5c5
|
233
|
* If an IP address is given, return a pending or stable ARP entry that matches
|
mbed714 |
0:98aadd37a5c5
|
234
|
* the address. If no match is found, create a new entry with this address set,
|
mbed714 |
0:98aadd37a5c5
|
235
|
* but in state ETHARP_EMPTY. The caller must check and possibly change the
|
mbed714 |
0:98aadd37a5c5
|
236
|
* state of the returned entry.
|
mbed714 |
0:98aadd37a5c5
|
237
|
*
|
mbed714 |
0:98aadd37a5c5
|
238
|
* If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
|
mbed714 |
0:98aadd37a5c5
|
239
|
*
|
mbed714 |
0:98aadd37a5c5
|
240
|
* In all cases, attempt to create new entries from an empty entry. If no
|
mbed714 |
0:98aadd37a5c5
|
241
|
* empty entries are available and ETHARP_FLAG_TRY_HARD flag is set, recycle
|
mbed714 |
0:98aadd37a5c5
|
242
|
* old entries. Heuristic choose the least important entry for recycling.
|
mbed714 |
0:98aadd37a5c5
|
243
|
*
|
mbed714 |
0:98aadd37a5c5
|
244
|
* @param ipaddr IP address to find in ARP cache, or to add if not found.
|
mbed714 |
0:98aadd37a5c5
|
245
|
* @param flags @see definition of ETHARP_FLAG_*
|
mbed714 |
0:98aadd37a5c5
|
246
|
* @param netif netif related to this address (used for NETIF_HWADDRHINT)
|
mbed714 |
0:98aadd37a5c5
|
247
|
*
|
mbed714 |
0:98aadd37a5c5
|
248
|
* @return The ARP entry index that matched or is created, ERR_MEM if no
|
mbed714 |
0:98aadd37a5c5
|
249
|
* entry is found or could be recycled.
|
mbed714 |
0:98aadd37a5c5
|
250
|
*/
|
mbed714 |
0:98aadd37a5c5
|
251
|
static s8_t
|
mbed714 |
0:98aadd37a5c5
|
252
|
find_entry(ip_addr_t *ipaddr, u8_t flags)
|
mbed714 |
0:98aadd37a5c5
|
253
|
{
|
mbed714 |
0:98aadd37a5c5
|
254
|
s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
|
mbed714 |
0:98aadd37a5c5
|
255
|
s8_t empty = ARP_TABLE_SIZE;
|
mbed714 |
0:98aadd37a5c5
|
256
|
u8_t i = 0, age_pending = 0, age_stable = 0;
|
mbed714 |
0:98aadd37a5c5
|
257
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
258
|
/* oldest entry with packets on queue */
|
mbed714 |
0:98aadd37a5c5
|
259
|
s8_t old_queue = ARP_TABLE_SIZE;
|
mbed714 |
0:98aadd37a5c5
|
260
|
/* its age */
|
mbed714 |
0:98aadd37a5c5
|
261
|
u8_t age_queue = 0;
|
mbed714 |
0:98aadd37a5c5
|
262
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
263
|
|
mbed714 |
0:98aadd37a5c5
|
264
|
/**
|
mbed714 |
0:98aadd37a5c5
|
265
|
* a) do a search through the cache, remember candidates
|
mbed714 |
0:98aadd37a5c5
|
266
|
* b) select candidate entry
|
mbed714 |
0:98aadd37a5c5
|
267
|
* c) create new entry
|
mbed714 |
0:98aadd37a5c5
|
268
|
*/
|
mbed714 |
0:98aadd37a5c5
|
269
|
|
mbed714 |
0:98aadd37a5c5
|
270
|
/* a) in a single search sweep, do all of this
|
mbed714 |
0:98aadd37a5c5
|
271
|
* 1) remember the first empty entry (if any)
|
mbed714 |
0:98aadd37a5c5
|
272
|
* 2) remember the oldest stable entry (if any)
|
mbed714 |
0:98aadd37a5c5
|
273
|
* 3) remember the oldest pending entry without queued packets (if any)
|
mbed714 |
0:98aadd37a5c5
|
274
|
* 4) remember the oldest pending entry with queued packets (if any)
|
mbed714 |
0:98aadd37a5c5
|
275
|
* 5) search for a matching IP entry, either pending or stable
|
mbed714 |
0:98aadd37a5c5
|
276
|
* until 5 matches, or all entries are searched for.
|
mbed714 |
0:98aadd37a5c5
|
277
|
*/
|
mbed714 |
0:98aadd37a5c5
|
278
|
|
mbed714 |
0:98aadd37a5c5
|
279
|
for (i = 0; i < ARP_TABLE_SIZE; ++i) {
|
mbed714 |
0:98aadd37a5c5
|
280
|
u8_t state = arp_table[i].state;
|
mbed714 |
0:98aadd37a5c5
|
281
|
/* no empty entry found yet and now we do find one? */
|
mbed714 |
0:98aadd37a5c5
|
282
|
if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
|
mbed714 |
0:98aadd37a5c5
|
283
|
LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %"U16_F"\n", (u16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
284
|
/* remember first empty entry */
|
mbed714 |
0:98aadd37a5c5
|
285
|
empty = i;
|
mbed714 |
0:98aadd37a5c5
|
286
|
} else if (state != ETHARP_STATE_EMPTY) {
|
mbed714 |
0:98aadd37a5c5
|
287
|
LWIP_ASSERT("state == ETHARP_STATE_PENDING || state == ETHARP_STATE_STABLE",
|
mbed714 |
0:98aadd37a5c5
|
288
|
state == ETHARP_STATE_PENDING || state == ETHARP_STATE_STABLE);
|
mbed714 |
0:98aadd37a5c5
|
289
|
/* if given, does IP address match IP address in ARP entry? */
|
mbed714 |
0:98aadd37a5c5
|
290
|
if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
|
mbed714 |
0:98aadd37a5c5
|
291
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: found matching entry %"U16_F"\n", (u16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
292
|
/* found exact IP address match, simply bail out */
|
mbed714 |
0:98aadd37a5c5
|
293
|
return i;
|
mbed714 |
0:98aadd37a5c5
|
294
|
}
|
mbed714 |
0:98aadd37a5c5
|
295
|
/* pending entry? */
|
mbed714 |
0:98aadd37a5c5
|
296
|
if (state == ETHARP_STATE_PENDING) {
|
mbed714 |
0:98aadd37a5c5
|
297
|
/* pending with queued packets? */
|
mbed714 |
0:98aadd37a5c5
|
298
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
299
|
if (arp_table[i].q != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
300
|
if (arp_table[i].ctime >= age_queue) {
|
mbed714 |
0:98aadd37a5c5
|
301
|
old_queue = i;
|
mbed714 |
0:98aadd37a5c5
|
302
|
age_queue = arp_table[i].ctime;
|
mbed714 |
0:98aadd37a5c5
|
303
|
}
|
mbed714 |
0:98aadd37a5c5
|
304
|
} else
|
mbed714 |
0:98aadd37a5c5
|
305
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
306
|
/* pending without queued packets? */
|
mbed714 |
0:98aadd37a5c5
|
307
|
{
|
mbed714 |
0:98aadd37a5c5
|
308
|
if (arp_table[i].ctime >= age_pending) {
|
mbed714 |
0:98aadd37a5c5
|
309
|
old_pending = i;
|
mbed714 |
0:98aadd37a5c5
|
310
|
age_pending = arp_table[i].ctime;
|
mbed714 |
0:98aadd37a5c5
|
311
|
}
|
mbed714 |
0:98aadd37a5c5
|
312
|
}
|
mbed714 |
0:98aadd37a5c5
|
313
|
/* stable entry? */
|
mbed714 |
0:98aadd37a5c5
|
314
|
} else if (state == ETHARP_STATE_STABLE) {
|
mbed714 |
0:98aadd37a5c5
|
315
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
316
|
/* don't record old_stable for static entries since they never expire */
|
mbed714 |
0:98aadd37a5c5
|
317
|
if (arp_table[i].static_entry == 0)
|
mbed714 |
0:98aadd37a5c5
|
318
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
319
|
{
|
mbed714 |
0:98aadd37a5c5
|
320
|
/* remember entry with oldest stable entry in oldest, its age in maxtime */
|
mbed714 |
0:98aadd37a5c5
|
321
|
if (arp_table[i].ctime >= age_stable) {
|
mbed714 |
0:98aadd37a5c5
|
322
|
old_stable = i;
|
mbed714 |
0:98aadd37a5c5
|
323
|
age_stable = arp_table[i].ctime;
|
mbed714 |
0:98aadd37a5c5
|
324
|
}
|
mbed714 |
0:98aadd37a5c5
|
325
|
}
|
mbed714 |
0:98aadd37a5c5
|
326
|
}
|
mbed714 |
0:98aadd37a5c5
|
327
|
}
|
mbed714 |
0:98aadd37a5c5
|
328
|
}
|
mbed714 |
0:98aadd37a5c5
|
329
|
/* { we have no match } => try to create a new entry */
|
mbed714 |
0:98aadd37a5c5
|
330
|
|
mbed714 |
0:98aadd37a5c5
|
331
|
/* don't create new entry, only search? */
|
mbed714 |
0:98aadd37a5c5
|
332
|
if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
|
mbed714 |
0:98aadd37a5c5
|
333
|
/* or no empty entry found and not allowed to recycle? */
|
mbed714 |
0:98aadd37a5c5
|
334
|
((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
|
mbed714 |
0:98aadd37a5c5
|
335
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty entry found and not allowed to recycle\n"));
|
mbed714 |
0:98aadd37a5c5
|
336
|
return (s8_t)ERR_MEM;
|
mbed714 |
0:98aadd37a5c5
|
337
|
}
|
mbed714 |
0:98aadd37a5c5
|
338
|
|
mbed714 |
0:98aadd37a5c5
|
339
|
/* b) choose the least destructive entry to recycle:
|
mbed714 |
0:98aadd37a5c5
|
340
|
* 1) empty entry
|
mbed714 |
0:98aadd37a5c5
|
341
|
* 2) oldest stable entry
|
mbed714 |
0:98aadd37a5c5
|
342
|
* 3) oldest pending entry without queued packets
|
mbed714 |
0:98aadd37a5c5
|
343
|
* 4) oldest pending entry with queued packets
|
mbed714 |
0:98aadd37a5c5
|
344
|
*
|
mbed714 |
0:98aadd37a5c5
|
345
|
* { ETHARP_FLAG_TRY_HARD is set at this point }
|
mbed714 |
0:98aadd37a5c5
|
346
|
*/
|
mbed714 |
0:98aadd37a5c5
|
347
|
|
mbed714 |
0:98aadd37a5c5
|
348
|
/* 1) empty entry available? */
|
mbed714 |
0:98aadd37a5c5
|
349
|
if (empty < ARP_TABLE_SIZE) {
|
mbed714 |
0:98aadd37a5c5
|
350
|
i = empty;
|
mbed714 |
0:98aadd37a5c5
|
351
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting empty entry %"U16_F"\n", (u16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
352
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
353
|
/* 2) found recyclable stable entry? */
|
mbed714 |
0:98aadd37a5c5
|
354
|
if (old_stable < ARP_TABLE_SIZE) {
|
mbed714 |
0:98aadd37a5c5
|
355
|
/* recycle oldest stable*/
|
mbed714 |
0:98aadd37a5c5
|
356
|
i = old_stable;
|
mbed714 |
0:98aadd37a5c5
|
357
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest stable entry %"U16_F"\n", (u16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
358
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
359
|
/* no queued packets should exist on stable entries */
|
mbed714 |
0:98aadd37a5c5
|
360
|
LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
|
mbed714 |
0:98aadd37a5c5
|
361
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
362
|
/* 3) found recyclable pending entry without queued packets? */
|
mbed714 |
0:98aadd37a5c5
|
363
|
} else if (old_pending < ARP_TABLE_SIZE) {
|
mbed714 |
0:98aadd37a5c5
|
364
|
/* recycle oldest pending */
|
mbed714 |
0:98aadd37a5c5
|
365
|
i = old_pending;
|
mbed714 |
0:98aadd37a5c5
|
366
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F" (without queue)\n", (u16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
367
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
368
|
/* 4) found recyclable pending entry with queued packets? */
|
mbed714 |
0:98aadd37a5c5
|
369
|
} else if (old_queue < ARP_TABLE_SIZE) {
|
mbed714 |
0:98aadd37a5c5
|
370
|
/* recycle oldest pending (queued packets are free in free_entry) */
|
mbed714 |
0:98aadd37a5c5
|
371
|
i = old_queue;
|
mbed714 |
0:98aadd37a5c5
|
372
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: selecting oldest pending entry %"U16_F", freeing packet queue %p\n", (u16_t)i, (void *)(arp_table[i].q)));
|
mbed714 |
0:98aadd37a5c5
|
373
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
374
|
/* no empty or recyclable entries found */
|
mbed714 |
0:98aadd37a5c5
|
375
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
376
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("find_entry: no empty or recyclable entries found\n"));
|
mbed714 |
0:98aadd37a5c5
|
377
|
return (s8_t)ERR_MEM;
|
mbed714 |
0:98aadd37a5c5
|
378
|
}
|
mbed714 |
0:98aadd37a5c5
|
379
|
|
mbed714 |
0:98aadd37a5c5
|
380
|
/* { empty or recyclable entry found } */
|
mbed714 |
0:98aadd37a5c5
|
381
|
LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
|
mbed714 |
0:98aadd37a5c5
|
382
|
free_entry(i);
|
mbed714 |
0:98aadd37a5c5
|
383
|
}
|
mbed714 |
0:98aadd37a5c5
|
384
|
|
mbed714 |
0:98aadd37a5c5
|
385
|
LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
|
mbed714 |
0:98aadd37a5c5
|
386
|
LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
|
mbed714 |
0:98aadd37a5c5
|
387
|
arp_table[i].state == ETHARP_STATE_EMPTY);
|
mbed714 |
0:98aadd37a5c5
|
388
|
|
mbed714 |
0:98aadd37a5c5
|
389
|
/* IP address given? */
|
mbed714 |
0:98aadd37a5c5
|
390
|
if (ipaddr != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
391
|
/* set IP address */
|
mbed714 |
0:98aadd37a5c5
|
392
|
ip_addr_copy(arp_table[i].ipaddr, *ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
393
|
}
|
mbed714 |
0:98aadd37a5c5
|
394
|
arp_table[i].ctime = 0;
|
mbed714 |
0:98aadd37a5c5
|
395
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
396
|
arp_table[i].static_entry = 0;
|
mbed714 |
0:98aadd37a5c5
|
397
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
398
|
return (err_t)i;
|
mbed714 |
0:98aadd37a5c5
|
399
|
}
|
mbed714 |
0:98aadd37a5c5
|
400
|
|
mbed714 |
0:98aadd37a5c5
|
401
|
/**
|
mbed714 |
0:98aadd37a5c5
|
402
|
* Send an IP packet on the network using netif->linkoutput
|
mbed714 |
0:98aadd37a5c5
|
403
|
* The ethernet header is filled in before sending.
|
mbed714 |
0:98aadd37a5c5
|
404
|
*
|
mbed714 |
0:98aadd37a5c5
|
405
|
* @params netif the lwIP network interface on which to send the packet
|
mbed714 |
0:98aadd37a5c5
|
406
|
* @params p the packet to send, p->payload pointing to the (uninitialized) ethernet header
|
mbed714 |
0:98aadd37a5c5
|
407
|
* @params src the source MAC address to be copied into the ethernet header
|
mbed714 |
0:98aadd37a5c5
|
408
|
* @params dst the destination MAC address to be copied into the ethernet header
|
mbed714 |
0:98aadd37a5c5
|
409
|
* @return ERR_OK if the packet was sent, any other err_t on failure
|
mbed714 |
0:98aadd37a5c5
|
410
|
*/
|
mbed714 |
0:98aadd37a5c5
|
411
|
static err_t
|
mbed714 |
0:98aadd37a5c5
|
412
|
etharp_send_ip(struct netif *netif, struct pbuf *p, struct eth_addr *src, struct eth_addr *dst)
|
mbed714 |
0:98aadd37a5c5
|
413
|
{
|
mbed714 |
0:98aadd37a5c5
|
414
|
struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
|
mbed714 |
0:98aadd37a5c5
|
415
|
|
mbed714 |
0:98aadd37a5c5
|
416
|
LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
|
mbed714 |
0:98aadd37a5c5
|
417
|
(netif->hwaddr_len == ETHARP_HWADDR_LEN));
|
mbed714 |
0:98aadd37a5c5
|
418
|
ETHADDR32_COPY(ðhdr->dest, dst);
|
mbed714 |
0:98aadd37a5c5
|
419
|
ETHADDR16_COPY(ðhdr->src, src);
|
mbed714 |
0:98aadd37a5c5
|
420
|
ethhdr->type = PP_HTONS(ETHTYPE_IP);
|
mbed714 |
0:98aadd37a5c5
|
421
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_send_ip: sending packet %p\n", (void *)p));
|
mbed714 |
0:98aadd37a5c5
|
422
|
/* send the packet */
|
mbed714 |
0:98aadd37a5c5
|
423
|
return netif->linkoutput(netif, p);
|
mbed714 |
0:98aadd37a5c5
|
424
|
}
|
mbed714 |
0:98aadd37a5c5
|
425
|
|
mbed714 |
0:98aadd37a5c5
|
426
|
/**
|
mbed714 |
0:98aadd37a5c5
|
427
|
* Update (or insert) a IP/MAC address pair in the ARP cache.
|
mbed714 |
0:98aadd37a5c5
|
428
|
*
|
mbed714 |
0:98aadd37a5c5
|
429
|
* If a pending entry is resolved, any queued packets will be sent
|
mbed714 |
0:98aadd37a5c5
|
430
|
* at this point.
|
mbed714 |
0:98aadd37a5c5
|
431
|
*
|
mbed714 |
0:98aadd37a5c5
|
432
|
* @param netif netif related to this entry (used for NETIF_ADDRHINT)
|
mbed714 |
0:98aadd37a5c5
|
433
|
* @param ipaddr IP address of the inserted ARP entry.
|
mbed714 |
0:98aadd37a5c5
|
434
|
* @param ethaddr Ethernet address of the inserted ARP entry.
|
mbed714 |
0:98aadd37a5c5
|
435
|
* @param flags @see definition of ETHARP_FLAG_*
|
mbed714 |
0:98aadd37a5c5
|
436
|
*
|
mbed714 |
0:98aadd37a5c5
|
437
|
* @return
|
mbed714 |
0:98aadd37a5c5
|
438
|
* - ERR_OK Succesfully updated ARP cache.
|
mbed714 |
0:98aadd37a5c5
|
439
|
* - ERR_MEM If we could not add a new ARP entry when ETHARP_FLAG_TRY_HARD was set.
|
mbed714 |
0:98aadd37a5c5
|
440
|
* - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
|
mbed714 |
0:98aadd37a5c5
|
441
|
*
|
mbed714 |
0:98aadd37a5c5
|
442
|
* @see pbuf_free()
|
mbed714 |
0:98aadd37a5c5
|
443
|
*/
|
mbed714 |
0:98aadd37a5c5
|
444
|
static err_t
|
mbed714 |
0:98aadd37a5c5
|
445
|
update_arp_entry(struct netif *netif, ip_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
|
mbed714 |
0:98aadd37a5c5
|
446
|
{
|
mbed714 |
0:98aadd37a5c5
|
447
|
s8_t i;
|
mbed714 |
0:98aadd37a5c5
|
448
|
LWIP_ASSERT("netif->hwaddr_len == ETHARP_HWADDR_LEN", netif->hwaddr_len == ETHARP_HWADDR_LEN);
|
mbed714 |
0:98aadd37a5c5
|
449
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
|
mbed714 |
0:98aadd37a5c5
|
450
|
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
|
mbed714 |
0:98aadd37a5c5
|
451
|
ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
|
mbed714 |
0:98aadd37a5c5
|
452
|
ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
|
mbed714 |
0:98aadd37a5c5
|
453
|
/* non-unicast address? */
|
mbed714 |
0:98aadd37a5c5
|
454
|
if (ip_addr_isany(ipaddr) ||
|
mbed714 |
0:98aadd37a5c5
|
455
|
ip_addr_isbroadcast(ipaddr, netif) ||
|
mbed714 |
0:98aadd37a5c5
|
456
|
ip_addr_ismulticast(ipaddr)) {
|
mbed714 |
0:98aadd37a5c5
|
457
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
|
mbed714 |
0:98aadd37a5c5
|
458
|
return ERR_ARG;
|
mbed714 |
0:98aadd37a5c5
|
459
|
}
|
mbed714 |
0:98aadd37a5c5
|
460
|
/* find or create ARP entry */
|
mbed714 |
0:98aadd37a5c5
|
461
|
i = find_entry(ipaddr, flags);
|
mbed714 |
0:98aadd37a5c5
|
462
|
/* bail out if no entry could be found */
|
mbed714 |
0:98aadd37a5c5
|
463
|
if (i < 0) {
|
mbed714 |
0:98aadd37a5c5
|
464
|
return (err_t)i;
|
mbed714 |
0:98aadd37a5c5
|
465
|
}
|
mbed714 |
0:98aadd37a5c5
|
466
|
|
mbed714 |
0:98aadd37a5c5
|
467
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
468
|
if (flags & ETHARP_FLAG_STATIC_ENTRY) {
|
mbed714 |
0:98aadd37a5c5
|
469
|
/* record static type */
|
mbed714 |
0:98aadd37a5c5
|
470
|
arp_table[i].static_entry = 1;
|
mbed714 |
0:98aadd37a5c5
|
471
|
}
|
mbed714 |
0:98aadd37a5c5
|
472
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
473
|
|
mbed714 |
0:98aadd37a5c5
|
474
|
/* mark it stable */
|
mbed714 |
0:98aadd37a5c5
|
475
|
arp_table[i].state = ETHARP_STATE_STABLE;
|
mbed714 |
0:98aadd37a5c5
|
476
|
|
mbed714 |
0:98aadd37a5c5
|
477
|
#if LWIP_SNMP
|
mbed714 |
0:98aadd37a5c5
|
478
|
/* record network interface */
|
mbed714 |
0:98aadd37a5c5
|
479
|
arp_table[i].netif = netif;
|
mbed714 |
0:98aadd37a5c5
|
480
|
#endif /* LWIP_SNMP */
|
mbed714 |
0:98aadd37a5c5
|
481
|
/* insert in SNMP ARP index tree */
|
mbed714 |
0:98aadd37a5c5
|
482
|
snmp_insert_arpidx_tree(netif, &arp_table[i].ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
483
|
|
mbed714 |
0:98aadd37a5c5
|
484
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("update_arp_entry: updating stable entry %"S16_F"\n", (s16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
485
|
/* update address */
|
mbed714 |
0:98aadd37a5c5
|
486
|
ETHADDR32_COPY(&arp_table[i].ethaddr, ethaddr);
|
mbed714 |
0:98aadd37a5c5
|
487
|
/* reset time stamp */
|
mbed714 |
0:98aadd37a5c5
|
488
|
arp_table[i].ctime = 0;
|
mbed714 |
0:98aadd37a5c5
|
489
|
#if ARP_QUEUEING
|
mbed714 |
0:98aadd37a5c5
|
490
|
/* this is where we will send out queued packets! */
|
mbed714 |
0:98aadd37a5c5
|
491
|
while (arp_table[i].q != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
492
|
struct pbuf *p;
|
mbed714 |
0:98aadd37a5c5
|
493
|
/* remember remainder of queue */
|
mbed714 |
0:98aadd37a5c5
|
494
|
struct etharp_q_entry *q = arp_table[i].q;
|
mbed714 |
0:98aadd37a5c5
|
495
|
/* pop first item off the queue */
|
mbed714 |
0:98aadd37a5c5
|
496
|
arp_table[i].q = q->next;
|
mbed714 |
0:98aadd37a5c5
|
497
|
/* get the packet pointer */
|
mbed714 |
0:98aadd37a5c5
|
498
|
p = q->p;
|
mbed714 |
0:98aadd37a5c5
|
499
|
/* now queue entry can be freed */
|
mbed714 |
0:98aadd37a5c5
|
500
|
memp_free(MEMP_ARP_QUEUE, q);
|
mbed714 |
0:98aadd37a5c5
|
501
|
/* send the queued IP packet */
|
mbed714 |
0:98aadd37a5c5
|
502
|
etharp_send_ip(netif, p, (struct eth_addr*)(netif->hwaddr), ethaddr);
|
mbed714 |
0:98aadd37a5c5
|
503
|
/* free the queued IP packet */
|
mbed714 |
0:98aadd37a5c5
|
504
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
505
|
}
|
mbed714 |
0:98aadd37a5c5
|
506
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
507
|
return ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
508
|
}
|
mbed714 |
0:98aadd37a5c5
|
509
|
|
mbed714 |
0:98aadd37a5c5
|
510
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
mbed714 |
0:98aadd37a5c5
|
511
|
/** Add a new static entry to the ARP table. If an entry exists for the
|
mbed714 |
0:98aadd37a5c5
|
512
|
* specified IP address, this entry is overwritten.
|
mbed714 |
0:98aadd37a5c5
|
513
|
* If packets are queued for the specified IP address, they are sent out.
|
mbed714 |
0:98aadd37a5c5
|
514
|
*
|
mbed714 |
0:98aadd37a5c5
|
515
|
* @param ipaddr IP address for the new static entry
|
mbed714 |
0:98aadd37a5c5
|
516
|
* @param ethaddr ethernet address for the new static entry
|
mbed714 |
0:98aadd37a5c5
|
517
|
* @return @see return values of etharp_add_static_entry
|
mbed714 |
0:98aadd37a5c5
|
518
|
*/
|
mbed714 |
0:98aadd37a5c5
|
519
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
520
|
etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr)
|
mbed714 |
0:98aadd37a5c5
|
521
|
{
|
mbed714 |
0:98aadd37a5c5
|
522
|
struct netif *netif;
|
mbed714 |
0:98aadd37a5c5
|
523
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
|
mbed714 |
0:98aadd37a5c5
|
524
|
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
|
mbed714 |
0:98aadd37a5c5
|
525
|
ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
|
mbed714 |
0:98aadd37a5c5
|
526
|
ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
|
mbed714 |
0:98aadd37a5c5
|
527
|
|
mbed714 |
0:98aadd37a5c5
|
528
|
netif = ip_route(ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
529
|
if (netif == NULL) {
|
mbed714 |
0:98aadd37a5c5
|
530
|
return ERR_RTE;
|
mbed714 |
0:98aadd37a5c5
|
531
|
}
|
mbed714 |
0:98aadd37a5c5
|
532
|
|
mbed714 |
0:98aadd37a5c5
|
533
|
return update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
|
mbed714 |
0:98aadd37a5c5
|
534
|
}
|
mbed714 |
0:98aadd37a5c5
|
535
|
|
mbed714 |
0:98aadd37a5c5
|
536
|
/** Remove a static entry from the ARP table previously added with a call to
|
mbed714 |
0:98aadd37a5c5
|
537
|
* etharp_add_static_entry.
|
mbed714 |
0:98aadd37a5c5
|
538
|
*
|
mbed714 |
0:98aadd37a5c5
|
539
|
* @param ipaddr IP address of the static entry to remove
|
mbed714 |
0:98aadd37a5c5
|
540
|
* @return ERR_OK: entry removed
|
mbed714 |
0:98aadd37a5c5
|
541
|
* ERR_MEM: entry wasn't found
|
mbed714 |
0:98aadd37a5c5
|
542
|
* ERR_ARG: entry wasn't a static entry but a dynamic one
|
mbed714 |
0:98aadd37a5c5
|
543
|
*/
|
mbed714 |
0:98aadd37a5c5
|
544
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
545
|
etharp_remove_static_entry(ip_addr_t *ipaddr)
|
mbed714 |
0:98aadd37a5c5
|
546
|
{
|
mbed714 |
0:98aadd37a5c5
|
547
|
s8_t i;
|
mbed714 |
0:98aadd37a5c5
|
548
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
mbed714 |
0:98aadd37a5c5
|
549
|
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
|
mbed714 |
0:98aadd37a5c5
|
550
|
|
mbed714 |
0:98aadd37a5c5
|
551
|
/* find or create ARP entry */
|
mbed714 |
0:98aadd37a5c5
|
552
|
i = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
|
mbed714 |
0:98aadd37a5c5
|
553
|
/* bail out if no entry could be found */
|
mbed714 |
0:98aadd37a5c5
|
554
|
if (i < 0) {
|
mbed714 |
0:98aadd37a5c5
|
555
|
return (err_t)i;
|
mbed714 |
0:98aadd37a5c5
|
556
|
}
|
mbed714 |
0:98aadd37a5c5
|
557
|
|
mbed714 |
0:98aadd37a5c5
|
558
|
if ((arp_table[i].state != ETHARP_STATE_STABLE) ||
|
mbed714 |
0:98aadd37a5c5
|
559
|
(arp_table[i].static_entry == 0)) {
|
mbed714 |
0:98aadd37a5c5
|
560
|
/* entry wasn't a static entry, cannot remove it */
|
mbed714 |
0:98aadd37a5c5
|
561
|
return ERR_ARG;
|
mbed714 |
0:98aadd37a5c5
|
562
|
}
|
mbed714 |
0:98aadd37a5c5
|
563
|
/* entry found, free it */
|
mbed714 |
0:98aadd37a5c5
|
564
|
free_entry(i);
|
mbed714 |
0:98aadd37a5c5
|
565
|
return ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
566
|
}
|
mbed714 |
0:98aadd37a5c5
|
567
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
mbed714 |
0:98aadd37a5c5
|
568
|
|
mbed714 |
0:98aadd37a5c5
|
569
|
/**
|
mbed714 |
0:98aadd37a5c5
|
570
|
* Finds (stable) ethernet/IP address pair from ARP table
|
mbed714 |
0:98aadd37a5c5
|
571
|
* using interface and IP address index.
|
mbed714 |
0:98aadd37a5c5
|
572
|
* @note the addresses in the ARP table are in network order!
|
mbed714 |
0:98aadd37a5c5
|
573
|
*
|
mbed714 |
0:98aadd37a5c5
|
574
|
* @param netif points to interface index
|
mbed714 |
0:98aadd37a5c5
|
575
|
* @param ipaddr points to the (network order) IP address index
|
mbed714 |
0:98aadd37a5c5
|
576
|
* @param eth_ret points to return pointer
|
mbed714 |
0:98aadd37a5c5
|
577
|
* @param ip_ret points to return pointer
|
mbed714 |
0:98aadd37a5c5
|
578
|
* @return table index if found, -1 otherwise
|
mbed714 |
0:98aadd37a5c5
|
579
|
*/
|
mbed714 |
0:98aadd37a5c5
|
580
|
s8_t
|
mbed714 |
0:98aadd37a5c5
|
581
|
etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
|
mbed714 |
0:98aadd37a5c5
|
582
|
struct eth_addr **eth_ret, ip_addr_t **ip_ret)
|
mbed714 |
0:98aadd37a5c5
|
583
|
{
|
mbed714 |
0:98aadd37a5c5
|
584
|
s8_t i;
|
mbed714 |
0:98aadd37a5c5
|
585
|
|
mbed714 |
0:98aadd37a5c5
|
586
|
LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
|
mbed714 |
0:98aadd37a5c5
|
587
|
eth_ret != NULL && ip_ret != NULL);
|
mbed714 |
0:98aadd37a5c5
|
588
|
|
mbed714 |
0:98aadd37a5c5
|
589
|
LWIP_UNUSED_ARG(netif);
|
mbed714 |
0:98aadd37a5c5
|
590
|
|
mbed714 |
0:98aadd37a5c5
|
591
|
i = find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY);
|
mbed714 |
0:98aadd37a5c5
|
592
|
if((i >= 0) && arp_table[i].state == ETHARP_STATE_STABLE) {
|
mbed714 |
0:98aadd37a5c5
|
593
|
*eth_ret = &arp_table[i].ethaddr;
|
mbed714 |
0:98aadd37a5c5
|
594
|
*ip_ret = &arp_table[i].ipaddr;
|
mbed714 |
0:98aadd37a5c5
|
595
|
return i;
|
mbed714 |
0:98aadd37a5c5
|
596
|
}
|
mbed714 |
0:98aadd37a5c5
|
597
|
return -1;
|
mbed714 |
0:98aadd37a5c5
|
598
|
}
|
mbed714 |
0:98aadd37a5c5
|
599
|
|
mbed714 |
0:98aadd37a5c5
|
600
|
#if ETHARP_TRUST_IP_MAC
|
mbed714 |
0:98aadd37a5c5
|
601
|
/**
|
mbed714 |
0:98aadd37a5c5
|
602
|
* Updates the ARP table using the given IP packet.
|
mbed714 |
0:98aadd37a5c5
|
603
|
*
|
mbed714 |
0:98aadd37a5c5
|
604
|
* Uses the incoming IP packet's source address to update the
|
mbed714 |
0:98aadd37a5c5
|
605
|
* ARP cache for the local network. The function does not alter
|
mbed714 |
0:98aadd37a5c5
|
606
|
* or free the packet. This function must be called before the
|
mbed714 |
0:98aadd37a5c5
|
607
|
* packet p is passed to the IP layer.
|
mbed714 |
0:98aadd37a5c5
|
608
|
*
|
mbed714 |
0:98aadd37a5c5
|
609
|
* @param netif The lwIP network interface on which the IP packet pbuf arrived.
|
mbed714 |
0:98aadd37a5c5
|
610
|
* @param p The IP packet that arrived on netif.
|
mbed714 |
0:98aadd37a5c5
|
611
|
*
|
mbed714 |
0:98aadd37a5c5
|
612
|
* @return NULL
|
mbed714 |
0:98aadd37a5c5
|
613
|
*
|
mbed714 |
0:98aadd37a5c5
|
614
|
* @see pbuf_free()
|
mbed714 |
0:98aadd37a5c5
|
615
|
*/
|
mbed714 |
0:98aadd37a5c5
|
616
|
static void
|
mbed714 |
0:98aadd37a5c5
|
617
|
etharp_ip_input(struct netif *netif, struct pbuf *p)
|
mbed714 |
0:98aadd37a5c5
|
618
|
{
|
mbed714 |
0:98aadd37a5c5
|
619
|
struct eth_hdr *ethhdr;
|
mbed714 |
0:98aadd37a5c5
|
620
|
struct ip_hdr *iphdr;
|
mbed714 |
0:98aadd37a5c5
|
621
|
ip_addr_t iphdr_src;
|
mbed714 |
0:98aadd37a5c5
|
622
|
LWIP_ERROR("netif != NULL", (netif != NULL), return;);
|
mbed714 |
0:98aadd37a5c5
|
623
|
|
mbed714 |
0:98aadd37a5c5
|
624
|
/* Only insert an entry if the source IP address of the
|
mbed714 |
0:98aadd37a5c5
|
625
|
incoming IP packet comes from a host on the local network. */
|
mbed714 |
0:98aadd37a5c5
|
626
|
ethhdr = (struct eth_hdr *)p->payload;
|
mbed714 |
0:98aadd37a5c5
|
627
|
iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
|
mbed714 |
0:98aadd37a5c5
|
628
|
#if ETHARP_SUPPORT_VLAN
|
mbed714 |
0:98aadd37a5c5
|
629
|
if (ethhdr->type == ETHTYPE_VLAN) {
|
mbed714 |
0:98aadd37a5c5
|
630
|
iphdr = (struct ip_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
|
mbed714 |
0:98aadd37a5c5
|
631
|
}
|
mbed714 |
0:98aadd37a5c5
|
632
|
#endif /* ETHARP_SUPPORT_VLAN */
|
mbed714 |
0:98aadd37a5c5
|
633
|
|
mbed714 |
0:98aadd37a5c5
|
634
|
ip_addr_copy(iphdr_src, iphdr->src);
|
mbed714 |
0:98aadd37a5c5
|
635
|
|
mbed714 |
0:98aadd37a5c5
|
636
|
/* source is not on the local network? */
|
mbed714 |
0:98aadd37a5c5
|
637
|
if (!ip_addr_netcmp(&iphdr_src, &(netif->ip_addr), &(netif->netmask))) {
|
mbed714 |
0:98aadd37a5c5
|
638
|
/* do nothing */
|
mbed714 |
0:98aadd37a5c5
|
639
|
return;
|
mbed714 |
0:98aadd37a5c5
|
640
|
}
|
mbed714 |
0:98aadd37a5c5
|
641
|
|
mbed714 |
0:98aadd37a5c5
|
642
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
|
mbed714 |
0:98aadd37a5c5
|
643
|
/* update the source IP address in the cache, if present */
|
mbed714 |
0:98aadd37a5c5
|
644
|
/* @todo We could use ETHARP_FLAG_TRY_HARD if we think we are going to talk
|
mbed714 |
0:98aadd37a5c5
|
645
|
* back soon (for example, if the destination IP address is ours. */
|
mbed714 |
0:98aadd37a5c5
|
646
|
update_arp_entry(netif, &iphdr_src, &(ethhdr->src), ETHARP_FLAG_FIND_ONLY);
|
mbed714 |
0:98aadd37a5c5
|
647
|
}
|
mbed714 |
0:98aadd37a5c5
|
648
|
#endif /* ETHARP_TRUST_IP_MAC */
|
mbed714 |
0:98aadd37a5c5
|
649
|
|
mbed714 |
0:98aadd37a5c5
|
650
|
/**
|
mbed714 |
0:98aadd37a5c5
|
651
|
* Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
|
mbed714 |
0:98aadd37a5c5
|
652
|
* send out queued IP packets. Updates cache with snooped address pairs.
|
mbed714 |
0:98aadd37a5c5
|
653
|
*
|
mbed714 |
0:98aadd37a5c5
|
654
|
* Should be called for incoming ARP packets. The pbuf in the argument
|
mbed714 |
0:98aadd37a5c5
|
655
|
* is freed by this function.
|
mbed714 |
0:98aadd37a5c5
|
656
|
*
|
mbed714 |
0:98aadd37a5c5
|
657
|
* @param netif The lwIP network interface on which the ARP packet pbuf arrived.
|
mbed714 |
0:98aadd37a5c5
|
658
|
* @param ethaddr Ethernet address of netif.
|
mbed714 |
0:98aadd37a5c5
|
659
|
* @param p The ARP packet that arrived on netif. Is freed by this function.
|
mbed714 |
0:98aadd37a5c5
|
660
|
*
|
mbed714 |
0:98aadd37a5c5
|
661
|
* @return NULL
|
mbed714 |
0:98aadd37a5c5
|
662
|
*
|
mbed714 |
0:98aadd37a5c5
|
663
|
* @see pbuf_free()
|
mbed714 |
0:98aadd37a5c5
|
664
|
*/
|
mbed714 |
0:98aadd37a5c5
|
665
|
static void
|
mbed714 |
0:98aadd37a5c5
|
666
|
etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
|
mbed714 |
0:98aadd37a5c5
|
667
|
{
|
mbed714 |
0:98aadd37a5c5
|
668
|
struct etharp_hdr *hdr;
|
mbed714 |
0:98aadd37a5c5
|
669
|
struct eth_hdr *ethhdr;
|
mbed714 |
0:98aadd37a5c5
|
670
|
/* these are aligned properly, whereas the ARP header fields might not be */
|
mbed714 |
0:98aadd37a5c5
|
671
|
ip_addr_t sipaddr, dipaddr;
|
mbed714 |
0:98aadd37a5c5
|
672
|
u8_t for_us;
|
mbed714 |
0:98aadd37a5c5
|
673
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
674
|
const u8_t * ethdst_hwaddr;
|
mbed714 |
0:98aadd37a5c5
|
675
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
676
|
|
mbed714 |
0:98aadd37a5c5
|
677
|
LWIP_ERROR("netif != NULL", (netif != NULL), return;);
|
mbed714 |
0:98aadd37a5c5
|
678
|
|
mbed714 |
0:98aadd37a5c5
|
679
|
/* drop short ARP packets: we have to check for p->len instead of p->tot_len here
|
mbed714 |
0:98aadd37a5c5
|
680
|
since a struct etharp_hdr is pointed to p->payload, so it musn't be chained! */
|
mbed714 |
0:98aadd37a5c5
|
681
|
if (p->len < SIZEOF_ETHARP_PACKET) {
|
mbed714 |
0:98aadd37a5c5
|
682
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
|
mbed714 |
0:98aadd37a5c5
|
683
|
("etharp_arp_input: packet dropped, too short (%"S16_F"/%"S16_F")\n", p->tot_len,
|
mbed714 |
0:98aadd37a5c5
|
684
|
(s16_t)SIZEOF_ETHARP_PACKET));
|
mbed714 |
0:98aadd37a5c5
|
685
|
ETHARP_STATS_INC(etharp.lenerr);
|
mbed714 |
0:98aadd37a5c5
|
686
|
ETHARP_STATS_INC(etharp.drop);
|
mbed714 |
0:98aadd37a5c5
|
687
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
688
|
return;
|
mbed714 |
0:98aadd37a5c5
|
689
|
}
|
mbed714 |
0:98aadd37a5c5
|
690
|
|
mbed714 |
0:98aadd37a5c5
|
691
|
ethhdr = (struct eth_hdr *)p->payload;
|
mbed714 |
0:98aadd37a5c5
|
692
|
hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
|
mbed714 |
0:98aadd37a5c5
|
693
|
#if ETHARP_SUPPORT_VLAN
|
mbed714 |
0:98aadd37a5c5
|
694
|
if (ethhdr->type == ETHTYPE_VLAN) {
|
mbed714 |
0:98aadd37a5c5
|
695
|
hdr = (struct etharp_hdr *)(((u8_t*)ethhdr) + SIZEOF_ETH_HDR + SIZEOF_VLAN_HDR);
|
mbed714 |
0:98aadd37a5c5
|
696
|
}
|
mbed714 |
0:98aadd37a5c5
|
697
|
#endif /* ETHARP_SUPPORT_VLAN */
|
mbed714 |
0:98aadd37a5c5
|
698
|
|
mbed714 |
0:98aadd37a5c5
|
699
|
/* RFC 826 "Packet Reception": */
|
mbed714 |
0:98aadd37a5c5
|
700
|
if ((hdr->hwtype != PP_HTONS(HWTYPE_ETHERNET)) ||
|
mbed714 |
0:98aadd37a5c5
|
701
|
(hdr->hwlen != ETHARP_HWADDR_LEN) ||
|
mbed714 |
0:98aadd37a5c5
|
702
|
(hdr->protolen != sizeof(ip_addr_t)) ||
|
mbed714 |
0:98aadd37a5c5
|
703
|
(hdr->proto != PP_HTONS(ETHTYPE_IP)) ||
|
mbed714 |
0:98aadd37a5c5
|
704
|
(ethhdr->type != PP_HTONS(ETHTYPE_ARP))) {
|
mbed714 |
0:98aadd37a5c5
|
705
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
|
mbed714 |
0:98aadd37a5c5
|
706
|
("etharp_arp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
|
mbed714 |
0:98aadd37a5c5
|
707
|
hdr->hwtype, hdr->hwlen, hdr->proto, hdr->protolen, ethhdr->type));
|
mbed714 |
0:98aadd37a5c5
|
708
|
ETHARP_STATS_INC(etharp.proterr);
|
mbed714 |
0:98aadd37a5c5
|
709
|
ETHARP_STATS_INC(etharp.drop);
|
mbed714 |
0:98aadd37a5c5
|
710
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
711
|
return;
|
mbed714 |
0:98aadd37a5c5
|
712
|
}
|
mbed714 |
0:98aadd37a5c5
|
713
|
ETHARP_STATS_INC(etharp.recv);
|
mbed714 |
0:98aadd37a5c5
|
714
|
|
mbed714 |
0:98aadd37a5c5
|
715
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
716
|
/* We have to check if a host already has configured our random
|
mbed714 |
0:98aadd37a5c5
|
717
|
* created link local address and continously check if there is
|
mbed714 |
0:98aadd37a5c5
|
718
|
* a host with this IP-address so we can detect collisions */
|
mbed714 |
0:98aadd37a5c5
|
719
|
autoip_arp_reply(netif, hdr);
|
mbed714 |
0:98aadd37a5c5
|
720
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
721
|
|
mbed714 |
0:98aadd37a5c5
|
722
|
/* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
|
mbed714 |
0:98aadd37a5c5
|
723
|
* structure packing (not using structure copy which breaks strict-aliasing rules). */
|
mbed714 |
0:98aadd37a5c5
|
724
|
IPADDR2_COPY(&sipaddr, &hdr->sipaddr);
|
mbed714 |
0:98aadd37a5c5
|
725
|
IPADDR2_COPY(&dipaddr, &hdr->dipaddr);
|
mbed714 |
0:98aadd37a5c5
|
726
|
|
mbed714 |
0:98aadd37a5c5
|
727
|
/* this interface is not configured? */
|
mbed714 |
0:98aadd37a5c5
|
728
|
if (ip_addr_isany(&netif->ip_addr)) {
|
mbed714 |
0:98aadd37a5c5
|
729
|
for_us = 0;
|
mbed714 |
0:98aadd37a5c5
|
730
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
731
|
/* ARP packet directed to us? */
|
mbed714 |
0:98aadd37a5c5
|
732
|
for_us = (u8_t)ip_addr_cmp(&dipaddr, &(netif->ip_addr));
|
mbed714 |
0:98aadd37a5c5
|
733
|
}
|
mbed714 |
0:98aadd37a5c5
|
734
|
|
mbed714 |
0:98aadd37a5c5
|
735
|
/* ARP message directed to us?
|
mbed714 |
0:98aadd37a5c5
|
736
|
-> add IP address in ARP cache; assume requester wants to talk to us,
|
mbed714 |
0:98aadd37a5c5
|
737
|
can result in directly sending the queued packets for this host.
|
mbed714 |
0:98aadd37a5c5
|
738
|
ARP message not directed to us?
|
mbed714 |
0:98aadd37a5c5
|
739
|
-> update the source IP address in the cache, if present */
|
mbed714 |
0:98aadd37a5c5
|
740
|
update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
|
mbed714 |
0:98aadd37a5c5
|
741
|
for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
|
mbed714 |
0:98aadd37a5c5
|
742
|
|
mbed714 |
0:98aadd37a5c5
|
743
|
/* now act on the message itself */
|
mbed714 |
0:98aadd37a5c5
|
744
|
switch (hdr->opcode) {
|
mbed714 |
0:98aadd37a5c5
|
745
|
/* ARP request? */
|
mbed714 |
0:98aadd37a5c5
|
746
|
case PP_HTONS(ARP_REQUEST):
|
mbed714 |
0:98aadd37a5c5
|
747
|
/* ARP request. If it asked for our address, we send out a
|
mbed714 |
0:98aadd37a5c5
|
748
|
* reply. In any case, we time-stamp any existing ARP entry,
|
mbed714 |
0:98aadd37a5c5
|
749
|
* and possiby send out an IP packet that was queued on it. */
|
mbed714 |
0:98aadd37a5c5
|
750
|
|
mbed714 |
0:98aadd37a5c5
|
751
|
LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
|
mbed714 |
0:98aadd37a5c5
|
752
|
/* ARP request for our address? */
|
mbed714 |
0:98aadd37a5c5
|
753
|
if (for_us) {
|
mbed714 |
0:98aadd37a5c5
|
754
|
|
mbed714 |
0:98aadd37a5c5
|
755
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
|
mbed714 |
0:98aadd37a5c5
|
756
|
/* Re-use pbuf to send ARP reply.
|
mbed714 |
0:98aadd37a5c5
|
757
|
Since we are re-using an existing pbuf, we can't call etharp_raw since
|
mbed714 |
0:98aadd37a5c5
|
758
|
that would allocate a new pbuf. */
|
mbed714 |
0:98aadd37a5c5
|
759
|
hdr->opcode = htons(ARP_REPLY);
|
mbed714 |
0:98aadd37a5c5
|
760
|
|
mbed714 |
0:98aadd37a5c5
|
761
|
IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr);
|
mbed714 |
0:98aadd37a5c5
|
762
|
IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr);
|
mbed714 |
0:98aadd37a5c5
|
763
|
|
mbed714 |
0:98aadd37a5c5
|
764
|
LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
|
mbed714 |
0:98aadd37a5c5
|
765
|
(netif->hwaddr_len == ETHARP_HWADDR_LEN));
|
mbed714 |
0:98aadd37a5c5
|
766
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
767
|
/* If we are using Link-Local, all ARP packets that contain a Link-Local
|
mbed714 |
0:98aadd37a5c5
|
768
|
* 'sender IP address' MUST be sent using link-layer broadcast instead of
|
mbed714 |
0:98aadd37a5c5
|
769
|
* link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
|
mbed714 |
0:98aadd37a5c5
|
770
|
ethdst_hwaddr = ip_addr_islinklocal(&netif->ip_addr) ? (u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr;
|
mbed714 |
0:98aadd37a5c5
|
771
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
772
|
|
mbed714 |
0:98aadd37a5c5
|
773
|
ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr);
|
mbed714 |
0:98aadd37a5c5
|
774
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
775
|
ETHADDR16_COPY(ðhdr->dest, ethdst_hwaddr);
|
mbed714 |
0:98aadd37a5c5
|
776
|
#else /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
777
|
ETHADDR16_COPY(ðhdr->dest, &hdr->shwaddr);
|
mbed714 |
0:98aadd37a5c5
|
778
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
779
|
ETHADDR16_COPY(&hdr->shwaddr, ethaddr);
|
mbed714 |
0:98aadd37a5c5
|
780
|
ETHADDR16_COPY(ðhdr->src, ethaddr);
|
mbed714 |
0:98aadd37a5c5
|
781
|
|
mbed714 |
0:98aadd37a5c5
|
782
|
/* hwtype, hwaddr_len, proto, protolen and the type in the ethernet header
|
mbed714 |
0:98aadd37a5c5
|
783
|
are already correct, we tested that before */
|
mbed714 |
0:98aadd37a5c5
|
784
|
|
mbed714 |
0:98aadd37a5c5
|
785
|
/* return ARP reply */
|
mbed714 |
0:98aadd37a5c5
|
786
|
netif->linkoutput(netif, p);
|
mbed714 |
0:98aadd37a5c5
|
787
|
/* we are not configured? */
|
mbed714 |
0:98aadd37a5c5
|
788
|
} else if (ip_addr_isany(&netif->ip_addr)) {
|
mbed714 |
0:98aadd37a5c5
|
789
|
/* { for_us == 0 and netif->ip_addr.addr == 0 } */
|
mbed714 |
0:98aadd37a5c5
|
790
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
|
mbed714 |
0:98aadd37a5c5
|
791
|
/* request was not directed to us */
|
mbed714 |
0:98aadd37a5c5
|
792
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
793
|
/* { for_us == 0 and netif->ip_addr.addr != 0 } */
|
mbed714 |
0:98aadd37a5c5
|
794
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
|
mbed714 |
0:98aadd37a5c5
|
795
|
}
|
mbed714 |
0:98aadd37a5c5
|
796
|
break;
|
mbed714 |
0:98aadd37a5c5
|
797
|
case PP_HTONS(ARP_REPLY):
|
mbed714 |
0:98aadd37a5c5
|
798
|
/* ARP reply. We already updated the ARP cache earlier. */
|
mbed714 |
0:98aadd37a5c5
|
799
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
|
mbed714 |
0:98aadd37a5c5
|
800
|
#if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
|
mbed714 |
0:98aadd37a5c5
|
801
|
/* DHCP wants to know about ARP replies from any host with an
|
mbed714 |
0:98aadd37a5c5
|
802
|
* IP address also offered to us by the DHCP server. We do not
|
mbed714 |
0:98aadd37a5c5
|
803
|
* want to take a duplicate IP address on a single network.
|
mbed714 |
0:98aadd37a5c5
|
804
|
* @todo How should we handle redundant (fail-over) interfaces? */
|
mbed714 |
0:98aadd37a5c5
|
805
|
dhcp_arp_reply(netif, &sipaddr);
|
mbed714 |
0:98aadd37a5c5
|
806
|
#endif /* (LWIP_DHCP && DHCP_DOES_ARP_CHECK) */
|
mbed714 |
0:98aadd37a5c5
|
807
|
break;
|
mbed714 |
0:98aadd37a5c5
|
808
|
default:
|
mbed714 |
0:98aadd37a5c5
|
809
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %"S16_F"\n", htons(hdr->opcode)));
|
mbed714 |
0:98aadd37a5c5
|
810
|
ETHARP_STATS_INC(etharp.err);
|
mbed714 |
0:98aadd37a5c5
|
811
|
break;
|
mbed714 |
0:98aadd37a5c5
|
812
|
}
|
mbed714 |
0:98aadd37a5c5
|
813
|
/* free ARP packet */
|
mbed714 |
0:98aadd37a5c5
|
814
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
815
|
}
|
mbed714 |
0:98aadd37a5c5
|
816
|
|
mbed714 |
0:98aadd37a5c5
|
817
|
/**
|
mbed714 |
0:98aadd37a5c5
|
818
|
* Resolve and fill-in Ethernet address header for outgoing IP packet.
|
mbed714 |
0:98aadd37a5c5
|
819
|
*
|
mbed714 |
0:98aadd37a5c5
|
820
|
* For IP multicast and broadcast, corresponding Ethernet addresses
|
mbed714 |
0:98aadd37a5c5
|
821
|
* are selected and the packet is transmitted on the link.
|
mbed714 |
0:98aadd37a5c5
|
822
|
*
|
mbed714 |
0:98aadd37a5c5
|
823
|
* For unicast addresses, the packet is submitted to etharp_query(). In
|
mbed714 |
0:98aadd37a5c5
|
824
|
* case the IP address is outside the local network, the IP address of
|
mbed714 |
0:98aadd37a5c5
|
825
|
* the gateway is used.
|
mbed714 |
0:98aadd37a5c5
|
826
|
*
|
mbed714 |
0:98aadd37a5c5
|
827
|
* @param netif The lwIP network interface which the IP packet will be sent on.
|
mbed714 |
0:98aadd37a5c5
|
828
|
* @param q The pbuf(s) containing the IP packet to be sent.
|
mbed714 |
0:98aadd37a5c5
|
829
|
* @param ipaddr The IP address of the packet destination.
|
mbed714 |
0:98aadd37a5c5
|
830
|
*
|
mbed714 |
0:98aadd37a5c5
|
831
|
* @return
|
mbed714 |
0:98aadd37a5c5
|
832
|
* - ERR_RTE No route to destination (no gateway to external networks),
|
mbed714 |
0:98aadd37a5c5
|
833
|
* or the return type of either etharp_query() or etharp_send_ip().
|
mbed714 |
0:98aadd37a5c5
|
834
|
*/
|
mbed714 |
0:98aadd37a5c5
|
835
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
836
|
etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)
|
mbed714 |
0:98aadd37a5c5
|
837
|
{
|
mbed714 |
0:98aadd37a5c5
|
838
|
struct eth_addr *dest, mcastaddr;
|
mbed714 |
0:98aadd37a5c5
|
839
|
|
mbed714 |
0:98aadd37a5c5
|
840
|
/* make room for Ethernet header - should not fail */
|
mbed714 |
0:98aadd37a5c5
|
841
|
if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
|
mbed714 |
0:98aadd37a5c5
|
842
|
/* bail out */
|
mbed714 |
0:98aadd37a5c5
|
843
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
|
mbed714 |
0:98aadd37a5c5
|
844
|
("etharp_output: could not allocate room for header.\n"));
|
mbed714 |
0:98aadd37a5c5
|
845
|
LINK_STATS_INC(link.lenerr);
|
mbed714 |
0:98aadd37a5c5
|
846
|
return ERR_BUF;
|
mbed714 |
0:98aadd37a5c5
|
847
|
}
|
mbed714 |
0:98aadd37a5c5
|
848
|
|
mbed714 |
0:98aadd37a5c5
|
849
|
/* assume unresolved Ethernet address */
|
mbed714 |
0:98aadd37a5c5
|
850
|
dest = NULL;
|
mbed714 |
0:98aadd37a5c5
|
851
|
/* Determine on destination hardware address. Broadcasts and multicasts
|
mbed714 |
0:98aadd37a5c5
|
852
|
* are special, other IP addresses are looked up in the ARP table. */
|
mbed714 |
0:98aadd37a5c5
|
853
|
|
mbed714 |
0:98aadd37a5c5
|
854
|
/* broadcast destination IP address? */
|
mbed714 |
0:98aadd37a5c5
|
855
|
if (ip_addr_isbroadcast(ipaddr, netif)) {
|
mbed714 |
0:98aadd37a5c5
|
856
|
/* broadcast on Ethernet also */
|
mbed714 |
0:98aadd37a5c5
|
857
|
dest = (struct eth_addr *)ðbroadcast;
|
mbed714 |
0:98aadd37a5c5
|
858
|
/* multicast destination IP address? */
|
mbed714 |
0:98aadd37a5c5
|
859
|
} else if (ip_addr_ismulticast(ipaddr)) {
|
mbed714 |
0:98aadd37a5c5
|
860
|
/* Hash IP multicast address to MAC address.*/
|
mbed714 |
0:98aadd37a5c5
|
861
|
mcastaddr.addr[0] = 0x01;
|
mbed714 |
0:98aadd37a5c5
|
862
|
mcastaddr.addr[1] = 0x00;
|
mbed714 |
0:98aadd37a5c5
|
863
|
mcastaddr.addr[2] = 0x5e;
|
mbed714 |
0:98aadd37a5c5
|
864
|
mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
|
mbed714 |
0:98aadd37a5c5
|
865
|
mcastaddr.addr[4] = ip4_addr3(ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
866
|
mcastaddr.addr[5] = ip4_addr4(ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
867
|
/* destination Ethernet address is multicast */
|
mbed714 |
0:98aadd37a5c5
|
868
|
dest = &mcastaddr;
|
mbed714 |
0:98aadd37a5c5
|
869
|
/* unicast destination IP address? */
|
mbed714 |
0:98aadd37a5c5
|
870
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
871
|
/* outside local network? */
|
mbed714 |
0:98aadd37a5c5
|
872
|
if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) &&
|
mbed714 |
0:98aadd37a5c5
|
873
|
!ip_addr_islinklocal(ipaddr)) {
|
mbed714 |
0:98aadd37a5c5
|
874
|
/* interface has default gateway? */
|
mbed714 |
0:98aadd37a5c5
|
875
|
if (!ip_addr_isany(&netif->gw)) {
|
mbed714 |
0:98aadd37a5c5
|
876
|
/* send to hardware address of default gateway IP address */
|
mbed714 |
0:98aadd37a5c5
|
877
|
ipaddr = &(netif->gw);
|
mbed714 |
0:98aadd37a5c5
|
878
|
/* no default gateway available */
|
mbed714 |
0:98aadd37a5c5
|
879
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
880
|
/* no route to destination error (default gateway missing) */
|
mbed714 |
0:98aadd37a5c5
|
881
|
return ERR_RTE;
|
mbed714 |
0:98aadd37a5c5
|
882
|
}
|
mbed714 |
0:98aadd37a5c5
|
883
|
}
|
mbed714 |
0:98aadd37a5c5
|
884
|
#if LWIP_NETIF_HWADDRHINT
|
mbed714 |
0:98aadd37a5c5
|
885
|
if (netif->addr_hint != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
886
|
/* per-pcb cached entry was given */
|
mbed714 |
0:98aadd37a5c5
|
887
|
u8_t etharp_cached_entry = *(netif->addr_hint);
|
mbed714 |
0:98aadd37a5c5
|
888
|
if (etharp_cached_entry < ARP_TABLE_SIZE) {
|
mbed714 |
0:98aadd37a5c5
|
889
|
#endif /* LWIP_NETIF_HWADDRHINT */
|
mbed714 |
0:98aadd37a5c5
|
890
|
if ((arp_table[etharp_cached_entry].state == ETHARP_STATE_STABLE) &&
|
mbed714 |
0:98aadd37a5c5
|
891
|
(ip_addr_cmp(ipaddr, &arp_table[etharp_cached_entry].ipaddr))) {
|
mbed714 |
0:98aadd37a5c5
|
892
|
/* the per-pcb-cached entry is stable and the right one! */
|
mbed714 |
0:98aadd37a5c5
|
893
|
ETHARP_STATS_INC(etharp.cachehit);
|
mbed714 |
0:98aadd37a5c5
|
894
|
return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr),
|
mbed714 |
0:98aadd37a5c5
|
895
|
&arp_table[etharp_cached_entry].ethaddr);
|
mbed714 |
0:98aadd37a5c5
|
896
|
}
|
mbed714 |
0:98aadd37a5c5
|
897
|
#if LWIP_NETIF_HWADDRHINT
|
mbed714 |
0:98aadd37a5c5
|
898
|
}
|
mbed714 |
0:98aadd37a5c5
|
899
|
}
|
mbed714 |
0:98aadd37a5c5
|
900
|
#endif /* LWIP_NETIF_HWADDRHINT */
|
mbed714 |
0:98aadd37a5c5
|
901
|
/* queue on destination Ethernet address belonging to ipaddr */
|
mbed714 |
0:98aadd37a5c5
|
902
|
return etharp_query(netif, ipaddr, q);
|
mbed714 |
0:98aadd37a5c5
|
903
|
}
|
mbed714 |
0:98aadd37a5c5
|
904
|
|
mbed714 |
0:98aadd37a5c5
|
905
|
/* continuation for multicast/broadcast destinations */
|
mbed714 |
0:98aadd37a5c5
|
906
|
/* obtain source Ethernet address of the given interface */
|
mbed714 |
0:98aadd37a5c5
|
907
|
/* send packet directly on the link */
|
mbed714 |
0:98aadd37a5c5
|
908
|
return etharp_send_ip(netif, q, (struct eth_addr*)(netif->hwaddr), dest);
|
mbed714 |
0:98aadd37a5c5
|
909
|
}
|
mbed714 |
0:98aadd37a5c5
|
910
|
|
mbed714 |
0:98aadd37a5c5
|
911
|
/**
|
mbed714 |
0:98aadd37a5c5
|
912
|
* Send an ARP request for the given IP address and/or queue a packet.
|
mbed714 |
0:98aadd37a5c5
|
913
|
*
|
mbed714 |
0:98aadd37a5c5
|
914
|
* If the IP address was not yet in the cache, a pending ARP cache entry
|
mbed714 |
0:98aadd37a5c5
|
915
|
* is added and an ARP request is sent for the given address. The packet
|
mbed714 |
0:98aadd37a5c5
|
916
|
* is queued on this entry.
|
mbed714 |
0:98aadd37a5c5
|
917
|
*
|
mbed714 |
0:98aadd37a5c5
|
918
|
* If the IP address was already pending in the cache, a new ARP request
|
mbed714 |
0:98aadd37a5c5
|
919
|
* is sent for the given address. The packet is queued on this entry.
|
mbed714 |
0:98aadd37a5c5
|
920
|
*
|
mbed714 |
0:98aadd37a5c5
|
921
|
* If the IP address was already stable in the cache, and a packet is
|
mbed714 |
0:98aadd37a5c5
|
922
|
* given, it is directly sent and no ARP request is sent out.
|
mbed714 |
0:98aadd37a5c5
|
923
|
*
|
mbed714 |
0:98aadd37a5c5
|
924
|
* If the IP address was already stable in the cache, and no packet is
|
mbed714 |
0:98aadd37a5c5
|
925
|
* given, an ARP request is sent out.
|
mbed714 |
0:98aadd37a5c5
|
926
|
*
|
mbed714 |
0:98aadd37a5c5
|
927
|
* @param netif The lwIP network interface on which ipaddr
|
mbed714 |
0:98aadd37a5c5
|
928
|
* must be queried for.
|
mbed714 |
0:98aadd37a5c5
|
929
|
* @param ipaddr The IP address to be resolved.
|
mbed714 |
0:98aadd37a5c5
|
930
|
* @param q If non-NULL, a pbuf that must be delivered to the IP address.
|
mbed714 |
0:98aadd37a5c5
|
931
|
* q is not freed by this function.
|
mbed714 |
0:98aadd37a5c5
|
932
|
*
|
mbed714 |
0:98aadd37a5c5
|
933
|
* @note q must only be ONE packet, not a packet queue!
|
mbed714 |
0:98aadd37a5c5
|
934
|
*
|
mbed714 |
0:98aadd37a5c5
|
935
|
* @return
|
mbed714 |
0:98aadd37a5c5
|
936
|
* - ERR_BUF Could not make room for Ethernet header.
|
mbed714 |
0:98aadd37a5c5
|
937
|
* - ERR_MEM Hardware address unknown, and no more ARP entries available
|
mbed714 |
0:98aadd37a5c5
|
938
|
* to query for address or queue the packet.
|
mbed714 |
0:98aadd37a5c5
|
939
|
* - ERR_MEM Could not queue packet due to memory shortage.
|
mbed714 |
0:98aadd37a5c5
|
940
|
* - ERR_RTE No route to destination (no gateway to external networks).
|
mbed714 |
0:98aadd37a5c5
|
941
|
* - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
|
mbed714 |
0:98aadd37a5c5
|
942
|
*
|
mbed714 |
0:98aadd37a5c5
|
943
|
*/
|
mbed714 |
0:98aadd37a5c5
|
944
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
945
|
etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)
|
mbed714 |
0:98aadd37a5c5
|
946
|
{
|
mbed714 |
0:98aadd37a5c5
|
947
|
struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
|
mbed714 |
0:98aadd37a5c5
|
948
|
err_t result = ERR_MEM;
|
mbed714 |
0:98aadd37a5c5
|
949
|
s8_t i; /* ARP entry index */
|
mbed714 |
0:98aadd37a5c5
|
950
|
|
mbed714 |
0:98aadd37a5c5
|
951
|
/* non-unicast address? */
|
mbed714 |
0:98aadd37a5c5
|
952
|
if (ip_addr_isbroadcast(ipaddr, netif) ||
|
mbed714 |
0:98aadd37a5c5
|
953
|
ip_addr_ismulticast(ipaddr) ||
|
mbed714 |
0:98aadd37a5c5
|
954
|
ip_addr_isany(ipaddr)) {
|
mbed714 |
0:98aadd37a5c5
|
955
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
|
mbed714 |
0:98aadd37a5c5
|
956
|
return ERR_ARG;
|
mbed714 |
0:98aadd37a5c5
|
957
|
}
|
mbed714 |
0:98aadd37a5c5
|
958
|
|
mbed714 |
0:98aadd37a5c5
|
959
|
/* find entry in ARP cache, ask to create entry if queueing packet */
|
mbed714 |
0:98aadd37a5c5
|
960
|
i = find_entry(ipaddr, ETHARP_FLAG_TRY_HARD);
|
mbed714 |
0:98aadd37a5c5
|
961
|
|
mbed714 |
0:98aadd37a5c5
|
962
|
/* could not find or create entry? */
|
mbed714 |
0:98aadd37a5c5
|
963
|
if (i < 0) {
|
mbed714 |
0:98aadd37a5c5
|
964
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
|
mbed714 |
0:98aadd37a5c5
|
965
|
if (q) {
|
mbed714 |
0:98aadd37a5c5
|
966
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
|
mbed714 |
0:98aadd37a5c5
|
967
|
ETHARP_STATS_INC(etharp.memerr);
|
mbed714 |
0:98aadd37a5c5
|
968
|
}
|
mbed714 |
0:98aadd37a5c5
|
969
|
return (err_t)i;
|
mbed714 |
0:98aadd37a5c5
|
970
|
}
|
mbed714 |
0:98aadd37a5c5
|
971
|
|
mbed714 |
0:98aadd37a5c5
|
972
|
/* mark a fresh entry as pending (we just sent a request) */
|
mbed714 |
0:98aadd37a5c5
|
973
|
if (arp_table[i].state == ETHARP_STATE_EMPTY) {
|
mbed714 |
0:98aadd37a5c5
|
974
|
arp_table[i].state = ETHARP_STATE_PENDING;
|
mbed714 |
0:98aadd37a5c5
|
975
|
}
|
mbed714 |
0:98aadd37a5c5
|
976
|
|
mbed714 |
0:98aadd37a5c5
|
977
|
/* { i is either a STABLE or (new or existing) PENDING entry } */
|
mbed714 |
0:98aadd37a5c5
|
978
|
LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
|
mbed714 |
0:98aadd37a5c5
|
979
|
((arp_table[i].state == ETHARP_STATE_PENDING) ||
|
mbed714 |
0:98aadd37a5c5
|
980
|
(arp_table[i].state == ETHARP_STATE_STABLE)));
|
mbed714 |
0:98aadd37a5c5
|
981
|
|
mbed714 |
0:98aadd37a5c5
|
982
|
/* do we have a pending entry? or an implicit query request? */
|
mbed714 |
0:98aadd37a5c5
|
983
|
if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
|
mbed714 |
0:98aadd37a5c5
|
984
|
/* try to resolve it; send out ARP request */
|
mbed714 |
0:98aadd37a5c5
|
985
|
result = etharp_request(netif, ipaddr);
|
mbed714 |
0:98aadd37a5c5
|
986
|
if (result != ERR_OK) {
|
mbed714 |
0:98aadd37a5c5
|
987
|
/* ARP request couldn't be sent */
|
mbed714 |
0:98aadd37a5c5
|
988
|
/* We don't re-send arp request in etharp_tmr, but we still queue packets,
|
mbed714 |
0:98aadd37a5c5
|
989
|
since this failure could be temporary, and the next packet calling
|
mbed714 |
0:98aadd37a5c5
|
990
|
etharp_query again could lead to sending the queued packets. */
|
mbed714 |
0:98aadd37a5c5
|
991
|
}
|
mbed714 |
0:98aadd37a5c5
|
992
|
if (q == NULL) {
|
mbed714 |
0:98aadd37a5c5
|
993
|
return result;
|
mbed714 |
0:98aadd37a5c5
|
994
|
}
|
mbed714 |
0:98aadd37a5c5
|
995
|
}
|
mbed714 |
0:98aadd37a5c5
|
996
|
|
mbed714 |
0:98aadd37a5c5
|
997
|
/* packet given? */
|
mbed714 |
0:98aadd37a5c5
|
998
|
LWIP_ASSERT("q != NULL", q != NULL);
|
mbed714 |
0:98aadd37a5c5
|
999
|
/* stable entry? */
|
mbed714 |
0:98aadd37a5c5
|
1000
|
if (arp_table[i].state == ETHARP_STATE_STABLE) {
|
mbed714 |
0:98aadd37a5c5
|
1001
|
/* we have a valid IP->Ethernet address mapping */
|
mbed714 |
0:98aadd37a5c5
|
1002
|
ETHARP_SET_HINT(netif, i);
|
mbed714 |
0:98aadd37a5c5
|
1003
|
/* send the packet */
|
mbed714 |
0:98aadd37a5c5
|
1004
|
result = etharp_send_ip(netif, q, srcaddr, &(arp_table[i].ethaddr));
|
mbed714 |
0:98aadd37a5c5
|
1005
|
/* pending entry? (either just created or already pending */
|
mbed714 |
0:98aadd37a5c5
|
1006
|
} else if (arp_table[i].state == ETHARP_STATE_PENDING) {
|
mbed714 |
0:98aadd37a5c5
|
1007
|
#if ARP_QUEUEING /* queue the given q packet */
|
mbed714 |
0:98aadd37a5c5
|
1008
|
struct pbuf *p;
|
mbed714 |
0:98aadd37a5c5
|
1009
|
int copy_needed = 0;
|
mbed714 |
0:98aadd37a5c5
|
1010
|
/* IF q includes a PBUF_REF, PBUF_POOL or PBUF_RAM, we have no choice but
|
mbed714 |
0:98aadd37a5c5
|
1011
|
* to copy the whole queue into a new PBUF_RAM (see bug #11400)
|
mbed714 |
0:98aadd37a5c5
|
1012
|
* PBUF_ROMs can be left as they are, since ROM must not get changed. */
|
mbed714 |
0:98aadd37a5c5
|
1013
|
p = q;
|
mbed714 |
0:98aadd37a5c5
|
1014
|
while (p) {
|
mbed714 |
0:98aadd37a5c5
|
1015
|
LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
|
mbed714 |
0:98aadd37a5c5
|
1016
|
if(p->type != PBUF_ROM) {
|
mbed714 |
0:98aadd37a5c5
|
1017
|
copy_needed = 1;
|
mbed714 |
0:98aadd37a5c5
|
1018
|
break;
|
mbed714 |
0:98aadd37a5c5
|
1019
|
}
|
mbed714 |
0:98aadd37a5c5
|
1020
|
p = p->next;
|
mbed714 |
0:98aadd37a5c5
|
1021
|
}
|
mbed714 |
0:98aadd37a5c5
|
1022
|
if(copy_needed) {
|
mbed714 |
0:98aadd37a5c5
|
1023
|
/* copy the whole packet into new pbufs */
|
mbed714 |
0:98aadd37a5c5
|
1024
|
p = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
|
mbed714 |
0:98aadd37a5c5
|
1025
|
if(p != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
1026
|
if (pbuf_copy(p, q) != ERR_OK) {
|
mbed714 |
0:98aadd37a5c5
|
1027
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
1028
|
p = NULL;
|
mbed714 |
0:98aadd37a5c5
|
1029
|
}
|
mbed714 |
0:98aadd37a5c5
|
1030
|
}
|
mbed714 |
0:98aadd37a5c5
|
1031
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
1032
|
/* referencing the old pbuf is enough */
|
mbed714 |
0:98aadd37a5c5
|
1033
|
p = q;
|
mbed714 |
0:98aadd37a5c5
|
1034
|
pbuf_ref(p);
|
mbed714 |
0:98aadd37a5c5
|
1035
|
}
|
mbed714 |
0:98aadd37a5c5
|
1036
|
/* packet could be taken over? */
|
mbed714 |
0:98aadd37a5c5
|
1037
|
if (p != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
1038
|
/* queue packet ... */
|
mbed714 |
0:98aadd37a5c5
|
1039
|
struct etharp_q_entry *new_entry;
|
mbed714 |
0:98aadd37a5c5
|
1040
|
/* allocate a new arp queue entry */
|
mbed714 |
0:98aadd37a5c5
|
1041
|
new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
|
mbed714 |
0:98aadd37a5c5
|
1042
|
if (new_entry != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
1043
|
new_entry->next = 0;
|
mbed714 |
0:98aadd37a5c5
|
1044
|
new_entry->p = p;
|
mbed714 |
0:98aadd37a5c5
|
1045
|
if(arp_table[i].q != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
1046
|
/* queue was already existent, append the new entry to the end */
|
mbed714 |
0:98aadd37a5c5
|
1047
|
struct etharp_q_entry *r;
|
mbed714 |
0:98aadd37a5c5
|
1048
|
r = arp_table[i].q;
|
mbed714 |
0:98aadd37a5c5
|
1049
|
while (r->next != NULL) {
|
mbed714 |
0:98aadd37a5c5
|
1050
|
r = r->next;
|
mbed714 |
0:98aadd37a5c5
|
1051
|
}
|
mbed714 |
0:98aadd37a5c5
|
1052
|
r->next = new_entry;
|
mbed714 |
0:98aadd37a5c5
|
1053
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
1054
|
/* queue did not exist, first item in queue */
|
mbed714 |
0:98aadd37a5c5
|
1055
|
arp_table[i].q = new_entry;
|
mbed714 |
0:98aadd37a5c5
|
1056
|
}
|
mbed714 |
0:98aadd37a5c5
|
1057
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"S16_F"\n", (void *)q, (s16_t)i));
|
mbed714 |
0:98aadd37a5c5
|
1058
|
result = ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
1059
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
1060
|
/* the pool MEMP_ARP_QUEUE is empty */
|
mbed714 |
0:98aadd37a5c5
|
1061
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
1062
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
|
mbed714 |
0:98aadd37a5c5
|
1063
|
/* { result == ERR_MEM } through initialization */
|
mbed714 |
0:98aadd37a5c5
|
1064
|
}
|
mbed714 |
0:98aadd37a5c5
|
1065
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
1066
|
ETHARP_STATS_INC(etharp.memerr);
|
mbed714 |
0:98aadd37a5c5
|
1067
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
|
mbed714 |
0:98aadd37a5c5
|
1068
|
/* { result == ERR_MEM } through initialization */
|
mbed714 |
0:98aadd37a5c5
|
1069
|
}
|
mbed714 |
0:98aadd37a5c5
|
1070
|
#else /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
1071
|
/* q && state == PENDING && ARP_QUEUEING == 0 => result = ERR_MEM */
|
mbed714 |
0:98aadd37a5c5
|
1072
|
/* { result == ERR_MEM } through initialization */
|
mbed714 |
0:98aadd37a5c5
|
1073
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: Ethernet destination address unknown, queueing disabled, packet %p dropped\n", (void *)q));
|
mbed714 |
0:98aadd37a5c5
|
1074
|
#endif /* ARP_QUEUEING */
|
mbed714 |
0:98aadd37a5c5
|
1075
|
}
|
mbed714 |
0:98aadd37a5c5
|
1076
|
return result;
|
mbed714 |
0:98aadd37a5c5
|
1077
|
}
|
mbed714 |
0:98aadd37a5c5
|
1078
|
|
mbed714 |
0:98aadd37a5c5
|
1079
|
/**
|
mbed714 |
0:98aadd37a5c5
|
1080
|
* Send a raw ARP packet (opcode and all addresses can be modified)
|
mbed714 |
0:98aadd37a5c5
|
1081
|
*
|
mbed714 |
0:98aadd37a5c5
|
1082
|
* @param netif the lwip network interface on which to send the ARP packet
|
mbed714 |
0:98aadd37a5c5
|
1083
|
* @param ethsrc_addr the source MAC address for the ethernet header
|
mbed714 |
0:98aadd37a5c5
|
1084
|
* @param ethdst_addr the destination MAC address for the ethernet header
|
mbed714 |
0:98aadd37a5c5
|
1085
|
* @param hwsrc_addr the source MAC address for the ARP protocol header
|
mbed714 |
0:98aadd37a5c5
|
1086
|
* @param ipsrc_addr the source IP address for the ARP protocol header
|
mbed714 |
0:98aadd37a5c5
|
1087
|
* @param hwdst_addr the destination MAC address for the ARP protocol header
|
mbed714 |
0:98aadd37a5c5
|
1088
|
* @param ipdst_addr the destination IP address for the ARP protocol header
|
mbed714 |
0:98aadd37a5c5
|
1089
|
* @param opcode the type of the ARP packet
|
mbed714 |
0:98aadd37a5c5
|
1090
|
* @return ERR_OK if the ARP packet has been sent
|
mbed714 |
0:98aadd37a5c5
|
1091
|
* ERR_MEM if the ARP packet couldn't be allocated
|
mbed714 |
0:98aadd37a5c5
|
1092
|
* any other err_t on failure
|
mbed714 |
0:98aadd37a5c5
|
1093
|
*/
|
mbed714 |
0:98aadd37a5c5
|
1094
|
#if !LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
1095
|
static
|
mbed714 |
0:98aadd37a5c5
|
1096
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
1097
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
1098
|
etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
|
mbed714 |
0:98aadd37a5c5
|
1099
|
const struct eth_addr *ethdst_addr,
|
mbed714 |
0:98aadd37a5c5
|
1100
|
const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr,
|
mbed714 |
0:98aadd37a5c5
|
1101
|
const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr,
|
mbed714 |
0:98aadd37a5c5
|
1102
|
const u16_t opcode)
|
mbed714 |
0:98aadd37a5c5
|
1103
|
{
|
mbed714 |
0:98aadd37a5c5
|
1104
|
struct pbuf *p;
|
mbed714 |
0:98aadd37a5c5
|
1105
|
err_t result = ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
1106
|
struct eth_hdr *ethhdr;
|
mbed714 |
0:98aadd37a5c5
|
1107
|
struct etharp_hdr *hdr;
|
mbed714 |
0:98aadd37a5c5
|
1108
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
1109
|
const u8_t * ethdst_hwaddr;
|
mbed714 |
0:98aadd37a5c5
|
1110
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
1111
|
|
mbed714 |
0:98aadd37a5c5
|
1112
|
/* allocate a pbuf for the outgoing ARP request packet */
|
mbed714 |
0:98aadd37a5c5
|
1113
|
p = pbuf_alloc(PBUF_RAW, SIZEOF_ETHARP_PACKET, PBUF_RAM);
|
mbed714 |
0:98aadd37a5c5
|
1114
|
/* could allocate a pbuf for an ARP request? */
|
mbed714 |
0:98aadd37a5c5
|
1115
|
if (p == NULL) {
|
mbed714 |
0:98aadd37a5c5
|
1116
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
|
mbed714 |
0:98aadd37a5c5
|
1117
|
("etharp_raw: could not allocate pbuf for ARP request.\n"));
|
mbed714 |
0:98aadd37a5c5
|
1118
|
ETHARP_STATS_INC(etharp.memerr);
|
mbed714 |
0:98aadd37a5c5
|
1119
|
return ERR_MEM;
|
mbed714 |
0:98aadd37a5c5
|
1120
|
}
|
mbed714 |
0:98aadd37a5c5
|
1121
|
LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
|
mbed714 |
0:98aadd37a5c5
|
1122
|
(p->len >= SIZEOF_ETHARP_PACKET));
|
mbed714 |
0:98aadd37a5c5
|
1123
|
|
mbed714 |
0:98aadd37a5c5
|
1124
|
ethhdr = (struct eth_hdr *)p->payload;
|
mbed714 |
0:98aadd37a5c5
|
1125
|
hdr = (struct etharp_hdr *)((u8_t*)ethhdr + SIZEOF_ETH_HDR);
|
mbed714 |
0:98aadd37a5c5
|
1126
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
|
mbed714 |
0:98aadd37a5c5
|
1127
|
hdr->opcode = htons(opcode);
|
mbed714 |
0:98aadd37a5c5
|
1128
|
|
mbed714 |
0:98aadd37a5c5
|
1129
|
LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!",
|
mbed714 |
0:98aadd37a5c5
|
1130
|
(netif->hwaddr_len == ETHARP_HWADDR_LEN));
|
mbed714 |
0:98aadd37a5c5
|
1131
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
1132
|
/* If we are using Link-Local, all ARP packets that contain a Link-Local
|
mbed714 |
0:98aadd37a5c5
|
1133
|
* 'sender IP address' MUST be sent using link-layer broadcast instead of
|
mbed714 |
0:98aadd37a5c5
|
1134
|
* link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */
|
mbed714 |
0:98aadd37a5c5
|
1135
|
ethdst_hwaddr = ip_addr_islinklocal(ipsrc_addr) ? (u8_t*)(ethbroadcast.addr) : ethdst_addr->addr;
|
mbed714 |
0:98aadd37a5c5
|
1136
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
1137
|
/* Write the ARP MAC-Addresses */
|
mbed714 |
0:98aadd37a5c5
|
1138
|
ETHADDR16_COPY(&hdr->shwaddr, hwsrc_addr);
|
mbed714 |
0:98aadd37a5c5
|
1139
|
ETHADDR16_COPY(&hdr->dhwaddr, hwdst_addr);
|
mbed714 |
0:98aadd37a5c5
|
1140
|
/* Write the Ethernet MAC-Addresses */
|
mbed714 |
0:98aadd37a5c5
|
1141
|
#if LWIP_AUTOIP
|
mbed714 |
0:98aadd37a5c5
|
1142
|
ETHADDR16_COPY(ðhdr->dest, ethdst_hwaddr);
|
mbed714 |
0:98aadd37a5c5
|
1143
|
#else /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
1144
|
ETHADDR16_COPY(ðhdr->dest, ethdst_addr);
|
mbed714 |
0:98aadd37a5c5
|
1145
|
#endif /* LWIP_AUTOIP */
|
mbed714 |
0:98aadd37a5c5
|
1146
|
ETHADDR16_COPY(ðhdr->src, ethsrc_addr);
|
mbed714 |
0:98aadd37a5c5
|
1147
|
/* Copy struct ip_addr2 to aligned ip_addr, to support compilers without
|
mbed714 |
0:98aadd37a5c5
|
1148
|
* structure packing. */
|
mbed714 |
0:98aadd37a5c5
|
1149
|
IPADDR2_COPY(&hdr->sipaddr, ipsrc_addr);
|
mbed714 |
0:98aadd37a5c5
|
1150
|
IPADDR2_COPY(&hdr->dipaddr, ipdst_addr);
|
mbed714 |
0:98aadd37a5c5
|
1151
|
|
mbed714 |
0:98aadd37a5c5
|
1152
|
hdr->hwtype = PP_HTONS(HWTYPE_ETHERNET);
|
mbed714 |
0:98aadd37a5c5
|
1153
|
hdr->proto = PP_HTONS(ETHTYPE_IP);
|
mbed714 |
0:98aadd37a5c5
|
1154
|
/* set hwlen and protolen */
|
mbed714 |
0:98aadd37a5c5
|
1155
|
hdr->hwlen = ETHARP_HWADDR_LEN;
|
mbed714 |
0:98aadd37a5c5
|
1156
|
hdr->protolen = sizeof(ip_addr_t);
|
mbed714 |
0:98aadd37a5c5
|
1157
|
|
mbed714 |
0:98aadd37a5c5
|
1158
|
ethhdr->type = PP_HTONS(ETHTYPE_ARP);
|
mbed714 |
0:98aadd37a5c5
|
1159
|
/* send ARP query */
|
mbed714 |
0:98aadd37a5c5
|
1160
|
result = netif->linkoutput(netif, p);
|
mbed714 |
0:98aadd37a5c5
|
1161
|
ETHARP_STATS_INC(etharp.xmit);
|
mbed714 |
0:98aadd37a5c5
|
1162
|
/* free ARP query packet */
|
mbed714 |
0:98aadd37a5c5
|
1163
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
1164
|
p = NULL;
|
mbed714 |
0:98aadd37a5c5
|
1165
|
/* could not allocate pbuf for ARP request */
|
mbed714 |
0:98aadd37a5c5
|
1166
|
|
mbed714 |
0:98aadd37a5c5
|
1167
|
return result;
|
mbed714 |
0:98aadd37a5c5
|
1168
|
}
|
mbed714 |
0:98aadd37a5c5
|
1169
|
|
mbed714 |
0:98aadd37a5c5
|
1170
|
/**
|
mbed714 |
0:98aadd37a5c5
|
1171
|
* Send an ARP request packet asking for ipaddr.
|
mbed714 |
0:98aadd37a5c5
|
1172
|
*
|
mbed714 |
0:98aadd37a5c5
|
1173
|
* @param netif the lwip network interface on which to send the request
|
mbed714 |
0:98aadd37a5c5
|
1174
|
* @param ipaddr the IP address for which to ask
|
mbed714 |
0:98aadd37a5c5
|
1175
|
* @return ERR_OK if the request has been sent
|
mbed714 |
0:98aadd37a5c5
|
1176
|
* ERR_MEM if the ARP packet couldn't be allocated
|
mbed714 |
0:98aadd37a5c5
|
1177
|
* any other err_t on failure
|
mbed714 |
0:98aadd37a5c5
|
1178
|
*/
|
mbed714 |
0:98aadd37a5c5
|
1179
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
1180
|
etharp_request(struct netif *netif, ip_addr_t *ipaddr)
|
mbed714 |
0:98aadd37a5c5
|
1181
|
{
|
mbed714 |
0:98aadd37a5c5
|
1182
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
|
mbed714 |
0:98aadd37a5c5
|
1183
|
return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, ðbroadcast,
|
mbed714 |
0:98aadd37a5c5
|
1184
|
(struct eth_addr *)netif->hwaddr, &netif->ip_addr, ðzero,
|
mbed714 |
0:98aadd37a5c5
|
1185
|
ipaddr, ARP_REQUEST);
|
mbed714 |
0:98aadd37a5c5
|
1186
|
}
|
mbed714 |
0:98aadd37a5c5
|
1187
|
#endif /* LWIP_ARP */
|
mbed714 |
0:98aadd37a5c5
|
1188
|
|
mbed714 |
0:98aadd37a5c5
|
1189
|
/**
|
mbed714 |
0:98aadd37a5c5
|
1190
|
* Process received ethernet frames. Using this function instead of directly
|
mbed714 |
0:98aadd37a5c5
|
1191
|
* calling ip_input and passing ARP frames through etharp in ethernetif_input,
|
mbed714 |
0:98aadd37a5c5
|
1192
|
* the ARP cache is protected from concurrent access.
|
mbed714 |
0:98aadd37a5c5
|
1193
|
*
|
mbed714 |
0:98aadd37a5c5
|
1194
|
* @param p the recevied packet, p->payload pointing to the ethernet header
|
mbed714 |
0:98aadd37a5c5
|
1195
|
* @param netif the network interface on which the packet was received
|
mbed714 |
0:98aadd37a5c5
|
1196
|
*/
|
mbed714 |
0:98aadd37a5c5
|
1197
|
err_t
|
mbed714 |
0:98aadd37a5c5
|
1198
|
ethernet_input(struct pbuf *p, struct netif *netif)
|
mbed714 |
0:98aadd37a5c5
|
1199
|
{
|
mbed714 |
0:98aadd37a5c5
|
1200
|
struct eth_hdr* ethhdr;
|
mbed714 |
0:98aadd37a5c5
|
1201
|
u16_t type;
|
mbed714 |
0:98aadd37a5c5
|
1202
|
|
mbed714 |
0:98aadd37a5c5
|
1203
|
/* points to packet payload, which starts with an Ethernet header */
|
mbed714 |
0:98aadd37a5c5
|
1204
|
ethhdr = (struct eth_hdr *)p->payload;
|
mbed714 |
0:98aadd37a5c5
|
1205
|
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE,
|
mbed714 |
0:98aadd37a5c5
|
1206
|
("ethernet_input: dest:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", src:%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F":%"X8_F", type:%"X16_F"\n",
|
mbed714 |
0:98aadd37a5c5
|
1207
|
(unsigned)ethhdr->dest.addr[0], (unsigned)ethhdr->dest.addr[1], (unsigned)ethhdr->dest.addr[2],
|
mbed714 |
0:98aadd37a5c5
|
1208
|
(unsigned)ethhdr->dest.addr[3], (unsigned)ethhdr->dest.addr[4], (unsigned)ethhdr->dest.addr[5],
|
mbed714 |
0:98aadd37a5c5
|
1209
|
(unsigned)ethhdr->src.addr[0], (unsigned)ethhdr->src.addr[1], (unsigned)ethhdr->src.addr[2],
|
mbed714 |
0:98aadd37a5c5
|
1210
|
(unsigned)ethhdr->src.addr[3], (unsigned)ethhdr->src.addr[4], (unsigned)ethhdr->src.addr[5],
|
mbed714 |
0:98aadd37a5c5
|
1211
|
(unsigned)htons(ethhdr->type)));
|
mbed714 |
0:98aadd37a5c5
|
1212
|
|
mbed714 |
0:98aadd37a5c5
|
1213
|
type = ethhdr->type;
|
mbed714 |
0:98aadd37a5c5
|
1214
|
#if ETHARP_SUPPORT_VLAN
|
mbed714 |
0:98aadd37a5c5
|
1215
|
if (type == PP_HTONS(ETHTYPE_VLAN)) {
|
mbed714 |
0:98aadd37a5c5
|
1216
|
struct eth_vlan_hdr *vlan = (struct eth_vlan_hdr*)(((char*)ethhdr) + SIZEOF_ETH_HDR);
|
mbed714 |
0:98aadd37a5c5
|
1217
|
#ifdef ETHARP_VLAN_CHECK /* if not, allow all VLANs */
|
mbed714 |
0:98aadd37a5c5
|
1218
|
if (VLAN_ID(vlan) != ETHARP_VLAN_CHECK) {
|
mbed714 |
0:98aadd37a5c5
|
1219
|
/* silently ignore this packet: not for our VLAN */
|
mbed714 |
0:98aadd37a5c5
|
1220
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
1221
|
return ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
1222
|
}
|
mbed714 |
0:98aadd37a5c5
|
1223
|
#endif /* ETHARP_VLAN_CHECK */
|
mbed714 |
0:98aadd37a5c5
|
1224
|
type = vlan->tpid;
|
mbed714 |
0:98aadd37a5c5
|
1225
|
}
|
mbed714 |
0:98aadd37a5c5
|
1226
|
#endif /* ETHARP_SUPPORT_VLAN */
|
mbed714 |
0:98aadd37a5c5
|
1227
|
|
mbed714 |
0:98aadd37a5c5
|
1228
|
#if LWIP_ARP_FILTER_NETIF
|
mbed714 |
0:98aadd37a5c5
|
1229
|
netif = LWIP_ARP_FILTER_NETIF_FN(p, netif, htons(type));
|
mbed714 |
0:98aadd37a5c5
|
1230
|
#endif /* LWIP_ARP_FILTER_NETIF*/
|
mbed714 |
0:98aadd37a5c5
|
1231
|
|
mbed714 |
0:98aadd37a5c5
|
1232
|
switch (type) {
|
mbed714 |
0:98aadd37a5c5
|
1233
|
#if LWIP_ARP
|
mbed714 |
0:98aadd37a5c5
|
1234
|
/* IP packet? */
|
mbed714 |
0:98aadd37a5c5
|
1235
|
case PP_HTONS(ETHTYPE_IP):
|
mbed714 |
0:98aadd37a5c5
|
1236
|
if (!(netif->flags & NETIF_FLAG_ETHARP)) {
|
mbed714 |
0:98aadd37a5c5
|
1237
|
goto free_and_return;
|
mbed714 |
0:98aadd37a5c5
|
1238
|
}
|
mbed714 |
0:98aadd37a5c5
|
1239
|
#if ETHARP_TRUST_IP_MAC
|
mbed714 |
0:98aadd37a5c5
|
1240
|
/* update ARP table */
|
mbed714 |
0:98aadd37a5c5
|
1241
|
etharp_ip_input(netif, p);
|
mbed714 |
0:98aadd37a5c5
|
1242
|
#endif /* ETHARP_TRUST_IP_MAC */
|
mbed714 |
0:98aadd37a5c5
|
1243
|
/* skip Ethernet header */
|
mbed714 |
0:98aadd37a5c5
|
1244
|
if(pbuf_header(p, -(s16_t)SIZEOF_ETH_HDR)) {
|
mbed714 |
0:98aadd37a5c5
|
1245
|
LWIP_ASSERT("Can't move over header in packet", 0);
|
mbed714 |
0:98aadd37a5c5
|
1246
|
goto free_and_return;
|
mbed714 |
0:98aadd37a5c5
|
1247
|
} else {
|
mbed714 |
0:98aadd37a5c5
|
1248
|
/* pass to IP layer */
|
mbed714 |
0:98aadd37a5c5
|
1249
|
ip_input(p, netif);
|
mbed714 |
0:98aadd37a5c5
|
1250
|
}
|
mbed714 |
0:98aadd37a5c5
|
1251
|
break;
|
mbed714 |
0:98aadd37a5c5
|
1252
|
|
mbed714 |
0:98aadd37a5c5
|
1253
|
case PP_HTONS(ETHTYPE_ARP):
|
mbed714 |
0:98aadd37a5c5
|
1254
|
if (!(netif->flags & NETIF_FLAG_ETHARP)) {
|
mbed714 |
0:98aadd37a5c5
|
1255
|
goto free_and_return;
|
mbed714 |
0:98aadd37a5c5
|
1256
|
}
|
mbed714 |
0:98aadd37a5c5
|
1257
|
/* pass p to ARP module */
|
mbed714 |
0:98aadd37a5c5
|
1258
|
etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
|
mbed714 |
0:98aadd37a5c5
|
1259
|
break;
|
mbed714 |
0:98aadd37a5c5
|
1260
|
#endif /* LWIP_ARP */
|
mbed714 |
0:98aadd37a5c5
|
1261
|
#if PPPOE_SUPPORT
|
mbed714 |
0:98aadd37a5c5
|
1262
|
case PP_HTONS(ETHTYPE_PPPOEDISC): /* PPP Over Ethernet Discovery Stage */
|
mbed714 |
0:98aadd37a5c5
|
1263
|
pppoe_disc_input(netif, p);
|
mbed714 |
0:98aadd37a5c5
|
1264
|
break;
|
mbed714 |
0:98aadd37a5c5
|
1265
|
|
mbed714 |
0:98aadd37a5c5
|
1266
|
case PP_HTONS(ETHTYPE_PPPOE): /* PPP Over Ethernet Session Stage */
|
mbed714 |
0:98aadd37a5c5
|
1267
|
pppoe_data_input(netif, p);
|
mbed714 |
0:98aadd37a5c5
|
1268
|
break;
|
mbed714 |
0:98aadd37a5c5
|
1269
|
#endif /* PPPOE_SUPPORT */
|
mbed714 |
0:98aadd37a5c5
|
1270
|
|
mbed714 |
0:98aadd37a5c5
|
1271
|
default:
|
mbed714 |
0:98aadd37a5c5
|
1272
|
ETHARP_STATS_INC(etharp.proterr);
|
mbed714 |
0:98aadd37a5c5
|
1273
|
ETHARP_STATS_INC(etharp.drop);
|
mbed714 |
0:98aadd37a5c5
|
1274
|
goto free_and_return;
|
mbed714 |
0:98aadd37a5c5
|
1275
|
}
|
mbed714 |
0:98aadd37a5c5
|
1276
|
|
mbed714 |
0:98aadd37a5c5
|
1277
|
/* This means the pbuf is freed or consumed,
|
mbed714 |
0:98aadd37a5c5
|
1278
|
so the caller doesn't have to free it again */
|
mbed714 |
0:98aadd37a5c5
|
1279
|
return ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
1280
|
|
mbed714 |
0:98aadd37a5c5
|
1281
|
free_and_return:
|
mbed714 |
0:98aadd37a5c5
|
1282
|
pbuf_free(p);
|
mbed714 |
0:98aadd37a5c5
|
1283
|
return ERR_OK;
|
mbed714 |
0:98aadd37a5c5
|
1284
|
}
|
mbed714 |
0:98aadd37a5c5
|
1285
|
#endif /* LWIP_ARP || LWIP_ETHERNET */
|