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