summaryrefslogtreecommitdiff
path: root/nuttx/net/devif/devif_input.c
blob: 7a5142add68fba663115220c36a55776320d8759 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/****************************************************************************
 * net/devif/devif_input.c
 * The uIP TCP/IP stack code.
 *
 *   Copyright (C) 2007-2009, 2013-2014 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Adapted for NuttX from logic in uIP which also has a BSD-like license:
 *
 * uIP is an implementation of the TCP/IP protocol stack intended for
 * small 8-bit and 16-bit microcontrollers.
 *
 * uIP provides the necessary protocols for Internet communication,
 * with a very small code footprint and RAM requirements - the uIP
 * code size is on the order of a few kilobytes and RAM usage is on
 * the order of a few hundred bytes.
 *
 *   Original author Adam Dunkels <adam@dunkels.com>
 *   Copyright () 2001-2003, Adam Dunkels.
 *   All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * uIP is a small implementation of the IP, UDP and TCP protocols (as
 * well as some basic ICMP stuff). The implementation couples the IP,
 * UDP, TCP and the application layers very tightly. To keep the size
 * of the compiled code down, this code frequently uses the goto
 * statement. While it would be possible to break the devif_input()
 * function into many smaller functions, this would increase the code
 * size because of the overhead of parameter passing and the fact that
 * the optimizer would not be as efficient.
 *
 * The principle is that we have a small buffer, called the d_buf,
 * in which the device driver puts an incoming packet. The TCP/IP
 * stack parses the headers in the packet, and calls the
 * application. If the remote host has sent data to the application,
 * this data is present in the d_buf and the application read the
 * data from there. It is up to the application to put this data into
 * a byte stream if needed. The application will not be fed with data
 * that is out of sequence.
 *
 * If the application wishes to send data to the peer, it should put
 * its data into the d_buf. The d_appdata pointer points to the
 * first available byte. The TCP/IP stack will calculate the
 * checksums, and fill in the necessary header fields and finally send
 * the packet back to the peer.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>
#ifdef CONFIG_NET

#include <sys/ioctl.h>
#include <stdint.h>
#include <debug.h>
#include <string.h>

#include <nuttx/net/netconfig.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/netstats.h>
#include <nuttx/net/ip.h>

#ifdef CONFIG_NET_IPv6
#  include "net_neighbor.h"
#endif /* CONFIG_NET_IPv6 */

#include "devif/devif.h"
#include "tcp/tcp.h"
#include "udp/udp.h"
#include "pkt/pkt.h"
#include "icmp/icmp.h"
#include "igmp/igmp.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* Macros */

#define BUF                  ((FAR struct net_iphdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)])
#define FBUF                 ((FAR struct net_iphdr_s *)&g_reassembly_buffer[0])

/* IP fragment re-assembly */

#define IP_MF                0x20  /* See IP_FLAG_MOREFRAGS */
#define TCP_REASS_BUFSIZE    (NET_DEV_MTU(dev) - NET_LL_HDRLEN(dev))
#define TCP_REASS_LASTFRAG   0x01

/****************************************************************************
 * Public Variables
 ****************************************************************************/

/****************************************************************************
 * Private Variables
 ****************************************************************************/

#if defined(CONFIG_NET_TCP_REASSEMBLY) && !defined(CONFIG_NET_IPv6)

static uint8_t g_reassembly_buffer[TCP_REASS_BUFSIZE];
static uint8_t g_reassembly_bitmap[TCP_REASS_BUFSIZE / (8 * 8)];

static const uint8_t g_bitmap_bits[8] =
  {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};

static uint16_t g_reassembly_len;
static uint8_t g_reassembly_flags;

#endif /* CONFIG_NET_TCP_REASSEMBLY */

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Function: devif_reassembly
 *
 * Description:
 *   IP fragment reassembly: not well-tested.
 *
 * Assumptions:
 *
 ****************************************************************************/

#if defined(CONFIG_NET_TCP_REASSEMBLY) && !defined(CONFIG_NET_IPv6)
static uint8_t devif_reassembly(void)
{
  FAR struct net_iphdr_s *pbuf  = BUF;
  FAR struct net_iphdr_s *pfbuf = FBUF;
  uint16_t offset;
  uint16_t len;
  uint16_t i;

  /* If g_reassembly_timer is zero, no packet is present in the buffer, so
   * we write the IP header of the fragment into the reassembly buffer.  The
   * timer is updated with the maximum age.
   */

  if (!g_reassembly_timer)
    {
      memcpy(g_reassembly_buffer, &pbuf->vhl, IP_HDRLEN);
      g_reassembly_timer = CONFIG_NET_TCP_REASS_MAXAGE;
      g_reassembly_flags = 0;

      /* Clear the bitmap. */

      memset(g_reassembly_bitmap, 0, sizeof(g_reassembly_bitmap));
    }

  /* Check if the incoming fragment matches the one currently present
   * in the reassembly buffer. If so, we proceed with copying the
   * fragment into the buffer.
   */

  if (net_ipaddr_hdrcmp(pbuf->srcipaddr, pfbuf->srcipaddr) &&
      net_ipaddr_hdrcmp(pbuf->destipaddr, pfbuf->destipaddr) &&
      pbuf->g_ipid[0] == pfbuf->g_ipid[0] && pbuf->g_ipid[1] == pfbuf->g_ipid[1])
    {
      len = (pbuf->len[0] << 8) + pbuf->len[1] - (pbuf->vhl & 0x0f) * 4;
      offset = (((pbuf->ipoffset[0] & 0x3f) << 8) + pbuf->ipoffset[1]) * 8;

      /* If the offset or the offset + fragment length overflows the
       * reassembly buffer, we discard the entire packet.
       */

      if (offset > TCP_REASS_BUFSIZE || offset + len > TCP_REASS_BUFSIZE)
        {
          g_reassembly_timer = 0;
          goto nullreturn;
        }

      /* Copy the fragment into the reassembly buffer, at the right offset. */

      memcpy(&g_reassembly_buffer[IP_HDRLEN + offset], (char *)pbuf + (int)((pbuf->vhl & 0x0f) * 4), len);

    /* Update the bitmap. */

    if (offset / (8 * 8) == (offset + len) / (8 * 8))
      {
        /* If the two endpoints are in the same byte, we only update that byte. */

        g_reassembly_bitmap[offset / (8 * 8)] |=
          g_bitmap_bits[(offset / 8 ) & 7] & ~g_bitmap_bits[((offset + len) / 8 ) & 7];

      }
    else
      {
        /* If the two endpoints are in different bytes, we update the bytes
         * in the endpoints and fill the stuff inbetween with 0xff.
         */

        g_reassembly_bitmap[offset / (8 * 8)] |= g_bitmap_bits[(offset / 8 ) & 7];
        for (i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i)
          {
            g_reassembly_bitmap[i] = 0xff;
          }
        g_reassembly_bitmap[(offset + len) / (8 * 8)] |= ~g_bitmap_bits[((offset + len) / 8 ) & 7];
      }

    /* If this fragment has the More Fragments flag set to zero, we know that
     * this is the last fragment, so we can calculate the size of the entire
     * packet. We also set the IP_REASS_FLAG_LASTFRAG flag to indicate that
     * we have received the final fragment.
     */

    if ((pbuf->ipoffset[0] & IP_MF) == 0)
      {
        g_reassembly_flags |= TCP_REASS_LASTFRAG;
        g_reassembly_len = offset + len;
      }

    /* Finally, we check if we have a full packet in the buffer. We do this
     * by checking if we have the last fragment and if all bits in the bitmap
     * are set.
     */

    if (g_reassembly_flags & TCP_REASS_LASTFRAG)
      {
        /* Check all bytes up to and including all but the last byte in
         * the bitmap.
         */

        for (i = 0; i < g_reassembly_len / (8 * 8) - 1; ++i)
          {
            if (g_reassembly_bitmap[i] != 0xff)
              {
                goto nullreturn;
              }
          }

        /* Check the last byte in the bitmap. It should contain just the
         * right amount of bits.
         */

        if (g_reassembly_bitmap[g_reassembly_len / (8 * 8)] != (uint8_t)~g_bitmap_bits[g_reassembly_len / 8 & 7])
          {
            goto nullreturn;
          }

        /* If we have come this far, we have a full packet in the buffer,
         * so we allocate a pbuf and copy the packet into it. We also reset
         * the timer.
         */

        g_reassembly_timer = 0;
        memcpy(pbuf, pfbuf, g_reassembly_len);

        /* Pretend to be a "normal" (i.e., not fragmented) IP packet from
         * now on.
         */

        pbuf->ipoffset[0] = pbuf->ipoffset[1] = 0;
        pbuf->len[0] = g_reassembly_len >> 8;
        pbuf->len[1] = g_reassembly_len & 0xff;
        pbuf->ipchksum = 0;
        pbuf->ipchksum = ~(ip_chksum(dev));

        return g_reassembly_len;
      }
  }

nullreturn:
  return 0;
}
#endif /* CONFIG_NET_TCP_REASSEMBLY */

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Function: devif_input
 *
 * Description:
 *
 * Returned Value:
 *   OK    The packet was processed (or dropped) and can be discarded.
 *   ERROR There is a matching connection, but could not dispatch the packet
 *         yet.  Currently useful for UDP when a packet arrives before a recv
 *         call is in place.
 *
 * Assumptions:
 *
 ****************************************************************************/

int devif_input(FAR struct net_driver_s *dev)
{
  FAR struct net_iphdr_s *pbuf = BUF;
  uint16_t iplen;

  /* This is where the input processing starts. */

#ifdef CONFIG_NET_STATISTICS
  g_netstats.ip.recv++;
#endif

  /* Start of IP input header processing code. */

#ifdef CONFIG_NET_IPv6
  /* Check validity of the IP header. */

  if ((pbuf->vtc & 0xf0) != 0x60)
    {
      /* IP version and header length. */

#ifdef CONFIG_NET_STATISTICS
      g_netstats.ip.drop++;
      g_netstats.ip.vhlerr++;
#endif
      nlldbg("Invalid IPv6 version: %d\n", pbuf->vtc >> 4);
      goto drop;
    }

#else /* CONFIG_NET_IPv6 */
  /* Check validity of the IP header. */

  if (pbuf->vhl != 0x45)
    {
      /* IP version and header length. */

#ifdef CONFIG_NET_STATISTICS
      g_netstats.ip.drop++;
      g_netstats.ip.vhlerr++;
#endif
      nlldbg("Invalid IP version or header length: %02x\n", pbuf->vhl);
      goto drop;
    }
#endif /* CONFIG_NET_IPv6 */

  /* Check the size of the packet. If the size reported to us in d_len is
   * smaller the size reported in the IP header, we assume that the packet
   * has been corrupted in transit. If the size of d_len is larger than the
   * size reported in the IP packet header, the packet has been padded and
   * we set d_len to the correct value.
   */

#ifdef CONFIG_NET_IPv6
  /* The length reported in the IPv6 header is the length of the payload
   * that follows the header. However, uIP uses the d_len variable for
   * holding the size of the entire packet, including the IP header. For
   * IPv4 this is not a problem as the length field in the IPv4 header
   * contains the length of the entire packet. But for IPv6 we need to add
   * the size of the IPv6 header (40 bytes).
   */

  iplen = (pbuf->len[0] << 8) + pbuf->len[1] + IP_HDRLEN;
#else
  iplen = (pbuf->len[0] << 8) + pbuf->len[1];
#endif /* CONFIG_NET_IPv6 */

  if (iplen <= dev->d_len)
    {
      dev->d_len = iplen;
    }
  else
    {
      nlldbg("IP packet shorter than length in IP header\n");
      goto drop;
    }

#ifndef CONFIG_NET_IPv6
  /* Check the fragment flag. */

  if ((pbuf->ipoffset[0] & 0x3f) != 0 || pbuf->ipoffset[1] != 0)
    {
#if defined(CONFIG_NET_TCP_REASSEMBLY)
      dev->d_len = devif_reassembly();
      if (dev->d_len == 0)
        {
          goto drop;
        }
#else /* CONFIG_NET_TCP_REASSEMBLY */
#ifdef CONFIG_NET_STATISTICS
      g_netstats.ip.drop++;
      g_netstats.ip.fragerr++;
#endif
      nlldbg("IP fragment dropped\n");
      goto drop;
#endif /* CONFIG_NET_TCP_REASSEMBLY */
    }
#endif /* CONFIG_NET_IPv6 */

   /* If IP broadcast support is configured, we check for a broadcast
    * UDP packet, which may be destined to us (even if there is no IP
    * address yet assigned to the device as is the case when we are
    * negotiating over DHCP for an address).
    */

#if defined(CONFIG_NET_BROADCAST) && defined(CONFIG_NET_UDP)
  if (pbuf->proto == IP_PROTO_UDP &&
#ifndef CONFIG_NET_IPv6
      net_ipaddr_cmp(net_ip4addr_conv32(pbuf->destipaddr), g_alloneaddr))
#else
      net_ipaddr_cmp(pbuf->destipaddr, g_alloneaddr))
#endif
    {
      return udp_input(dev);
    }

  /* In most other cases, the device must be assigned a non-zero IP
   * address.  Another exception is when CONFIG_NET_PINGADDRCONF is
   * enabled...
   */

  else
#endif
#ifdef CONFIG_NET_ICMP
  if (net_ipaddr_cmp(dev->d_ipaddr, g_allzeroaddr))
    {
      /* If we are configured to use ping IP address configuration and
       * hasn't been assigned an IP address yet, we accept all ICMP
       * packets.
       */

#if defined(CONFIG_NET_PINGADDRCONF) && !defined(CONFIG_NET_IPv6)
      if (pbuf->proto == IP_PROTO_ICMP)
        {
          nlldbg("Possible ping config packet received\n");
          icmp_input(dev);
          goto drop;
        }
      else
#endif
        {
          nlldbg("No IP address assigned\n");
          goto drop;
        }
    }

  /* Check if the packet is destined for out IP address */
  else
#endif
    {
      /* Check if the packet is destined for our IP address. */
#ifndef CONFIG_NET_IPv6
      if (!net_ipaddr_cmp(net_ip4addr_conv32(pbuf->destipaddr), dev->d_ipaddr))
        {
#ifdef CONFIG_NET_IGMP
          net_ipaddr_t destip = net_ip4addr_conv32(pbuf->destipaddr);
          if (igmp_grpfind(dev, &destip) == NULL)
#endif
            {
#ifdef CONFIG_NET_STATISTICS
              g_netstats.ip.drop++;
#endif
              goto drop;
            }
        }

#else /* CONFIG_NET_IPv6 */
      /* For IPv6, packet reception is a little trickier as we need to
       * make sure that we listen to certain multicast addresses (all
       * hosts multicast address, and the solicited-node multicast
       * address) as well. However, we will cheat here and accept all
       * multicast packets that are sent to the ff02::/16 addresses.
       */

      if (!net_ipaddr_cmp(pbuf->destipaddr, dev->d_ipaddr) &&
          pbuf->destipaddr[0] != 0xff02)
        {
#ifdef CONFIG_NET_STATISTICS
          g_netstats.ip.drop++;
#endif
          goto drop;
        }
#endif /* CONFIG_NET_IPv6 */
    }

#ifndef CONFIG_NET_IPv6
  if (ip_chksum(dev) != 0xffff)
    {
      /* Compute and check the IP header checksum. */

#ifdef CONFIG_NET_STATISTICS
      g_netstats.ip.drop++;
      g_netstats.ip.chkerr++;
#endif
      nlldbg("Bad IP checksum\n");
      goto drop;
    }
#endif /* CONFIG_NET_IPv6 */

  /* Everything looks good so far.  Now process the incoming packet
   * according to the protocol.
   */

  switch (pbuf->proto)
    {
#ifdef CONFIG_NET_TCP
      case IP_PROTO_TCP:   /* TCP input */
        tcp_input(dev);
        break;
#endif

#ifdef CONFIG_NET_UDP
      case IP_PROTO_UDP:   /* UDP input */
        udp_input(dev);
        break;
#endif

  /* Check for ICMP input */

#ifdef CONFIG_NET_ICMP
#ifndef CONFIG_NET_IPv6
      case IP_PROTO_ICMP:  /* ICMP input */
#else
      case IP_PROTO_ICMP6: /* ICMP6 input */
#endif
        icmp_input(dev);
        break;
#endif

  /* Check for ICMP input */

#ifdef CONFIG_NET_IGMP
#ifndef CONFIG_NET_IPv6
      case IP_PROTO_IGMP:  /* IGMP input */
        igmp_input(dev);
        break;
#endif
#endif

      default:              /* Unrecognized/unsupported protocol */
#ifdef CONFIG_NET_STATISTICS
        g_netstats.ip.drop++;
        g_netstats.ip.protoerr++;
#endif

        nlldbg("Unrecognized IP protocol\n");
        goto drop;
    }

  /* Return and let the caller do any pending transmission. */

  return OK;

  /* Drop the packet.  NOTE that OK is returned meaning that the
   * packet has been processed (although processed unsuccessfully).
   */

drop:
  dev->d_len = 0;
  return OK;
}
#endif /* CONFIG_NET */