00001 /* 00002 * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the copyright holders nor the names of 00014 * contributors may be used to endorse or promote products derived 00015 * from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS 00018 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00019 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00020 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE 00021 * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00022 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00023 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00025 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00027 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00028 * SUCH DAMAGE. 00029 * 00030 * For additional information see http://www.ethernut.de/ 00031 * 00032 * - 00033 * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk> 00034 * 00035 * This file is distributed in the hope that it will be useful, but WITHOUT 00036 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00037 * FITNESS FOR A PARTICULAR PURPOSE. 00038 * 00039 * You can redistribute this file and/or modify it under the terms of the GNU 00040 * General Public License (GPL) as published by the Free Software Foundation; 00041 * either version 2 of the License, or (at your discretion) any later version. 00042 * See the accompanying file "copying-gpl.txt" for more details. 00043 * 00044 * As a special exception to the GPL, permission is granted for additional 00045 * uses of the text contained in this file. See the accompanying file 00046 * "copying-liquorice.txt" for details. 00047 * - 00048 * Portions Copyright (c) 1983, 1993 by 00049 * The Regents of the University of California. All rights reserved. 00050 * 00051 * Redistribution and use in source and binary forms, with or without 00052 * modification, are permitted provided that the following conditions 00053 * are met: 00054 * 1. Redistributions of source code must retain the above copyright 00055 * notice, this list of conditions and the following disclaimer. 00056 * 2. Redistributions in binary form must reproduce the above copyright 00057 * notice, this list of conditions and the following disclaimer in the 00058 * documentation and/or other materials provided with the distribution. 00059 * 3. Neither the name of the University nor the names of its contributors 00060 * may be used to endorse or promote products derived from this software 00061 * without specific prior written permission. 00062 * 00063 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00064 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00065 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00066 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00067 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00068 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00069 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00070 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00071 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00072 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00073 * SUCH DAMAGE. 00074 * - 00075 * Portions Copyright (c) 1993 by Digital Equipment Corporation. 00076 * 00077 * Permission to use, copy, modify, and distribute this software for any 00078 * purpose with or without fee is hereby granted, provided that the above 00079 * copyright notice and this permission notice appear in all copies, and that 00080 * the name of Digital Equipment Corporation not be used in advertising or 00081 * publicity pertaining to distribution of the document or software without 00082 * specific, written prior permission. 00083 * 00084 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL 00085 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES 00086 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT 00087 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 00088 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 00089 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 00090 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 00091 * SOFTWARE. 00092 */ 00093 00094 /* 00095 * $Log$ 00096 * Revision 1.11 2009/02/22 12:30:36 olereinhardt 00097 * Include "include/errno.h" instead of "include/net/errno.h" 00098 * 00099 * Revision 1.10 2008/08/11 07:00:32 haraldkipp 00100 * BSD types replaced by stdint types (feature request #1282721). 00101 * 00102 * Revision 1.9 2008/04/06 13:29:01 haraldkipp 00103 * In unreliable or high traffic networks connections may suddenly freeze. 00104 * The problem is, that during overflows (happening every 65s) the 00105 * retransmission timer may be loaded with 0, which in turn disables all 00106 * outstanding retransmission. Applied fix contributed by Henrik Maier. 00107 * 00108 * Revision 1.8 2006/10/08 16:48:22 haraldkipp 00109 * Documentation fixed 00110 * 00111 * Revision 1.7 2006/03/02 19:57:34 haraldkipp 00112 * ICCARM insists on a (void *) typecast for the second parameter of memcpy(). 00113 * 00114 * Revision 1.6 2006/01/23 17:33:47 haraldkipp 00115 * Avoid memory alignment errors. 00116 * 00117 * Revision 1.5 2005/04/30 16:42:42 chaac 00118 * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG 00119 * is defined in NutConf, it will make effect where it is used. 00120 * 00121 * Revision 1.4 2005/04/05 17:41:48 haraldkipp 00122 * Header is dangerous when you forget to remove all debug outputs. 00123 * 00124 * Revision 1.3 2004/07/30 19:54:46 drsung 00125 * Some code of TCP stack redesigned. Round trip time calculation is now 00126 * supported. Fixed several bugs in TCP state machine. Now TCP connections 00127 * should be more reliable under heavy traffic or poor physical connections. 00128 * 00129 * Revision 1.2 2004/04/15 11:05:35 haraldkipp 00130 * Set retransmission timer on first transmit queue entry 00131 * 00132 * Revision 1.1.1.1 2003/05/09 14:41:40 haraldkipp 00133 * Initial using 3.2.1 00134 * 00135 * Revision 1.15 2003/03/31 12:29:17 harald 00136 * Check NEBUF allocation 00137 * 00138 * Revision 1.14 2003/02/04 18:14:57 harald 00139 * Version 3 released 00140 * 00141 * Revision 1.13 2002/09/15 16:50:44 harald 00142 * *** empty log message *** 00143 * 00144 * Revision 1.12 2002/06/26 17:29:36 harald 00145 * First pre-release with 2.4 stack 00146 * 00147 */ 00148 00149 #include <cfg/os.h> 00150 #include <string.h> 00151 #include <errno.h> 00152 00153 #include <sys/heap.h> 00154 #include <sys/event.h> 00155 #include <sys/timer.h> 00156 00157 #include <netinet/in.h> 00158 #include <netinet/ip.h> 00159 #include <netinet/icmp.h> 00160 #include <netinet/ip_icmp.h> 00161 #include <netinet/ipcsum.h> 00162 #include <sys/socket.h> 00163 #include <netinet/tcp.h> 00164 #include <sys/thread.h> 00165 00166 #ifdef NUTDEBUG 00167 #include <net/netdebug.h> 00168 #endif 00169 00209 int NutTcpOutput(TCPSOCKET * sock, CONST uint8_t * data, uint16_t size) 00210 { 00211 NETBUF *nb; 00212 NETBUF *nb_clone = 0; 00213 TCPHDR *th; 00214 uint16_t csum; 00215 uint8_t hlen; 00216 00217 /* 00218 * Check if anything to send at all. 00219 */ 00220 if (size == 0 00221 && (sock->so_tx_flags & (SO_SYN | SO_FIN | SO_FORCE)) == 0) 00222 return 0; 00223 00224 /* 00225 * Build TCP header. Add room for MAXSEG option if this is a 00226 * SYN segment. 00227 */ 00228 hlen = sizeof(TCPHDR); 00229 if (sock->so_tx_flags & SO_SYN) 00230 hlen += 4; 00231 if ((nb = NutNetBufAlloc(0, NBAF_TRANSPORT, hlen)) == 0) { 00232 sock->so_last_error = ENOBUFS; 00233 return -1; 00234 } 00235 th = (TCPHDR *) nb->nb_tp.vp; 00236 th->th_sport = sock->so_local_port; 00237 th->th_dport = sock->so_remote_port; 00238 th->th_x2 = 0; 00239 th->th_off = hlen >> 2; 00240 00241 sock->so_tx_flags &= ~SO_FORCE; 00242 00243 /* 00244 * Process ACK flag. 00245 */ 00246 th->th_seq = htonl(sock->so_tx_nxt); 00247 if (sock->so_tx_flags & SO_ACK) { 00248 th->th_flags = TH_ACK; 00249 sock->so_tx_flags &= ~SO_ACK; 00250 th->th_ack = htonl(sock->so_rx_nxt); 00251 } else { 00252 th->th_flags = 0; 00253 th->th_ack = 0; 00254 } 00255 00256 /* 00257 * Any SYN is sent first. Add options too. We rely on the caller 00258 * not to send a SYN segment with data, because this may break 00259 * some old stacks. 00260 */ 00261 if (sock->so_tx_flags & SO_SYN) { 00262 uint8_t *cp; 00263 uint16_t n_mss = htons(sock->so_mss); 00264 00265 th->th_flags |= TH_SYN; 00266 sock->so_tx_flags &= ~SO_SYN; 00267 sock->so_tx_nxt++; 00268 00269 cp = (uint8_t *) (th + 1); 00270 *cp++ = TCPOPT_MAXSEG; 00271 *cp++ = TCPOLEN_MAXSEG; 00272 *cp++ = *(uint8_t *)&n_mss; 00273 *cp = *((uint8_t *)(&n_mss) + 1); 00274 } 00275 00276 /* 00277 * Next preference is sending data. Set PUSH flag. 00278 */ 00279 else if (size) { 00280 if ((nb = NutNetBufAlloc(nb, NBAF_APPLICATION, size)) == 0) { 00281 sock->so_last_error = ENOBUFS; 00282 return -1; 00283 } 00284 memcpy(nb->nb_ap.vp, (void *)data, size); 00285 sock->so_tx_nxt += size; 00286 th->th_flags |= TH_PUSH; 00287 } 00288 00289 /* 00290 * If all data sent, transmit any waiting FIN. 00291 */ 00292 else if (sock->so_tx_flags & SO_FIN) { 00293 th->th_flags |= TH_FIN; 00294 sock->so_tx_flags &= ~SO_FIN; 00295 sock->so_tx_nxt++; 00296 //@@@printf ("[%04X]TcpOutput: sending FIN\n", (u_short) sock); 00297 } 00298 00299 /* 00300 * We close our receiver window, if it is 00301 * below the maximum segment size. 00302 */ 00303 if (sock->so_rx_win < sock->so_mss) 00304 th->th_win = 0; 00305 else 00306 th->th_win = htons(sock->so_rx_win); 00307 th->th_sum = 0; 00308 th->th_urp = 0; 00309 00310 /* 00311 * Calculate TCP checksum. 00312 */ 00313 csum = 00314 NutIpPseudoChkSumPartial(sock->so_local_addr, sock->so_remote_addr, 00315 IPPROTO_TCP, 00316 htons(nb->nb_tp.sz + nb->nb_ap.sz)); 00317 csum = NutIpChkSumPartial(csum, th, nb->nb_tp.sz); 00318 th->th_sum = NutIpChkSum(csum, nb->nb_ap.vp, nb->nb_ap.sz); 00319 00320 #ifdef NUTDEBUG 00321 if (__tcp_trf) 00322 NutDumpTcpHeader(__tcp_trs, "OUT", sock, nb); 00323 #endif 00324 00325 /* 00326 * To avoid a race condition in tcp state machine, the segment is first 00327 * appended to the transmission que, and then sent to the network. 00328 */ 00329 00330 /* 00331 * Append the segment to our transmission queue. 00332 */ 00333 //@@@printf ("[%04X]TcpOutput: size: %u, flags: %u\n", (u_short) sock, size, th->th_flags); 00334 if (size || ((th->th_flags & (TH_FIN | TH_SYN)))) { 00335 //@@@printf ("[%04X]TcpOutput: appending nb to queue\n", (u_short) sock); 00336 NETBUF *nbp; 00337 00338 nb->nb_next = 0; 00339 if ((nbp = sock->so_tx_nbq) == 0) { 00340 sock->so_tx_nbq = nb; 00341 /* 00342 * First entry, so initialize our retransmission timer. 00343 * It is also set at various places in the state machine, 00344 * but here is the best central point to do it. We may 00345 * carefully check later, if we can remove some in the 00346 * state machine. 00347 */ 00348 sock->so_retran_time = (uint16_t) NutGetMillis() | 1; 00349 } 00350 else { 00351 while (nbp->nb_next) 00352 nbp = nbp->nb_next; 00353 nbp->nb_next = nb; 00354 } 00355 if (sock->so_rtt_seq == 0) 00356 sock->so_rtt_seq = ntohl (th->th_seq); 00357 nb_clone = NutNetBufClonePart(nb, 0); 00358 if (nb_clone == NULL) { 00359 sock->so_last_error = ENOBUFS; 00360 return -1; 00361 } 00362 } 00363 else 00364 nb_clone = nb; 00365 00366 /* 00367 * IP output might fail because of routing, ARP or network device 00368 * problems or because the system ran out of memory. 00369 */ 00370 if (NutIpOutput(IPPROTO_TCP, sock->so_remote_addr, nb_clone)) 00371 return -1; 00372 00373 NutNetBufFree (nb_clone); 00374 return 0; 00375 } 00376 00377 00395 int NutTcpReject(NETBUF * nb) 00396 { 00397 uint16_t csum; 00398 IPHDR *ih = (IPHDR *) nb->nb_nw.vp; 00399 TCPHDR *th = (TCPHDR *) nb->nb_tp.vp; 00400 00401 /* 00402 * Never send RST in response to RST. 00403 */ 00404 if (th->th_flags & TH_RST) { 00405 NutNetBufFree(nb); 00406 return 0; 00407 } 00408 00409 /* 00410 * Remove any application data and TCP header options. 00411 */ 00412 nb->nb_ap.sz = 0; 00413 nb->nb_tp.sz = sizeof(TCPHDR); 00414 00415 /* 00416 * Swap ports. 00417 */ 00418 csum = th->th_sport; 00419 th->th_sport = th->th_dport; 00420 th->th_dport = csum; 00421 00422 /* 00423 * If the incoming segment has an ACK field, the reset 00424 * takes its sequence number from the ACK field of the 00425 * segment, otherwise the reset has sequence number zero 00426 * and the ACK field is set to the sum of the sequence 00427 * number and segment length of the incoming segment. 00428 */ 00429 if (th->th_flags & TH_ACK) { 00430 th->th_flags = TH_RST; 00431 th->th_seq = th->th_ack; 00432 th->th_ack = 0; 00433 } else { 00434 if (th->th_flags & TH_SYN) 00435 th->th_ack = htonl(ntohl(th->th_seq) + 1); 00436 else 00437 th->th_ack = th->th_seq; 00438 th->th_seq = 0; 00439 th->th_flags = TH_RST | TH_ACK; 00440 } 00441 th->th_x2 = 0; 00442 th->th_off = sizeof(TCPHDR) / 4; 00443 th->th_win = 0; 00444 th->th_urp = 0; 00445 00446 /* 00447 * Calculate TCP checksum without application data. 00448 */ 00449 th->th_sum = 0; 00450 csum = 00451 NutIpPseudoChkSumPartial(ih->ip_dst, ih->ip_src, IPPROTO_TCP, 00452 htons(nb->nb_tp.sz)); 00453 th->th_sum = NutIpChkSum(csum, th, nb->nb_tp.sz); 00454 00455 /* 00456 * Sent segment back to the source. 00457 */ 00458 #ifdef NUTDEBUG 00459 if (__tcp_trf) 00460 NutDumpTcpHeader(__tcp_trs, "REJ", 0, nb); 00461 #endif 00462 if(NutIpOutput(IPPROTO_TCP, ih->ip_src, nb) == 0) 00463 NutNetBufFree(nb); 00464 return 0; 00465 }