Mercurial > templog
annotate network/uip.c @ 107:56d09a0969b5 avr-http
Import uIP and the PPP implementation from
https://code.google.com/p/avrusbmodem/
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 07 Sep 2012 23:53:53 +0800 |
parents | |
children |
rev | line source |
---|---|
107
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 #define DEBUG_PRINTF(...) /*printf(__VA_ARGS__)*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 /** |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * \addtogroup uip |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 * @{ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 /** |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
9 * \file |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 * The uIP TCP/IP stack code. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 * \author Adam Dunkels <[email protected]> |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 /* |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 * Copyright (c) 2001-2003, Adam Dunkels. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 * All rights reserved. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 * Redistribution and use in source and binary forms, with or without |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 * modification, are permitted provided that the following conditions |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 * are met: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 * 1. Redistributions of source code must retain the above copyright |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 * notice, this list of conditions and the following disclaimer. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 * 2. Redistributions in binary form must reproduce the above copyright |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 * notice, this list of conditions and the following disclaimer in the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
25 * documentation and/or other materials provided with the distribution. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 * 3. The name of the author may not be used to endorse or promote |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 * products derived from this software without specific prior |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
28 * written permission. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
31 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
36 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
38 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
41 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
42 * This file is part of the uIP TCP/IP stack. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
43 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
44 * $Id: uip.c,v 1.30 2010/10/19 18:29:04 adamdunkels Exp $ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
45 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 /* |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 * uIP is a small implementation of the IP, UDP and TCP protocols (as |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
50 * well as some basic ICMP stuff). The implementation couples the IP, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
51 * UDP, TCP and the application layers very tightly. To keep the size |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
52 * of the compiled code down, this code frequently uses the goto |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
53 * statement. While it would be possible to break the uip_process() |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
54 * function into many smaller functions, this would increase the code |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
55 * size because of the overhead of parameter passing and the fact that |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 * the optimier would not be as efficient. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
57 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
58 * The principle is that we have a small buffer, called the uip_buf, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
59 * in which the device driver puts an incoming packet. The TCP/IP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
60 * stack parses the headers in the packet, and calls the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
61 * application. If the remote host has sent data to the application, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
62 * this data is present in the uip_buf and the application read the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
63 * data from there. It is up to the application to put this data into |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
64 * a byte stream if needed. The application will not be fed with data |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
65 * that is out of sequence. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
66 * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
67 * If the application whishes to send data to the peer, it should put |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
68 * its data into the uip_buf. The uip_appdata pointer points to the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 * first available byte. The TCP/IP stack will calculate the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 * checksums, and fill in the necessary header fields and finally send |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
71 * the packet back to the peer. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
72 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
73 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 #include "uip.h" |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
75 #include "uipopt.h" |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
76 //#include "net/uip_arp.h" |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
77 //#include "net/uip_arch.h" |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
78 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
79 #if !UIP_CONF_IPV6 /* If UIP_CONF_IPV6 is defined, we compile the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
80 uip6.c file instead of this one. Therefore |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
81 this #ifndef removes the entire compilation |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
82 output of the uip.c file */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
83 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
84 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
85 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
86 #include "net/uip-neighbor.h" |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
87 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
88 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
89 #include <string.h> |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
90 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
91 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
92 /* Variable definitions. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
93 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
94 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
95 /* The IP address of this host. If it is defined to be fixed (by |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
96 setting UIP_FIXEDADDR to 1 in uipopt.h), the address is set |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
97 here. Otherwise, the address */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
98 #if UIP_FIXEDADDR > 0 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
99 const uip_ipaddr_t uip_hostaddr = |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
100 { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3 }; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
101 const uip_ipaddr_t uip_draddr = |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
102 { UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3 }; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
103 const uip_ipaddr_t uip_netmask = |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
104 { UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3 }; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
105 #else |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
106 uip_ipaddr_t uip_hostaddr, uip_draddr, uip_netmask; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
107 #endif /* UIP_FIXEDADDR */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
108 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
109 const uip_ipaddr_t uip_broadcast_addr = |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
110 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
111 { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
112 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
113 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
114 { { 0xff, 0xff, 0xff, 0xff } }; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
115 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
116 const uip_ipaddr_t uip_all_zeroes_addr = { { 0x0, /* rest is 0 */ } }; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
117 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
118 #if UIP_FIXEDETHADDR |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
119 const struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
120 UIP_ETHADDR1, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
121 UIP_ETHADDR2, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
122 UIP_ETHADDR3, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
123 UIP_ETHADDR4, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
124 UIP_ETHADDR5}}; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
125 #else |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
126 struct uip_eth_addr uip_ethaddr = {{0,0,0,0,0,0}}; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
127 #endif |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
128 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
129 /* The packet buffer that contains incoming packets. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
130 uip_buf_t uip_aligned_buf; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
131 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
132 void *uip_appdata; /* The uip_appdata pointer points to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
133 application data. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
134 void *uip_sappdata; /* The uip_appdata pointer points to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
135 the application data which is to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
136 be sent. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
137 #if UIP_URGDATA > 0 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
138 void *uip_urgdata; /* The uip_urgdata pointer points to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
139 urgent data (out-of-band data), if |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
140 present. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
141 u16_t uip_urglen, uip_surglen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
142 #endif /* UIP_URGDATA > 0 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
143 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
144 u16_t uip_len, uip_slen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
145 /* The uip_len is either 8 or 16 bits, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
146 depending on the maximum packet |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
147 size. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
148 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
149 u8_t uip_flags; /* The uip_flags variable is used for |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
150 communication between the TCP/IP stack |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
151 and the application program. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
152 struct uip_conn *uip_conn; /* uip_conn always points to the current |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
153 connection. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
154 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
155 struct uip_conn uip_conns[UIP_CONNS]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
156 /* The uip_conns array holds all TCP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
157 connections. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
158 u16_t uip_listenports[UIP_LISTENPORTS]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
159 /* The uip_listenports list all currently |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
160 listning ports. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
161 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
162 struct uip_udp_conn *uip_udp_conn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
163 struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
164 #endif /* UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
165 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
166 static u16_t ipid; /* Ths ipid variable is an increasing |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
167 number that is used for the IP ID |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
168 field. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
169 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
170 void uip_setipid(u16_t id) { ipid = id; } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
171 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
172 static u8_t iss[4]; /* The iss variable is used for the TCP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
173 initial sequence number. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
174 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
175 #if UIP_ACTIVE_OPEN || UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
176 static u16_t lastport; /* Keeps track of the last port used for |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
177 a new connection. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
178 #endif /* UIP_ACTIVE_OPEN || UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
179 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
180 /* Temporary variables. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
181 u8_t uip_acc32[4]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
182 static u8_t c, opt; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
183 static u16_t tmp16; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
184 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
185 /* Structures and definitions. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
186 #define TCP_FIN 0x01 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
187 #define TCP_SYN 0x02 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
188 #define TCP_RST 0x04 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
189 #define TCP_PSH 0x08 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
190 #define TCP_ACK 0x10 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
191 #define TCP_URG 0x20 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
192 #define TCP_CTL 0x3f |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
193 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
194 #define TCP_OPT_END 0 /* End of TCP options list */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
195 #define TCP_OPT_NOOP 1 /* "No-operation" TCP option */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
196 #define TCP_OPT_MSS 2 /* Maximum segment size TCP option */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
197 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
198 #define TCP_OPT_MSS_LEN 4 /* Length of TCP MSS option. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
199 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
200 #define ICMP_ECHO_REPLY 0 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
201 #define ICMP_ECHO 8 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
202 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
203 #define ICMP_DEST_UNREACHABLE 3 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
204 #define ICMP_PORT_UNREACHABLE 3 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
205 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
206 #define ICMP6_ECHO_REPLY 129 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
207 #define ICMP6_ECHO 128 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
208 #define ICMP6_NEIGHBOR_SOLICITATION 135 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
209 #define ICMP6_NEIGHBOR_ADVERTISEMENT 136 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
210 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
211 #define ICMP6_FLAG_S (1 << 6) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
212 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
213 #define ICMP6_OPTION_SOURCE_LINK_ADDRESS 1 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
214 #define ICMP6_OPTION_TARGET_LINK_ADDRESS 2 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
215 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
216 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
217 /* Macros. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
218 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
219 #define FBUF ((struct uip_tcpip_hdr *)&uip_reassbuf[0]) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
220 #define ICMPBUF ((struct uip_icmpip_hdr *)&uip_buf[UIP_LLH_LEN]) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
221 #define UDPBUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
222 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
223 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
224 #if UIP_STATISTICS == 1 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
225 struct uip_stats uip_stat; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
226 #define UIP_STAT(s) s |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
227 #else |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
228 #define UIP_STAT(s) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
229 #endif /* UIP_STATISTICS == 1 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
230 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
231 #if UIP_LOGGING == 1 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
232 #include <stdio.h> |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
233 void uip_log(char *msg); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
234 #define UIP_LOG(m) uip_log(m) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
235 #else |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
236 #define UIP_LOG(m) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
237 #endif /* UIP_LOGGING == 1 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
238 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
239 #if ! UIP_ARCH_ADD32 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
240 void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
241 uip_add32(u8_t *op32, u16_t op16) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
242 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
243 uip_acc32[3] = op32[3] + (op16 & 0xff); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
244 uip_acc32[2] = op32[2] + (op16 >> 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
245 uip_acc32[1] = op32[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
246 uip_acc32[0] = op32[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
247 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
248 if(uip_acc32[2] < (op16 >> 8)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
249 ++uip_acc32[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
250 if(uip_acc32[1] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
251 ++uip_acc32[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
252 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
253 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
254 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
255 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
256 if(uip_acc32[3] < (op16 & 0xff)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
257 ++uip_acc32[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
258 if(uip_acc32[2] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
259 ++uip_acc32[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
260 if(uip_acc32[1] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
261 ++uip_acc32[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
262 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
263 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
264 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
265 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
266 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
267 #endif /* UIP_ARCH_ADD32 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
268 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
269 #if ! UIP_ARCH_CHKSUM |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
270 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
271 static u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
272 chksum(u16_t sum, const u8_t *data, u16_t len) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
273 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
274 u16_t t; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
275 const u8_t *dataptr; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
276 const u8_t *last_byte; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
277 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
278 dataptr = data; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
279 last_byte = data + len - 1; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
280 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
281 while(dataptr < last_byte) { /* At least two more bytes */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
282 t = (dataptr[0] << 8) + dataptr[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
283 sum += t; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
284 if(sum < t) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
285 sum++; /* carry */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
286 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
287 dataptr += 2; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
288 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
289 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
290 if(dataptr == last_byte) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
291 t = (dataptr[0] << 8) + 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
292 sum += t; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
293 if(sum < t) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
294 sum++; /* carry */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
295 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
296 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
297 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
298 /* Return sum in host byte order. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
299 return sum; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
300 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
301 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
302 u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
303 uip_chksum(u16_t *data, u16_t len) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
304 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
305 return uip_htons(chksum(0, (u8_t *)data, len)); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
306 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
307 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
308 #ifndef UIP_ARCH_IPCHKSUM |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
309 u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
310 uip_ipchksum(void) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
311 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
312 u16_t sum; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
313 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
314 sum = chksum(0, &uip_buf[UIP_LLH_LEN], UIP_IPH_LEN); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
315 DEBUG_PRINTF("uip_ipchksum: sum 0x%04x\n", sum); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
316 return (sum == 0) ? 0xffff : uip_htons(sum); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
317 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
318 #endif |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
319 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
320 static u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
321 upper_layer_chksum(u8_t proto) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
322 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
323 u16_t upper_layer_len; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
324 u16_t sum; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
325 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
326 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
327 upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
328 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
329 upper_layer_len = (((u16_t)(BUF->len[0]) << 8) + BUF->len[1]) - UIP_IPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
330 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
331 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
332 /* First sum pseudoheader. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
333 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
334 /* IP protocol and length fields. This addition cannot carry. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
335 sum = upper_layer_len + proto; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
336 /* Sum IP source and destination addresses. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
337 sum = chksum(sum, (u8_t *)&BUF->srcipaddr, 2 * sizeof(uip_ipaddr_t)); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
338 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
339 /* Sum TCP header and data. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
340 sum = chksum(sum, &uip_buf[UIP_IPH_LEN + UIP_LLH_LEN], |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
341 upper_layer_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
342 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
343 return (sum == 0) ? 0xffff : uip_htons(sum); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
344 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
345 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
346 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
347 u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
348 uip_icmp6chksum(void) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
349 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
350 return upper_layer_chksum(UIP_PROTO_ICMP6); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
351 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
352 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
353 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
354 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
355 u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
356 uip_tcpchksum(void) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
357 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
358 return upper_layer_chksum(UIP_PROTO_TCP); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
359 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
360 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
361 #if UIP_UDP_CHECKSUMS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
362 u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
363 uip_udpchksum(void) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
364 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
365 return upper_layer_chksum(UIP_PROTO_UDP); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
366 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
367 #endif /* UIP_UDP_CHECKSUMS */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
368 #endif /* UIP_ARCH_CHKSUM */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
369 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
370 void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
371 uip_init(void) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
372 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
373 for(c = 0; c < UIP_LISTENPORTS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
374 uip_listenports[c] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
375 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
376 for(c = 0; c < UIP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
377 uip_conns[c].tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
378 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
379 #if UIP_ACTIVE_OPEN || UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
380 lastport = 1024; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
381 #endif /* UIP_ACTIVE_OPEN || UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
382 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
383 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
384 for(c = 0; c < UIP_UDP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
385 uip_udp_conns[c].lport = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
386 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
387 #endif /* UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
388 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
389 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
390 /* IPv4 initialization. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
391 #if UIP_FIXEDADDR == 0 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
392 /* uip_hostaddr[0] = uip_hostaddr[1] = 0;*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
393 #endif /* UIP_FIXEDADDR */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
394 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
395 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
396 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
397 #if UIP_ACTIVE_OPEN |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
398 struct uip_conn * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
399 uip_connect(uip_ipaddr_t *ripaddr, u16_t rport) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
400 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
401 register struct uip_conn *conn, *cconn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
402 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
403 /* Find an unused local port. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
404 again: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
405 ++lastport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
406 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
407 if(lastport >= 32000) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
408 lastport = 4096; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
409 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
410 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
411 /* Check if this port is already in use, and if so try to find |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
412 another one. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
413 for(c = 0; c < UIP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
414 conn = &uip_conns[c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
415 if(conn->tcpstateflags != UIP_CLOSED && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
416 conn->lport == uip_htons(lastport)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
417 goto again; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
418 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
419 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
420 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
421 conn = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
422 for(c = 0; c < UIP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
423 cconn = &uip_conns[c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
424 if(cconn->tcpstateflags == UIP_CLOSED) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
425 conn = cconn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
426 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
427 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
428 if(cconn->tcpstateflags == UIP_TIME_WAIT) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
429 if(conn == 0 || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
430 cconn->timer > conn->timer) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
431 conn = cconn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
432 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
433 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
434 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
435 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
436 if(conn == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
437 return 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
438 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
439 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
440 conn->tcpstateflags = UIP_SYN_SENT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
441 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
442 conn->snd_nxt[0] = iss[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
443 conn->snd_nxt[1] = iss[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
444 conn->snd_nxt[2] = iss[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
445 conn->snd_nxt[3] = iss[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
446 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
447 conn->initialmss = conn->mss = UIP_TCP_MSS; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
448 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
449 conn->len = 1; /* TCP length of the SYN is one. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
450 conn->nrtx = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
451 conn->timer = 1; /* Send the SYN next time around. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
452 conn->rto = UIP_RTO; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
453 conn->sa = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
454 conn->sv = 16; /* Initial value of the RTT variance. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
455 conn->lport = uip_htons(lastport); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
456 conn->rport = rport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
457 uip_ipaddr_copy(&conn->ripaddr, ripaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
458 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
459 return conn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
460 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
461 #endif /* UIP_ACTIVE_OPEN */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
462 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
463 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
464 struct uip_udp_conn * |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
465 uip_udp_new(const uip_ipaddr_t *ripaddr, u16_t rport) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
466 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
467 register struct uip_udp_conn *conn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
468 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
469 /* Find an unused local port. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
470 again: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
471 ++lastport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
472 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
473 if(lastport >= 32000) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
474 lastport = 4096; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
475 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
476 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
477 for(c = 0; c < UIP_UDP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
478 if(uip_udp_conns[c].lport == uip_htons(lastport)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
479 goto again; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
480 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
481 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
482 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
483 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
484 conn = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
485 for(c = 0; c < UIP_UDP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
486 if(uip_udp_conns[c].lport == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
487 conn = &uip_udp_conns[c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
488 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
489 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
490 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
491 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
492 if(conn == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
493 return 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
494 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
495 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
496 conn->lport = UIP_HTONS(lastport); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
497 conn->rport = rport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
498 if(ripaddr == NULL) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
499 memset(&conn->ripaddr, 0, sizeof(uip_ipaddr_t)); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
500 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
501 uip_ipaddr_copy(&conn->ripaddr, ripaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
502 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
503 conn->ttl = UIP_TTL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
504 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
505 return conn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
506 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
507 #endif /* UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
508 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
509 void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
510 uip_unlisten(u16_t port) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
511 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
512 for(c = 0; c < UIP_LISTENPORTS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
513 if(uip_listenports[c] == port) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
514 uip_listenports[c] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
515 return; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
516 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
517 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
518 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
519 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
520 void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
521 uip_listen(u16_t port) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
522 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
523 for(c = 0; c < UIP_LISTENPORTS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
524 if(uip_listenports[c] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
525 uip_listenports[c] = port; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
526 return; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
527 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
528 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
529 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
530 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
531 /* XXX: IP fragment reassembly: not well-tested. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
532 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
533 #if UIP_REASSEMBLY && !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
534 #define UIP_REASS_BUFSIZE (UIP_BUFSIZE - UIP_LLH_LEN) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
535 static u8_t uip_reassbuf[UIP_REASS_BUFSIZE]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
536 static u8_t uip_reassbitmap[UIP_REASS_BUFSIZE / (8 * 8)]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
537 static const u8_t bitmap_bits[8] = {0xff, 0x7f, 0x3f, 0x1f, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
538 0x0f, 0x07, 0x03, 0x01}; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
539 static u16_t uip_reasslen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
540 static u8_t uip_reassflags; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
541 #define UIP_REASS_FLAG_LASTFRAG 0x01 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
542 static u8_t uip_reasstmr; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
543 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
544 #define IP_MF 0x20 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
545 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
546 static u8_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
547 uip_reass(void) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
548 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
549 u16_t offset, len; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
550 u16_t i; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
551 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
552 /* If ip_reasstmr is zero, no packet is present in the buffer, so we |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
553 write the IP header of the fragment into the reassembly |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
554 buffer. The timer is updated with the maximum age. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
555 if(uip_reasstmr == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
556 memcpy(uip_reassbuf, &BUF->vhl, UIP_IPH_LEN); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
557 uip_reasstmr = UIP_REASS_MAXAGE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
558 uip_reassflags = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
559 /* Clear the bitmap. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
560 memset(uip_reassbitmap, 0, sizeof(uip_reassbitmap)); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
561 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
562 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
563 /* Check if the incoming fragment matches the one currently present |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
564 in the reasembly buffer. If so, we proceed with copying the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
565 fragment into the buffer. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
566 if(BUF->srcipaddr[0] == FBUF->srcipaddr[0] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
567 BUF->srcipaddr[1] == FBUF->srcipaddr[1] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
568 BUF->destipaddr[0] == FBUF->destipaddr[0] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
569 BUF->destipaddr[1] == FBUF->destipaddr[1] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
570 BUF->ipid[0] == FBUF->ipid[0] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
571 BUF->ipid[1] == FBUF->ipid[1]) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
572 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
573 len = (BUF->len[0] << 8) + BUF->len[1] - (BUF->vhl & 0x0f) * 4; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
574 offset = (((BUF->ipoffset[0] & 0x3f) << 8) + BUF->ipoffset[1]) * 8; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
575 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
576 /* If the offset or the offset + fragment length overflows the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
577 reassembly buffer, we discard the entire packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
578 if(offset > UIP_REASS_BUFSIZE || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
579 offset + len > UIP_REASS_BUFSIZE) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
580 uip_reasstmr = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
581 goto nullreturn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
582 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
583 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
584 /* Copy the fragment into the reassembly buffer, at the right |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
585 offset. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
586 memcpy(&uip_reassbuf[UIP_IPH_LEN + offset], |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
587 (char *)BUF + (int)((BUF->vhl & 0x0f) * 4), |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
588 len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
589 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
590 /* Update the bitmap. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
591 if(offset / (8 * 8) == (offset + len) / (8 * 8)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
592 /* If the two endpoints are in the same byte, we only update |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
593 that byte. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
594 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
595 uip_reassbitmap[offset / (8 * 8)] |= |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
596 bitmap_bits[(offset / 8 ) & 7] & |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
597 ~bitmap_bits[((offset + len) / 8 ) & 7]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
598 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
599 /* If the two endpoints are in different bytes, we update the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
600 bytes in the endpoints and fill the stuff inbetween with |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
601 0xff. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
602 uip_reassbitmap[offset / (8 * 8)] |= |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
603 bitmap_bits[(offset / 8 ) & 7]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
604 for(i = 1 + offset / (8 * 8); i < (offset + len) / (8 * 8); ++i) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
605 uip_reassbitmap[i] = 0xff; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
606 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
607 uip_reassbitmap[(offset + len) / (8 * 8)] |= |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
608 ~bitmap_bits[((offset + len) / 8 ) & 7]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
609 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
610 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
611 /* If this fragment has the More Fragments flag set to zero, we |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
612 know that this is the last fragment, so we can calculate the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
613 size of the entire packet. We also set the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
614 IP_REASS_FLAG_LASTFRAG flag to indicate that we have received |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
615 the final fragment. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
616 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
617 if((BUF->ipoffset[0] & IP_MF) == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
618 uip_reassflags |= UIP_REASS_FLAG_LASTFRAG; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
619 uip_reasslen = offset + len; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
620 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
621 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
622 /* Finally, we check if we have a full packet in the buffer. We do |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
623 this by checking if we have the last fragment and if all bits |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
624 in the bitmap are set. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
625 if(uip_reassflags & UIP_REASS_FLAG_LASTFRAG) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
626 /* Check all bytes up to and including all but the last byte in |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
627 the bitmap. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
628 for(i = 0; i < uip_reasslen / (8 * 8) - 1; ++i) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
629 if(uip_reassbitmap[i] != 0xff) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
630 goto nullreturn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
631 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
632 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
633 /* Check the last byte in the bitmap. It should contain just the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
634 right amount of bits. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
635 if(uip_reassbitmap[uip_reasslen / (8 * 8)] != |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
636 (u8_t)~bitmap_bits[uip_reasslen / 8 & 7]) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
637 goto nullreturn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
638 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
639 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
640 /* If we have come this far, we have a full packet in the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
641 buffer, so we allocate a pbuf and copy the packet into it. We |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
642 also reset the timer. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
643 uip_reasstmr = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
644 memcpy(BUF, FBUF, uip_reasslen); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
645 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
646 /* Pretend to be a "normal" (i.e., not fragmented) IP packet |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
647 from now on. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
648 BUF->ipoffset[0] = BUF->ipoffset[1] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
649 BUF->len[0] = uip_reasslen >> 8; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
650 BUF->len[1] = uip_reasslen & 0xff; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
651 BUF->ipchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
652 BUF->ipchksum = ~(uip_ipchksum()); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
653 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
654 return uip_reasslen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
655 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
656 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
657 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
658 nullreturn: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
659 return 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
660 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
661 #endif /* UIP_REASSEMBLY */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
662 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
663 static void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
664 uip_add_rcv_nxt(u16_t n) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
665 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
666 uip_add32(uip_conn->rcv_nxt, n); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
667 uip_conn->rcv_nxt[0] = uip_acc32[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
668 uip_conn->rcv_nxt[1] = uip_acc32[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
669 uip_conn->rcv_nxt[2] = uip_acc32[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
670 uip_conn->rcv_nxt[3] = uip_acc32[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
671 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
672 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
673 void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
674 uip_process(u8_t flag) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
675 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
676 register struct uip_conn *uip_connr = uip_conn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
677 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
678 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
679 if(flag == UIP_UDP_SEND_CONN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
680 goto udp_send; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
681 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
682 #endif /* UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
683 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
684 uip_sappdata = uip_appdata = &uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
685 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
686 /* Check if we were invoked because of a poll request for a |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
687 particular connection. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
688 if(flag == UIP_POLL_REQUEST) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
689 if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
690 !uip_outstanding(uip_connr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
691 uip_flags = UIP_POLL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
692 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
693 goto appsend; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
694 #if UIP_ACTIVE_OPEN |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
695 } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
696 /* In the SYN_SENT state, we retransmit out SYN. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
697 BUF->flags = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
698 goto tcp_send_syn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
699 #endif /* UIP_ACTIVE_OPEN */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
700 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
701 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
702 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
703 /* Check if we were invoked because of the perodic timer fireing. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
704 } else if(flag == UIP_TIMER) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
705 #if UIP_REASSEMBLY |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
706 if(uip_reasstmr != 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
707 --uip_reasstmr; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
708 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
709 #endif /* UIP_REASSEMBLY */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
710 /* Increase the initial sequence number. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
711 if(++iss[3] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
712 if(++iss[2] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
713 if(++iss[1] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
714 ++iss[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
715 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
716 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
717 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
718 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
719 /* Reset the length variables. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
720 uip_len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
721 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
722 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
723 /* Check if the connection is in a state in which we simply wait |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
724 for the connection to time out. If so, we increase the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
725 connection's timer and remove the connection if it times |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
726 out. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
727 if(uip_connr->tcpstateflags == UIP_TIME_WAIT || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
728 uip_connr->tcpstateflags == UIP_FIN_WAIT_2) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
729 ++(uip_connr->timer); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
730 if(uip_connr->timer == UIP_TIME_WAIT_TIMEOUT) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
731 uip_connr->tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
732 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
733 } else if(uip_connr->tcpstateflags != UIP_CLOSED) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
734 /* If the connection has outstanding data, we increase the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
735 connection's timer and see if it has reached the RTO value |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
736 in which case we retransmit. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
737 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
738 if(uip_outstanding(uip_connr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
739 if(uip_connr->timer-- == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
740 if(uip_connr->nrtx == UIP_MAXRTX || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
741 ((uip_connr->tcpstateflags == UIP_SYN_SENT || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
742 uip_connr->tcpstateflags == UIP_SYN_RCVD) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
743 uip_connr->nrtx == UIP_MAXSYNRTX)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
744 uip_connr->tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
745 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
746 /* We call UIP_APPCALL() with uip_flags set to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
747 UIP_TIMEDOUT to inform the application that the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
748 connection has timed out. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
749 uip_flags = UIP_TIMEDOUT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
750 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
751 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
752 /* We also send a reset packet to the remote host. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
753 BUF->flags = TCP_RST | TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
754 goto tcp_send_nodata; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
755 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
756 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
757 /* Exponential backoff. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
758 uip_connr->timer = UIP_RTO << (uip_connr->nrtx > 4? |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
759 4: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
760 uip_connr->nrtx); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
761 ++(uip_connr->nrtx); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
762 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
763 /* Ok, so we need to retransmit. We do this differently |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
764 depending on which state we are in. In ESTABLISHED, we |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
765 call upon the application so that it may prepare the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
766 data for the retransmit. In SYN_RCVD, we resend the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
767 SYNACK that we sent earlier and in LAST_ACK we have to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
768 retransmit our FINACK. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
769 UIP_STAT(++uip_stat.tcp.rexmit); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
770 switch(uip_connr->tcpstateflags & UIP_TS_MASK) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
771 case UIP_SYN_RCVD: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
772 /* In the SYN_RCVD state, we should retransmit our |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
773 SYNACK. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
774 goto tcp_send_synack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
775 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
776 #if UIP_ACTIVE_OPEN |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
777 case UIP_SYN_SENT: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
778 /* In the SYN_SENT state, we retransmit out SYN. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
779 BUF->flags = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
780 goto tcp_send_syn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
781 #endif /* UIP_ACTIVE_OPEN */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
782 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
783 case UIP_ESTABLISHED: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
784 /* In the ESTABLISHED state, we call upon the application |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
785 to do the actual retransmit after which we jump into |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
786 the code for sending out the packet (the apprexmit |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
787 label). */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
788 uip_flags = UIP_REXMIT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
789 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
790 goto apprexmit; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
791 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
792 case UIP_FIN_WAIT_1: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
793 case UIP_CLOSING: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
794 case UIP_LAST_ACK: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
795 /* In all these states we should retransmit a FINACK. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
796 goto tcp_send_finack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
797 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
798 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
799 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
800 } else if((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
801 /* If there was no need for a retransmission, we poll the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
802 application for new data. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
803 uip_flags = UIP_POLL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
804 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
805 goto appsend; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
806 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
807 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
808 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
809 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
810 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
811 if(flag == UIP_UDP_TIMER) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
812 if(uip_udp_conn->lport != 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
813 uip_conn = NULL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
814 uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
815 uip_len = uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
816 uip_flags = UIP_POLL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
817 UIP_UDP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
818 goto udp_send; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
819 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
820 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
821 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
822 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
823 #endif |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
824 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
825 /* This is where the input processing starts. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
826 UIP_STAT(++uip_stat.ip.recv); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
827 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
828 /* Start of IP input header processing code. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
829 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
830 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
831 /* Check validity of the IP header. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
832 if((BUF->vtc & 0xf0) != 0x60) { /* IP version and header length. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
833 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
834 UIP_STAT(++uip_stat.ip.vhlerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
835 UIP_LOG("ipv6: invalid version."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
836 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
837 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
838 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
839 /* Check validity of the IP header. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
840 if(BUF->vhl != 0x45) { /* IP version and header length. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
841 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
842 UIP_STAT(++uip_stat.ip.vhlerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
843 UIP_LOG("ip: invalid version or header length."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
844 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
845 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
846 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
847 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
848 /* Check the size of the packet. If the size reported to us in |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
849 uip_len is smaller the size reported in the IP header, we assume |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
850 that the packet has been corrupted in transit. If the size of |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
851 uip_len is larger than the size reported in the IP packet header, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
852 the packet has been padded and we set uip_len to the correct |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
853 value.. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
854 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
855 if((BUF->len[0] << 8) + BUF->len[1] <= uip_len) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
856 uip_len = (BUF->len[0] << 8) + BUF->len[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
857 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
858 uip_len += 40; /* The length reported in the IPv6 header is the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
859 length of the payload that follows the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
860 header. However, uIP uses the uip_len variable |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
861 for holding the size of the entire packet, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
862 including the IP header. For IPv4 this is not a |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
863 problem as the length field in the IPv4 header |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
864 contains the length of the entire packet. But |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
865 for IPv6 we need to add the size of the IPv6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
866 header (40 bytes). */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
867 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
868 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
869 UIP_LOG("ip: packet shorter than reported in IP header."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
870 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
871 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
872 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
873 #if !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
874 /* Check the fragment flag. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
875 if((BUF->ipoffset[0] & 0x3f) != 0 || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
876 BUF->ipoffset[1] != 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
877 #if UIP_REASSEMBLY |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
878 uip_len = uip_reass(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
879 if(uip_len == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
880 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
881 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
882 #else /* UIP_REASSEMBLY */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
883 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
884 UIP_STAT(++uip_stat.ip.fragerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
885 UIP_LOG("ip: fragment dropped."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
886 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
887 #endif /* UIP_REASSEMBLY */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
888 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
889 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
890 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
891 if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
892 /* If we are configured to use ping IP address configuration and |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
893 hasn't been assigned an IP address yet, we accept all ICMP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
894 packets. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
895 #if UIP_PINGADDRCONF && !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
896 if(BUF->proto == UIP_PROTO_ICMP) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
897 UIP_LOG("ip: possible ping config packet received."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
898 goto icmp_input; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
899 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
900 UIP_LOG("ip: packet dropped since no address assigned."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
901 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
902 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
903 #endif /* UIP_PINGADDRCONF */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
904 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
905 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
906 /* If IP broadcast support is configured, we check for a broadcast |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
907 UDP packet, which may be destined to us. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
908 #if UIP_BROADCAST |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
909 DEBUG_PRINTF("UDP IP checksum 0x%04x\n", uip_ipchksum()); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
910 if(BUF->proto == UIP_PROTO_UDP && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
911 (uip_ipaddr_cmp(&BUF->destipaddr, &uip_broadcast_addr) || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
912 (BUF->destipaddr.u8[0] & 224) == 224)) { /* XXX this is a |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
913 hack to be able |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
914 to receive UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
915 multicast |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
916 packets. We check |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
917 for the bit |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
918 pattern of the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
919 multicast |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
920 prefix. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
921 goto udp_input; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
922 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
923 #endif /* UIP_BROADCAST */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
924 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
925 /* Check if the packet is destined for our IP address. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
926 #if !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
927 if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
928 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
929 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
930 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
931 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
932 /* For IPv6, packet reception is a little trickier as we need to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
933 make sure that we listen to certain multicast addresses (all |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
934 hosts multicast address, and the solicited-node multicast |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
935 address) as well. However, we will cheat here and accept all |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
936 multicast packets that are sent to the ff02::/16 addresses. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
937 if(!uip_ipaddr_cmp(&BUF->destipaddr, &uip_hostaddr) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
938 BUF->destipaddr.u16[0] != UIP_HTONS(0xff02)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
939 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
940 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
941 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
942 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
943 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
944 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
945 #if !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
946 if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
947 checksum. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
948 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
949 UIP_STAT(++uip_stat.ip.chkerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
950 UIP_LOG("ip: bad checksum."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
951 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
952 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
953 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
954 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
955 if(BUF->proto == UIP_PROTO_TCP) { /* Check for TCP packet. If so, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
956 proceed with TCP input |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
957 processing. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
958 goto tcp_input; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
959 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
960 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
961 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
962 if(BUF->proto == UIP_PROTO_UDP) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
963 goto udp_input; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
964 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
965 #endif /* UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
966 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
967 #if !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
968 /* ICMPv4 processing code follows. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
969 if(BUF->proto != UIP_PROTO_ICMP) { /* We only allow ICMP packets from |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
970 here. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
971 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
972 UIP_STAT(++uip_stat.ip.protoerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
973 UIP_LOG("ip: neither tcp nor icmp."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
974 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
975 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
976 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
977 #if UIP_PINGADDRCONF |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
978 icmp_input: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
979 #endif /* UIP_PINGADDRCONF */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
980 UIP_STAT(++uip_stat.icmp.recv); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
981 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
982 /* ICMP echo (i.e., ping) processing. This is simple, we only change |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
983 the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
984 checksum before we return the packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
985 if(ICMPBUF->type != ICMP_ECHO) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
986 UIP_STAT(++uip_stat.icmp.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
987 UIP_STAT(++uip_stat.icmp.typeerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
988 UIP_LOG("icmp: not icmp echo."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
989 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
990 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
991 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
992 /* If we are configured to use ping IP address assignment, we use |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
993 the destination IP address of this ping packet and assign it to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
994 ourself. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
995 #if UIP_PINGADDRCONF |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
996 if(uip_ipaddr_cmp(&uip_hostaddr, &uip_all_zeroes_addr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
997 uip_hostaddr = BUF->destipaddr; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
998 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
999 #endif /* UIP_PINGADDRCONF */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1000 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1001 ICMPBUF->type = ICMP_ECHO_REPLY; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1002 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1003 if(ICMPBUF->icmpchksum >= UIP_HTONS(0xffff - (ICMP_ECHO << 8))) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1004 ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8) + 1; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1005 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1006 ICMPBUF->icmpchksum += UIP_HTONS(ICMP_ECHO << 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1007 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1008 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1009 /* Swap IP addresses. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1010 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1011 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1012 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1013 UIP_STAT(++uip_stat.icmp.sent); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1014 BUF->ttl = UIP_TTL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1015 goto ip_send_nolen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1016 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1017 /* End of IPv4 input header processing code. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1018 #else /* !UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1019 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1020 /* This is IPv6 ICMPv6 processing code. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1021 DEBUG_PRINTF("icmp6_input: length %d\n", uip_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1022 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1023 if(BUF->proto != UIP_PROTO_ICMP6) { /* We only allow ICMPv6 packets from |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1024 here. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1025 UIP_STAT(++uip_stat.ip.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1026 UIP_STAT(++uip_stat.ip.protoerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1027 UIP_LOG("ip: neither tcp nor icmp6."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1028 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1029 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1030 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1031 UIP_STAT(++uip_stat.icmp.recv); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1032 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1033 /* If we get a neighbor solicitation for our address we should send |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1034 a neighbor advertisement message back. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1035 if(ICMPBUF->type == ICMP6_NEIGHBOR_SOLICITATION) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1036 if(uip_ipaddr_cmp(&ICMPBUF->icmp6data, &uip_hostaddr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1037 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1038 if(ICMPBUF->options[0] == ICMP6_OPTION_SOURCE_LINK_ADDRESS) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1039 /* Save the sender's address in our neighbor list. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1040 uip_neighbor_add(&ICMPBUF->srcipaddr, &(ICMPBUF->options[2])); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1041 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1042 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1043 /* We should now send a neighbor advertisement back to where the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1044 neighbor solicication came from. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1045 ICMPBUF->type = ICMP6_NEIGHBOR_ADVERTISEMENT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1046 ICMPBUF->flags = ICMP6_FLAG_S; /* Solicited flag. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1047 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1048 ICMPBUF->reserved1 = ICMPBUF->reserved2 = ICMPBUF->reserved3 = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1049 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1050 uip_ipaddr_copy(&ICMPBUF->destipaddr, &ICMPBUF->srcipaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1051 uip_ipaddr_copy(&ICMPBUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1052 ICMPBUF->options[0] = ICMP6_OPTION_TARGET_LINK_ADDRESS; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1053 ICMPBUF->options[1] = 1; /* Options length, 1 = 8 bytes. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1054 memcpy(&(ICMPBUF->options[2]), &uip_ethaddr, sizeof(uip_ethaddr)); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1055 ICMPBUF->icmpchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1056 ICMPBUF->icmpchksum = ~uip_icmp6chksum(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1057 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1058 goto send; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1059 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1060 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1061 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1062 } else if(ICMPBUF->type == ICMP6_ECHO) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1063 /* ICMP echo (i.e., ping) processing. This is simple, we only |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1064 change the ICMP type from ECHO to ECHO_REPLY and update the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1065 ICMP checksum before we return the packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1066 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1067 ICMPBUF->type = ICMP6_ECHO_REPLY; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1068 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1069 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1070 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1071 ICMPBUF->icmpchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1072 ICMPBUF->icmpchksum = ~uip_icmp6chksum(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1073 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1074 UIP_STAT(++uip_stat.icmp.sent); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1075 goto send; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1076 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1077 DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1078 UIP_STAT(++uip_stat.icmp.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1079 UIP_STAT(++uip_stat.icmp.typeerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1080 UIP_LOG("icmp: unknown ICMP message."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1081 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1082 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1083 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1084 /* End of IPv6 ICMP processing. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1085 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1086 #endif /* !UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1087 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1088 #if UIP_UDP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1089 /* UDP input processing. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1090 udp_input: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1091 /* UDP processing is really just a hack. We don't do anything to the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1092 UDP/IP headers, but let the UDP application do all the hard |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1093 work. If the application sets uip_slen, it has a packet to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1094 send. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1095 #if UIP_UDP_CHECKSUMS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1096 uip_len = uip_len - UIP_IPUDPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1097 uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1098 if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1099 UIP_STAT(++uip_stat.udp.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1100 UIP_STAT(++uip_stat.udp.chkerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1101 UIP_LOG("udp: bad checksum."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1102 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1103 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1104 #else /* UIP_UDP_CHECKSUMS */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1105 uip_len = uip_len - UIP_IPUDPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1106 #endif /* UIP_UDP_CHECKSUMS */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1107 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1108 /* Make sure that the UDP destination port number is not zero. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1109 if(UDPBUF->destport == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1110 UIP_LOG("udp: zero port."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1111 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1112 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1113 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1114 /* Demultiplex this UDP packet between the UDP "connections". */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1115 for(uip_udp_conn = &uip_udp_conns[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1116 uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1117 ++uip_udp_conn) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1118 /* If the local UDP port is non-zero, the connection is considered |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1119 to be used. If so, the local port number is checked against the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1120 destination port number in the received packet. If the two port |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1121 numbers match, the remote port number is checked if the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1122 connection is bound to a remote port. Finally, if the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1123 connection is bound to a remote IP address, the source IP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1124 address of the packet is checked. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1125 if(uip_udp_conn->lport != 0 && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1126 UDPBUF->destport == uip_udp_conn->lport && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1127 (uip_udp_conn->rport == 0 || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1128 UDPBUF->srcport == uip_udp_conn->rport) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1129 (uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_all_zeroes_addr) || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1130 uip_ipaddr_cmp(&uip_udp_conn->ripaddr, &uip_broadcast_addr) || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1131 uip_ipaddr_cmp(&BUF->srcipaddr, &uip_udp_conn->ripaddr))) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1132 goto udp_found; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1133 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1134 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1135 UIP_LOG("udp: no matching connection found"); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1136 #if UIP_CONF_ICMP_DEST_UNREACH && !UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1137 /* Copy fields from packet header into payload of this ICMP packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1138 memcpy(&(ICMPBUF->payload[0]), ICMPBUF, UIP_IPH_LEN + 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1139 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1140 /* Set the ICMP type and code. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1141 ICMPBUF->type = ICMP_DEST_UNREACHABLE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1142 ICMPBUF->icode = ICMP_PORT_UNREACHABLE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1143 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1144 /* Calculate the ICMP checksum. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1145 ICMPBUF->icmpchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1146 ICMPBUF->icmpchksum = ~uip_chksum((u16_t *)&(ICMPBUF->type), 36); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1147 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1148 /* Set the IP destination address to be the source address of the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1149 original packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1150 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1151 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1152 /* Set our IP address as the source address. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1153 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1154 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1155 /* The size of the ICMP destination unreachable packet is 36 + the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1156 size of the IP header (20) = 56. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1157 uip_len = 36 + UIP_IPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1158 ICMPBUF->len[0] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1159 ICMPBUF->len[1] = (u8_t)uip_len; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1160 ICMPBUF->ttl = UIP_TTL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1161 ICMPBUF->proto = UIP_PROTO_ICMP; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1162 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1163 goto ip_send_nolen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1164 #else /* UIP_CONF_ICMP_DEST_UNREACH */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1165 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1166 #endif /* UIP_CONF_ICMP_DEST_UNREACH */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1167 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1168 udp_found: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1169 uip_conn = NULL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1170 uip_flags = UIP_NEWDATA; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1171 uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1172 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1173 UIP_UDP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1174 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1175 udp_send: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1176 if(uip_slen == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1177 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1178 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1179 uip_len = uip_slen + UIP_IPUDPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1180 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1181 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1182 /* For IPv6, the IP length field does not include the IPv6 IP header |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1183 length. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1184 BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1185 BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1186 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1187 BUF->len[0] = (uip_len >> 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1188 BUF->len[1] = (uip_len & 0xff); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1189 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1190 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1191 BUF->ttl = uip_udp_conn->ttl; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1192 BUF->proto = UIP_PROTO_UDP; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1193 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1194 UDPBUF->udplen = UIP_HTONS(uip_slen + UIP_UDPH_LEN); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1195 UDPBUF->udpchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1196 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1197 BUF->srcport = uip_udp_conn->lport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1198 BUF->destport = uip_udp_conn->rport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1199 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1200 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1201 uip_ipaddr_copy(&BUF->destipaddr, &uip_udp_conn->ripaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1202 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1203 uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1204 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1205 #if UIP_UDP_CHECKSUMS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1206 /* Calculate UDP checksum. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1207 UDPBUF->udpchksum = ~(uip_udpchksum()); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1208 if(UDPBUF->udpchksum == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1209 UDPBUF->udpchksum = 0xffff; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1210 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1211 #endif /* UIP_UDP_CHECKSUMS */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1212 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1213 goto ip_send_nolen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1214 #endif /* UIP_UDP */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1215 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1216 /* TCP input processing. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1217 tcp_input: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1218 UIP_STAT(++uip_stat.tcp.recv); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1219 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1220 /* Start of TCP input header processing code. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1221 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1222 if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1223 checksum. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1224 UIP_STAT(++uip_stat.tcp.drop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1225 UIP_STAT(++uip_stat.tcp.chkerr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1226 UIP_LOG("tcp: bad checksum."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1227 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1228 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1229 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1230 /* Make sure that the TCP port number is not zero. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1231 if(BUF->destport == 0 || BUF->srcport == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1232 UIP_LOG("tcp: zero port."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1233 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1234 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1235 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1236 /* Demultiplex this segment. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1237 /* First check any active connections. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1238 for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1239 ++uip_connr) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1240 if(uip_connr->tcpstateflags != UIP_CLOSED && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1241 BUF->destport == uip_connr->lport && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1242 BUF->srcport == uip_connr->rport && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1243 uip_ipaddr_cmp(&BUF->srcipaddr, &uip_connr->ripaddr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1244 goto found; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1245 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1246 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1247 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1248 /* If we didn't find and active connection that expected the packet, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1249 either this packet is an old duplicate, or this is a SYN packet |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1250 destined for a connection in LISTEN. If the SYN flag isn't set, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1251 it is an old packet and we send a RST. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1252 if((BUF->flags & TCP_CTL) != TCP_SYN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1253 goto reset; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1254 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1255 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1256 tmp16 = BUF->destport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1257 /* Next, check listening connections. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1258 for(c = 0; c < UIP_LISTENPORTS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1259 if(tmp16 == uip_listenports[c]) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1260 goto found_listen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1261 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1262 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1263 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1264 /* No matching connection found, so we send a RST packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1265 UIP_STAT(++uip_stat.tcp.synrst); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1266 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1267 reset: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1268 /* We do not send resets in response to resets. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1269 if(BUF->flags & TCP_RST) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1270 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1271 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1272 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1273 UIP_STAT(++uip_stat.tcp.rst); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1274 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1275 BUF->flags = TCP_RST | TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1276 uip_len = UIP_IPTCPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1277 BUF->tcpoffset = 5 << 4; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1278 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1279 /* Flip the seqno and ackno fields in the TCP header. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1280 c = BUF->seqno[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1281 BUF->seqno[3] = BUF->ackno[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1282 BUF->ackno[3] = c; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1283 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1284 c = BUF->seqno[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1285 BUF->seqno[2] = BUF->ackno[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1286 BUF->ackno[2] = c; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1287 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1288 c = BUF->seqno[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1289 BUF->seqno[1] = BUF->ackno[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1290 BUF->ackno[1] = c; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1291 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1292 c = BUF->seqno[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1293 BUF->seqno[0] = BUF->ackno[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1294 BUF->ackno[0] = c; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1295 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1296 /* We also have to increase the sequence number we are |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1297 acknowledging. If the least significant byte overflowed, we need |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1298 to propagate the carry to the other bytes as well. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1299 if(++BUF->ackno[3] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1300 if(++BUF->ackno[2] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1301 if(++BUF->ackno[1] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1302 ++BUF->ackno[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1303 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1304 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1305 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1306 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1307 /* Swap port numbers. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1308 tmp16 = BUF->srcport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1309 BUF->srcport = BUF->destport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1310 BUF->destport = tmp16; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1311 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1312 /* Swap IP addresses. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1313 uip_ipaddr_copy(&BUF->destipaddr, &BUF->srcipaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1314 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1315 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1316 /* And send out the RST packet! */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1317 goto tcp_send_noconn; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1318 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1319 /* This label will be jumped to if we matched the incoming packet |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1320 with a connection in LISTEN. In that case, we should create a new |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1321 connection and send a SYNACK in return. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1322 found_listen: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1323 /* First we check if there are any connections avaliable. Unused |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1324 connections are kept in the same table as used connections, but |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1325 unused ones have the tcpstate set to CLOSED. Also, connections in |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1326 TIME_WAIT are kept track of and we'll use the oldest one if no |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1327 CLOSED connections are found. Thanks to Eddie C. Dost for a very |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1328 nice algorithm for the TIME_WAIT search. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1329 uip_connr = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1330 for(c = 0; c < UIP_CONNS; ++c) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1331 if(uip_conns[c].tcpstateflags == UIP_CLOSED) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1332 uip_connr = &uip_conns[c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1333 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1334 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1335 if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1336 if(uip_connr == 0 || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1337 uip_conns[c].timer > uip_connr->timer) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1338 uip_connr = &uip_conns[c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1339 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1340 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1341 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1342 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1343 if(uip_connr == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1344 /* All connections are used already, we drop packet and hope that |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1345 the remote end will retransmit the packet at a time when we |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1346 have more spare connections. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1347 UIP_STAT(++uip_stat.tcp.syndrop); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1348 UIP_LOG("tcp: found no unused connections."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1349 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1350 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1351 uip_conn = uip_connr; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1352 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1353 /* Fill in the necessary fields for the new connection. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1354 uip_connr->rto = uip_connr->timer = UIP_RTO; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1355 uip_connr->sa = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1356 uip_connr->sv = 4; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1357 uip_connr->nrtx = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1358 uip_connr->lport = BUF->destport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1359 uip_connr->rport = BUF->srcport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1360 uip_ipaddr_copy(&uip_connr->ripaddr, &BUF->srcipaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1361 uip_connr->tcpstateflags = UIP_SYN_RCVD; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1362 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1363 uip_connr->snd_nxt[0] = iss[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1364 uip_connr->snd_nxt[1] = iss[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1365 uip_connr->snd_nxt[2] = iss[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1366 uip_connr->snd_nxt[3] = iss[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1367 uip_connr->len = 1; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1368 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1369 /* rcv_nxt should be the seqno from the incoming packet + 1. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1370 uip_connr->rcv_nxt[3] = BUF->seqno[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1371 uip_connr->rcv_nxt[2] = BUF->seqno[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1372 uip_connr->rcv_nxt[1] = BUF->seqno[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1373 uip_connr->rcv_nxt[0] = BUF->seqno[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1374 uip_add_rcv_nxt(1); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1375 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1376 /* Parse the TCP MSS option, if present. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1377 if((BUF->tcpoffset & 0xf0) > 0x50) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1378 for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1379 opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1380 if(opt == TCP_OPT_END) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1381 /* End of options. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1382 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1383 } else if(opt == TCP_OPT_NOOP) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1384 ++c; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1385 /* NOP option. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1386 } else if(opt == TCP_OPT_MSS && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1387 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1388 /* An MSS option with the right option length. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1389 tmp16 = ((u16_t)uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1390 (u16_t)uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + 3 + c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1391 uip_connr->initialmss = uip_connr->mss = |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1392 tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1393 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1394 /* And we are done processing options. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1395 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1396 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1397 /* All other options have a length field, so that we easily |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1398 can skip past them. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1399 if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1400 /* If the length field is zero, the options are malformed |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1401 and we don't process them further. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1402 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1403 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1404 c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1405 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1406 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1407 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1408 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1409 /* Our response will be a SYNACK. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1410 #if UIP_ACTIVE_OPEN |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1411 tcp_send_synack: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1412 BUF->flags = TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1413 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1414 tcp_send_syn: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1415 BUF->flags |= TCP_SYN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1416 #else /* UIP_ACTIVE_OPEN */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1417 tcp_send_synack: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1418 BUF->flags = TCP_SYN | TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1419 #endif /* UIP_ACTIVE_OPEN */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1420 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1421 /* We send out the TCP Maximum Segment Size option with our |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1422 SYNACK. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1423 BUF->optdata[0] = TCP_OPT_MSS; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1424 BUF->optdata[1] = TCP_OPT_MSS_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1425 BUF->optdata[2] = (UIP_TCP_MSS) / 256; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1426 BUF->optdata[3] = (UIP_TCP_MSS) & 255; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1427 uip_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1428 BUF->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1429 goto tcp_send; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1430 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1431 /* This label will be jumped to if we found an active connection. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1432 found: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1433 uip_conn = uip_connr; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1434 uip_flags = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1435 /* We do a very naive form of TCP reset processing; we just accept |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1436 any RST and kill our connection. We should in fact check if the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1437 sequence number of this reset is wihtin our advertised window |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1438 before we accept the reset. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1439 if(BUF->flags & TCP_RST) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1440 uip_connr->tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1441 UIP_LOG("tcp: got reset, aborting connection."); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1442 uip_flags = UIP_ABORT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1443 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1444 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1445 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1446 /* Calculate the length of the data, if the application has sent |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1447 any data to us. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1448 c = (BUF->tcpoffset >> 4) << 2; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1449 /* uip_len will contain the length of the actual TCP data. This is |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1450 calculated by subtracing the length of the TCP header (in |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1451 c) and the length of the IP header (20 bytes). */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1452 uip_len = uip_len - c - UIP_IPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1453 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1454 /* First, check if the sequence number of the incoming packet is |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1455 what we're expecting next. If not, we send out an ACK with the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1456 correct numbers in, unless we are in the SYN_RCVD state and |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1457 receive a SYN, in which case we should retransmit our SYNACK |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1458 (which is done futher down). */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1459 if(!((((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_SENT) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1460 ((BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK))) || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1461 (((uip_connr->tcpstateflags & UIP_TS_MASK) == UIP_SYN_RCVD) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1462 ((BUF->flags & TCP_CTL) == TCP_SYN)))) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1463 if((uip_len > 0 || ((BUF->flags & (TCP_SYN | TCP_FIN)) != 0)) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1464 (BUF->seqno[0] != uip_connr->rcv_nxt[0] || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1465 BUF->seqno[1] != uip_connr->rcv_nxt[1] || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1466 BUF->seqno[2] != uip_connr->rcv_nxt[2] || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1467 BUF->seqno[3] != uip_connr->rcv_nxt[3])) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1468 goto tcp_send_ack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1469 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1470 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1471 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1472 /* Next, check if the incoming segment acknowledges any outstanding |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1473 data. If so, we update the sequence number, reset the length of |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1474 the outstanding data, calculate RTT estimations, and reset the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1475 retransmission timer. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1476 if((BUF->flags & TCP_ACK) && uip_outstanding(uip_connr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1477 uip_add32(uip_connr->snd_nxt, uip_connr->len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1478 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1479 if(BUF->ackno[0] == uip_acc32[0] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1480 BUF->ackno[1] == uip_acc32[1] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1481 BUF->ackno[2] == uip_acc32[2] && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1482 BUF->ackno[3] == uip_acc32[3]) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1483 /* Update sequence number. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1484 uip_connr->snd_nxt[0] = uip_acc32[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1485 uip_connr->snd_nxt[1] = uip_acc32[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1486 uip_connr->snd_nxt[2] = uip_acc32[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1487 uip_connr->snd_nxt[3] = uip_acc32[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1488 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1489 /* Do RTT estimation, unless we have done retransmissions. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1490 if(uip_connr->nrtx == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1491 signed char m; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1492 m = uip_connr->rto - uip_connr->timer; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1493 /* This is taken directly from VJs original code in his paper */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1494 m = m - (uip_connr->sa >> 3); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1495 uip_connr->sa += m; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1496 if(m < 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1497 m = -m; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1498 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1499 m = m - (uip_connr->sv >> 2); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1500 uip_connr->sv += m; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1501 uip_connr->rto = (uip_connr->sa >> 3) + uip_connr->sv; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1502 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1503 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1504 /* Set the acknowledged flag. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1505 uip_flags = UIP_ACKDATA; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1506 /* Reset the retransmission timer. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1507 uip_connr->timer = uip_connr->rto; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1508 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1509 /* Reset length of outstanding data. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1510 uip_connr->len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1511 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1512 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1513 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1514 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1515 /* Do different things depending on in what state the connection is. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1516 switch(uip_connr->tcpstateflags & UIP_TS_MASK) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1517 /* CLOSED and LISTEN are not handled here. CLOSE_WAIT is not |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1518 implemented, since we force the application to close when the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1519 peer sends a FIN (hence the application goes directly from |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1520 ESTABLISHED to LAST_ACK). */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1521 case UIP_SYN_RCVD: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1522 /* In SYN_RCVD we have sent out a SYNACK in response to a SYN, and |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1523 we are waiting for an ACK that acknowledges the data we sent |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1524 out the last time. Therefore, we want to have the UIP_ACKDATA |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1525 flag set. If so, we enter the ESTABLISHED state. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1526 if(uip_flags & UIP_ACKDATA) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1527 uip_connr->tcpstateflags = UIP_ESTABLISHED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1528 uip_flags = UIP_CONNECTED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1529 uip_connr->len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1530 if(uip_len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1531 uip_flags |= UIP_NEWDATA; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1532 uip_add_rcv_nxt(uip_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1533 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1534 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1535 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1536 goto appsend; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1537 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1538 /* We need to retransmit the SYNACK */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1539 if((BUF->flags & TCP_CTL) == TCP_SYN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1540 goto tcp_send_synack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1541 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1542 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1543 #if UIP_ACTIVE_OPEN |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1544 case UIP_SYN_SENT: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1545 /* In SYN_SENT, we wait for a SYNACK that is sent in response to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1546 our SYN. The rcv_nxt is set to sequence number in the SYNACK |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1547 plus one, and we send an ACK. We move into the ESTABLISHED |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1548 state. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1549 if((uip_flags & UIP_ACKDATA) && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1550 (BUF->flags & TCP_CTL) == (TCP_SYN | TCP_ACK)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1551 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1552 /* Parse the TCP MSS option, if present. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1553 if((BUF->tcpoffset & 0xf0) > 0x50) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1554 for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1555 opt = uip_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN + c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1556 if(opt == TCP_OPT_END) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1557 /* End of options. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1558 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1559 } else if(opt == TCP_OPT_NOOP) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1560 ++c; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1561 /* NOP option. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1562 } else if(opt == TCP_OPT_MSS && |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1563 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1564 /* An MSS option with the right option length. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1565 tmp16 = (uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 2 + c] << 8) | |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1566 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 3 + c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1567 uip_connr->initialmss = |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1568 uip_connr->mss = tmp16 > UIP_TCP_MSS? UIP_TCP_MSS: tmp16; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1569 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1570 /* And we are done processing options. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1571 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1572 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1573 /* All other options have a length field, so that we easily |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1574 can skip past them. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1575 if(uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1576 /* If the length field is zero, the options are malformed |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1577 and we don't process them further. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1578 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1579 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1580 c += uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1581 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1582 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1583 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1584 uip_connr->tcpstateflags = UIP_ESTABLISHED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1585 uip_connr->rcv_nxt[0] = BUF->seqno[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1586 uip_connr->rcv_nxt[1] = BUF->seqno[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1587 uip_connr->rcv_nxt[2] = BUF->seqno[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1588 uip_connr->rcv_nxt[3] = BUF->seqno[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1589 uip_add_rcv_nxt(1); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1590 uip_flags = UIP_CONNECTED | UIP_NEWDATA; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1591 uip_connr->len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1592 uip_len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1593 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1594 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1595 goto appsend; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1596 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1597 /* Inform the application that the connection failed */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1598 uip_flags = UIP_ABORT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1599 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1600 /* The connection is closed after we send the RST */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1601 uip_conn->tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1602 goto reset; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1603 #endif /* UIP_ACTIVE_OPEN */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1604 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1605 case UIP_ESTABLISHED: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1606 /* In the ESTABLISHED state, we call upon the application to feed |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1607 data into the uip_buf. If the UIP_ACKDATA flag is set, the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1608 application should put new data into the buffer, otherwise we are |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1609 retransmitting an old segment, and the application should put that |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1610 data into the buffer. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1611 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1612 If the incoming packet is a FIN, we should close the connection on |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1613 this side as well, and we send out a FIN and enter the LAST_ACK |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1614 state. We require that there is no outstanding data; otherwise the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1615 sequence numbers will be screwed up. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1616 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1617 if(BUF->flags & TCP_FIN && !(uip_connr->tcpstateflags & UIP_STOPPED)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1618 if(uip_outstanding(uip_connr)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1619 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1620 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1621 uip_add_rcv_nxt(1 + uip_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1622 uip_flags |= UIP_CLOSE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1623 if(uip_len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1624 uip_flags |= UIP_NEWDATA; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1625 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1626 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1627 uip_connr->len = 1; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1628 uip_connr->tcpstateflags = UIP_LAST_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1629 uip_connr->nrtx = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1630 tcp_send_finack: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1631 BUF->flags = TCP_FIN | TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1632 goto tcp_send_nodata; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1633 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1634 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1635 /* Check the URG flag. If this is set, the segment carries urgent |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1636 data that we must pass to the application. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1637 if((BUF->flags & TCP_URG) != 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1638 #if UIP_URGDATA > 0 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1639 uip_urglen = (BUF->urgp[0] << 8) | BUF->urgp[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1640 if(uip_urglen > uip_len) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1641 /* There is more urgent data in the next segment to come. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1642 uip_urglen = uip_len; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1643 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1644 uip_add_rcv_nxt(uip_urglen); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1645 uip_len -= uip_urglen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1646 uip_urgdata = uip_appdata; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1647 uip_appdata += uip_urglen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1648 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1649 uip_urglen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1650 #else /* UIP_URGDATA > 0 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1651 uip_appdata = ((char *)uip_appdata) + ((BUF->urgp[0] << 8) | BUF->urgp[1]); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1652 uip_len -= (BUF->urgp[0] << 8) | BUF->urgp[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1653 #endif /* UIP_URGDATA > 0 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1654 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1655 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1656 /* If uip_len > 0 we have TCP data in the packet, and we flag this |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1657 by setting the UIP_NEWDATA flag and update the sequence number |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1658 we acknowledge. If the application has stopped the dataflow |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1659 using uip_stop(), we must not accept any data packets from the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1660 remote host. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1661 if(uip_len > 0 && !(uip_connr->tcpstateflags & UIP_STOPPED)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1662 uip_flags |= UIP_NEWDATA; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1663 uip_add_rcv_nxt(uip_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1664 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1665 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1666 /* Check if the available buffer space advertised by the other end |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1667 is smaller than the initial MSS for this connection. If so, we |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1668 set the current MSS to the window size to ensure that the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1669 application does not send more data than the other end can |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1670 handle. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1671 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1672 If the remote host advertises a zero window, we set the MSS to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1673 the initial MSS so that the application will send an entire MSS |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1674 of data. This data will not be acknowledged by the receiver, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1675 and the application will retransmit it. This is called the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1676 "persistent timer" and uses the retransmission mechanim. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1677 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1678 tmp16 = ((u16_t)BUF->wnd[0] << 8) + (u16_t)BUF->wnd[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1679 if(tmp16 > uip_connr->initialmss || |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1680 tmp16 == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1681 tmp16 = uip_connr->initialmss; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1682 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1683 uip_connr->mss = tmp16; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1684 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1685 /* If this packet constitutes an ACK for outstanding data (flagged |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1686 by the UIP_ACKDATA flag, we should call the application since it |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1687 might want to send more data. If the incoming packet had data |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1688 from the peer (as flagged by the UIP_NEWDATA flag), the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1689 application must also be notified. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1690 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1691 When the application is called, the global variable uip_len |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1692 contains the length of the incoming data. The application can |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1693 access the incoming data through the global pointer |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1694 uip_appdata, which usually points UIP_IPTCPH_LEN + UIP_LLH_LEN |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1695 bytes into the uip_buf array. |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1696 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1697 If the application wishes to send any data, this data should be |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1698 put into the uip_appdata and the length of the data should be |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1699 put into uip_len. If the application don't have any data to |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1700 send, uip_len must be set to 0. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1701 if(uip_flags & (UIP_NEWDATA | UIP_ACKDATA)) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1702 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1703 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1704 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1705 appsend: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1706 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1707 if(uip_flags & UIP_ABORT) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1708 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1709 uip_connr->tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1710 BUF->flags = TCP_RST | TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1711 goto tcp_send_nodata; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1712 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1713 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1714 if(uip_flags & UIP_CLOSE) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1715 uip_slen = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1716 uip_connr->len = 1; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1717 uip_connr->tcpstateflags = UIP_FIN_WAIT_1; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1718 uip_connr->nrtx = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1719 BUF->flags = TCP_FIN | TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1720 goto tcp_send_nodata; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1721 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1722 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1723 /* If uip_slen > 0, the application has data to be sent. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1724 if(uip_slen > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1725 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1726 /* If the connection has acknowledged data, the contents of |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1727 the ->len variable should be discarded. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1728 if((uip_flags & UIP_ACKDATA) != 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1729 uip_connr->len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1730 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1731 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1732 /* If the ->len variable is non-zero the connection has |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1733 already data in transit and cannot send anymore right |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1734 now. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1735 if(uip_connr->len == 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1736 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1737 /* The application cannot send more than what is allowed by |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1738 the mss (the minumum of the MSS and the available |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1739 window). */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1740 if(uip_slen > uip_connr->mss) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1741 uip_slen = uip_connr->mss; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1742 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1743 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1744 /* Remember how much data we send out now so that we know |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1745 when everything has been acknowledged. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1746 uip_connr->len = uip_slen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1747 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1748 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1749 /* If the application already had unacknowledged data, we |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1750 make sure that the application does not send (i.e., |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1751 retransmit) out more than it previously sent out. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1752 uip_slen = uip_connr->len; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1753 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1754 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1755 uip_connr->nrtx = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1756 apprexmit: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1757 uip_appdata = uip_sappdata; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1758 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1759 /* If the application has data to be sent, or if the incoming |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1760 packet had new data in it, we must send out a packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1761 if(uip_slen > 0 && uip_connr->len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1762 /* Add the length of the IP and TCP headers. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1763 uip_len = uip_connr->len + UIP_TCPIP_HLEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1764 /* We always set the ACK flag in response packets. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1765 BUF->flags = TCP_ACK | TCP_PSH; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1766 /* Send the packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1767 goto tcp_send_noopts; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1768 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1769 /* If there is no data to send, just send out a pure ACK if |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1770 there is newdata. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1771 if(uip_flags & UIP_NEWDATA) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1772 uip_len = UIP_TCPIP_HLEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1773 BUF->flags = TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1774 goto tcp_send_noopts; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1775 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1776 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1777 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1778 case UIP_LAST_ACK: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1779 /* We can close this connection if the peer has acknowledged our |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1780 FIN. This is indicated by the UIP_ACKDATA flag. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1781 if(uip_flags & UIP_ACKDATA) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1782 uip_connr->tcpstateflags = UIP_CLOSED; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1783 uip_flags = UIP_CLOSE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1784 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1785 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1786 break; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1787 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1788 case UIP_FIN_WAIT_1: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1789 /* The application has closed the connection, but the remote host |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1790 hasn't closed its end yet. Thus we do nothing but wait for a |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1791 FIN from the other side. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1792 if(uip_len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1793 uip_add_rcv_nxt(uip_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1794 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1795 if(BUF->flags & TCP_FIN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1796 if(uip_flags & UIP_ACKDATA) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1797 uip_connr->tcpstateflags = UIP_TIME_WAIT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1798 uip_connr->timer = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1799 uip_connr->len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1800 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1801 uip_connr->tcpstateflags = UIP_CLOSING; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1802 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1803 uip_add_rcv_nxt(1); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1804 uip_flags = UIP_CLOSE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1805 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1806 goto tcp_send_ack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1807 } else if(uip_flags & UIP_ACKDATA) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1808 uip_connr->tcpstateflags = UIP_FIN_WAIT_2; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1809 uip_connr->len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1810 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1811 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1812 if(uip_len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1813 goto tcp_send_ack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1814 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1815 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1816 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1817 case UIP_FIN_WAIT_2: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1818 if(uip_len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1819 uip_add_rcv_nxt(uip_len); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1820 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1821 if(BUF->flags & TCP_FIN) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1822 uip_connr->tcpstateflags = UIP_TIME_WAIT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1823 uip_connr->timer = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1824 uip_add_rcv_nxt(1); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1825 uip_flags = UIP_CLOSE; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1826 UIP_APPCALL(); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1827 goto tcp_send_ack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1828 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1829 if(uip_len > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1830 goto tcp_send_ack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1831 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1832 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1833 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1834 case UIP_TIME_WAIT: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1835 goto tcp_send_ack; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1836 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1837 case UIP_CLOSING: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1838 if(uip_flags & UIP_ACKDATA) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1839 uip_connr->tcpstateflags = UIP_TIME_WAIT; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1840 uip_connr->timer = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1841 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1842 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1843 goto drop; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1844 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1845 /* We jump here when we are ready to send the packet, and just want |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1846 to set the appropriate TCP sequence numbers in the TCP header. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1847 tcp_send_ack: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1848 BUF->flags = TCP_ACK; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1849 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1850 tcp_send_nodata: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1851 uip_len = UIP_IPTCPH_LEN; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1852 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1853 tcp_send_noopts: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1854 BUF->tcpoffset = (UIP_TCPH_LEN / 4) << 4; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1855 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1856 /* We're done with the input processing. We are now ready to send a |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1857 reply. Our job is to fill in all the fields of the TCP and IP |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1858 headers before calculating the checksum and finally send the |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1859 packet. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1860 tcp_send: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1861 BUF->ackno[0] = uip_connr->rcv_nxt[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1862 BUF->ackno[1] = uip_connr->rcv_nxt[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1863 BUF->ackno[2] = uip_connr->rcv_nxt[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1864 BUF->ackno[3] = uip_connr->rcv_nxt[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1865 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1866 BUF->seqno[0] = uip_connr->snd_nxt[0]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1867 BUF->seqno[1] = uip_connr->snd_nxt[1]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1868 BUF->seqno[2] = uip_connr->snd_nxt[2]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1869 BUF->seqno[3] = uip_connr->snd_nxt[3]; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1870 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1871 BUF->proto = UIP_PROTO_TCP; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1872 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1873 BUF->srcport = uip_connr->lport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1874 BUF->destport = uip_connr->rport; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1875 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1876 uip_ipaddr_copy(&BUF->srcipaddr, &uip_hostaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1877 uip_ipaddr_copy(&BUF->destipaddr, &uip_connr->ripaddr); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1878 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1879 if(uip_connr->tcpstateflags & UIP_STOPPED) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1880 /* If the connection has issued uip_stop(), we advertise a zero |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1881 window so that the remote host will stop sending data. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1882 BUF->wnd[0] = BUF->wnd[1] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1883 } else { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1884 BUF->wnd[0] = ((UIP_RECEIVE_WINDOW) >> 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1885 BUF->wnd[1] = ((UIP_RECEIVE_WINDOW) & 0xff); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1886 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1887 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1888 tcp_send_noconn: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1889 BUF->ttl = UIP_TTL; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1890 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1891 /* For IPv6, the IP length field does not include the IPv6 IP header |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1892 length. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1893 BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1894 BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1895 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1896 BUF->len[0] = (uip_len >> 8); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1897 BUF->len[1] = (uip_len & 0xff); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1898 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1899 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1900 BUF->urgp[0] = BUF->urgp[1] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1901 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1902 /* Calculate TCP checksum. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1903 BUF->tcpchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1904 BUF->tcpchksum = ~(uip_tcpchksum()); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1905 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1906 ip_send_nolen: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1907 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1908 BUF->vtc = 0x60; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1909 BUF->tcflow = 0x00; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1910 BUF->flow = 0x00; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1911 #else /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1912 BUF->vhl = 0x45; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1913 BUF->tos = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1914 BUF->ipoffset[0] = BUF->ipoffset[1] = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1915 ++ipid; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1916 BUF->ipid[0] = ipid >> 8; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1917 BUF->ipid[1] = ipid & 0xff; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1918 /* Calculate IP checksum. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1919 BUF->ipchksum = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1920 BUF->ipchksum = ~(uip_ipchksum()); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1921 DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum()); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1922 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1923 UIP_STAT(++uip_stat.tcp.sent); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1924 #if UIP_CONF_IPV6 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1925 send: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1926 #endif /* UIP_CONF_IPV6 */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1927 DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len, |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1928 (BUF->len[0] << 8) | BUF->len[1]); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1929 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1930 UIP_STAT(++uip_stat.ip.sent); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1931 /* Return and let the caller do the actual transmission. */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1932 uip_flags = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1933 return; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1934 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1935 drop: |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1936 uip_len = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1937 uip_flags = 0; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1938 return; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1939 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1940 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1941 u16_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1942 uip_htons(u16_t val) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1943 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1944 return UIP_HTONS(val); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1945 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1946 |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1947 u32_t |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1948 uip_htonl(u32_t val) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1949 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1950 return UIP_HTONL(val); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1951 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1952 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1953 void |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1954 uip_send(const void *data, int len) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1955 { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1956 int copylen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1957 #define MIN(a,b) ((a) < (b)? (a): (b)) |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1958 copylen = MIN(len, UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN - |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1959 (int)((char *)uip_sappdata - (char *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN])); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1960 if(copylen > 0) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1961 uip_slen = copylen; |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1962 if(data != uip_sappdata) { |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1963 memcpy(uip_sappdata, (data), uip_slen); |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1964 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1965 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1966 } |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1967 /*---------------------------------------------------------------------------*/ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1968 /** @} */ |
56d09a0969b5
Import uIP and the PPP implementation from
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1969 #endif /* UIP_CONF_IPV6 */ |