tcpout.c

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

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/