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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [FreeTCPIP/] [uip_arp.c] - Blame information for rev 615

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/**
2
 * \addtogroup uip
3
 * @{
4
 */
5
 
6
/**
7
 * \defgroup uiparp uIP Address Resolution Protocol
8
 * @{
9
 *
10
 * The Address Resolution Protocol ARP is used for mapping between IP
11
 * addresses and link level addresses such as the Ethernet MAC
12
 * addresses. ARP uses broadcast queries to ask for the link level
13
 * address of a known IP address and the host which is configured with
14
 * the IP address for which the query was meant, will respond with its
15
 * link level address.
16
 *
17
 * \note This ARP implementation only supports Ethernet.
18
 */
19
 
20
/**
21
 * \file
22
 * Implementation of the ARP Address Resolution Protocol.
23
 * \author Adam Dunkels <adam@dunkels.com>
24
 *
25
 */
26
 
27
/*
28
 * Copyright (c) 2001-2003, Adam Dunkels.
29
 * All rights reserved.
30
 *
31
 * Redistribution and use in source and binary forms, with or without
32
 * modification, are permitted provided that the following conditions
33
 * are met:
34
 * 1. Redistributions of source code must retain the above copyright
35
 *    notice, this list of conditions and the following disclaimer.
36
 * 2. Redistributions in binary form must reproduce the above copyright
37
 *    notice, this list of conditions and the following disclaimer in the
38
 *    documentation and/or other materials provided with the distribution.
39
 * 3. The name of the author may not be used to endorse or promote
40
 *    products derived from this software without specific prior
41
 *    written permission.
42
 *
43
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 * This file is part of the uIP TCP/IP stack.
56
 *
57
 * $Id: uip_arp.c 2 2011-07-17 20:13:17Z filepang@gmail.com $
58
 *
59
 */
60
#include "net/uip_arp.h"
61
 
62
#include <string.h>
63
 
64
#include "net/pack_struct_start.h"
65
struct arp_hdr
66
{
67
        struct uip_eth_hdr      ethhdr;
68
        u16_t                           hwtype;
69
        u16_t                           protocol;
70
        u8_t                            hwlen;
71
        u8_t                            protolen;
72
        u16_t                           opcode;
73
        struct uip_eth_addr shwaddr;
74
        uip_ipaddr_t            sipaddr;
75
        struct uip_eth_addr dhwaddr;
76
        uip_ipaddr_t            dipaddr;
77
}
78
 
79
#include "net/pack_struct_end.h"
80
 
81
#include "net/pack_struct_start.h"
82
 
83
struct ethip_hdr
84
{
85
        struct uip_eth_hdr      ethhdr;
86
 
87
        /* IP header. */
88
        u8_t                            vhl, tos, len[2], ipid[2], ipoffset[2], ttl, proto;
89
        u16_t                           ipchksum;
90
        uip_ipaddr_t            srcipaddr, destipaddr;
91
}
92
 
93
#include "net/pack_struct_end.h"
94
 
95
#define ARP_REQUEST             1
96
#define ARP_REPLY               2
97
 
98
#define ARP_HWTYPE_ETH  1
99
 
100
struct arp_entry
101
{
102
        uip_ipaddr_t            ipaddr;
103
        struct uip_eth_addr ethaddr;
104
        u8_t                            time;
105
};
106
 
107
static const struct uip_eth_addr        broadcast_ethaddr = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } };
108
 
109
static struct arp_entry                         arp_table[UIP_ARPTAB_SIZE];
110
static uip_ipaddr_t                                     ipaddr;
111
static u8_t                                                     i, c;
112
 
113
static u8_t                                                     arptime;
114
static u8_t                                                     tmpage;
115
 
116
#define BUF             ( ( struct arp_hdr * ) &uip_buf[0] )
117
#define IPBUF   ( ( struct ethip_hdr * ) &uip_buf[0] )
118
 
119
#ifdef DEBUG
120
        #undef DEBUG
121
#endif
122
#define DEBUG   0
123
#if DEBUG
124
#include <stdio.h>
125
#define PRINTF( ... )   printf( __VA_ARGS__ )
126
#else
127
 
128
//#define PRINTF( ... )
129
#endif
130
 
131
/*-----------------------------------------------------------------------------------*/
132
 
133
/**
134
 * Initialize the ARP module.
135
 *
136
 */
137
 
138
/*-----------------------------------------------------------------------------------*/
139
void uip_arp_init( void )
140
{
141
        for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
142
        {
143
                memset( &arp_table[i].ipaddr, 0, 4 );
144
        }
145
}
146
 
147
/*-----------------------------------------------------------------------------------*/
148
 
149
/**
150
 * Periodic ARP processing function.
151
 *
152
 * This function performs periodic timer processing in the ARP module
153
 * and should be called at regular intervals. The recommended interval
154
 * is 10 seconds between the calls.
155
 *
156
 */
157
 
158
/*-----------------------------------------------------------------------------------*/
159
void uip_arp_timer( void )
160
{
161
        struct arp_entry        *tabptr;
162
 
163
        ++arptime;
164
        for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
165
        {
166
                tabptr = &arp_table[i];
167
                if( uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) && arptime - tabptr->time >= UIP_ARP_MAXAGE )
168
                {
169
                        memset( &tabptr->ipaddr, 0, 4 );
170
                }
171
        }
172
}
173
 
174
/*-----------------------------------------------------------------------------------*/
175
static void uip_arp_update( uip_ipaddr_t *ipaddr, struct uip_eth_addr *ethaddr )
176
{
177
        register struct arp_entry       *tabptr;
178
 
179
        /* Walk through the ARP mapping table and try to find an entry to
180
     update. If none is found, the IP -> MAC address mapping is
181
     inserted in the ARP table. */
182
        for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
183
        {
184
                tabptr = &arp_table[i];
185
 
186
                /* Only check those entries that are actually in use. */
187
                if( !uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) )
188
                {
189
                        /* Check if the source IP address of the incoming packet matches
190
         the IP address in this ARP table entry. */
191
                        if( uip_ipaddr_cmp(ipaddr, &tabptr->ipaddr) )
192
                        {
193
                                /* An old entry found, update this and return. */
194
                                memcpy( tabptr->ethaddr.addr, ethaddr->addr, 6 );
195
                                tabptr->time = arptime;
196
 
197
                                return;
198
                        }
199
                }
200
        }
201
 
202
        /* If we get here, no existing ARP table entry was found, so we
203
     create one. */
204
 
205
        /* First, we try to find an unused entry in the ARP table. */
206
        for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
207
        {
208
                tabptr = &arp_table[i];
209
                if( uip_ipaddr_cmp(&tabptr->ipaddr, &uip_all_zeroes_addr) )
210
                {
211
                        break;
212
                }
213
        }
214
 
215
        /* If no unused entry is found, we try to find the oldest entry and
216
     throw it away. */
217
        if( i == UIP_ARPTAB_SIZE )
218
        {
219
                tmpage = 0;
220
                c = 0;
221
                for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
222
                {
223
                        tabptr = &arp_table[i];
224
                        if( arptime - tabptr->time > tmpage )
225
                        {
226
                                tmpage = arptime - tabptr->time;
227
                                c = i;
228
                        }
229
                }
230
 
231
                i = c;
232
                tabptr = &arp_table[i];
233
        }
234
 
235
        /* Now, i is the ARP table entry which we will fill with the new
236
     information. */
237
        uip_ipaddr_copy( &tabptr->ipaddr, ipaddr );
238
        memcpy( tabptr->ethaddr.addr, ethaddr->addr, 6 );
239
        tabptr->time = arptime;
240
}
241
 
242
/*-----------------------------------------------------------------------------------*/
243
 
244
/**
245
 * ARP processing for incoming IP packets
246
 *
247
 * This function should be called by the device driver when an IP
248
 * packet has been received. The function will check if the address is
249
 * in the ARP cache, and if so the ARP cache entry will be
250
 * refreshed. If no ARP cache entry was found, a new one is created.
251
 *
252
 * This function expects an IP packet with a prepended Ethernet header
253
 * in the uip_buf[] buffer, and the length of the packet in the global
254
 * variable uip_len.
255
 */
256
 
257
/*-----------------------------------------------------------------------------------*/
258
#if 0
259
void uip_arp_ipin( void )
260
{
261
        uip_len -= sizeof( struct uip_eth_hdr );
262
 
263
        /* Only insert/update an entry if the source IP address of the
264
     incoming IP packet comes from a host on the local network. */
265
        if( (IPBUF->srcipaddr[0] & uip_netmask[0]) != (uip_hostaddr[0] & uip_netmask[0]) )
266
        {
267
                return;
268
        }
269
 
270
        if( (IPBUF->srcipaddr[1] & uip_netmask[1]) != (uip_hostaddr[1] & uip_netmask[1]) )
271
        {
272
                return;
273
        }
274
 
275
        uip_arp_update( IPBUF->srcipaddr, &(IPBUF->ethhdr.src) );
276
 
277
        return;
278
}
279
 
280
#endif /* 0 */
281
 
282
/*-----------------------------------------------------------------------------------*/
283
 
284
/**
285
 * ARP processing for incoming ARP packets.
286
 *
287
 * This function should be called by the device driver when an ARP
288
 * packet has been received. The function will act differently
289
 * depending on the ARP packet type: if it is a reply for a request
290
 * that we previously sent out, the ARP cache will be filled in with
291
 * the values from the ARP reply. If the incoming ARP packet is an ARP
292
 * request for our IP address, an ARP reply packet is created and put
293
 * into the uip_buf[] buffer.
294
 *
295
 * When the function returns, the value of the global variable uip_len
296
 * indicates whether the device driver should send out a packet or
297
 * not. If uip_len is zero, no packet should be sent. If uip_len is
298
 * non-zero, it contains the length of the outbound packet that is
299
 * present in the uip_buf[] buffer.
300
 *
301
 * This function expects an ARP packet with a prepended Ethernet
302
 * header in the uip_buf[] buffer, and the length of the packet in the
303
 * global variable uip_len.
304
 */
305
 
306
/*-----------------------------------------------------------------------------------*/
307
void uip_arp_arpin( void )
308
{
309
        if( uip_len < sizeof(struct arp_hdr) )
310
        {
311
                uip_len = 0;
312
                return;
313
        }
314
 
315
        uip_len = 0;
316
 
317
        switch( BUF->opcode )
318
        {
319
                case HTONS( ARP_REQUEST ):
320
                        /* ARP request. If it asked for our address, we send out a
321
                reply. */
322
 
323
                        /*    if(BUF->dipaddr[0] == uip_hostaddr[0] &&
324
                        BUF->dipaddr[1] == uip_hostaddr[1]) {*/
325
 
326
                        //PRINTF( "uip_arp_arpin: request for %d.%d.%d.%d (we are %d.%d.%d.%d)\n", BUF->dipaddr.u8[0], BUF->dipaddr.u8[1], BUF->dipaddr.u8[2],                  
327
                        //BUF->dipaddr.u8[3], uip_hostaddr.u8[0], uip_hostaddr.u8[1], uip_hostaddr.u8[2], uip_hostaddr.u8[3] );
328
                        if( uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr) )
329
                        {
330
                                /* First, we register the one who made the request in our ARP
331
                                table, since it is likely that we will do more communication
332
                                with this host in the future. */
333
                                uip_arp_update( &BUF->sipaddr, &BUF->shwaddr );
334
 
335
                                BUF->opcode = HTONS( ARP_REPLY );
336
 
337
                                memcpy( BUF->dhwaddr.addr, BUF->shwaddr.addr, 6 );
338
                                memcpy( BUF->shwaddr.addr, uip_ethaddr.addr, 6 );
339
                                memcpy( BUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
340
                                memcpy( BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6 );
341
 
342
                                uip_ipaddr_copy( &BUF->dipaddr, &BUF->sipaddr );
343
                                uip_ipaddr_copy( &BUF->sipaddr, &uip_hostaddr );
344
 
345
                                BUF->ethhdr.type = HTONS( UIP_ETHTYPE_ARP );
346
                                uip_len = sizeof( struct arp_hdr );
347
                        }
348
 
349
                        break;
350
 
351
                case HTONS( ARP_REPLY ):
352
                        /* ARP reply. We insert or update the ARP table if it was meant
353
                        for us. */
354
                        if( uip_ipaddr_cmp(&BUF->dipaddr, &uip_hostaddr) )
355
                        {
356
                                uip_arp_update( &BUF->sipaddr, &BUF->shwaddr );
357
                        }
358
 
359
                        break;
360
        }
361
 
362
        return;
363
}
364
 
365
/*-----------------------------------------------------------------------------------*/
366
 
367
/**
368
 * Prepend Ethernet header to an outbound IP packet and see if we need
369
 * to send out an ARP request.
370
 *
371
 * This function should be called before sending out an IP packet. The
372
 * function checks the destination IP address of the IP packet to see
373
 * what Ethernet MAC address that should be used as a destination MAC
374
 * address on the Ethernet.
375
 *
376
 * If the destination IP address is in the local network (determined
377
 * by logical ANDing of netmask and our IP address), the function
378
 * checks the ARP cache to see if an entry for the destination IP
379
 * address is found. If so, an Ethernet header is prepended and the
380
 * function returns. If no ARP cache entry is found for the
381
 * destination IP address, the packet in the uip_buf[] is replaced by
382
 * an ARP request packet for the IP address. The IP packet is dropped
383
 * and it is assumed that they higher level protocols (e.g., TCP)
384
 * eventually will retransmit the dropped packet.
385
 *
386
 * If the destination IP address is not on the local network, the IP
387
 * address of the default router is used instead.
388
 *
389
 * When the function returns, a packet is present in the uip_buf[]
390
 * buffer, and the length of the packet is in the global variable
391
 * uip_len.
392
 */
393
 
394
/*-----------------------------------------------------------------------------------*/
395
void uip_arp_out( void )
396
{
397
        struct arp_entry        *tabptr;
398
 
399
        /* Find the destination IP address in the ARP table and construct
400
     the Ethernet header. If the destination IP addres isn't on the
401
     local network, we use the default router's IP address instead.
402
 
403
     If not ARP table entry is found, we overwrite the original IP
404
     packet with an ARP request for the IP address. */
405
 
406
        /* First check if destination is a local broadcast. */
407
        if( uip_ipaddr_cmp(&IPBUF->destipaddr, &uip_broadcast_addr) )
408
        {
409
                memcpy( IPBUF->ethhdr.dest.addr, broadcast_ethaddr.addr, 6 );
410
        }
411
        else
412
        {
413
                /* Check if the destination address is on the local network. */
414
                if( !uip_ipaddr_maskcmp(&IPBUF->destipaddr, &uip_hostaddr, &uip_netmask) )
415
                {
416
                        /* Destination address was not on the local network, so we need to
417
         use the default router's IP address instead of the destination
418
         address when determining the MAC address. */
419
                        uip_ipaddr_copy( &ipaddr, &uip_draddr );
420
                }
421
                else
422
                {
423
                        /* Else, we use the destination IP address. */
424
                        uip_ipaddr_copy( &ipaddr, &IPBUF->destipaddr );
425
                }
426
 
427
                for( i = 0; i < UIP_ARPTAB_SIZE; ++i )
428
                {
429
                        tabptr = &arp_table[i];
430
                        if( uip_ipaddr_cmp(&ipaddr, &tabptr->ipaddr) )
431
                        {
432
                                break;
433
                        }
434
                }
435
 
436
                if( i == UIP_ARPTAB_SIZE )
437
                {
438
                        /* The destination address was not in our ARP table, so we
439
         overwrite the IP packet with an ARP request. */
440
                        memset( BUF->ethhdr.dest.addr, 0xff, 6 );
441
                        memset( BUF->dhwaddr.addr, 0x00, 6 );
442
                        memcpy( BUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
443
                        memcpy( BUF->shwaddr.addr, uip_ethaddr.addr, 6 );
444
 
445
                        uip_ipaddr_copy( &BUF->dipaddr, &ipaddr );
446
                        uip_ipaddr_copy( &BUF->sipaddr, &uip_hostaddr );
447
                        BUF->opcode = HTONS( ARP_REQUEST ); /* ARP request. */
448
                        BUF->hwtype = HTONS( ARP_HWTYPE_ETH );
449
                        BUF->protocol = HTONS( UIP_ETHTYPE_IP );
450
                        BUF->hwlen = 6;
451
                        BUF->protolen = 4;
452
                        BUF->ethhdr.type = HTONS( UIP_ETHTYPE_ARP );
453
 
454
                        uip_appdata = &uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN];
455
 
456
                        uip_len = sizeof( struct arp_hdr );
457
                        return;
458
                }
459
 
460
                /* Build an ethernet header. */
461
                memcpy( IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6 );
462
        }
463
 
464
        memcpy( IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6 );
465
 
466
        IPBUF->ethhdr.type = HTONS( UIP_ETHTYPE_IP );
467
 
468
        uip_len += sizeof( struct uip_eth_hdr );
469
}
470
 
471
/*-----------------------------------------------------------------------------------*/
472
 
473
/** @} */
474
 
475
/** @} */

powered by: WebSVN 2.1.0

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