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.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 00162 #include <sys/event.h> 00163 #include <dev/uart.h> 00164 #include <sys/confnet.h> 00165 00166 #include <pro/dhcp.h> 00167 00168 #ifdef NUTDEBUG 00169 #include <sys/osdebug.h> 00170 #include <net/netdebug.h> 00171 #endif 00172 00177 00191 int NutNetIfSetup(NUTDEVICE * dev, uint32_t ip_addr, uint32_t ip_mask, uint32_t gateway) 00192 { 00193 IFNET *nif; 00194 00195 nif = dev->dev_icb; 00196 00197 /* 00198 * Use specified or previously used IP address. 00199 */ 00200 if (ip_addr == 0 && (ip_addr = confnet.cdn_ip_addr) == 0) 00201 return -1; 00202 nif->if_local_ip = ip_addr; 00203 00204 /* 00205 * Use specified or default mask. 00206 */ 00207 if (ip_mask == 0) 00208 ip_mask = inet_addr("255.255.255.0"); 00209 nif->if_mask = ip_mask; 00210 00211 /* 00212 * Add routing entries. 00213 */ 00214 NutIpRouteAdd(ip_addr & ip_mask, ip_mask, 0, dev); 00215 if (gateway) 00216 NutIpRouteAdd(0, 0, gateway, dev); 00217 00218 /* 00219 * Save configuration in EEPROM. 00220 */ 00221 memcpy(confnet.cd_name, dev->dev_name, sizeof(confnet.cd_name)); 00222 /* Never save an invalid MAC address. */ 00223 if (ETHER_IS_UNICAST(nif->if_mac)) { 00224 memcpy(confnet.cdn_mac, nif->if_mac, sizeof(nif->if_mac)); 00225 } 00226 confnet.cdn_ip_addr = ip_addr; 00227 confnet.cdn_ip_mask = ip_mask; 00228 00229 /* 00230 * Set gateway, if one was provided by the caller. Remove 00231 * gateway, if it's outside of our network. 00232 */ 00233 if (gateway || (confnet.cdn_gateway & ip_mask) != (ip_addr & ip_mask)) 00234 confnet.cdn_gateway = gateway; 00235 00236 return NutNetSaveConfig(); 00237 } 00238 00274 int NutNetIfConfig(CONST char *name, void *params, uint32_t ip_addr, uint32_t ip_mask) 00275 { 00276 return NutNetIfConfig2(name, params, ip_addr, ip_mask, 0); 00277 } 00278 00309 int NutNetIfConfig2(CONST char *name, void *params, uint32_t ip_addr, uint32_t ip_mask, uint32_t gateway) 00310 { 00311 NUTDEVICE *dev; 00312 IFNET *nif; 00313 00314 /* 00315 * Check if this is a registered network device. 00316 */ 00317 if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET) 00318 return -1; 00319 00320 /* 00321 * Setup Ethernet interfaces. 00322 */ 00323 nif = dev->dev_icb; 00324 if (nif->if_type == IFT_ETHER) { 00325 /* Check if ioctl is supported. */ 00326 if (dev->dev_ioctl) { 00327 uint32_t flags; 00328 00329 /* Driver has ioctl, use it. */ 00330 dev->dev_ioctl(dev, SIOCGIFFLAGS, &flags); 00331 dev->dev_ioctl(dev, SIOCSIFADDR, params); 00332 flags |= IFF_UP; 00333 dev->dev_ioctl(dev, SIOCSIFFLAGS, &flags); 00334 } else { 00335 /* No ioctl, set MAC address to start driver. */ 00336 memcpy(nif->if_mac, params, sizeof(nif->if_mac)); 00337 } 00338 return NutNetIfSetup(dev, ip_addr, ip_mask, gateway); 00339 } 00340 00341 /* 00342 * Setup PPP interfaces. 00343 */ 00344 else if (nif->if_type == IFT_PPP) { 00345 PPPDCB *dcb = dev->dev_dcb; 00346 00347 /* 00348 * Set the interface's IP address, make sure that the state 00349 * change queue is empty and switch hardware driver into 00350 * network mode. 00351 */ 00352 dcb->dcb_local_ip = ip_addr; 00353 dcb->dcb_ip_mask = ip_mask ? ip_mask : 0xffffffff; 00354 NutEventBroadcast(&dcb->dcb_state_chg); 00355 _ioctl(dcb->dcb_fd, HDLC_SETIFNET, &dev); 00356 00357 /* 00358 * Wait for network layer up and configure the interface on 00359 * success. 00360 */ 00361 if (NutEventWait(&dcb->dcb_state_chg, 60000) == 0 && dcb->dcb_ipcp_state == PPPS_OPENED) { 00362 return NutNetIfSetup(dev, dcb->dcb_local_ip, dcb->dcb_ip_mask, dcb->dcb_remote_ip); 00363 } 00364 } 00365 return -1; 00366 } 00367