Nut/OS  4.10.3
API Reference
ipout.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2007 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.7  2008/08/11 07:00:30  haraldkipp
00097  * BSD types replaced by stdint types (feature request #1282721).
00098  *
00099  * Revision 1.6  2007/05/02 11:18:32  haraldkipp
00100  * IGMP support added. Incomplete.
00101  *
00102  * Revision 1.5  2006/10/17 11:05:03  haraldkipp
00103  * Failed ARP requests are no longer classified as fatal transmission errors.
00104  *
00105  * Revision 1.4  2005/04/08 15:20:50  olereinhardt
00106  * added <sys/types.h> (__APPLE__) and <netinet/in.h> (__linux__)
00107  * for htons and simmilar.
00108  *
00109  * Revision 1.3  2004/10/10 16:37:03  drsung
00110  * Detection of directed broadcasts to local network added.
00111  *
00112  * Revision 1.2  2003/07/20 16:00:23  haraldkipp
00113  * Memory hole fixed.
00114  *
00115  * Revision 1.1.1.1  2003/05/09 14:41:33  haraldkipp
00116  * Initial using 3.2.1
00117  *
00118  * Revision 1.17  2003/04/01 18:31:47  harald
00119  * *** empty log message ***
00120  *
00121  * Revision 1.16  2003/03/31 12:28:10  harald
00122  * Check NEBUF allocation
00123  *
00124  * Revision 1.15  2003/02/04 18:14:57  harald
00125  * Version 3 released
00126  *
00127  * Revision 1.14  2003/01/14 16:44:08  harald
00128  * *** empty log message ***
00129  *
00130  * Revision 1.13  2002/10/29 15:32:14  harald
00131  * PPP support
00132  *
00133  * Revision 1.12  2002/09/15 16:49:42  harald
00134  * *** empty log message ***
00135  *
00136  * Revision 1.11  2002/06/26 17:29:36  harald
00137  * First pre-release with 2.4 stack
00138  *
00139  */
00140 
00141 #include <string.h>
00142 
00143 #include <sys/types.h>
00144 #include <net/ether.h>
00145 #include <net/route.h>
00146 #include <netinet/if_ether.h>
00147 #include <netinet/if_ppp.h>
00148 #include <netinet/ipcsum.h>
00149 #include <netinet/ip.h>
00150 #include <netinet/in.h>
00155 
00182 int NutIpOutput(uint8_t proto, uint32_t dest, NETBUF * nb)
00183 {
00184     uint8_t ha[6];
00185     IPHDR_OPT *ip;
00186     NUTDEVICE *dev;
00187     IFNET *nif;
00188     uint32_t gate;
00189 
00190     if (proto != IPPROTO_IGMP) {
00191         if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(IPHDR))) == 0)
00192             return -1;
00193     } else {
00194         if ((nb = NutNetBufAlloc(nb, NBAF_NETWORK, sizeof(IPHDR_OPT))) == 0)
00195             return -1;
00196     }
00197 
00198     /*
00199      * Set those items in the IP header, which are common for
00200      * all interfaces.
00201      */
00202     ip = nb->nb_nw.vp;
00203     ip->ip_v = 4;
00204     if (proto == IPPROTO_IGMP) {
00205         ip->ip_hl = sizeof(IPHDR_OPT) / 4;
00206     } else {
00207         ip->ip_hl = sizeof(IPHDR) / 4;
00208     }
00209     ip->ip_tos = 0;
00210     ip->ip_len = htons(nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz);
00211     ip->ip_off = 0;
00212     if (proto == IPPROTO_IGMP) {
00213         ip->ip_ttl = 1;
00214     } else {
00215         ip->ip_ttl = 0x40;
00216     }
00217     ip->ip_p = proto;
00218     ip->ip_dst = dest;
00219 
00220     if (proto == IPPROTO_IGMP) {
00221         /* Router Alert Option */
00222         ip->ip_option = htonl(0x94040000);
00223     }
00224 
00225 
00226     /*
00227      * Limited broadcasts are sent on all network interfaces.
00228      * See RFC 919.
00229      */
00230     if ((dest == 0xffffffff) || (IP_IS_MULTICAST(dest))) {
00231         if (dest == 0xffffffff) {
00232             /* Broadcast */
00233             memset(ha, 0xff, sizeof(ha));
00234         } else {
00235             /* Multicast */
00236             ha[0] = 0x01;
00237             ha[1] = 0x00;
00238             ha[2] = 0x5E;
00239             ha[3] = ((uint8_t *) & dest)[1] & 0x7f;
00240             ha[4] = ((uint8_t *) & dest)[2];
00241             ha[5] = ((uint8_t *) & dest)[3];
00242         }
00243 
00244         for (dev = nutDeviceList; dev; dev = dev->dev_next) {
00245             if (dev->dev_type == IFTYP_NET) {
00246                 /*
00247                  * Set remaining IP header items using a NETBUF clone and calculate 
00248                  * the checksum.
00249                  */
00250                 int rc = 0;
00251                 NETBUF *nb_clone = NutNetBufClonePart(nb, NBAF_NETWORK);
00252 
00253                 nif = dev->dev_icb;
00254                 ip = nb_clone->nb_nw.vp;
00255                 ip->ip_id = htons(nif->if_pkt_id);
00256                 nif->if_pkt_id++;
00257                 ip->ip_src = nif->if_local_ip;
00258                 ip->ip_ttl = 1;
00259                 ip->ip_sum = 0;
00260                 ip->ip_sum = NutIpChkSum(0, nb_clone->nb_nw.vp, nb_clone->nb_nw.sz);
00261                 if (nif->if_type == IFT_ETHER)
00262                     rc = (*nif->if_output) (dev, ETHERTYPE_IP, ha, nb_clone);
00263                 else if (nif->if_type == IFT_PPP)
00264                     rc = (*nif->if_output) (dev, PPP_IP, 0, nb_clone);
00265                 if (rc == 0) {
00266                     NutNetBufFree(nb_clone);
00267                 }
00268             }
00269         }
00270         return 0;
00271     }
00272 
00273     /*
00274      * Get destination's route. This will also return the proper 
00275      * interface.
00276      */
00277     if ((dev = NutIpRouteQuery(dest, &gate)) == 0) {
00278         NutNetBufFree(nb);
00279         return -1;
00280     }
00281 
00282     /*
00283      * Set remaining IP header items and calculate the checksum.
00284      */
00285     nif = dev->dev_icb;
00286     ip->ip_id = htons(nif->if_pkt_id);
00287     nif->if_pkt_id++;
00288     ip->ip_src = nif->if_local_ip;
00289     ip->ip_sum = 0;
00290     ip->ip_sum = NutIpChkSum(0, nb->nb_nw.vp, nb->nb_nw.sz);
00291 
00292     /*
00293      * On Ethernet we query the MAC address of our next hop,
00294      * which might be the destination or the gateway to this
00295      * destination.
00296      */
00297     if (nif->if_type == IFT_ETHER) {
00298         /* 
00299          * Detect directed broadcasts for the local network. In this
00300          * case don't send ARP queries, but send directly to MAC broadcast
00301          * address. 
00302          */
00303         if ((gate == 0) && ((dest | nif->if_mask) == 0xffffffff)) {
00304             memset(ha, 0xff, sizeof(ha));
00305         } else if (NutArpCacheQuery(dev, gate ? gate : dest, ha)) {
00306             /* Note, that a failed ARP request is not considered a 
00307                transmission error. It might be caused by a simple 
00308                packet loss. */
00309             return 0;
00310         }
00311         return (*nif->if_output) (dev, ETHERTYPE_IP, ha, nb);
00312     } else if (nif->if_type == IFT_PPP)
00313         return (*nif->if_output) (dev, PPP_IP, 0, nb);
00314 
00315     NutNetBufFree(nb);
00316     return -1;
00317 }
00318