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/] [core/] [ipv4/] [icmp.c] - Blame information for rev 583

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/*
2
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. The name of the author may not be used to endorse or promote products
14
 *    derived from this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25
 * OF SUCH DAMAGE.
26
 *
27
 * This file is part of the lwIP TCP/IP stack.
28
 *
29
 * Author: Adam Dunkels <adam@sics.se>
30
 *
31
 */
32
 
33
/* Some ICMP messages should be passed to the transport protocols. This
34
   is not implemented. */
35
 
36
#include "lwip/opt.h"
37
 
38
#include "lwip/icmp.h"
39
#include "lwip/inet.h"
40
#include "lwip/ip.h"
41
#include "lwip/def.h"
42
 
43
#include "lwip/stats.h"
44
 
45
#include "lwip/snmp.h"
46
 
47
#include <string.h>
48
 
49
void
50
icmp_input(struct pbuf *p, struct netif *inp)
51
{
52
  unsigned char type;
53
  unsigned char code;
54
  struct icmp_echo_hdr *iecho;
55
  struct ip_hdr *iphdr;
56
  struct ip_addr tmpaddr;
57
  u16_t hlen;
58
 
59
  ICMP_STATS_INC(icmp.recv);
60
  snmp_inc_icmpinmsgs();
61
 
62
 
63
  iphdr = p->payload;
64
  hlen = IPH_HL(iphdr) * 4;
65
  if (pbuf_header(p, -((s16_t)hlen)) || (p->tot_len < sizeof(u16_t)*2)) {
66
    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: short ICMP (%u bytes) received\n", p->tot_len));
67
    pbuf_free(p);
68
    ICMP_STATS_INC(icmp.lenerr);
69
    snmp_inc_icmpinerrors();
70
    return;
71
  }
72
 
73
  type = *((u8_t *)p->payload);
74
  code = *(((u8_t *)p->payload)+1);
75
  switch (type) {
76
  case ICMP_ECHO:
77
    /* broadcast or multicast destination address? */
78
    if (ip_addr_isbroadcast(&iphdr->dest, inp) || ip_addr_ismulticast(&iphdr->dest)) {
79
      LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: Not echoing to multicast or broadcast pings\n"));
80
      ICMP_STATS_INC(icmp.err);
81
      pbuf_free(p);
82
      return;
83
    }
84
    LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ping\n"));
85
    if (p->tot_len < sizeof(struct icmp_echo_hdr)) {
86
      LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: bad ICMP echo received\n"));
87
      pbuf_free(p);
88
      ICMP_STATS_INC(icmp.lenerr);
89
      snmp_inc_icmpinerrors();
90
 
91
      return;
92
    }
93
    iecho = p->payload;
94
    if (inet_chksum_pbuf(p) != 0) {
95
      LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: checksum failed for received ICMP echo\n"));
96
      pbuf_free(p);
97
      ICMP_STATS_INC(icmp.chkerr);
98
      snmp_inc_icmpinerrors();
99
      return;
100
    }
101
    tmpaddr.addr = iphdr->src.addr;
102
    iphdr->src.addr = iphdr->dest.addr;
103
    iphdr->dest.addr = tmpaddr.addr;
104
    ICMPH_TYPE_SET(iecho, ICMP_ER);
105
    /* adjust the checksum */
106
    if (iecho->chksum >= htons(0xffff - (ICMP_ECHO << 8))) {
107
      iecho->chksum += htons(ICMP_ECHO << 8) + 1;
108
    } else {
109
      iecho->chksum += htons(ICMP_ECHO << 8);
110
    }
111
    ICMP_STATS_INC(icmp.xmit);
112
    /* increase number of messages attempted to send */
113
    snmp_inc_icmpoutmsgs();
114
    /* increase number of echo replies attempted to send */
115
    snmp_inc_icmpoutechoreps();
116
 
117
    pbuf_header(p, hlen);
118
    ip_output_if(p, &(iphdr->src), IP_HDRINCL,
119
                 IPH_TTL(iphdr), 0, IP_PROTO_ICMP, inp);
120
    break;
121
  default:
122
  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_input: ICMP type %d code %d not supported.\n", (int)type, (int)code));
123
    ICMP_STATS_INC(icmp.proterr);
124
    ICMP_STATS_INC(icmp.drop);
125
  }
126
  pbuf_free(p);
127
}
128
 
129
void
130
icmp_dest_unreach(struct pbuf *p, enum icmp_dur_type t)
131
{
132
  struct pbuf *q;
133
  struct ip_hdr *iphdr;
134
  struct icmp_dur_hdr *idur;
135
 
136
  q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
137
  /* ICMP header + IP header + 8 bytes of data */
138
 
139
  iphdr = p->payload;
140
 
141
  idur = q->payload;
142
  ICMPH_TYPE_SET(idur, ICMP_DUR);
143
  ICMPH_CODE_SET(idur, t);
144
 
145
  memcpy((char *)q->payload + 8, p->payload, IP_HLEN + 8);
146
 
147
  /* calculate checksum */
148
  idur->chksum = 0;
149
  idur->chksum = inet_chksum(idur, q->len);
150
  ICMP_STATS_INC(icmp.xmit);
151
  /* increase number of messages attempted to send */
152
  snmp_inc_icmpoutmsgs();
153
  /* increase number of destination unreachable messages attempted to send */
154
  snmp_inc_icmpoutdestunreachs();
155
 
156
  ip_output(q, NULL, &(iphdr->src),
157
            ICMP_TTL, 0, IP_PROTO_ICMP);
158
  pbuf_free(q);
159
}
160
 
161
#if IP_FORWARD
162
void
163
icmp_time_exceeded(struct pbuf *p, enum icmp_te_type t)
164
{
165
  struct pbuf *q;
166
  struct ip_hdr *iphdr;
167
  struct icmp_te_hdr *tehdr;
168
 
169
  q = pbuf_alloc(PBUF_IP, 8 + IP_HLEN + 8, PBUF_RAM);
170
 
171
  iphdr = p->payload;
172
  LWIP_DEBUGF(ICMP_DEBUG, ("icmp_time_exceeded from "));
173
  ip_addr_debug_print(ICMP_DEBUG, &(iphdr->src));
174
  LWIP_DEBUGF(ICMP_DEBUG, (" to "));
175
  ip_addr_debug_print(ICMP_DEBUG, &(iphdr->dest));
176
  LWIP_DEBUGF(ICMP_DEBUG, ("\n"));
177
 
178
  tehdr = q->payload;
179
  ICMPH_TYPE_SET(tehdr, ICMP_TE);
180
  ICMPH_CODE_SET(tehdr, t);
181
 
182
  /* copy fields from original packet */
183
  memcpy((char *)q->payload + 8, (char *)p->payload, IP_HLEN + 8);
184
 
185
  /* calculate checksum */
186
  tehdr->chksum = 0;
187
  tehdr->chksum = inet_chksum(tehdr, q->len);
188
  ICMP_STATS_INC(icmp.xmit);
189
  /* increase number of messages attempted to send */
190
  snmp_inc_icmpoutmsgs();
191
  /* increase number of destination unreachable messages attempted to send */
192
  snmp_inc_icmpouttimeexcds();
193
  ip_output(q, NULL, &(iphdr->src),
194
            ICMP_TTL, 0, IP_PROTO_ICMP);
195
  pbuf_free(q);
196
}
197
 
198
#endif /* IP_FORWARD */
199
 
200
 
201
 
202
 
203
 
204
 
205
 

powered by: WebSVN 2.1.0

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