ifconfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. * Copyright (c) 1993 by Digital Equipment Corporation
  4. * Copyright (c) 1983, 1993 by The Regents of the University of California
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file net/ifconfig.c
  38. * \brief Network interface configuration functions.
  39. *
  40. * \verbatim
  41. * $Id: ifconfig.c 4636 2012-09-20 09:02:23Z haraldkipp $
  42. * \endverbatim
  43. */
  44. #include <cfg/os.h>
  45. #include <string.h>
  46. #include <io.h>
  47. #include <dev/ppp.h>
  48. #include <net/if.h>
  49. #include <net/ether.h>
  50. #include <net/route.h>
  51. #include <arpa/inet.h>
  52. #include <netinet/ppp_fsm.h>
  53. #include <netinet/if_ether.h>
  54. #include <netinet/igmp.h>
  55. #include <netinet/in.h>
  56. #include <sys/event.h>
  57. #include <dev/uart.h>
  58. #include <sys/confnet.h>
  59. #include <pro/dhcp.h>
  60. #ifdef NUTDEBUG
  61. #include <sys/osdebug.h>
  62. #include <net/netdebug.h>
  63. #endif
  64. /*!
  65. * \addtogroup xgIP
  66. */
  67. /*@{*/
  68. /*!
  69. * \brief Network interface setup.
  70. *
  71. * \param dev Identifies the network device to setup.
  72. * \param ip_addr Specified IP address in network byte order.
  73. * \param ip_mask Specified IP network mask in network byte order.
  74. * \param gateway Optional default gateway.
  75. *
  76. * \return 0 on success, -1 otherwise.
  77. *
  78. * \note Typical applications do not use this function, but call
  79. * NutDhcpIfConfig() or NutNetIfConfig().
  80. */
  81. int NutNetIfSetup(NUTDEVICE * dev, uint32_t ip_addr, uint32_t ip_mask, uint32_t gateway)
  82. {
  83. IFNET *nif;
  84. nif = dev->dev_icb;
  85. /*
  86. * Use specified or previously used IP address.
  87. */
  88. if (ip_addr == 0 && (ip_addr = confnet.cdn_ip_addr) == 0)
  89. return -1;
  90. nif->if_local_ip = ip_addr;
  91. /*
  92. * Use specified or default mask.
  93. */
  94. if (ip_mask == 0) {
  95. #ifdef __BIG_ENDIAN__
  96. ip_mask = 0xFFFFFF00UL;
  97. #else
  98. ip_mask = 0x00FFFFFFUL;
  99. #endif
  100. }
  101. nif->if_mask = ip_mask;
  102. /*
  103. * Add routing entries.
  104. */
  105. NutIpRouteAdd(ip_addr & ip_mask, ip_mask, 0, dev);
  106. if (gateway)
  107. NutIpRouteAdd(0, 0, gateway, dev);
  108. /*
  109. * Save configuration in EEPROM.
  110. */
  111. memcpy(confnet.cd_name, dev->dev_name, sizeof(confnet.cd_name));
  112. /* Never save an invalid MAC address. */
  113. if (!ETHER_IS_BROADCAST(nif->if_mac) && !ETHER_IS_ZERO(nif->if_mac)) {
  114. memcpy(confnet.cdn_mac, nif->if_mac, sizeof(nif->if_mac));
  115. }
  116. confnet.cdn_ip_addr = ip_addr;
  117. confnet.cdn_ip_mask = ip_mask;
  118. /*
  119. * Set gateway, if one was provided by the caller. Remove
  120. * gateway, if it's outside of our network.
  121. */
  122. if (gateway || (confnet.cdn_gateway & ip_mask) != (ip_addr & ip_mask))
  123. confnet.cdn_gateway = gateway;
  124. return NutNetSaveConfig();
  125. }
  126. /*!
  127. * \brief Configure a network interface.
  128. *
  129. * Devices must have been registered by NutRegisterDevice() before
  130. * calling this function.
  131. *
  132. * For Ethernet devices applications may alternatively call
  133. * NutDhcpIfConfig(), which allows automatic configuration by DHCP or
  134. * the so called ARP method.
  135. *
  136. * \param name Name of the device to configure.
  137. * \param params Pointer to interface specific parameters. For Ethernet
  138. * interfaces this parameter may be a pointer to a buffer
  139. * containing the 6 byte long MAC address. This will
  140. * override the MAC address stored in the non-volatile
  141. * configuration memory. If this memory is uninitialized
  142. * or not available, the MAC address must be specified.
  143. * For PPP client interfaces this parameter is NULL,
  144. * while PPP server interfaces expect a pointer to a
  145. * \ref PPPSERVER_CFG structure containing the server
  146. * configuration.
  147. * \param ip_addr Specified IP address in network byte order. This must
  148. * be a unique address within the Internet. If you do not
  149. * directly communicate with other Internet hosts, you can
  150. * use a locally assigned address. With PPP interfaces this
  151. * may be set to 0.0.0.0, in which case the remote peer
  152. * will be queried for an IP address.
  153. * \param ip_mask Specified IP network mask in network byte order.
  154. * Typical Ethernet networks use 255.255.255.0, which
  155. * allows up to 254 hosts. For PPP interfaces 255.255.255.255
  156. * is the default.
  157. *
  158. * \return 0 on success, -1 otherwise.
  159. *
  160. * \note The whole interface configuration has become a mess over
  161. * the years and need a major redesign.
  162. */
  163. int NutNetIfConfig(const char *name, void *params, uint32_t ip_addr, uint32_t ip_mask)
  164. {
  165. return NutNetIfConfig2(name, params, ip_addr, ip_mask, 0);
  166. }
  167. /*!
  168. * \brief Configure a network interface including the default gateway.
  169. *
  170. * Devices must have been registered by NutRegisterDevice() before
  171. * calling this function.
  172. *
  173. * For Ethernet devices applications may alternatively call
  174. * NutDhcpIfConfig(), which allows automatic configuration by DHCP or
  175. * the so called ARP method.
  176. *
  177. * \param name Name of the device to configure.
  178. * \param params Pointer to interface specific parameters. For Ethernet
  179. * interfaces this parameter may be a pointer to a buffer
  180. * containing the 6 byte long MAC address. This will
  181. * override the MAC address stored in the non-volatile
  182. * configuration memory. If this memory is uninitialized
  183. * or not available, the MAC address must be specified.
  184. * For PPP client interfaces this parameter is NULL,
  185. * while PPP server interfaces expect a pointer to a
  186. * \ref PPPSERVER_CFG structure containing the server
  187. * configuration.
  188. * \param ip_addr Specified IP address in network byte order. This must
  189. * be a unique address within the Internet. If you do not
  190. * directly communicate with other Internet hosts, you can
  191. * use a locally assigned address. With PPP interfaces this
  192. * may be set to 0.0.0.0, in which case the remote peer
  193. * will be queried for an IP address.
  194. * \param ip_mask Specified IP network mask in network byte order.
  195. * Typical Ethernet networks use 255.255.255.0, which
  196. * allows up to 254 hosts. For PPP interfaces 255.255.255.255
  197. * is the default.
  198. * \param gateway Specified IP address of gateway or next router in LAN.
  199. *
  200. * \return 0 on success, -1 otherwise.
  201. *
  202. * \note I do not like this function, because setting a gateway should
  203. * be handled by NutIpRouteAdd(). It's not yet deprecated, but I
  204. * recommend not to use it in application code.
  205. */
  206. int NutNetIfConfig2(const char *name, void *params, uint32_t ip_addr, uint32_t ip_mask, uint32_t gateway)
  207. {
  208. NUTDEVICE *dev;
  209. IFNET *nif;
  210. /*
  211. * Check if this is a registered network device.
  212. */
  213. if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
  214. return -1;
  215. /*
  216. * Setup Ethernet interfaces.
  217. */
  218. nif = dev->dev_icb;
  219. if (nif->if_type == IFT_ETHER) {
  220. /* Check if ioctl is supported. */
  221. if (dev->dev_ioctl) {
  222. uint32_t flags;
  223. /* Driver has ioctl, use it. */
  224. dev->dev_ioctl(dev, SIOCGIFFLAGS, &flags);
  225. dev->dev_ioctl(dev, SIOCSIFADDR, params);
  226. flags |= IFF_UP;
  227. dev->dev_ioctl(dev, SIOCSIFFLAGS, &flags);
  228. } else {
  229. /* No ioctl, set MAC address to start driver. */
  230. memcpy(nif->if_mac, params, sizeof(nif->if_mac));
  231. }
  232. return NutNetIfSetup(dev, ip_addr, ip_mask, gateway);
  233. }
  234. /*
  235. * Setup PPP interfaces.
  236. */
  237. else if (nif->if_type == IFT_PPP) {
  238. PPPDCB *dcb = dev->dev_dcb;
  239. PPPSERVER_CFG *ppsc = params;
  240. /*
  241. * Set the interface's IP address, make sure that the state
  242. * change queue is empty and switch hardware driver into
  243. * network mode.
  244. */
  245. dcb->dcb_local_ip = ip_addr;
  246. dcb->dcb_ip_mask = ip_mask ? ip_mask : 0xffffffff;
  247. if (ppsc) {
  248. dcb->dcb_remote_ip = ppsc->ppsc_remote_ip;
  249. }
  250. NutEventBroadcast(&dcb->dcb_state_chg);
  251. _ioctl(dcb->dcb_fd, HDLC_SETIFNET, &dev);
  252. /*
  253. * Wait for network layer up and configure the interface on
  254. * success.
  255. */
  256. if (NutEventWait(&dcb->dcb_state_chg, 60000) == 0 && dcb->dcb_ipcp_state == PPPS_OPENED) {
  257. return NutNetIfSetup(dev, dcb->dcb_local_ip, dcb->dcb_ip_mask, dcb->dcb_remote_ip);
  258. }
  259. }
  260. return -1;
  261. }
  262. int NutNetIfAddMcastAddr(const char *name, uint32_t ip_addr)
  263. {
  264. NUTDEVICE *dev;
  265. IFNET *nif;
  266. int rc = -1;
  267. /*
  268. * Check if this is a registered network device.
  269. */
  270. if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
  271. return -1;
  272. /*
  273. * Setup multicast address
  274. */
  275. nif = dev->dev_icb;
  276. if (nif->if_type == IFT_ETHER) {
  277. /* Check if ioctl is supported. */
  278. if (dev->dev_ioctl) {
  279. /* Driver has ioctl, use it. */
  280. rc = dev->dev_ioctl(dev, SIOCADDMULTI, &ip_addr);
  281. if ((rc == 0) && (ip_addr != INADDR_ALLHOSTS_GROUP)) {
  282. NutIgmpJoinGroup(dev, ip_addr);
  283. }
  284. }
  285. }
  286. return rc;
  287. }
  288. int NutNetIfDelMcastAddr(const char *name, uint32_t ip_addr)
  289. {
  290. NUTDEVICE *dev;
  291. IFNET *nif;
  292. int rc = -1;
  293. /*
  294. * Check if this is a registered network device.
  295. */
  296. if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
  297. return -1;
  298. /*
  299. * Setup multicast address
  300. */
  301. nif = dev->dev_icb;
  302. if (nif->if_type == IFT_ETHER) {
  303. /* Check if ioctl is supported. */
  304. if (dev->dev_ioctl) {
  305. /* Driver has ioctl, use it. */
  306. rc = dev->dev_ioctl(dev, SIOCDELMULTI, &ip_addr);
  307. if ((rc == 0) && (ip_addr != INADDR_ALLHOSTS_GROUP)) {
  308. NutIgmpLeaveGroup(dev, ip_addr);
  309. }
  310. }
  311. }
  312. return rc;
  313. }
  314. /*@}*/