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: ifconfig.c,v $ 00081 * Revision 1.8 2005/04/30 16:42:42 chaac 00082 * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG 00083 * is defined in NutConf, it will make effect where it is used. 00084 * 00085 * Revision 1.7 2004/03/18 11:08:32 haraldkipp 00086 * Comments updated 00087 * 00088 * Revision 1.6 2004/03/18 10:49:59 haraldkipp 00089 * Comments updated 00090 * 00091 * Revision 1.5 2004/03/16 16:48:45 haraldkipp 00092 * Added Jan Dubiec's H8/300 port. 00093 * 00094 * Revision 1.4 2004/03/08 11:24:19 haraldkipp 00095 * Ugly PPP activation hack replaced by HDLC ioctl. 00096 * 00097 * Revision 1.3 2004/02/25 16:38:20 haraldkipp 00098 * Do not use zero MAC 00099 * 00100 * Revision 1.2 2004/02/02 18:54:22 drsung 00101 * gateway ip address was not set, if static network configuration from EEPROM is used. 00102 * 00103 * Revision 1.1.1.1 2003/05/09 14:41:29 haraldkipp 00104 * Initial using 3.2.1 00105 * 00106 * Revision 1.24 2003/05/06 18:02:14 harald 00107 * PPP hack for simple UART support 00108 * 00109 * Revision 1.23 2003/04/21 17:02:22 harald 00110 * Added comments. 00111 * New PPP state machine implemented. 00112 * Auto config moved to DHCP. 00113 * 00114 * Revision 1.22 2003/03/31 12:03:15 harald 00115 * Adapt new PPP structure 00116 * 00117 * Revision 1.21 2003/02/04 18:14:57 harald 00118 * Version 3 released 00119 * 00120 * Revision 1.20 2003/01/14 16:43:40 harald 00121 * *** empty log message *** 00122 * 00123 * Revision 1.19 2002/11/02 15:18:27 harald 00124 * Library dependencies removed 00125 * 00126 * Revision 1.18 2002/10/31 16:08:09 harald 00127 * *** empty log message *** 00128 * 00129 * Revision 1.17 2002/10/29 15:29:17 harald 00130 * PPP support 00131 * 00132 * Revision 1.16 2002/07/03 16:41:32 harald 00133 * Bugfix: Failed to store gateway 00134 * 00135 * Revision 1.15 2002/06/26 17:29:36 harald 00136 * First pre-release with 2.4 stack 00137 * 00138 */ 00139 00140 #include <cfg/os.h> 00141 #include <string.h> 00142 #include <io.h> 00143 00144 #include <dev/ppp.h> 00145 00146 #include <net/ether.h> 00147 #include <net/route.h> 00148 #include <arpa/inet.h> 00149 #include <netinet/ppp_fsm.h> 00150 00151 #include <sys/event.h> 00152 #include <dev/uart.h> 00153 #include <sys/confnet.h> 00154 00155 #include <pro/dhcp.h> 00156 00157 #ifdef NUTDEBUG 00158 #include <sys/osdebug.h> 00159 #include <net/netdebug.h> 00160 #endif 00161 00166 00180 int NutNetIfSetup(NUTDEVICE * dev, u_long ip_addr, u_long ip_mask, u_long gateway) 00181 { 00182 IFNET *nif; 00183 00184 nif = dev->dev_icb; 00185 00186 /* 00187 * Use specified or previously used IP address. 00188 */ 00189 if (ip_addr == 0 && (ip_addr = confnet.cdn_ip_addr) == 0) 00190 return -1; 00191 nif->if_local_ip = ip_addr; 00192 00193 /* 00194 * Use specified or default mask. 00195 */ 00196 if (ip_mask == 0) 00197 ip_mask = inet_addr("255.255.255.0"); 00198 nif->if_mask = ip_mask; 00199 00200 /* 00201 * Add routing entries. 00202 */ 00203 NutIpRouteAdd(ip_addr & ip_mask, ip_mask, 0, dev); 00204 if (gateway) 00205 NutIpRouteAdd(0, 0, gateway, dev); 00206 00207 /* 00208 * Save configuration in EEPROM. 00209 */ 00210 memcpy(confnet.cd_name, dev->dev_name, sizeof(confnet.cd_name)); 00211 /* Never save MAC address 0. */ 00212 if (nif->if_mac[0] || nif->if_mac[1] || nif->if_mac[2] || nif->if_mac[3] || nif->if_mac[4] || nif->if_mac[5]) { 00213 memcpy(confnet.cdn_mac, nif->if_mac, sizeof(nif->if_mac)); 00214 } 00215 confnet.cdn_ip_addr = ip_addr; 00216 confnet.cdn_ip_mask = ip_mask; 00217 00218 /* 00219 * Set gateway, if one was provided by the caller. Remove 00220 * gateway, if it's outside of our network. 00221 */ 00222 if (gateway || (confnet.cdn_gateway & ip_mask) != (ip_addr & ip_mask)) 00223 confnet.cdn_gateway = gateway; 00224 00225 return NutNetSaveConfig(); 00226 } 00227 00263 int NutNetIfConfig(CONST char *name, void *params, u_long ip_addr, u_long ip_mask) 00264 { 00265 return NutNetIfConfig2(name, params, ip_addr, ip_mask, 0); 00266 } 00267 00298 int NutNetIfConfig2(CONST char *name, void *params, u_long ip_addr, u_long ip_mask, u_long gateway) 00299 { 00300 NUTDEVICE *dev; 00301 IFNET *nif; 00302 00303 /* 00304 * Check if this is a registered network device. 00305 */ 00306 if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET) 00307 return -1; 00308 00309 /* 00310 * Setup Ethernet interfaces. 00311 */ 00312 nif = dev->dev_icb; 00313 if (nif->if_type == IFT_ETHER) { 00314 memcpy(nif->if_mac, params, sizeof(nif->if_mac)); 00315 return NutNetIfSetup(dev, ip_addr, ip_mask, gateway); 00316 } 00317 00318 /* 00319 * Setup PPP interfaces. 00320 */ 00321 else if (nif->if_type == IFT_PPP) { 00322 PPPDCB *dcb = dev->dev_dcb; 00323 00324 /* 00325 * Set the interface's IP address, make sure that the state 00326 * change queue is empty and switch hardware driver into 00327 * network mode. 00328 */ 00329 dcb->dcb_local_ip = ip_addr; 00330 dcb->dcb_ip_mask = ip_mask ? ip_mask : 0xffffffff; 00331 NutEventBroadcast(&dcb->dcb_state_chg); 00332 _ioctl(dcb->dcb_fd, HDLC_SETIFNET, &dev); 00333 00334 /* 00335 * Wait for network layer up and configure the interface on 00336 * success. 00337 */ 00338 if (NutEventWait(&dcb->dcb_state_chg, 60000) == 0 && dcb->dcb_ipcp_state == PPPS_OPENED) { 00339 return NutNetIfSetup(dev, dcb->dcb_local_ip, dcb->dcb_ip_mask, dcb->dcb_remote_ip); 00340 } 00341 } 00342 return -1; 00343 } 00344