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