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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [redboot/] [current/] [src/] [net/] [ip.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      net/ip.c
4
//
5
//      Stand-alone IP networking support for RedBoot
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    gthomas
43
// Contributors: gthomas
44
// Date:         2000-07-14
45
// Purpose:      
46
// Description:  
47
//              
48
// This code is part of RedBoot (tm).
49
//
50
//####DESCRIPTIONEND####
51
//
52
//==========================================================================
53
 
54
#include <net/net.h>
55
 
56
#ifndef CYGDAT_REDBOOT_DEFAULT_IP_ADDR
57
# define CYGDAT_REDBOOT_DEFAULT_IP_ADDR 0, 0, 0, 0
58
#endif
59
#ifndef CYGDAT_REDBOOT_DEFAULT_IP_ADDR_MASK
60
# define CYGDAT_REDBOOT_DEFAULT_IP_ADDR_MASK 255, 255, 255, 0
61
#endif
62
#ifndef CYGDAT_REDBOOT_DEFAULT_GATEWAY_IP_ADDR
63
# define CYGDAT_REDBOOT_DEFAULT_GATEWAY_IP_ADDR 0, 0, 0, 0
64
#endif
65
 
66
ip_addr_t __local_ip_addr = { CYGDAT_REDBOOT_DEFAULT_IP_ADDR };
67
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
68
ip_addr_t __local_ip_mask = { CYGDAT_REDBOOT_DEFAULT_IP_ADDR_MASK };
69
ip_addr_t __local_ip_gate = { CYGDAT_REDBOOT_DEFAULT_GATEWAY_IP_ADDR };
70
#endif
71
 
72
static word ip_ident;
73
 
74
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
75
/*
76
 * See if an address is on the local network
77
 */
78
int
79
__ip_addr_local(ip_addr_t *addr)
80
{
81
  return !(
82
           ((__local_ip_addr[0] ^ (*addr)[0]) & __local_ip_mask[0]) |
83
           ((__local_ip_addr[1] ^ (*addr)[1]) & __local_ip_mask[1]) |
84
           ((__local_ip_addr[2] ^ (*addr)[2]) & __local_ip_mask[2]) |
85
           ((__local_ip_addr[3] ^ (*addr)[3]) & __local_ip_mask[3]));
86
}
87
#endif
88
 
89
/*
90
 * Match given IP address to our address.
91
 * Check for broadcast matches as well.
92
 */
93
static int
94
ip_addr_match(ip_addr_t addr)
95
{
96
    if (addr[0] == 255 && addr[1] == 255 && addr[2] == 255 && addr[3] == 255)
97
        return 1;
98
 
99
    if (!memcmp(addr, __local_ip_addr, sizeof(ip_addr_t)))
100
        return 1;
101
 
102
    /*
103
     * Consider it an address match if we haven't gotten our IP address yet.
104
     * Some DHCP servers will address IP packets to the assigned address
105
     * instead of a IP broadcast address.
106
     */
107
    if (__local_ip_addr[0] == 0 && __local_ip_addr[1] == 0 &&
108
        __local_ip_addr[2] == 0 && __local_ip_addr[3] == 0)
109
        return 1;
110
 
111
    return 0;
112
}
113
 
114
 
115
extern void __tcp_handler(pktbuf_t *, ip_route_t *);
116
 
117
/*
118
 * Handle IP packets coming from the polled ethernet interface.
119
 */
120
void
121
__ip_handler(pktbuf_t *pkt, enet_addr_t *src_enet_addr)
122
{
123
    ip_header_t *ip = pkt->ip_hdr;
124
    ip_route_t  r;
125
    int         hdr_bytes;
126
 
127
    /* first make sure its ours and has a good checksum. */
128
    if (!ip_addr_match(ip->destination) ||
129
        __sum((word *)ip, ip->hdr_len << 2, 0) != 0) {
130
        __pktbuf_free(pkt);
131
        return;
132
    }
133
 
134
    memcpy(r.ip_addr, ip->source, sizeof(ip_addr_t));
135
    memcpy(r.enet_addr, src_enet_addr, sizeof(enet_addr_t));
136
 
137
    hdr_bytes = ip->hdr_len << 2;
138
    pkt->pkt_bytes = ntohs(ip->length) - hdr_bytes;
139
 
140
    switch (ip->protocol) {
141
 
142
#if NET_SUPPORT_ICMP
143
      case IP_PROTO_ICMP:
144
        pkt->icmp_hdr = (icmp_header_t *)(((char *)ip) + hdr_bytes);
145
        __icmp_handler(pkt, &r);
146
        break;
147
#endif
148
 
149
#if NET_SUPPORT_TCP
150
      case IP_PROTO_TCP:
151
        pkt->tcp_hdr = (tcp_header_t *)(((char *)ip) + hdr_bytes);
152
        __tcp_handler(pkt, &r);
153
        break;
154
#endif
155
 
156
#if NET_SUPPORT_UDP
157
      case IP_PROTO_UDP:
158
        pkt->udp_hdr = (udp_header_t *)(((char *)ip) + hdr_bytes);
159
        __udp_handler(pkt, &r);
160
        break;
161
#endif
162
 
163
      default:
164
        __pktbuf_free(pkt);
165
        break;
166
    }
167
}
168
 
169
 
170
/*
171
 * Send an IP packet.
172
 *
173
 * The IP data field should contain pkt->pkt_bytes of data.
174
 * pkt->[udp|tcp|icmp]_hdr points to the IP data field. Any
175
 * IP options are assumed to be already in place in the IP
176
 * options field.
177
 */
178
int
179
__ip_send(pktbuf_t *pkt, int protocol, ip_route_t *dest)
180
{
181
    ip_header_t *ip = pkt->ip_hdr;
182
    int         hdr_bytes;
183
    unsigned short cksum;
184
 
185
    /*
186
     * Figure out header length. The use udp_hdr is
187
     * somewhat arbitrary, but works because it is
188
     * a union with other IP protocol headers.
189
     */
190
    hdr_bytes = (((char *)pkt->udp_hdr) - ((char *)ip));
191
 
192
    pkt->pkt_bytes += hdr_bytes;
193
 
194
    ip->version = 4;
195
    ip->hdr_len = hdr_bytes >> 2;
196
    ip->tos = 0;
197
    ip->length = htons(pkt->pkt_bytes);
198
    ip->ident = htons(ip_ident);
199
    ip_ident++;
200
    ip->fragment = 0;
201
    ip->ttl = 255;
202
    ip->ttl = 64;
203
    ip->protocol = protocol;
204
    ip->checksum = 0;
205
    memcpy(ip->source, __local_ip_addr, sizeof(ip_addr_t));
206
    memcpy(ip->destination, dest->ip_addr, sizeof(ip_addr_t));
207
    cksum = __sum((word *)ip, hdr_bytes, 0);
208
    ip->checksum = htons(cksum);
209
 
210
    __enet_send(pkt, &dest->enet_addr, ETH_TYPE_IP);
211
    return 0;
212
}
213
 
214
 

powered by: WebSVN 2.1.0

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