OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_Demo_Rowley_ARM7/] [lwip-1.1.0/] [src/] [netif/] [etharp.c] - Blame information for rev 583

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/**
2
 * @file
3
 * Address Resolution Protocol module for IP over Ethernet
4
 *
5
 * Functionally, ARP is divided into two parts. The first maps an IP address
6
 * to a physical address when sending a packet, and the second part answers
7
 * requests from other machines for our physical address.
8
 *
9
 * This implementation complies with RFC 826 (Ethernet ARP). It supports
10
 * Gratuitious ARP from RFC3220 (IP Mobility Support for IPv4) section 4.6
11
 * if an interface calls etharp_query(our_netif, its_ip_addr, NULL) upon
12
 * address change.
13
 */
14
 
15
/*
16
 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
17
 * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
18
 * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
19
 * All rights reserved.
20
 *
21
 * Redistribution and use in source and binary forms, with or without modification,
22
 * are permitted provided that the following conditions are met:
23
 *
24
 * 1. Redistributions of source code must retain the above copyright notice,
25
 *    this list of conditions and the following disclaimer.
26
 * 2. Redistributions in binary form must reproduce the above copyright notice,
27
 *    this list of conditions and the following disclaimer in the documentation
28
 *    and/or other materials provided with the distribution.
29
 * 3. The name of the author may not be used to endorse or promote products
30
 *    derived from this software without specific prior written permission.
31
 *
32
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
33
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
35
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
41
 * OF SUCH DAMAGE.
42
 *
43
 * This file is part of the lwIP TCP/IP stack.
44
 *
45
 */
46
 
47
#include "lwip/opt.h"
48
#include "lwip/inet.h"
49
#include "netif/etharp.h"
50
#include "lwip/ip.h"
51
#include "lwip/stats.h"
52
 
53
/* ARP needs to inform DHCP of any ARP replies? */
54
#if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
55
#  include "lwip/dhcp.h"
56
#endif
57
 
58
/** the time an ARP entry stays valid after its last update,
59
 * (240 * 5) seconds = 20 minutes.
60
 */
61
#define ARP_MAXAGE 240
62
/** the time an ARP entry stays pending after first request,
63
 * (2 * 5) seconds = 10 seconds.
64
 *
65
 * @internal Keep this number at least 2, otherwise it might
66
 * run out instantly if the timeout occurs directly after a request.
67
 */
68
#define ARP_MAXPENDING 2
69
 
70
#define HWTYPE_ETHERNET 1
71
 
72
/** ARP message types */
73
#define ARP_REQUEST 1
74
#define ARP_REPLY 2
75
 
76
#define ARPH_HWLEN(hdr) (ntohs((hdr)->_hwlen_protolen) >> 8)
77
#define ARPH_PROTOLEN(hdr) (ntohs((hdr)->_hwlen_protolen) & 0xff)
78
 
79
#define ARPH_HWLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons(ARPH_PROTOLEN(hdr) | ((len) << 8))
80
#define ARPH_PROTOLEN_SET(hdr, len) (hdr)->_hwlen_protolen = htons((len) | (ARPH_HWLEN(hdr) << 8))
81
 
82
enum etharp_state {
83
  ETHARP_STATE_EMPTY,
84
  ETHARP_STATE_PENDING,
85
  ETHARP_STATE_STABLE,
86
  /** @internal transitional state used in etharp_tmr() for convenience*/
87
  ETHARP_STATE_EXPIRED
88
};
89
 
90
struct etharp_entry {
91
#if ARP_QUEUEING
92
  /**
93
   * Pointer to queue of pending outgoing packets on this ARP entry.
94
   */
95
   struct pbuf *p;
96
#endif
97
  struct ip_addr ipaddr;
98
  struct eth_addr ethaddr;
99
  enum etharp_state state;
100
  u8_t ctime;
101
};
102
 
103
static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
104
static struct etharp_entry arp_table[ARP_TABLE_SIZE];
105
 
106
/**
107
 * Try hard to create a new entry - we want the IP address to appear in
108
 * the cache (even if this means removing an active entry or so). */
109
#define ETHARP_TRY_HARD 1
110
 
111
static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags);
112
static err_t update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags);
113
/**
114
 * Initializes ARP module.
115
 */
116
void
117
etharp_init(void)
118
{
119
  u8_t i;
120
  /* clear ARP entries */
121
  for(i = 0; i < ARP_TABLE_SIZE; ++i) {
122
    arp_table[i].state = ETHARP_STATE_EMPTY;
123
#if ARP_QUEUEING
124
    arp_table[i].p = NULL;
125
#endif
126
    arp_table[i].ctime = 0;
127
  }
128
}
129
 
130
/**
131
 * Clears expired entries in the ARP table.
132
 *
133
 * This function should be called every ETHARP_TMR_INTERVAL microseconds (5 seconds),
134
 * in order to expire entries in the ARP table.
135
 */
136
void
137
etharp_tmr(void)
138
{
139
  u8_t i;
140
 
141
  LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
142
  /* remove expired entries from the ARP table */
143
  for (i = 0; i < ARP_TABLE_SIZE; ++i) {
144
    arp_table[i].ctime++;
145
    /* stable entry? */
146
    if ((arp_table[i].state == ETHARP_STATE_STABLE) &&
147
         /* entry has become old? */
148
        (arp_table[i].ctime >= ARP_MAXAGE)) {
149
      LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired stable entry %u.\n", i));
150
      arp_table[i].state = ETHARP_STATE_EXPIRED;
151
    /* pending entry? */
152
    } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
153
      /* entry unresolved/pending for too long? */
154
      if (arp_table[i].ctime >= ARP_MAXPENDING) {
155
        LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired pending entry %u.\n", i));
156
        arp_table[i].state = ETHARP_STATE_EXPIRED;
157
#if ARP_QUEUEING
158
      } else if (arp_table[i].p != NULL) {
159
        /* resend an ARP query here */
160
#endif
161
      }
162
    }
163
    /* clean up entries that have just been expired */
164
    if (arp_table[i].state == ETHARP_STATE_EXPIRED) {
165
#if ARP_QUEUEING
166
      /* and empty packet queue */
167
      if (arp_table[i].p != NULL) {
168
        /* remove all queued packets */
169
        LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: freeing entry %u, packet queue %p.\n", i, (void *)(arp_table[i].p)));
170
        pbuf_free(arp_table[i].p);
171
        arp_table[i].p = NULL;
172
      }
173
#endif
174
      /* recycle entry for re-use */
175
      arp_table[i].state = ETHARP_STATE_EMPTY;
176
    }
177
  }
178
}
179
 
180
/**
181
 * Search the ARP table for a matching or new entry.
182
 *
183
 * If an IP address is given, return a pending or stable ARP entry that matches
184
 * the address. If no match is found, create a new entry with this address set,
185
 * but in state ETHARP_EMPTY. The caller must check and possibly change the
186
 * state of the returned entry.
187
 *
188
 * If ipaddr is NULL, return a initialized new entry in state ETHARP_EMPTY.
189
 *
190
 * In all cases, attempt to create new entries from an empty entry. If no
191
 * empty entries are available and ETHARP_TRY_HARD flag is set, recycle
192
 * old entries. Heuristic choose the least important entry for recycling.
193
 *
194
 * @param ipaddr IP address to find in ARP cache, or to add if not found.
195
 * @param flags
196
 * - ETHARP_TRY_HARD: Try hard to create a entry by allowing recycling of
197
 * active (stable or pending) entries.
198
 *
199
 * @return The ARP entry index that matched or is created, ERR_MEM if no
200
 * entry is found or could be recycled.
201
 */
202
static s8_t find_entry(struct ip_addr *ipaddr, u8_t flags)
203
{
204
  s8_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
205
  s8_t empty = ARP_TABLE_SIZE;
206
  u8_t i = 0, age_pending = 0, age_stable = 0;
207
#if ARP_QUEUEING
208
  /* oldest entry with packets on queue */
209
  s8_t old_queue = ARP_TABLE_SIZE;
210
  /* its age */
211
  u8_t age_queue = 0;
212
#endif
213
 
214
  /**
215
   * a) do a search through the cache, remember candidates
216
   * b) select candidate entry
217
   * c) create new entry
218
   */
219
 
220
  /* a) in a single search sweep, do all of this
221
   * 1) remember the first empty entry (if any)
222
   * 2) remember the oldest stable entry (if any)
223
   * 3) remember the oldest pending entry without queued packets (if any)
224
   * 4) remember the oldest pending entry with queued packets (if any)
225
   * 5) search for a matching IP entry, either pending or stable
226
   *    until 5 matches, or all entries are searched for.
227
   */
228
 
229
  for (i = 0; i < ARP_TABLE_SIZE; ++i) {
230
    /* no empty entry found yet and now we do find one? */
231
    if ((empty == ARP_TABLE_SIZE) && (arp_table[i].state == ETHARP_STATE_EMPTY)) {
232
      LWIP_DEBUGF(ETHARP_DEBUG, ("find_entry: found empty entry %d\n", i));
233
      /* remember first empty entry */
234
      empty = i;
235
    }
236
    /* pending entry? */
237
    else if (arp_table[i].state == ETHARP_STATE_PENDING) {
238
      /* if given, does IP address match IP address in ARP entry? */
239
      if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
240
        LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: found matching pending entry %d\n", i));
241
        /* found exact IP address match, simply bail out */
242
        return i;
243
#if ARP_QUEUEING
244
      /* pending with queued packets? */
245
      } else if (arp_table[i].p != NULL) {
246
        if (arp_table[i].ctime >= age_queue) {
247
          old_queue = i;
248
          age_queue = arp_table[i].ctime;
249
        }
250
#endif
251
      /* pending without queued packets? */
252
      } else {
253
        if (arp_table[i].ctime >= age_pending) {
254
          old_pending = i;
255
          age_pending = arp_table[i].ctime;
256
        }
257
      }
258
    }
259
    /* stable entry? */
260
    else if (arp_table[i].state == ETHARP_STATE_STABLE) {
261
      /* if given, does IP address match IP address in ARP entry? */
262
      if (ipaddr && ip_addr_cmp(ipaddr, &arp_table[i].ipaddr)) {
263
        LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: found matching stable entry %d\n", i));
264
        /* found exact IP address match, simply bail out */
265
        return i;
266
      /* remember entry with oldest stable entry in oldest, its age in maxtime */
267
      } else if (arp_table[i].ctime >= age_stable) {
268
        old_stable = i;
269
        age_stable = arp_table[i].ctime;
270
      }
271
    }
272
  }
273
  /* { we have no match } => try to create a new entry */
274
 
275
  /* no empty entry found and not allowed to recycle? */
276
  if ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_TRY_HARD) == 0))
277
  {
278
        return (s8_t)ERR_MEM;
279
  }
280
 
281
  /* b) choose the least destructive entry to recycle:
282
   * 1) empty entry
283
   * 2) oldest stable entry
284
   * 3) oldest pending entry without queued packets
285
   * 4) oldest pending entry without queued packets
286
   *
287
   * { ETHARP_TRY_HARD is set at this point }
288
   */
289
 
290
  /* 1) empty entry available? */
291
  if (empty < ARP_TABLE_SIZE) {
292
    i = empty;
293
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting empty entry %d\n", i));
294
  }
295
  /* 2) found recyclable stable entry? */
296
  else if (old_stable < ARP_TABLE_SIZE) {
297
    /* recycle oldest stable*/
298
    i = old_stable;
299
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting oldest stable entry %d\n", i));
300
#if ARP_QUEUEING
301
    /* no queued packets should exist on stable entries */
302
    LWIP_ASSERT("arp_table[i].p == NULL", arp_table[i].p == NULL);
303
#endif
304
  /* 3) found recyclable pending entry without queued packets? */
305
  } else if (old_pending < ARP_TABLE_SIZE) {
306
    /* recycle oldest pending */
307
    i = old_pending;
308
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting oldest pending entry %d (without queue)\n", i));
309
#if ARP_QUEUEING
310
  /* 4) found recyclable pending entry with queued packets? */
311
  } else if (old_queue < ARP_TABLE_SIZE) {
312
    /* recycle oldest pending */
313
    i = old_queue;
314
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("find_entry: selecting oldest pending entry %d, freeing packet queue %p\n", i, (void *)(arp_table[i].p)));
315
    pbuf_free(arp_table[i].p);
316
    arp_table[i].p = NULL;
317
#endif
318
    /* no empty or recyclable entries found */
319
  } else {
320
    return (s8_t)ERR_MEM;
321
  }
322
 
323
  /* { empty or recyclable entry found } */
324
  LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
325
 
326
  /* recycle entry (no-op for an already empty entry) */
327
  arp_table[i].state = ETHARP_STATE_EMPTY;
328
 
329
  /* IP address given? */
330
  if (ipaddr != NULL) {
331
    /* set IP address */
332
    ip_addr_set(&arp_table[i].ipaddr, ipaddr);
333
  }
334
  arp_table[i].ctime = 0;
335
  return (err_t)i;
336
}
337
 
338
/**
339
 * Update (or insert) a IP/MAC address pair in the ARP cache.
340
 *
341
 * If a pending entry is resolved, any queued packets will be sent
342
 * at this point.
343
 *
344
 * @param ipaddr IP address of the inserted ARP entry.
345
 * @param ethaddr Ethernet address of the inserted ARP entry.
346
 * @param flags Defines behaviour:
347
 * - ETHARP_TRY_HARD Allows ARP to insert this as a new item. If not specified,
348
 * only existing ARP entries will be updated.
349
 *
350
 * @return
351
 * - ERR_OK Succesfully updated ARP cache.
352
 * - ERR_MEM If we could not add a new ARP entry when ETHARP_TRY_HARD was set.
353
 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
354
 *
355
 * @see pbuf_free()
356
 */
357
static err_t
358
update_arp_entry(struct netif *netif, struct ip_addr *ipaddr, struct eth_addr *ethaddr, u8_t flags)
359
{
360
  s8_t i, k;
361
  LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 3, ("update_arp_entry()\n"));
362
  LWIP_ASSERT("netif->hwaddr_len != 0", netif->hwaddr_len != 0);
363
  LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: %u.%u.%u.%u - %02x:%02x:%02x:%02x:%02x:%02x\n",
364
                                        ip4_addr1(ipaddr), ip4_addr2(ipaddr), ip4_addr3(ipaddr), ip4_addr4(ipaddr),
365
                                        ethaddr->addr[0], ethaddr->addr[1], ethaddr->addr[2],
366
                                        ethaddr->addr[3], ethaddr->addr[4], ethaddr->addr[5]));
367
  /* non-unicast address? */
368
  if (ip_addr_isany(ipaddr) ||
369
      ip_addr_isbroadcast(ipaddr, netif) ||
370
      ip_addr_ismulticast(ipaddr)) {
371
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
372
    return ERR_ARG;
373
  }
374
  /* find or create ARP entry */
375
  i = find_entry(ipaddr, flags);
376
  /* bail out if no entry could be found */
377
  if (i < 0) return (err_t)i;
378
 
379
  /* mark it stable */
380
  arp_table[i].state = ETHARP_STATE_STABLE;
381
 
382
  LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: updating stable entry %u\n", i));
383
  /* update address */
384
  for (k = 0; k < netif->hwaddr_len; ++k) {
385
    arp_table[i].ethaddr.addr[k] = ethaddr->addr[k];
386
  }
387
  /* reset time stamp */
388
  arp_table[i].ctime = 0;
389
/* this is where we will send out queued packets! */
390
#if ARP_QUEUEING
391
  while (arp_table[i].p != NULL) {
392
    /* get the first packet on the queue */
393
    struct pbuf *p = arp_table[i].p;
394
    /* Ethernet header */
395
    struct eth_hdr *ethhdr = p->payload;
396
    /* remember (and reference) remainder of queue */
397
    /* note: this will also terminate the p pbuf chain */
398
    arp_table[i].p = pbuf_dequeue(p);
399
    /* fill-in Ethernet header */
400
    for (k = 0; k < netif->hwaddr_len; ++k) {
401
      ethhdr->dest.addr[k] = ethaddr->addr[k];
402
      ethhdr->src.addr[k] = netif->hwaddr[k];
403
    }
404
    ethhdr->type = htons(ETHTYPE_IP);
405
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("update_arp_entry: sending queued IP packet %p.\n", (void *)p));
406
    /* send the queued IP packet */
407
    netif->linkoutput(netif, p);
408
    /* free the queued IP packet */
409
    pbuf_free(p);
410
  }
411
#endif
412
  return ERR_OK;
413
}
414
 
415
/**
416
 * Updates the ARP table using the given IP packet.
417
 *
418
 * Uses the incoming IP packet's source address to update the
419
 * ARP cache for the local network. The function does not alter
420
 * or free the packet. This function must be called before the
421
 * packet p is passed to the IP layer.
422
 *
423
 * @param netif The lwIP network interface on which the IP packet pbuf arrived.
424
 * @param pbuf The IP packet that arrived on netif.
425
 *
426
 * @return NULL
427
 *
428
 * @see pbuf_free()
429
 */
430
void
431
etharp_ip_input(struct netif *netif, struct pbuf *p)
432
{
433
  struct ethip_hdr *hdr;
434
 
435
  /* Only insert an entry if the source IP address of the
436
     incoming IP packet comes from a host on the local network. */
437
  hdr = p->payload;
438
  /* source is not on the local network? */
439
  if (!ip_addr_netcmp(&(hdr->ip.src), &(netif->ip_addr), &(netif->netmask))) {
440
    /* do nothing */
441
    return;
442
  }
443
 
444
  LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_ip_input: updating ETHARP table.\n"));
445
  /* update ARP table */
446
  /* @todo We could use ETHARP_TRY_HARD if we think we are going to talk
447
   * back soon (for example, if the destination IP address is ours. */
448
  update_arp_entry(netif, &(hdr->ip.src), &(hdr->eth.src), 0);
449
}
450
 
451
 
452
/**
453
 * Responds to ARP requests to us. Upon ARP replies to us, add entry to cache
454
 * send out queued IP packets. Updates cache with snooped address pairs.
455
 *
456
 * Should be called for incoming ARP packets. The pbuf in the argument
457
 * is freed by this function.
458
 *
459
 * @param netif The lwIP network interface on which the ARP packet pbuf arrived.
460
 * @param pbuf The ARP packet that arrived on netif. Is freed by this function.
461
 * @param ethaddr Ethernet address of netif.
462
 *
463
 * @return NULL
464
 *
465
 * @see pbuf_free()
466
 */
467
void
468
etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p)
469
{
470
  struct etharp_hdr *hdr;
471
  /* these are aligned properly, whereas the ARP header fields might not be */
472
  struct ip_addr sipaddr, dipaddr;
473
  u8_t i;
474
  u8_t for_us;
475
 
476
  /* drop short ARP packets */
477
  if (p->tot_len < sizeof(struct etharp_hdr)) {
478
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 1, ("etharp_arp_input: packet dropped, too short (%d/%d)\n", p->tot_len, sizeof(struct etharp_hdr)));
479
    pbuf_free(p);
480
    return;
481
  }
482
 
483
  hdr = p->payload;
484
 
485
  /* get aligned copies of addresses */
486
  *(struct ip_addr2 *)&sipaddr = hdr->sipaddr;
487
  *(struct ip_addr2 *)&dipaddr = hdr->dipaddr;
488
 
489
  /* this interface is not configured? */
490
  if (netif->ip_addr.addr == 0) {
491
    for_us = 0;
492
  } else {
493
    /* ARP packet directed to us? */
494
    for_us = ip_addr_cmp(&dipaddr, &(netif->ip_addr));
495
  }
496
 
497
  /* ARP message directed to us? */
498
  if (for_us) {
499
    /* add IP address in ARP cache; assume requester wants to talk to us.
500
     * can result in directly sending the queued packets for this host. */
501
    update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), ETHARP_TRY_HARD);
502
  /* ARP message not directed to us? */
503
  } else {
504
    /* update the source IP address in the cache, if present */
505
    update_arp_entry(netif, &sipaddr, &(hdr->shwaddr), 0);
506
  }
507
 
508
  /* now act on the message itself */
509
  switch (htons(hdr->opcode)) {
510
  /* ARP request? */
511
  case ARP_REQUEST:
512
    /* ARP request. If it asked for our address, we send out a
513
     * reply. In any case, we time-stamp any existing ARP entry,
514
     * and possiby send out an IP packet that was queued on it. */
515
 
516
    LWIP_DEBUGF (ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: incoming ARP request\n"));
517
    /* ARP request for our address? */
518
    if (for_us) {
519
 
520
      LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: replying to ARP request for our IP address\n"));
521
      /* re-use pbuf to send ARP reply */
522
      hdr->opcode = htons(ARP_REPLY);
523
 
524
      hdr->dipaddr = hdr->sipaddr;
525
      hdr->sipaddr = *(struct ip_addr2 *)&netif->ip_addr;
526
 
527
      for(i = 0; i < netif->hwaddr_len; ++i) {
528
        hdr->dhwaddr.addr[i] = hdr->shwaddr.addr[i];
529
        hdr->shwaddr.addr[i] = ethaddr->addr[i];
530
        hdr->ethhdr.dest.addr[i] = hdr->dhwaddr.addr[i];
531
        hdr->ethhdr.src.addr[i] = ethaddr->addr[i];
532
      }
533
 
534
      hdr->hwtype = htons(HWTYPE_ETHERNET);
535
      ARPH_HWLEN_SET(hdr, netif->hwaddr_len);
536
 
537
      hdr->proto = htons(ETHTYPE_IP);
538
      ARPH_PROTOLEN_SET(hdr, sizeof(struct ip_addr));
539
 
540
      hdr->ethhdr.type = htons(ETHTYPE_ARP);
541
      /* return ARP reply */
542
      netif->linkoutput(netif, p);
543
    /* we are not configured? */
544
    } else if (netif->ip_addr.addr == 0) {
545
      /* { for_us == 0 and netif->ip_addr.addr == 0 } */
546
      LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n"));
547
    /* request was not directed to us */
548
    } else {
549
      /* { for_us == 0 and netif->ip_addr.addr != 0 } */
550
      LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: ARP request was not for us.\n"));
551
    }
552
    break;
553
  case ARP_REPLY:
554
    /* ARP reply. We already updated the ARP cache earlier. */
555
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: incoming ARP reply\n"));
556
#if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
557
    /* When unconfigured, DHCP wants to know about ARP replies from the
558
     * address offered to us, as that means someone else uses it already! */
559
    if (netif->ip_addr.addr == 0) dhcp_arp_reply(netif, &sipaddr);
560
#endif
561
    break;
562
  default:
563
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_arp_input: ARP unknown opcode type %d\n", htons(hdr->opcode)));
564
    break;
565
  }
566
  /* free ARP packet */
567
  pbuf_free(p);
568
}
569
 
570
/**
571
 * Resolve and fill-in Ethernet address header for outgoing packet.
572
 *
573
 * For IP multicast and broadcast, corresponding Ethernet addresses
574
 * are selected and the packet is transmitted on the link.
575
 *
576
 * For unicast addresses, the packet is submitted to etharp_query(). In
577
 * case the IP address is outside the local network, the IP address of
578
 * the gateway is used.
579
 *
580
 * @param netif The lwIP network interface which the IP packet will be sent on.
581
 * @param ipaddr The IP address of the packet destination.
582
 * @param pbuf The pbuf(s) containing the IP packet to be sent.
583
 *
584
 * @return
585
 * - ERR_RTE No route to destination (no gateway to external networks),
586
 * or the return type of either etharp_query() or netif->linkoutput().
587
 */
588
err_t
589
etharp_output(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
590
{
591
  struct eth_addr *dest, *srcaddr, mcastaddr;
592
  struct eth_hdr *ethhdr;
593
  u8_t i;
594
 
595
  /* make room for Ethernet header - should not fail */
596
  if (pbuf_header(q, sizeof(struct eth_hdr)) != 0) {
597
    /* bail out */
598
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 2, ("etharp_output: could not allocate room for header.\n"));
599
    LINK_STATS_INC(link.lenerr);
600
    return ERR_BUF;
601
  }
602
 
603
  /* assume unresolved Ethernet address */
604
  dest = NULL;
605
  /* Determine on destination hardware address. Broadcasts and multicasts
606
   * are special, other IP addresses are looked up in the ARP table. */
607
 
608
  /* broadcast destination IP address? */
609
  if (ip_addr_isbroadcast(ipaddr, netif)) {
610
    /* broadcast on Ethernet also */
611
    dest = (struct eth_addr *)&ethbroadcast;
612
  /* multicast destination IP address? */
613
  } else if (ip_addr_ismulticast(ipaddr)) {
614
    /* Hash IP multicast address to MAC address.*/
615
    mcastaddr.addr[0] = 0x01;
616
    mcastaddr.addr[1] = 0x00;
617
    mcastaddr.addr[2] = 0x5e;
618
    mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
619
    mcastaddr.addr[4] = ip4_addr3(ipaddr);
620
    mcastaddr.addr[5] = ip4_addr4(ipaddr);
621
    /* destination Ethernet address is multicast */
622
    dest = &mcastaddr;
623
  /* unicast destination IP address? */
624
  } else {
625
    /* outside local network? */
626
    if (!ip_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {
627
      /* interface has default gateway? */
628
      if (netif->gw.addr != 0) {
629
        /* send to hardware address of default gateway IP address */
630
        ipaddr = &(netif->gw);
631
      /* no default gateway available */
632
      } else {
633
        /* no route to destination error (default gateway missing) */
634
        return ERR_RTE;
635
      }
636
    }
637
    /* queue on destination Ethernet address belonging to ipaddr */
638
    return etharp_query(netif, ipaddr, q);
639
  }
640
 
641
  /* continuation for multicast/broadcast destinations */
642
  /* obtain source Ethernet address of the given interface */
643
  srcaddr = (struct eth_addr *)netif->hwaddr;
644
  ethhdr = q->payload;
645
  for (i = 0; i < netif->hwaddr_len; i++) {
646
    ethhdr->dest.addr[i] = dest->addr[i];
647
    ethhdr->src.addr[i] = srcaddr->addr[i];
648
  }
649
  ethhdr->type = htons(ETHTYPE_IP);
650
  /* send packet directly on the link */
651
  return netif->linkoutput(netif, q);
652
}
653
 
654
/**
655
 * Send an ARP request for the given IP address and/or queue a packet.
656
 *
657
 * If the IP address was not yet in the cache, a pending ARP cache entry
658
 * is added and an ARP request is sent for the given address. The packet
659
 * is queued on this entry.
660
 *
661
 * If the IP address was already pending in the cache, a new ARP request
662
 * is sent for the given address. The packet is queued on this entry.
663
 *
664
 * If the IP address was already stable in the cache, and a packet is
665
 * given, it is directly sent and no ARP request is sent out.
666
 *
667
 * If the IP address was already stable in the cache, and no packet is
668
 * given, an ARP request is sent out.
669
 *
670
 * @param netif The lwIP network interface on which ipaddr
671
 * must be queried for.
672
 * @param ipaddr The IP address to be resolved.
673
 * @param q If non-NULL, a pbuf that must be delivered to the IP address.
674
 * q is not freed by this function.
675
 *
676
 * @return
677
 * - ERR_BUF Could not make room for Ethernet header.
678
 * - ERR_MEM Hardware address unknown, and no more ARP entries available
679
 *   to query for address or queue the packet.
680
 * - ERR_MEM Could not queue packet due to memory shortage.
681
 * - ERR_RTE No route to destination (no gateway to external networks).
682
 * - ERR_ARG Non-unicast address given, those will not appear in ARP cache.
683
 *
684
 */
685
err_t etharp_query(struct netif *netif, struct ip_addr *ipaddr, struct pbuf *q)
686
{
687
  struct pbuf *p;
688
  struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
689
  err_t result = ERR_MEM;
690
  s8_t i; /* ARP entry index */
691
  u8_t k; /* Ethernet address octet index */
692
 
693
  /* non-unicast address? */
694
  if (ip_addr_isbroadcast(ipaddr, netif) ||
695
      ip_addr_ismulticast(ipaddr) ||
696
      ip_addr_isany(ipaddr)) {
697
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
698
    return ERR_ARG;
699
  }
700
 
701
  /* find entry in ARP cache, ask to create entry if queueing packet */
702
  i = find_entry(ipaddr, ETHARP_TRY_HARD);
703
 
704
  /* could not find or create entry? */
705
  if (i < 0)
706
  {
707
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
708
        #ifdef LWIP_DEBUG
709
    if (q) LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: packet dropped\n"));
710
        #endif
711
    return (err_t)i;
712
  }
713
 
714
  /* mark a fresh entry as pending (we just sent a request) */
715
  if (arp_table[i].state == ETHARP_STATE_EMPTY) {
716
    arp_table[i].state = ETHARP_STATE_PENDING;
717
  }
718
 
719
  /* { i is either a STABLE or (new or existing) PENDING entry } */
720
  LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
721
  ((arp_table[i].state == ETHARP_STATE_PENDING) ||
722
   (arp_table[i].state == ETHARP_STATE_STABLE)));
723
 
724
  /* do we have a pending entry? or an implicit query request? */
725
  if ((arp_table[i].state == ETHARP_STATE_PENDING) || (q == NULL)) {
726
    /* try to resolve it; send out ARP request */
727
    result = etharp_request(netif, ipaddr);
728
  }
729
 
730
  /* packet given? */
731
  if (q != NULL) {
732
    /* stable entry? */
733
    if (arp_table[i].state == ETHARP_STATE_STABLE) {
734
      /* we have a valid IP->Ethernet address mapping,
735
       * fill in the Ethernet header for the outgoing packet */
736
      struct eth_hdr *ethhdr = q->payload;
737
      for(k = 0; k < netif->hwaddr_len; k++) {
738
        ethhdr->dest.addr[k] = arp_table[i].ethaddr.addr[k];
739
        ethhdr->src.addr[k]  = srcaddr->addr[k];
740
      }
741
      ethhdr->type = htons(ETHTYPE_IP);
742
      LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: sending packet %p\n", (void *)q));
743
      /* send the packet */
744
      result = netif->linkoutput(netif, q);
745
    /* pending entry? (either just created or already pending */
746
    } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
747
#if ARP_QUEUEING /* queue the given q packet */
748
      /* copy any PBUF_REF referenced payloads into PBUF_RAM */
749
      /* (the caller of lwIP assumes the referenced payload can be
750
       * freed after it returns from the lwIP call that brought us here) */
751
      p = pbuf_take(q);
752
      /* packet could be taken over? */
753
      if (p != NULL) {
754
        /* queue packet ... */
755
        if (arp_table[i].p == NULL) {
756
                /* ... in the empty queue */
757
                pbuf_ref(p);
758
                arp_table[i].p = p;
759
#if 0 /* multi-packet-queueing disabled, see bug #11400 */
760
        } else {
761
                /* ... at tail of non-empty queue */
762
          pbuf_queue(arp_table[i].p, p);
763
#endif
764
        }
765
        LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %d\n", (void *)q, i));
766
        result = ERR_OK;
767
      } else {
768
        LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
769
        /* { result == ERR_MEM } through initialization */
770
      }
771
#else /* ARP_QUEUEING == 0 */
772
      /* q && state == PENDING && ARP_QUEUEING == 0 => result = ERR_MEM */
773
      /* { result == ERR_MEM } through initialization */
774
      LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_query: Ethernet destination address unknown, queueing disabled, packet %p dropped\n", (void *)q));
775
#endif
776
    }
777
  }
778
  return result;
779
}
780
 
781
err_t etharp_request(struct netif *netif, struct ip_addr *ipaddr)
782
{
783
  struct pbuf *p;
784
  struct eth_addr * srcaddr = (struct eth_addr *)netif->hwaddr;
785
  err_t result = ERR_OK;
786
  u8_t k; /* ARP entry index */
787
 
788
  /* allocate a pbuf for the outgoing ARP request packet */
789
  p = pbuf_alloc(PBUF_LINK, sizeof(struct etharp_hdr), PBUF_RAM);
790
  /* could allocate a pbuf for an ARP request? */
791
  if (p != NULL) {
792
    struct etharp_hdr *hdr = p->payload;
793
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE, ("etharp_request: sending ARP request.\n"));
794
    hdr->opcode = htons(ARP_REQUEST);
795
    for (k = 0; k < netif->hwaddr_len; k++)
796
    {
797
      hdr->shwaddr.addr[k] = srcaddr->addr[k];
798
      /* the hardware address is what we ask for, in
799
       * a request it is a don't-care value, we use zeroes */
800
      hdr->dhwaddr.addr[k] = 0x00;
801
    }
802
    hdr->dipaddr = *(struct ip_addr2 *)ipaddr;
803
    hdr->sipaddr = *(struct ip_addr2 *)&netif->ip_addr;
804
 
805
    hdr->hwtype = htons(HWTYPE_ETHERNET);
806
    ARPH_HWLEN_SET(hdr, netif->hwaddr_len);
807
 
808
    hdr->proto = htons(ETHTYPE_IP);
809
    ARPH_PROTOLEN_SET(hdr, sizeof(struct ip_addr));
810
    for (k = 0; k < netif->hwaddr_len; ++k)
811
    {
812
      /* broadcast to all network interfaces on the local network */
813
      hdr->ethhdr.dest.addr[k] = 0xff;
814
      hdr->ethhdr.src.addr[k] = srcaddr->addr[k];
815
    }
816
    hdr->ethhdr.type = htons(ETHTYPE_ARP);
817
    /* send ARP query */
818
    result = netif->linkoutput(netif, p);
819
    /* free ARP query packet */
820
    pbuf_free(p);
821
    p = NULL;
822
  /* could not allocate pbuf for ARP request */
823
  } else {
824
    result = ERR_MEM;
825
    LWIP_DEBUGF(ETHARP_DEBUG | DBG_TRACE | 2, ("etharp_request: could not allocate pbuf for ARP request.\n"));
826
  }
827
  return result;
828
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.