Newer
Older
/****************************************************************************
* uip.c
* The uIP TCP/IP stack code.
* author Adam Dunkels <adam@dunkels.com>
*
* 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.
*
* Copyright (c) 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:
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
* 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 uip_interrupt()
* function into many smaller functions, this would increase the code
* size because of the overhead of parameter passing and the fact that
* the optimier would not be as efficient.
*
* The principle is that we have a small buffer, called the uip_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 uip_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 whishes to send data to the peer, it should put
* its data into the uip_buf. The uip_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>
#include <sys/types.h>
#include <debug.h>
#include <net/uip/uipopt.h>
#include <net/uip/uip.h>
#include <net/uip/uip-arch.h>
#include <string.h>
#if UIP_LOGGING == 1
#include <stdio.h>
extern void uip_log(char *msg);
# define UIP_LOG(m) uip_log(m)
#else
# define UIP_LOG(m)
#endif /* UIP_LOGGING == 1 */
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
/****************************************************************************
* Definitions
****************************************************************************/
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
#define TCP_CTL 0x3f
#define TCP_OPT_END 0 /* End of TCP options list */
#define TCP_OPT_NOOP 1 /* "No-operation" TCP option */
#define TCP_OPT_MSS 2 /* Maximum segment size TCP option */
#define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO 8
#define ICMP6_ECHO_REPLY 129
#define ICMP6_ECHO 128
#define ICMP6_NEIGHBOR_SOLICITATION 135
#define ICMP6_NEIGHBOR_ADVERTISEMENT 136
#define ICMP6_FLAG_S (1 << 6)
#define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1
#define ICMP6_OPTION_TARGET_LINK_ADDRESS 2
/* Macros. */
#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0])
#define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN])
#define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])
/****************************************************************************
* Public Variables
****************************************************************************/
/* The IP address of this host. If it is defined to be fixed (by
setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set
here. Otherwise, the address */
#if UIP_FIXEDADDR > 0
const uip_ipaddr_t uip_hostaddr =
{HTONS((UIP_IPADDR0 << 8) | UIP_IPADDR1),
HTONS((UIP_IPADDR2 << 8) | UIP_IPADDR3)};
const uip_ipaddr_t uip_draddr =
{HTONS((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1),
HTONS((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3)};
const uip_ipaddr_t uip_netmask =
{HTONS((UIP_NETMASK0 << 8) | UIP_NETMASK1),
HTONS((UIP_NETMASK2 << 8) | UIP_NETMASK3)};
#else
uip_ipaddr_t uip_hostaddr;
uip_ipaddr_t uip_draddr;
uip_ipaddr_t uip_netmask;
uint8 uip_buf[UIP_BUFSIZE + 2]; /* The packet buffer that contains incoming packets. */
#endif /* CONFIG_NET_EXTERNAL_BUFFER */
void *uip_appdata; /* The uip_appdata pointer points to application data. */
void *uip_sappdata; /* The uip_appdata pointer points to the application
* data which is to be sent. */
#if UIP_URGDATA > 0
void *uip_urgdata; /* The uip_urgdata pointer points to urgent data
* (out-of-band data), if present. */
uint16 uip_urglen, uip_surglen;
#endif /* UIP_URGDATA > 0 */
uint16 uip_len, uip_slen; /* The uip_len is either 8 or 16 bits, depending
* on the maximum packet size. */
uint8 uip_flags; /* The uip_flags variable is used for communication
* between the TCP/IP stack and the application
* program. */
struct uip_conn *uip_conn; /* uip_conn always points to the current connection. */
uint16 uip_listenports[UIP_LISTENPORTS];
/* The uip_listenports list all currently listening ports. */
uint8 uip_acc32[4];
#if UIP_STATISTICS == 1
struct uip_stats uip_stat;
# define UIP_STAT(s) s
#else
# define UIP_STAT(s)
#endif /* UIP_STATISTICS == 1 */
const uip_ipaddr_t all_ones_addr =
{0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff};
const uip_ipaddr_t all_zeroes_addr =
{0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
/****************************************************************************
* Private Variables
****************************************************************************/
#if UIP_FIXEDETHADDR
const struct uip_eth_addr uip_ethaddr =
{{ UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5 }};
#else
struct uip_eth_addr uip_ethaddr = {{ 0,0,0,0,0,0 }};
#endif
static uint16 ipid; /* Ths ipid variable is an increasing number that is
* used for the IP ID field. */
/* Temporary variables. */
static uint8 c;
static uint8 opt;
static uint16 tmp16;
/****************************************************************************
* Private Functions
****************************************************************************/
#if !UIP_ARCH_CHKSUM
static uint16 chksum(uint16 sum, const uint8 *data, uint16 len)
{
uint16 t;
const uint8 *dataptr;
const uint8 *last_byte;
dataptr = data;
last_byte = data + len - 1;
while(dataptr < last_byte)
{
/* At least two more bytes */
t = (dataptr[0] << 8) + dataptr[1];
sum += t;
if (sum < t)
{
sum++; /* carry */
}
dataptr += 2;
if (dataptr == last_byte)
{
t = (dataptr[0] << 8) + 0;
sum += t;
if (sum < t)
{
sum++; /* carry */
}
return sum;
}
static uint16 upper_layer_chksum(uint8 proto)
{
uint16 upper_layer_len;
uint16 sum;
upper_layer_len = (((uint16)(BUF->len[0]) << 8) + BUF->len[1]);
upper_layer_len = (((uint16)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN;
/* First sum pseudoheader. */
/* IP protocol and length fields. This addition cannot carry. */
sum = upper_layer_len + proto;
/* Sum IP source and destination addresses. */
sum = chksum(sum, (uint8 *)&BUF->srcipaddr[0], 2 * sizeof(uip_ipaddr_t));
/* Sum TCP header and data. */
sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], upper_layer_len);
return (sum == 0) ? 0xffff : htons(sum);
}
static uint16 uip_icmp6chksum(void)
{
return upper_layer_chksum(UIP_PROTO_ICMP6);
}
#endif /* UIP_ARCH_CHKSUM */
/****************************************************************************
* Public Functions
****************************************************************************/
/* This function may be used at boot time to set the initial ip_id.*/
void uip_setipid(uint16 id)
{
ipid = id;
}
/* Calculate the Internet checksum over a buffer. */
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
#if !UIP_ARCH_ADD32
void uip_add32(uint8 *op32, uint16 op16)
{
uip_acc32[3] = op32[3] + (op16 & 0xff);
uip_acc32[2] = op32[2] + (op16 >> 8);
uip_acc32[1] = op32[1];
uip_acc32[0] = op32[0];
if (uip_acc32[2] < (op16 >> 8))
{
++uip_acc32[1];
if (uip_acc32[1] == 0)
{
++uip_acc32[0];
}
}
if (uip_acc32[3] < (op16 & 0xff))
{
++uip_acc32[2];
if (uip_acc32[2] == 0)
{
++uip_acc32[1];
if (uip_acc32[1] == 0)
{
++uip_acc32[0];
}
}
}
}
#endif /* UIP_ARCH_ADD32 */
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
#if !UIP_ARCH_CHKSUM
uint16 uip_chksum(uint16 *data, uint16 len)
{
return htons(chksum(0, (uint8 *)data, len));
}
/* Calculate the IP header checksum of the packet header in uip_buf. */
#ifndef UIP_ARCH_IPCHKSUM
uint16 uip_ipchksum(void)
{
uint16 sum;
sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN);
dbg("uip_ipchksum: sum 0x%04x\n", sum);
return (sum == 0) ? 0xffff : htons(sum);
}
#endif
/* Calculate the TCP checksum of the packet in uip_buf and uip_appdata. */
uint16 uip_tcpchksum(void)
{
return upper_layer_chksum(UIP_PROTO_TCP);
}
/* Calculate the UDP checksum of the packet in uip_buf and uip_appdata. */
uint16 uip_udpchksum(void)
{
return upper_layer_chksum(UIP_PROTO_UDP);
}
#endif /* UIP_UDP_CHECKSUMS */
#endif /* UIP_ARCH_CHKSUM */
void uip_init(void)
{
for (c = 0; c < UIP_LISTENPORTS; ++c)
{
uip_listenports[c] = 0;
}
/* Initialize the TCP/IP connection structures */
uip_tcpinit();
/* Initialize the UDP connection structures */
/* IPv4 initialization. */
#if UIP_FIXEDADDR == 0
/* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/
#endif /* UIP_FIXEDADDR */
}
void uip_unlisten(uint16 port)
{
for (c = 0; c < UIP_LISTENPORTS; ++c)
{
if (uip_listenports[c] == port)
{
uip_listenports[c] = 0;
return;
}
for (c = 0; c < UIP_LISTENPORTS; ++c)
{
if (uip_listenports[c] == 0)
{
uip_listenports[c] = port;
return;
}
/* IP fragment reassembly: not well-tested. */
#if UIP_REASSEMBLY && !defined(CONFIG_NET_IPv6)
#define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN)
static uint8 uip_reassbuf[UIP_REASS_BUFSIZE];
static uint8 uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)];
static const uint8 bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
static uint16 uip_reasslen;
static uint8 uip_reassflags;
#define UIP_REASS_FLAG_LASTFRAG 0x01
static uint8 uip_reasstmr;
#define IP_MF 0x20
static uint8 uip_reass(void)
{
uint16 offset, len;
uint16 i;
/* If ip_reasstmr 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 (uip_reasstmr == 0)
{
memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN);
uip_reasstmr = UIP_REASS_MAXAGE;
uip_reassflags = 0;
/* Clear the bitmap. */
memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap));
/* Check if the incoming fragment matches the one currently present
* in the reasembly buffer. If so, we proceed with copying the
* fragment into the buffer.
*/
if (BUF->srcipaddr[0] == FBUF->srcipaddr[0] && BUF->srcipaddr[1] == FBUF->srcipaddr[1] &&
BUF->destipaddr[0] == FBUF->destipaddr[0] && BUF->destipaddr[1] == FBUF->destipaddr[1] &&
BUF->ipid[0] == FBUF->ipid[0] && BUF->ipid[1] == FBUF->ipid[1])
{
len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4;
offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8;
/* If the offset or the offset + fragment length overflows the
* reassembly buffer, we discard the entire packet.
*/
if (offset > UIP_REASS_BUFSIZE || offset + len > UIP_REASS_BUFSIZE)
{
uip_reasstmr = 0;
goto nullreturn;
}
/* Copy the fragment into the reassembly buffer, at the right offset. */
memcpy(&uip_reassbuf[UIP_IPH_LEN + offset], (char *)BUF + (int)((BUF->vhl & 0x0f) * 4), len);
if (offset / (8 * 8) == (offset + len) / (8 * 8))
{
/* If the two endpoints are in the same byte, we only update that byte. */
uip_reassbitmap[offset / (8 * 8)] |=
bitmap_bits[(offset / 8 ) & 7] & ~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.
*/
uip_reassbitmap[offset / (8 * 8)] |= bitmap_bits[(offset / 8 ) & 7];
for (i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i)
{
uip_reassbitmap[i] = 0xff;
}
uip_reassbitmap[(offset + len) / (8 * 8)] |= ~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 ((BUF->ipoffset[0] & IP_MF) == 0)
{
uip_reassflags |= UIP_REASS_FLAG_LASTFRAG;
uip_reasslen = offset + len;
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
575
576
577
578
579
/* 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 (uip_reassflags & UIP_REASS_FLAG_LASTFRAG)
{
/* Check all bytes up to and including all but the last byte in
* the bitmap.
*/
for (i = 0; i < uip_reasslen / (8 * 8) - 1; ++i)
{
if (uip_reassbitmap[i] != 0xff)
{
goto nullreturn;
}
}
/* Check the last byte in the bitmap. It should contain just the
* right amount of bits.
*/
if (uip_reassbitmap[uip_reasslen / (8 * 8)] != (uint8)~bitmap_bits[uip_reasslen / 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.
*/
uip_reasstmr = 0;
memcpy(BUF, FBUF, uip_reasslen);
/* Pretend to be a "normal" (i.e., not fragmented) IP packet from
* now on.
*/
BUF->ipoffset[0] = BUF->ipoffset[1] = 0;
BUF->len[0] = uip_reasslen >> 8;
BUF->len[1] = uip_reasslen & 0xff;
BUF->ipchksum = 0;
BUF->ipchksum = ~(uip_ipchksum());
return uip_reasslen;
}
return 0;
}
#endif /* UIP_REASSEMBLY */
static void uip_add_rcv_nxt(uint16 n)
{
uip_add32(uip_conn->rcv_nxt, n);
uip_conn->rcv_nxt[0] = uip_acc32[0];
uip_conn->rcv_nxt[1] = uip_acc32[1];
uip_conn->rcv_nxt[2] = uip_acc32[2];
uip_conn->rcv_nxt[3] = uip_acc32[3];
}
{
/* Some sanity checking */
if (uip_udp_conn && uip_udp_conn->callback)
{
/* Perform the callback */
uip_udp_conn->callback(uip_udp_conn->private);
}
}
static void uip_tcp_callback(void)
{
/* Some sanity checking */
if (uip_conn && uip_conn->callback)
{
/* Perform the callback */
uip_conn->callback(uip_conn->private);
}
}
void uip_interrupt(uint8 flag)
{
register struct uip_conn *uip_connr = uip_conn;
if (flag == UIP_UDP_SEND_CONN)
{
goto udp_send;
}
uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
/* Check if we were invoked because of a poll request for a
* particular connection.
*/
if (flag == UIP_POLL_REQUEST)
{
if ((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&
!uip_outstanding(uip_connr))
{
uip_flags = UIP_POLL;
goto appsend;
}
goto drop;
}
/* Check if we were invoked because of the perodic timer fireing. */
else if (flag == UIP_TIMER)
{
#if UIP_REASSEMBLY
if (uip_reasstmr != 0)
{
--uip_reasstmr;
}
#endif /* UIP_REASSEMBLY */
/* Increase the TCP sequence number */
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
uip_slen = 0;
/* Check if the connection is in a state in which we simply wait
* for the connection to time out. If so, we increase the
* connection's timer and remove the connection if it times
* out.
*/
if (uip_connr->tcpstateflags == UIP_TIME_WAIT ||
uip_connr->tcpstateflags == UIP_FIN_WAIT_2)
{
++(uip_connr->timer);
if (uip_connr->timer == UIP_TIME_WAIT_TIMEOUT)
{
uip_connr->tcpstateflags = UIP_CLOSED;
}
}
else if (uip_connr->tcpstateflags != UIP_CLOSED)
{
/* If the connection has outstanding data, we increase the
* connection's timer and see if it has reached the RTO value
* in which case we retransmit.
*/
if (uip_outstanding(uip_connr))
{
if (uip_connr->timer-- == 0)
{
if (uip_connr->nrtx == UIP_MAXRTX ||
((uip_connr->tcpstateflags == UIP_SYN_SENT ||
uip_connr->tcpstateflags == UIP_SYN_RCVD) &&
uip_connr->nrtx == UIP_MAXSYNRTX))
{
uip_connr->tcpstateflags = UIP_CLOSED;
* UIP_TIMEDOUT to inform the application that the
* connection has timed out.
*/
uip_flags = UIP_TIMEDOUT;
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* We also send a reset packet to the remote host. */
BUF->flags = TCP_RST | TCP_ACK;
goto tcp_send_nodata;
}
/* Exponential backoff. */
uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4 ? 4: uip_connr->nrtx);
++(uip_connr->nrtx);
/* Ok, so we need to retransmit. We do this differently
* depending on which state we are in. In ESTABLISHED, we
* call upon the application so that it may prepare the
* data for the retransmit. In SYN_RCVD, we resend the
* SYNACK that we sent earlier and in LAST_ACK we have to
* retransmit our FINACK.
*/
UIP_STAT(++uip_stat.tcp.rexmit);
switch(uip_connr->tcpstateflags & UIP_TS_MASK)
{
case UIP_SYN_RCVD:
/* In the SYN_RCVD state, we should retransmit our
* SYNACK.
*/
goto tcp_send_synack;
case UIP_SYN_SENT:
/* In the SYN_SENT state, we retransmit out SYN. */
BUF->flags = 0;
goto tcp_send_syn;
case UIP_ESTABLISHED:
/* In the ESTABLISHED state, we call upon the application
* to do the actual retransmit after which we jump into
* the code for sending out the packet (the apprexmit
* label).
*/
uip_flags = UIP_REXMIT;
goto apprexmit;
case UIP_FIN_WAIT_1:
case UIP_CLOSING:
case UIP_LAST_ACK:
/* In all these states we should retransmit a FINACK. */
goto tcp_send_finack;
}
}
}
else if ((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED)
{
/* If there was no need for a retransmission, we poll the
* application for new data.
*/
uip_flags = UIP_POLL;
if (flag == UIP_UDP_TIMER)
{
if (uip_udp_conn->lport != 0)
{
uip_conn = NULL;
uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
uip_len = uip_slen = 0;
uip_flags = UIP_POLL;
goto udp_send;
}
else
{
goto drop;
}
}
#endif
/* This is where the input processing starts. */
UIP_STAT(++uip_stat.ip.recv);
/* Start of IP input header processing code. */
/* Check validity of the IP header. */
if ((BUF->vtc & 0xf0) != 0x60)
{
/* IP version and header length. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.vhlerr);
UIP_LOG("ipv6: invalid version.");
goto drop;
}
/* Check validity of the IP header. */
if (BUF->vhl != 0x45)
{
/* IP version and header length. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.vhlerr);
UIP_LOG("ip: invalid version or header length.");
goto drop;
}
/* Check the size of the packet. If the size reported to us in
uip_len is smaller the size reported in the IP header, we assume
that the packet has been corrupted in transit. If the size of
uip_len is larger than the size reported in the IP packet header,
the packet has been padded and we set uip_len to the correct
value.. */
if ((BUF->len[0] << 8) + BUF->len[1] <= uip_len)
{
uip_len = (BUF->len[0] << 8) + BUF->len[1];
uip_len += 40; /* The length reported in the IPv6 header is the
length of the payload that follows the
header. However, uIP uses the uip_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). */
}
else
{
UIP_LOG("ip: packet shorter than reported in IP header.");
goto drop;
}
/* Check the fragment flag. */
if ((BUF->ipoffset[0] & 0x3f) != 0 ||
BUF->ipoffset[1] != 0)
{
#if UIP_REASSEMBLY
uip_len = uip_reass();
if (uip_len == 0)
{
goto drop;
}
#else /* UIP_REASSEMBLY */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.fragerr);
UIP_LOG("ip: fragment dropped.");
goto drop;
#endif /* UIP_REASSEMBLY */
}
if (uip_ipaddr_cmp(uip_hostaddr, all_zeroes_addr))
{
/* 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 UIP_PINGADDRCONF && !CONFIG_NET_IPv6
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
if (BUF->proto == UIP_PROTO_ICMP)
{
UIP_LOG("ip: possible ping config packet received.");
goto icmp_input;
}
else
{
UIP_LOG("ip: packet dropped since no address assigned.");
goto drop;
}
#endif /* UIP_PINGADDRCONF */
}
else
{
/* If IP broadcast support is configured, we check for a broadcast
UDP packet, which may be destined to us. */
#if UIP_BROADCAST
dbg("UDP IP checksum 0x%04x\n", uip_ipchksum());
if (BUF->proto == UIP_PROTO_UDP &&
uip_ipaddr_cmp(BUF->destipaddr, all_ones_addr)
/*&& uip_ipchksum() == 0xffff*/)
{
goto udp_input;
}
#endif /* UIP_BROADCAST */
/* Check if the packet is destined for our IP address. */
if (!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr))
{
UIP_STAT(++uip_stat.ip.drop);
goto drop;
}
/* 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 (!uip_ipaddr_cmp(BUF->destipaddr, uip_hostaddr) &&
BUF->destipaddr[0] != HTONS(0xff02))
{
UIP_STAT(++uip_stat.ip.drop);
goto drop;
}
if (uip_ipchksum() != 0xffff)
{
/* Compute and check the IP header checksum. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.chkerr);
UIP_LOG("ip: bad checksum.");
goto drop;
}
if (BUF->proto == UIP_PROTO_TCP)
{
/* Check for TCP packet. If so, proceed with TCP input processing. */
goto tcp_input;
}
if (BUF->proto == UIP_PROTO_UDP)
{
goto udp_input;
}
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ICMPv4 processing code follows. */
if (BUF->proto != UIP_PROTO_ICMP)
{
/* We only allow ICMP packets from here. */
UIP_STAT(++uip_stat.ip.drop);
UIP_STAT(++uip_stat.ip.protoerr);
UIP_LOG("ip: neither tcp nor icmp.");
goto drop;
}
#if UIP_PINGADDRCONF
icmp_input:
#endif /* UIP_PINGADDRCONF */
UIP_STAT(++uip_stat.icmp.recv);
/* ICMP echo (i.e., ping) processing. This is simple, we only change
the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP
checksum before we return the packet. */
if (ICMPBUF->type != ICMP_ECHO)
{
UIP_STAT(++uip_stat.icmp.drop);
UIP_STAT(++uip_stat.icmp.typeerr);
UIP_LOG("icmp: not icmp echo.");
goto drop;
}
/* If we are configured to use ping IP address assignment, we use
the destination IP address of this ping packet and assign it to
ourself. */
#if UIP_PINGADDRCONF
if ((uip_hostaddr[0] | uip_hostaddr[1]) == 0)
{
uip_hostaddr[0] = BUF->destipaddr[0];
uip_hostaddr[1] = BUF->destipaddr[1];
}
#endif /* UIP_PINGADDRCONF */
ICMPBUF->type = ICMP_ECHO_REPLY;
if (ICMPBUF->icmpchksum >= HTONS(0xffff - (ICMP_ECHO << 8)))
{
ICMPBUF->icmpchksum += HTONS(ICMP_ECHO << 8) + 1;
}
else
{