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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/* @file
2
 *
3
 * This is the IP packet segmentation and reassembly implementation.
4
 *
5
 */
6
 
7
/*
8
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without modification,
12
 * are permitted provided that the following conditions are met:
13
 *
14
 * 1. Redistributions of source code must retain the above copyright notice,
15
 *    this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
17
 *    this list of conditions and the following disclaimer in the documentation
18
 *    and/or other materials provided with the distribution.
19
 * 3. The name of the author may not be used to endorse or promote products
20
 *    derived from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31
 * OF SUCH DAMAGE.
32
 *
33
 * This file is part of the lwIP TCP/IP stack.
34
 *
35
 * Author: Jani Monoses <jani@iv.ro>
36
 * original reassembly code by Adam Dunkels <adam@sics.se>
37
 *
38
 */
39
 
40
#include "lwip/opt.h"
41
#include "lwip/sys.h"
42
#include "lwip/ip.h"
43
#include "lwip/ip_frag.h"
44
#include "lwip/netif.h"
45
 
46
#include "lwip/stats.h"
47
 
48
#include <string.h>
49
 
50
/*
51
 * Copy len bytes from offset in pbuf to buffer
52
 *
53
 * helper used by both ip_reass and ip_frag
54
 */
55
static struct pbuf *
56
copy_from_pbuf(struct pbuf *p, u16_t * offset,
57
           u8_t * buffer, u16_t len)
58
{
59
  u16_t l;
60
 
61
  p->payload = (u8_t *)p->payload + *offset;
62
  p->len -= *offset;
63
  while (len) {
64
    l = len < p->len ? len : p->len;
65
    memcpy(buffer, p->payload, l);
66
    buffer += l;
67
    len -= l;
68
    if (len)
69
      p = p->next;
70
    else
71
      *offset = l;
72
  }
73
  return p;
74
}
75
 
76
#define IP_REASS_BUFSIZE 5760
77
#define IP_REASS_MAXAGE 30
78
#define IP_REASS_TMO 1000
79
 
80
static u8_t ip_reassbuf[IP_HLEN + IP_REASS_BUFSIZE];
81
static u8_t ip_reassbitmap[IP_REASS_BUFSIZE / (8 * 8)];
82
static const u8_t bitmap_bits[8] = { 0xff, 0x7f, 0x3f, 0x1f,
83
  0x0f, 0x07, 0x03, 0x01
84
};
85
static u16_t ip_reasslen;
86
static u8_t ip_reassflags;
87
#define IP_REASS_FLAG_LASTFRAG 0x01
88
 
89
static u8_t ip_reasstmr;
90
 
91
/* Reassembly timer */
92
static void
93
ip_reass_timer(void *arg)
94
{
95
  (void)arg;
96
  if (ip_reasstmr > 1) {
97
    ip_reasstmr--;
98
    sys_timeout(IP_REASS_TMO, ip_reass_timer, NULL);
99
  } else if (ip_reasstmr == 1)
100
  ip_reasstmr = 0;
101
}
102
 
103
struct pbuf *
104
ip_reass(struct pbuf *p)
105
{
106
  struct pbuf *q;
107
  struct ip_hdr *fraghdr, *iphdr;
108
  u16_t offset, len;
109
  u16_t i;
110
 
111
  IPFRAG_STATS_INC(ip_frag.recv);
112
 
113
  iphdr = (struct ip_hdr *) ip_reassbuf;
114
  fraghdr = (struct ip_hdr *) p->payload;
115
  /* If ip_reasstmr is zero, no packet is present in the buffer, so we
116
     write the IP header of the fragment into the reassembly
117
     buffer. The timer is updated with the maximum age. */
118
  if (ip_reasstmr == 0) {
119
    LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: new packet\n"));
120
    memcpy(iphdr, fraghdr, IP_HLEN);
121
    ip_reasstmr = IP_REASS_MAXAGE;
122
    sys_timeout(IP_REASS_TMO, ip_reass_timer, NULL);
123
    ip_reassflags = 0;
124
    /* Clear the bitmap. */
125
    memset(ip_reassbitmap, 0, sizeof(ip_reassbitmap));
126
  }
127
 
128
  /* Check if the incoming fragment matches the one currently present
129
     in the reasembly buffer. If so, we proceed with copying the
130
     fragment into the buffer. */
131
  if (ip_addr_cmp(&iphdr->src, &fraghdr->src) &&
132
      ip_addr_cmp(&iphdr->dest, &fraghdr->dest) &&
133
      IPH_ID(iphdr) == IPH_ID(fraghdr)) {
134
    LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: matching old packet\n"));
135
    IPFRAG_STATS_INC(ip_frag.cachehit);
136
    /* Find out the offset in the reassembly buffer where we should
137
       copy the fragment. */
138
    len = ntohs(IPH_LEN(fraghdr)) - IPH_HL(fraghdr) * 4;
139
    offset = (ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) * 8;
140
 
141
    /* If the offset or the offset + fragment length overflows the
142
       reassembly buffer, we discard the entire packet. */
143
    if (offset > IP_REASS_BUFSIZE || offset + len > IP_REASS_BUFSIZE) {
144
      LWIP_DEBUGF(IP_REASS_DEBUG,
145
       ("ip_reass: fragment outside of buffer (%d:%d/%d).\n", offset,
146
        offset + len, IP_REASS_BUFSIZE));
147
      sys_untimeout(ip_reass_timer, NULL);
148
      ip_reasstmr = 0;
149
      goto nullreturn;
150
    }
151
 
152
    /* Copy the fragment into the reassembly buffer, at the right
153
       offset. */
154
    LWIP_DEBUGF(IP_REASS_DEBUG,
155
     ("ip_reass: copying with offset %d into %d:%d\n", offset,
156
      IP_HLEN + offset, IP_HLEN + offset + len));
157
    i = IPH_HL(fraghdr) * 4;
158
    copy_from_pbuf(p, &i, &ip_reassbuf[IP_HLEN + offset], len);
159
 
160
    /* Update the bitmap. */
161
    if (offset / (8 * 8) == (offset + len) / (8 * 8)) {
162
      LWIP_DEBUGF(IP_REASS_DEBUG,
163
       ("ip_reass: updating single byte in bitmap.\n"));
164
      /* If the two endpoints are in the same byte, we only update
165
         that byte. */
166
      ip_reassbitmap[offset / (8 * 8)] |=
167
    bitmap_bits[(offset / 8) & 7] &
168
    ~bitmap_bits[((offset + len) / 8) & 7];
169
    } else {
170
      /* If the two endpoints are in different bytes, we update the
171
         bytes in the endpoints and fill the stuff inbetween with
172
         0xff. */
173
      ip_reassbitmap[offset / (8 * 8)] |= bitmap_bits[(offset / 8) & 7];
174
      LWIP_DEBUGF(IP_REASS_DEBUG,
175
       ("ip_reass: updating many bytes in bitmap (%d:%d).\n",
176
        1 + offset / (8 * 8), (offset + len) / (8 * 8)));
177
      for (i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) {
178
  ip_reassbitmap[i] = 0xff;
179
      }
180
      ip_reassbitmap[(offset + len) / (8 * 8)] |=
181
    ~bitmap_bits[((offset + len) / 8) & 7];
182
    }
183
 
184
    /* If this fragment has the More Fragments flag set to zero, we
185
       know that this is the last fragment, so we can calculate the
186
       size of the entire packet. We also set the
187
       IP_REASS_FLAG_LASTFRAG flag to indicate that we have received
188
       the final fragment. */
189
 
190
    if ((ntohs(IPH_OFFSET(fraghdr)) & IP_MF) == 0) {
191
      ip_reassflags |= IP_REASS_FLAG_LASTFRAG;
192
      ip_reasslen = offset + len;
193
      LWIP_DEBUGF(IP_REASS_DEBUG,
194
       ("ip_reass: last fragment seen, total len %d\n",
195
        ip_reasslen));
196
    }
197
 
198
    /* Finally, we check if we have a full packet in the buffer. We do
199
       this by checking if we have the last fragment and if all bits
200
       in the bitmap are set. */
201
    if (ip_reassflags & IP_REASS_FLAG_LASTFRAG) {
202
      /* Check all bytes up to and including all but the last byte in
203
         the bitmap. */
204
      for (i = 0; i < ip_reasslen / (8 * 8) - 1; ++i) {
205
  if (ip_reassbitmap[i] != 0xff) {
206
    LWIP_DEBUGF(IP_REASS_DEBUG,
207
     ("ip_reass: last fragment seen, bitmap %d/%d failed (%x)\n",
208
      i, ip_reasslen / (8 * 8) - 1, ip_reassbitmap[i]));
209
    goto nullreturn;
210
  }
211
      }
212
      /* Check the last byte in the bitmap. It should contain just the
213
         right amount of bits. */
214
      if (ip_reassbitmap[ip_reasslen / (8 * 8)] !=
215
    (u8_t) ~ bitmap_bits[ip_reasslen / 8 & 7]) {
216
  LWIP_DEBUGF(IP_REASS_DEBUG,
217
         ("ip_reass: last fragment seen, bitmap %d didn't contain %x (%x)\n",
218
    ip_reasslen / (8 * 8), ~bitmap_bits[ip_reasslen / 8 & 7],
219
    ip_reassbitmap[ip_reasslen / (8 * 8)]));
220
  goto nullreturn;
221
      }
222
 
223
      /* Pretend to be a "normal" (i.e., not fragmented) IP packet
224
         from now on. */
225
      ip_reasslen += IP_HLEN;
226
 
227
      IPH_LEN_SET(iphdr, htons(ip_reasslen));
228
      IPH_OFFSET_SET(iphdr, 0);
229
      IPH_CHKSUM_SET(iphdr, 0);
230
      IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
231
 
232
      /* If we have come this far, we have a full packet in the
233
         buffer, so we allocate a pbuf and copy the packet into it. We
234
         also reset the timer. */
235
      sys_untimeout(ip_reass_timer, NULL);
236
      ip_reasstmr = 0;
237
      pbuf_free(p);
238
      p = pbuf_alloc(PBUF_LINK, ip_reasslen, PBUF_POOL);
239
      if (p != NULL) {
240
  i = 0;
241
  for (q = p; q != NULL; q = q->next) {
242
    /* Copy enough bytes to fill this pbuf in the chain. The
243
       available data in the pbuf is given by the q->len
244
       variable. */
245
    LWIP_DEBUGF(IP_REASS_DEBUG,
246
     ("ip_reass: memcpy from %p (%d) to %p, %d bytes\n",
247
      (void *)&ip_reassbuf[i], i, q->payload,
248
      q->len > ip_reasslen - i ? ip_reasslen - i : q->len));
249
    memcpy(q->payload, &ip_reassbuf[i],
250
    q->len > ip_reasslen - i ? ip_reasslen - i : q->len);
251
    i += q->len;
252
  }
253
  IPFRAG_STATS_INC(ip_frag.fw);
254
      } else {
255
  IPFRAG_STATS_INC(ip_frag.memerr);
256
      }
257
      LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass: p %p\n", (void*)p));
258
      return p;
259
    }
260
  }
261
 
262
nullreturn:
263
  IPFRAG_STATS_INC(ip_frag.drop);
264
  pbuf_free(p);
265
  return NULL;
266
}
267
 
268
#define MAX_MTU 1500
269
static u8_t buf[MEM_ALIGN_SIZE(MAX_MTU)];
270
 
271
/**
272
 * Fragment an IP packet if too large
273
 *
274
 * Chop the packet in mtu sized chunks and send them in order
275
 * by using a fixed size static memory buffer (PBUF_ROM)
276
 */
277
err_t
278
ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest)
279
{
280
  struct pbuf *rambuf;
281
  struct pbuf *header;
282
  struct ip_hdr *iphdr;
283
  u16_t nfb = 0;
284
  u16_t left, cop;
285
  u16_t mtu = netif->mtu;
286
  u16_t ofo, omf;
287
  u16_t last;
288
  u16_t poff = IP_HLEN;
289
  u16_t tmp;
290
 
291
  /* Get a RAM based MTU sized pbuf */
292
  rambuf = pbuf_alloc(PBUF_LINK, 0, PBUF_REF);
293
  rambuf->tot_len = rambuf->len = mtu;
294
  rambuf->payload = MEM_ALIGN((void *)buf);
295
 
296
  /* Copy the IP header in it */
297
  iphdr = rambuf->payload;
298
  memcpy(iphdr, p->payload, IP_HLEN);
299
 
300
  /* Save original offset */
301
  tmp = ntohs(IPH_OFFSET(iphdr));
302
  ofo = tmp & IP_OFFMASK;
303
  omf = tmp & IP_MF;
304
 
305
  left = p->tot_len - IP_HLEN;
306
 
307
  while (left) {
308
    last = (left <= mtu - IP_HLEN);
309
 
310
    /* Set new offset and MF flag */
311
    ofo += nfb;
312
    tmp = omf | (IP_OFFMASK & (ofo));
313
    if (!last)
314
      tmp = tmp | IP_MF;
315
    IPH_OFFSET_SET(iphdr, htons(tmp));
316
 
317
    /* Fill this fragment */
318
    nfb = (mtu - IP_HLEN) / 8;
319
    cop = last ? left : nfb * 8;
320
 
321
    p = copy_from_pbuf(p, &poff, (u8_t *) iphdr + IP_HLEN, cop);
322
 
323
    /* Correct header */
324
    IPH_LEN_SET(iphdr, htons(cop + IP_HLEN));
325
    IPH_CHKSUM_SET(iphdr, 0);
326
    IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
327
 
328
    if (last)
329
      pbuf_realloc(rambuf, left + IP_HLEN);
330
    /* This part is ugly: we alloc a RAM based pbuf for
331
     * the link level header for each chunk and then
332
     * free it.A PBUF_ROM style pbuf for which pbuf_header
333
     * worked would make things simpler.
334
     */
335
    header = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
336
    pbuf_chain(header, rambuf);
337
    netif->output(netif, header, dest);
338
    IPFRAG_STATS_INC(ip_frag.xmit);
339
    pbuf_free(header);
340
 
341
    left -= cop;
342
  }
343
  pbuf_free(rambuf);
344
  return ERR_OK;
345
}

powered by: WebSVN 2.1.0

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