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: tcpout.c,v $ 00096 * Revision 1.9 2008/04/06 13:29:01 haraldkipp 00097 * In unreliable or high traffic networks connections may suddenly freeze. 00098 * The problem is, that during overflows (happening every 65s) the 00099 * retransmission timer may be loaded with 0, which in turn disables all 00100 * outstanding retransmission. Applied fix contributed by Henrik Maier. 00101 * 00102 * Revision 1.8 2006/10/08 16:48:22 haraldkipp 00103 * Documentation fixed 00104 * 00105 * Revision 1.7 2006/03/02 19:57:34 haraldkipp 00106 * ICCARM insists on a (void *) typecast for the second parameter of memcpy(). 00107 * 00108 * Revision 1.6 2006/01/23 17:33:47 haraldkipp 00109 * Avoid memory alignment errors. 00110 * 00111 * Revision 1.5 2005/04/30 16:42:42 chaac 00112 * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG 00113 * is defined in NutConf, it will make effect where it is used. 00114 * 00115 * Revision 1.4 2005/04/05 17:41:48 haraldkipp 00116 * Header is dangerous when you forget to remove all debug outputs. 00117 * 00118 * Revision 1.3 2004/07/30 19:54:46 drsung 00119 * Some code of TCP stack redesigned. Round trip time calculation is now 00120 * supported. Fixed several bugs in TCP state machine. Now TCP connections 00121 * should be more reliable under heavy traffic or poor physical connections. 00122 * 00123 * Revision 1.2 2004/04/15 11:05:35 haraldkipp 00124 * Set retransmission timer on first transmit queue entry 00125 * 00126 * Revision 1.1.1.1 2003/05/09 14:41:40 haraldkipp 00127 * Initial using 3.2.1 00128 * 00129 * Revision 1.15 2003/03/31 12:29:17 harald 00130 * Check NEBUF allocation 00131 * 00132 * Revision 1.14 2003/02/04 18:14:57 harald 00133 * Version 3 released 00134 * 00135 * Revision 1.13 2002/09/15 16:50:44 harald 00136 * *** empty log message *** 00137 * 00138 * Revision 1.12 2002/06/26 17:29:36 harald 00139 * First pre-release with 2.4 stack 00140 * 00141 */ 00142 00143 #include <cfg/os.h> 00144 #include <string.h> 00145 00146 #include <sys/heap.h> 00147 #include <sys/event.h> 00148 #include <sys/timer.h> 00149 00150 #include <netinet/in.h> 00151 #include <netinet/ip.h> 00152 #include <netinet/icmp.h> 00153 #include <netinet/ip_icmp.h> 00154 #include <netinet/ipcsum.h> 00155 #include <sys/socket.h> 00156 #include <netinet/tcp.h> 00157 #include <net/errno.h> 00158 #include <sys/thread.h> 00159 00160 #ifdef NUTDEBUG 00161 #include <net/netdebug.h> 00162 #endif 00163 00203 int NutTcpOutput(TCPSOCKET * sock, CONST u_char * data, u_short size) 00204 { 00205 NETBUF *nb; 00206 NETBUF *nb_clone = 0; 00207 TCPHDR *th; 00208 u_short csum; 00209 u_char hlen; 00210 00211 /* 00212 * Check if anything to send at all. 00213 */ 00214 if (size == 0 00215 && (sock->so_tx_flags & (SO_SYN | SO_FIN | SO_FORCE)) == 0) 00216 return 0; 00217 00218 /* 00219 * Build TCP header. Add room for MAXSEG option if this is a 00220 * SYN segment. 00221 */ 00222 hlen = sizeof(TCPHDR); 00223 if (sock->so_tx_flags & SO_SYN) 00224 hlen += 4; 00225 if ((nb = NutNetBufAlloc(0, NBAF_TRANSPORT, hlen)) == 0) { 00226 sock->so_last_error = ENOBUFS; 00227 return -1; 00228 } 00229 th = (TCPHDR *) nb->nb_tp.vp; 00230 th->th_sport = sock->so_local_port; 00231 th->th_dport = sock->so_remote_port; 00232 th->th_x2 = 0; 00233 th->th_off = hlen >> 2; 00234 00235 sock->so_tx_flags &= ~SO_FORCE; 00236 00237 /* 00238 * Process ACK flag. 00239 */ 00240 th->th_seq = htonl(sock->so_tx_nxt); 00241 if (sock->so_tx_flags & SO_ACK) { 00242 th->th_flags = TH_ACK; 00243 sock->so_tx_flags &= ~SO_ACK; 00244 th->th_ack = htonl(sock->so_rx_nxt); 00245 } else { 00246 th->th_flags = 0; 00247 th->th_ack = 0; 00248 } 00249 00250 /* 00251 * Any SYN is sent first. Add options too. We rely on the caller 00252 * not to send a SYN segment with data, because this may break 00253 * some old stacks. 00254 */ 00255 if (sock->so_tx_flags & SO_SYN) { 00256 u_char *cp; 00257 u_short n_mss = htons(sock->so_mss); 00258 00259 th->th_flags |= TH_SYN; 00260 sock->so_tx_flags &= ~SO_SYN; 00261 sock->so_tx_nxt++; 00262 00263 cp = (u_char *) (th + 1); 00264 *cp++ = TCPOPT_MAXSEG; 00265 *cp++ = TCPOLEN_MAXSEG; 00266 *cp++ = *(u_char *)&n_mss; 00267 *cp = *((u_char *)(&n_mss) + 1); 00268 } 00269 00270 /* 00271 * Next preference is sending data. Set PUSH flag. 00272 */ 00273 else if (size) { 00274 if ((nb = NutNetBufAlloc(nb, NBAF_APPLICATION, size)) == 0) { 00275 sock->so_last_error = ENOBUFS; 00276 return -1; 00277 } 00278 memcpy(nb->nb_ap.vp, (void *)data, size); 00279 sock->so_tx_nxt += size; 00280 th->th_flags |= TH_PUSH; 00281 } 00282 00283 /* 00284 * If all data sent, transmit any waiting FIN. 00285 */ 00286 else if (sock->so_tx_flags & SO_FIN) { 00287 th->th_flags |= TH_FIN; 00288 sock->so_tx_flags &= ~SO_FIN; 00289 sock->so_tx_nxt++; 00290 //@@@printf ("[%04X]TcpOutput: sending FIN\n", (u_short) sock); 00291 } 00292 00293 /* 00294 * We close our receiver window, if it is 00295 * below the maximum segment size. 00296 */ 00297 if (sock->so_rx_win < sock->so_mss) 00298 th->th_win = 0; 00299 else 00300 th->th_win = htons(sock->so_rx_win); 00301 th->th_sum = 0; 00302 th->th_urp = 0; 00303 00304 /* 00305 * Calculate TCP checksum. 00306 */ 00307 csum = 00308 NutIpPseudoChkSumPartial(sock->so_local_addr, sock->so_remote_addr, 00309 IPPROTO_TCP, 00310 htons(nb->nb_tp.sz + nb->nb_ap.sz)); 00311 csum = NutIpChkSumPartial(csum, th, nb->nb_tp.sz); 00312 th->th_sum = NutIpChkSum(csum, nb->nb_ap.vp, nb->nb_ap.sz); 00313 00314 #ifdef NUTDEBUG 00315 if (__tcp_trf) 00316 NutDumpTcpHeader(__tcp_trs, "OUT", sock, nb); 00317 #endif 00318 00319 /* 00320 * To avoid a race condition in tcp state machine, the segment is first 00321 * appended to the transmission que, and then sent to the network. 00322 */ 00323 00324 /* 00325 * Append the segment to our transmission queue. 00326 */ 00327 //@@@printf ("[%04X]TcpOutput: size: %u, flags: %u\n", (u_short) sock, size, th->th_flags); 00328 if (size || ((th->th_flags & (TH_FIN | TH_SYN)))) { 00329 //@@@printf ("[%04X]TcpOutput: appending nb to queue\n", (u_short) sock); 00330 NETBUF *nbp; 00331 00332 nb->nb_next = 0; 00333 if ((nbp = sock->so_tx_nbq) == 0) { 00334 sock->so_tx_nbq = nb; 00335 /* 00336 * First entry, so initialize our retransmission timer. 00337 * It is also set at various places in the state machine, 00338 * but here is the best central point to do it. We may 00339 * carefully check later, if we can remove some in the 00340 * state machine. 00341 */ 00342 sock->so_retran_time = (u_short) NutGetMillis() | 1; 00343 } 00344 else { 00345 while (nbp->nb_next) 00346 nbp = nbp->nb_next; 00347 nbp->nb_next = nb; 00348 } 00349 if (sock->so_rtt_seq == 0) 00350 sock->so_rtt_seq = ntohl (th->th_seq); 00351 nb_clone = NutNetBufClone (nb); 00352 } 00353 else 00354 nb_clone = nb; 00355 00356 /* 00357 * IP output might fail because of routing, ARP or network device 00358 * problems or because the system ran out of memory. 00359 */ 00360 if (NutIpOutput(IPPROTO_TCP, sock->so_remote_addr, nb_clone)) 00361 return -1; 00362 00363 NutNetBufFree (nb_clone); 00364 return 0; 00365 } 00366 00367 00385 int NutTcpReject(NETBUF * nb) 00386 { 00387 u_short csum; 00388 IPHDR *ih = (IPHDR *) nb->nb_nw.vp; 00389 TCPHDR *th = (TCPHDR *) nb->nb_tp.vp; 00390 00391 /* 00392 * Never send RST in response to RST. 00393 */ 00394 if (th->th_flags & TH_RST) { 00395 NutNetBufFree(nb); 00396 return 0; 00397 } 00398 00399 /* 00400 * Remove any application data and TCP header options. 00401 */ 00402 nb->nb_ap.sz = 0; 00403 nb->nb_tp.sz = sizeof(TCPHDR); 00404 00405 /* 00406 * Swap ports. 00407 */ 00408 csum = th->th_sport; 00409 th->th_sport = th->th_dport; 00410 th->th_dport = csum; 00411 00412 /* 00413 * If the incoming segment has an ACK field, the reset 00414 * takes its sequence number from the ACK field of the 00415 * segment, otherwise the reset has sequence number zero 00416 * and the ACK field is set to the sum of the sequence 00417 * number and segment length of the incoming segment. 00418 */ 00419 if (th->th_flags & TH_ACK) { 00420 th->th_flags = TH_RST; 00421 th->th_seq = th->th_ack; 00422 th->th_ack = 0; 00423 } else { 00424 if (th->th_flags & TH_SYN) 00425 th->th_ack = htonl(ntohl(th->th_seq) + 1); 00426 else 00427 th->th_ack = th->th_seq; 00428 th->th_seq = 0; 00429 th->th_flags = TH_RST | TH_ACK; 00430 } 00431 th->th_x2 = 0; 00432 th->th_off = sizeof(TCPHDR) / 4; 00433 th->th_win = 0; 00434 th->th_urp = 0; 00435 00436 /* 00437 * Calculate TCP checksum without application data. 00438 */ 00439 th->th_sum = 0; 00440 csum = 00441 NutIpPseudoChkSumPartial(ih->ip_dst, ih->ip_src, IPPROTO_TCP, 00442 htons(nb->nb_tp.sz)); 00443 th->th_sum = NutIpChkSum(csum, th, nb->nb_tp.sz); 00444 00445 /* 00446 * Sent segment back to the source. 00447 */ 00448 #ifdef NUTDEBUG 00449 if (__tcp_trf) 00450 NutDumpTcpHeader(__tcp_trs, "REJ", 0, nb); 00451 #endif 00452 if(NutIpOutput(IPPROTO_TCP, ih->ip_src, nb) == 0) 00453 NutNetBufFree(nb); 00454 return 0; 00455 }