ipin.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: ipin.c,v $
00096  * Revision 1.12.2.1  2008/10/04 17:41:17  haraldkipp
00097  * Added size checks on incoming telegrams.
00098  *
00099  * Revision 1.12  2008/04/18 13:13:11  haraldkipp
00100  * Using fast ints.
00101  *
00102  * Revision 1.11  2007/07/09 16:20:19  olereinhardt
00103  * 2007-07-09  Ole Reinhardt <ole.reinhardt@embedded-it.de>
00104  *         * net/Makefile: Commented in igmp_in.c and igmp_out.c again
00105  *         * net/ipin.c: igmp support reenabled
00106  *
00107  * Revision 1.10  2007/07/09 15:59:00  olereinhardt
00108  * 2007-07-09  Ole Reinhardt <ole.reinhardt@embedded-it.de>
00109  *  * ipin.c: commented out igmp support as long as it does not compile
00110  *
00111  * Revision 1.9  2007/05/02 11:18:32  haraldkipp
00112  * IGMP support added. Incomplete.
00113  *
00114  * Revision 1.8  2007/03/23 12:43:50  haraldkipp
00115  * ARP method wasn't actually disabled by default. Fixed.
00116  *
00117  * Revision 1.7  2006/09/05 12:35:39  haraldkipp
00118  * DHCP servers may probe an IP/MAC relationship by sending an
00119  * ICMP request. This triggered the Nut/Net ARP method and
00120  * terminated the DHCP client, leaving the system with default
00121  * configurations of the network mask (255.255.255.0) and
00122  * default gateway (none). The rarely used ARP method is now
00123  * disabled by default.
00124  *
00125  * Revision 1.6  2006/07/10 17:46:59  haraldkipp
00126  * Now really like Jan suggested to fix it.
00127  *
00128  * Revision 1.5  2006/07/10 08:49:47  haraldkipp
00129  * Do not respond to broadcasts with unknown protocols. Many thanks to Jan.
00130  *
00131  * Revision 1.4  2005/06/05 16:48:26  haraldkipp
00132  * Additional parameter enables NutUdpInput() to avoid responding to UDP
00133  * broadcasts with ICMP unreachable messages. Fixes bug #1215192.
00134  *
00135  * Revision 1.3  2004/12/16 18:48:50  haraldkipp
00136  * Added Damian Slee's IP filter function.
00137  *
00138  * Revision 1.2  2004/02/02 18:59:25  drsung
00139  * Some more ICMP support added.
00140  *
00141  * Revision 1.1.1.1  2003/05/09 14:41:32  haraldkipp
00142  * Initial using 3.2.1
00143  *
00144  * Revision 1.16  2003/05/06 18:14:18  harald
00145  * Allow incoming DHCP telegrams, even if not broadcasted
00146  *
00147  * Revision 1.15  2003/03/31 12:26:05  harald
00148  * Accept masks with all bits set
00149  *
00150  * Revision 1.14  2003/02/04 18:14:57  harald
00151  * Version 3 released
00152  *
00153  * Revision 1.13  2002/06/26 17:29:36  harald
00154  * First pre-release with 2.4 stack
00155  *
00156  */
00157 
00158 #include <cfg/ip.h>
00159 
00160 #include <net/route.h>
00161 #include <netinet/in.h>
00162 #include <netinet/ip.h>
00163 #include <netinet/icmp.h>
00164 #include <netinet/ip_icmp.h>
00165 #include <netinet/igmp.h>
00166 #include <netinet/udp.h>
00167 #include <sys/socket.h>
00168 #include <arpa/inet.h>
00169 
00174 
00175 static NutIpFilterFunc NutIpFilter;
00176 
00190 void NutIpSetInputFilter(NutIpFilterFunc callbackFunc)
00191 {
00192     NutIpFilter = callbackFunc;
00193 }
00194 
00208 void NutIpInput(NUTDEVICE * dev, NETBUF * nb)
00209 {
00210     IPHDR *ip;
00211     u_short ip_hdrlen;
00212     u_long dst;
00213     uint_fast8_t bcast;
00214     IFNET *nif;
00215 
00216     ip = nb->nb_nw.vp;
00217 
00218     /*
00219      * Silently discard datagrams of different IP version as well as
00220      * fragmented or filtered datagrams.
00221      */
00222     if (ip->ip_v != IPVERSION ||        /* Version check. */
00223         (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0 ||      /* Fragmentation. */
00224         (NutIpFilter && NutIpFilter(ip->ip_src))) {     /* Filter. */
00225         NutNetBufFree(nb);
00226         return;
00227     }
00228 
00229     /*
00230      * IP header length is given in 32-bit fields. Calculate the size in
00231      * bytes and make sure that the header we know will fit in.
00232      */
00233     ip_hdrlen = ip->ip_hl * 4;
00234     if (ip_hdrlen < sizeof(IPHDR) || ip_hdrlen > nb->nb_nw.sz) {
00235         NutNetBufFree(nb);
00236         return;
00237     }
00238 
00239     /*
00240      * No checksum calculation on incoming datagrams!
00241      */
00242 
00243     /*
00244      * Check for broadcast.
00245      */
00246     dst = ip->ip_dst;
00247     nif = dev->dev_icb;
00248 
00249     if (dst == INADDR_BROADCAST || 
00250         (nif->if_local_ip && nif->if_mask != INADDR_BROADCAST && (dst | nif->if_mask) == INADDR_BROADCAST)) {
00251         bcast = 1;
00252     }
00253 
00254     /*
00255      * Check for multicast.
00256      */
00257     else if (IN_MULTICAST(dst)) {
00258         MCASTENTRY *mca;
00259 
00260         for (mca = nif->if_mcast; mca; mca = mca->mca_next) {
00261             if (dst == mca->mca_ip) {
00262                 break;
00263             }
00264         }
00265         if (mca == NULL) {
00266             NutNetBufFree(nb);
00267             return;
00268         }
00269         bcast = 2;
00270     }
00271 
00272     /*
00273      * Packet is unicast.
00274      */
00275     else {
00276         bcast = 0;
00277 
00278 #ifdef NUTIPCONF_ICMP_ARPMETHOD
00279         /*
00280          * Silently discard datagrams for other destinations.
00281          * However, if we haven't got an IP address yet, we
00282          * allow ICMP datagrams to support dynamic IP ARP method,
00283          * if this option had been enabled.
00284          */
00285         if (nif->if_local_ip == 0 && ip->ip_p == IPPROTO_ICMP && (dst & 0xff000000) != 0xff000000 && (dst & 0xff000000) != 0) {
00286             NutNetIfSetup(dev, dst, 0, 0);
00287         }
00288 #endif
00289         if (nif->if_local_ip && (dst == 0 || dst != nif->if_local_ip)) {
00290             NutNetBufFree(nb);
00291             return;
00292         }
00293     }
00294 
00295     /* Check the IP data length. */
00296     nb->nb_tp.sz = htons(ip->ip_len);
00297     if (nb->nb_tp.sz < ip_hdrlen || nb->nb_tp.sz > nb->nb_nw.sz) {
00298         NutNetBufFree(nb);
00299         return;
00300     }
00301     nb->nb_nw.sz = ip_hdrlen;
00302     nb->nb_tp.sz -= ip_hdrlen;
00303     if (nb->nb_tp.sz) {
00304         nb->nb_tp.vp = ((char *) ip) + ip_hdrlen;
00305     }
00306 
00307     switch (ip->ip_p) {
00308     case IPPROTO_ICMP:
00309         NutIcmpInput(dev, nb);
00310         break;
00311     case IPPROTO_UDP:
00312         NutUdpInput(nb, bcast);
00313         break;
00314     case IPPROTO_TCP:
00315         /*
00316          * Silently discard TCP broadcasts.
00317          */
00318         if (bcast)
00319             NutNetBufFree(nb);
00320         else
00321             NutTcpInput(nb);
00322         break;
00323     case IPPROTO_IGMP:
00324         NutIgmpInput(dev, nb);
00325         break;
00326     default:
00327         /* Unkown protocol, send ICMP destination (protocol)
00328          * unreachable message.
00329          */
00330         if (bcast || !NutIcmpResponse(ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, nb))
00331             NutNetBufFree(nb);
00332         break;
00333     }
00334 }
00335 

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