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

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