Nut/OS  4.10.3
API Reference
ifconfig.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2004 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) 1983, 1993 by
00034  *  The Regents of the University of California.  All rights reserved.
00035  *
00036  * Redistribution and use in source and binary forms, with or without
00037  * modification, are permitted provided that the following conditions
00038  * are met:
00039  * 1. Redistributions of source code must retain the above copyright
00040  *    notice, this list of conditions and the following disclaimer.
00041  * 2. Redistributions in binary form must reproduce the above copyright
00042  *    notice, this list of conditions and the following disclaimer in the
00043  *    documentation and/or other materials provided with the distribution.
00044  * 3. Neither the name of the University nor the names of its contributors
00045  *    may be used to endorse or promote products derived from this software
00046  *    without specific prior written permission.
00047  *
00048  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00049  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00050  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00051  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00052  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00053  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00054  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00055  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00056  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00057  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00058  * SUCH DAMAGE.
00059  * -
00060  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00061  *
00062  * Permission to use, copy, modify, and distribute this software for any
00063  * purpose with or without fee is hereby granted, provided that the above
00064  * copyright notice and this permission notice appear in all copies, and that
00065  * the name of Digital Equipment Corporation not be used in advertising or
00066  * publicity pertaining to distribution of the document or software without
00067  * specific, written prior permission.
00068  * 
00069  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00070  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00071  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00072  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00073  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00074  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00075  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00076  * SOFTWARE.
00077  */
00078 
00079 /*
00080  * $Log$
00081  * Revision 1.11  2008/08/28 11:07:45  haraldkipp
00082  * Make use of ioctl if the driver supports this.
00083  *
00084  * Revision 1.10  2008/08/11 07:00:29  haraldkipp
00085  * BSD types replaced by stdint types (feature request #1282721).
00086  *
00087  * Revision 1.9  2008/07/14 13:10:31  haraldkipp
00088  * Added macros to determine Ethernet address types.
00089  *
00090  * Revision 1.8  2005/04/30 16:42:42  chaac
00091  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00092  * is defined in NutConf, it will make effect where it is used.
00093  *
00094  * Revision 1.7  2004/03/18 11:08:32  haraldkipp
00095  * Comments updated
00096  *
00097  * Revision 1.6  2004/03/18 10:49:59  haraldkipp
00098  * Comments updated
00099  *
00100  * Revision 1.5  2004/03/16 16:48:45  haraldkipp
00101  * Added Jan Dubiec's H8/300 port.
00102  *
00103  * Revision 1.4  2004/03/08 11:24:19  haraldkipp
00104  * Ugly PPP activation hack replaced by HDLC ioctl.
00105  *
00106  * Revision 1.3  2004/02/25 16:38:20  haraldkipp
00107  * Do not use zero MAC
00108  *
00109  * Revision 1.2  2004/02/02 18:54:22  drsung
00110  * gateway ip address was not set, if static network configuration from EEPROM is used.
00111  *
00112  * Revision 1.1.1.1  2003/05/09 14:41:29  haraldkipp
00113  * Initial using 3.2.1
00114  *
00115  * Revision 1.24  2003/05/06 18:02:14  harald
00116  * PPP hack for simple UART support
00117  *
00118  * Revision 1.23  2003/04/21 17:02:22  harald
00119  * Added comments.
00120  * New PPP state machine implemented.
00121  * Auto config moved to DHCP.
00122  *
00123  * Revision 1.22  2003/03/31 12:03:15  harald
00124  * Adapt new PPP structure
00125  *
00126  * Revision 1.21  2003/02/04 18:14:57  harald
00127  * Version 3 released
00128  *
00129  * Revision 1.20  2003/01/14 16:43:40  harald
00130  * *** empty log message ***
00131  *
00132  * Revision 1.19  2002/11/02 15:18:27  harald
00133  * Library dependencies removed
00134  *
00135  * Revision 1.18  2002/10/31 16:08:09  harald
00136  * *** empty log message ***
00137  *
00138  * Revision 1.17  2002/10/29 15:29:17  harald
00139  * PPP support
00140  *
00141  * Revision 1.16  2002/07/03 16:41:32  harald
00142  * Bugfix: Failed to store gateway
00143  *
00144  * Revision 1.15  2002/06/26 17:29:36  harald
00145  * First pre-release with 2.4 stack
00146  *
00147  */
00148 
00149 #include <cfg/os.h>
00150 #include <string.h>
00151 #include <io.h>
00152 
00153 #include <dev/ppp.h>
00154 
00155 #include <net/if.h>
00156 #include <net/ether.h>
00157 #include <net/route.h>
00158 #include <arpa/inet.h>
00159 #include <netinet/ppp_fsm.h>
00160 #include <netinet/if_ether.h>
00161 #include <netinet/igmp.h>
00162 #include <netinet/in.h>
00163 
00164 #include <sys/event.h>
00165 #include <dev/uart.h>
00166 #include <sys/confnet.h>
00167 
00168 #include <pro/dhcp.h>
00169 
00170 #ifdef NUTDEBUG
00171 #include <sys/osdebug.h>
00172 #include <net/netdebug.h>
00173 #endif
00174 
00179 
00193 int NutNetIfSetup(NUTDEVICE * dev, uint32_t ip_addr, uint32_t ip_mask, uint32_t gateway)
00194 {
00195     IFNET *nif;
00196 
00197     nif = dev->dev_icb;
00198 
00199     /*
00200      * Use specified or previously used IP address.
00201      */
00202     if (ip_addr == 0 && (ip_addr = confnet.cdn_ip_addr) == 0)
00203         return -1;
00204     nif->if_local_ip = ip_addr;
00205 
00206     /*
00207      * Use specified or default mask.
00208      */
00209     if (ip_mask == 0)
00210         ip_mask = inet_addr("255.255.255.0");
00211     nif->if_mask = ip_mask;
00212 
00213     /*
00214      * Add routing entries.
00215      */
00216     NutIpRouteAdd(ip_addr & ip_mask, ip_mask, 0, dev);
00217     if (gateway)
00218         NutIpRouteAdd(0, 0, gateway, dev);
00219 
00220     /*
00221      * Save configuration in EEPROM.
00222      */
00223     memcpy(confnet.cd_name, dev->dev_name, sizeof(confnet.cd_name));
00224     /* Never save an invalid MAC address. */
00225     if (ETHER_IS_UNICAST(nif->if_mac)) {
00226         memcpy(confnet.cdn_mac, nif->if_mac, sizeof(nif->if_mac));
00227     }
00228     confnet.cdn_ip_addr = ip_addr;
00229     confnet.cdn_ip_mask = ip_mask;
00230 
00231     /*
00232      * Set gateway, if one was provided by the caller. Remove
00233      * gateway, if it's outside of our network.
00234      */
00235     if (gateway || (confnet.cdn_gateway & ip_mask) != (ip_addr & ip_mask))
00236         confnet.cdn_gateway = gateway;
00237 
00238     return NutNetSaveConfig();
00239 }
00240 
00276 int NutNetIfConfig(CONST char *name, void *params, uint32_t ip_addr, uint32_t ip_mask)
00277 {
00278     return NutNetIfConfig2(name, params, ip_addr, ip_mask, 0);
00279 }
00280 
00311 int NutNetIfConfig2(CONST char *name, void *params, uint32_t ip_addr, uint32_t ip_mask, uint32_t gateway)
00312 {
00313     NUTDEVICE *dev;
00314     IFNET *nif;
00315 
00316     /*
00317      * Check if this is a registered network device.
00318      */
00319     if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
00320         return -1;
00321 
00322     /*
00323      * Setup Ethernet interfaces.
00324      */
00325     nif = dev->dev_icb;
00326     if (nif->if_type == IFT_ETHER) {
00327         /* Check if ioctl is supported. */
00328         if (dev->dev_ioctl) {
00329             uint32_t flags;
00330 
00331             /* Driver has ioctl, use it. */
00332             dev->dev_ioctl(dev, SIOCGIFFLAGS, &flags);
00333             dev->dev_ioctl(dev, SIOCSIFADDR, params);
00334             flags |= IFF_UP;
00335             dev->dev_ioctl(dev, SIOCSIFFLAGS, &flags);
00336         } else {
00337             /* No ioctl, set MAC address to start driver. */
00338             memcpy(nif->if_mac, params, sizeof(nif->if_mac));
00339         }
00340         return NutNetIfSetup(dev, ip_addr, ip_mask, gateway);
00341     }
00342 
00343     /*
00344      * Setup PPP interfaces.
00345      */
00346     else if (nif->if_type == IFT_PPP) {
00347         PPPDCB *dcb = dev->dev_dcb;
00348 
00349         /*
00350          * Set the interface's IP address, make sure that the state
00351          * change queue is empty and switch hardware driver into 
00352          * network mode.
00353          */
00354         dcb->dcb_local_ip = ip_addr;
00355         dcb->dcb_ip_mask = ip_mask ? ip_mask : 0xffffffff;
00356         NutEventBroadcast(&dcb->dcb_state_chg);
00357         _ioctl(dcb->dcb_fd, HDLC_SETIFNET, &dev);
00358 
00359         /*
00360          * Wait for network layer up and configure the interface on
00361          * success.
00362          */
00363         if (NutEventWait(&dcb->dcb_state_chg, 60000) == 0 && dcb->dcb_ipcp_state == PPPS_OPENED) {
00364             return NutNetIfSetup(dev, dcb->dcb_local_ip, dcb->dcb_ip_mask, dcb->dcb_remote_ip);
00365         }
00366     }
00367     return -1;
00368 }
00369 
00370 int NutNetIfAddMcastAddr(CONST char *name, uint32_t ip_addr)
00371 {
00372     NUTDEVICE *dev;
00373     IFNET *nif;
00374     int rc = -1;
00375 
00376     /*
00377      * Check if this is a registered network device.
00378      */
00379     if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
00380         return -1;
00381 
00382     /*
00383      * Setup multicast address
00384      */
00385     nif = dev->dev_icb;
00386     if (nif->if_type == IFT_ETHER) {
00387         /* Check if ioctl is supported. */
00388         if (dev->dev_ioctl) {
00389             /* Driver has ioctl, use it. */
00390             rc = dev->dev_ioctl(dev, SIOCADDMULTI, &ip_addr);
00391             if ((rc == 0) && (ip_addr != INADDR_ALLHOSTS_GROUP)) {
00392                 NutIgmpJoinGroup(dev, ip_addr);
00393             }    
00394         }
00395     }
00396 
00397     return rc;
00398 }
00399 
00400 int NutNetIfDelMcastAddr(CONST char *name, uint32_t ip_addr)
00401 {
00402     NUTDEVICE *dev;
00403     IFNET *nif;
00404     int rc = -1;
00405 
00406     /*
00407      * Check if this is a registered network device.
00408      */
00409     if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
00410         return -1;
00411 
00412     /*
00413      * Setup multicast address
00414      */
00415     nif = dev->dev_icb;
00416     if (nif->if_type == IFT_ETHER) {
00417         /* Check if ioctl is supported. */
00418         if (dev->dev_ioctl) {
00419             /* Driver has ioctl, use it. */
00420             rc = dev->dev_ioctl(dev, SIOCDELMULTI, &ip_addr);
00421             if ((rc == 0) && (ip_addr != INADDR_ALLHOSTS_GROUP)) {
00422                 NutIgmpLeaveGroup(dev, ip_addr);
00423             }    
00424         }
00425     }
00426 
00427     return rc;
00428 }
00429 
00430 
00431