Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Committer:
DieterGraef
Date:
Thu Jun 23 09:04:23 2016 +0000
Revision:
1:28ba13dd96f7
Parent:
0:d26c1b55cfca
corrected MAC issue. The MAC is now 02:00:00:xx:xx:xx where xx is the sum over the unique device register

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:d26c1b55cfca 1 /**
DieterGraef 0:d26c1b55cfca 2 * @file
DieterGraef 0:d26c1b55cfca 3 * lwIP network interface abstraction
DieterGraef 0:d26c1b55cfca 4 *
DieterGraef 0:d26c1b55cfca 5 */
DieterGraef 0:d26c1b55cfca 6
DieterGraef 0:d26c1b55cfca 7 /*
DieterGraef 0:d26c1b55cfca 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
DieterGraef 0:d26c1b55cfca 9 * All rights reserved.
DieterGraef 0:d26c1b55cfca 10 *
DieterGraef 0:d26c1b55cfca 11 * Redistribution and use in source and binary forms, with or without modification,
DieterGraef 0:d26c1b55cfca 12 * are permitted provided that the following conditions are met:
DieterGraef 0:d26c1b55cfca 13 *
DieterGraef 0:d26c1b55cfca 14 * 1. Redistributions of source code must retain the above copyright notice,
DieterGraef 0:d26c1b55cfca 15 * this list of conditions and the following disclaimer.
DieterGraef 0:d26c1b55cfca 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
DieterGraef 0:d26c1b55cfca 17 * this list of conditions and the following disclaimer in the documentation
DieterGraef 0:d26c1b55cfca 18 * and/or other materials provided with the distribution.
DieterGraef 0:d26c1b55cfca 19 * 3. The name of the author may not be used to endorse or promote products
DieterGraef 0:d26c1b55cfca 20 * derived from this software without specific prior written permission.
DieterGraef 0:d26c1b55cfca 21 *
DieterGraef 0:d26c1b55cfca 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
DieterGraef 0:d26c1b55cfca 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
DieterGraef 0:d26c1b55cfca 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
DieterGraef 0:d26c1b55cfca 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
DieterGraef 0:d26c1b55cfca 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
DieterGraef 0:d26c1b55cfca 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
DieterGraef 0:d26c1b55cfca 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
DieterGraef 0:d26c1b55cfca 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
DieterGraef 0:d26c1b55cfca 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
DieterGraef 0:d26c1b55cfca 31 * OF SUCH DAMAGE.
DieterGraef 0:d26c1b55cfca 32 *
DieterGraef 0:d26c1b55cfca 33 * This file is part of the lwIP TCP/IP stack.
DieterGraef 0:d26c1b55cfca 34 *
DieterGraef 0:d26c1b55cfca 35 * Author: Adam Dunkels <adam@sics.se>
DieterGraef 0:d26c1b55cfca 36 *
DieterGraef 0:d26c1b55cfca 37 */
DieterGraef 0:d26c1b55cfca 38
DieterGraef 0:d26c1b55cfca 39 #include "lwip/opt.h"
DieterGraef 0:d26c1b55cfca 40
DieterGraef 0:d26c1b55cfca 41 #include "lwip/def.h"
DieterGraef 0:d26c1b55cfca 42 #include "lwip/ip_addr.h"
DieterGraef 0:d26c1b55cfca 43 #include "lwip/netif.h"
DieterGraef 0:d26c1b55cfca 44 #include "lwip/tcp_impl.h"
DieterGraef 0:d26c1b55cfca 45 #include "lwip/snmp.h"
DieterGraef 0:d26c1b55cfca 46 #include "lwip/igmp.h"
DieterGraef 0:d26c1b55cfca 47 #include "netif/etharp.h"
DieterGraef 0:d26c1b55cfca 48 #include "lwip/stats.h"
DieterGraef 0:d26c1b55cfca 49 #if ENABLE_LOOPBACK
DieterGraef 0:d26c1b55cfca 50 #include "lwip/sys.h"
DieterGraef 0:d26c1b55cfca 51 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
DieterGraef 0:d26c1b55cfca 52 #include "lwip/tcpip.h"
DieterGraef 0:d26c1b55cfca 53 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
DieterGraef 0:d26c1b55cfca 54 #endif /* ENABLE_LOOPBACK */
DieterGraef 0:d26c1b55cfca 55
DieterGraef 0:d26c1b55cfca 56 #if LWIP_AUTOIP
DieterGraef 0:d26c1b55cfca 57 #include "lwip/autoip.h"
DieterGraef 0:d26c1b55cfca 58 #endif /* LWIP_AUTOIP */
DieterGraef 0:d26c1b55cfca 59 #if LWIP_DHCP
DieterGraef 0:d26c1b55cfca 60 #include "lwip/dhcp.h"
DieterGraef 0:d26c1b55cfca 61 #endif /* LWIP_DHCP */
DieterGraef 0:d26c1b55cfca 62
DieterGraef 0:d26c1b55cfca 63 #if LWIP_NETIF_STATUS_CALLBACK
DieterGraef 0:d26c1b55cfca 64 #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
DieterGraef 0:d26c1b55cfca 65 #else
DieterGraef 0:d26c1b55cfca 66 #define NETIF_STATUS_CALLBACK(n)
DieterGraef 0:d26c1b55cfca 67 #endif /* LWIP_NETIF_STATUS_CALLBACK */
DieterGraef 0:d26c1b55cfca 68
DieterGraef 0:d26c1b55cfca 69 #if LWIP_NETIF_LINK_CALLBACK
DieterGraef 0:d26c1b55cfca 70 #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
DieterGraef 0:d26c1b55cfca 71 #else
DieterGraef 0:d26c1b55cfca 72 #define NETIF_LINK_CALLBACK(n)
DieterGraef 0:d26c1b55cfca 73 #endif /* LWIP_NETIF_LINK_CALLBACK */
DieterGraef 0:d26c1b55cfca 74
DieterGraef 0:d26c1b55cfca 75 struct netif *netif_list;
DieterGraef 0:d26c1b55cfca 76 struct netif *netif_default;
DieterGraef 0:d26c1b55cfca 77
DieterGraef 0:d26c1b55cfca 78 static u8_t netif_num;
DieterGraef 0:d26c1b55cfca 79
DieterGraef 0:d26c1b55cfca 80 #if LWIP_HAVE_LOOPIF
DieterGraef 0:d26c1b55cfca 81 static struct netif loop_netif;
DieterGraef 0:d26c1b55cfca 82
DieterGraef 0:d26c1b55cfca 83 /**
DieterGraef 0:d26c1b55cfca 84 * Initialize a lwip network interface structure for a loopback interface
DieterGraef 0:d26c1b55cfca 85 *
DieterGraef 0:d26c1b55cfca 86 * @param netif the lwip network interface structure for this loopif
DieterGraef 0:d26c1b55cfca 87 * @return ERR_OK if the loopif is initialized
DieterGraef 0:d26c1b55cfca 88 * ERR_MEM if private data couldn't be allocated
DieterGraef 0:d26c1b55cfca 89 */
DieterGraef 0:d26c1b55cfca 90 static err_t
DieterGraef 0:d26c1b55cfca 91 netif_loopif_init(struct netif *netif)
DieterGraef 0:d26c1b55cfca 92 {
DieterGraef 0:d26c1b55cfca 93 /* initialize the snmp variables and counters inside the struct netif
DieterGraef 0:d26c1b55cfca 94 * ifSpeed: no assumption can be made!
DieterGraef 0:d26c1b55cfca 95 */
DieterGraef 0:d26c1b55cfca 96 NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
DieterGraef 0:d26c1b55cfca 97
DieterGraef 0:d26c1b55cfca 98 netif->name[0] = 'l';
DieterGraef 0:d26c1b55cfca 99 netif->name[1] = 'o';
DieterGraef 0:d26c1b55cfca 100 netif->output = netif_loop_output;
DieterGraef 0:d26c1b55cfca 101 return ERR_OK;
DieterGraef 0:d26c1b55cfca 102 }
DieterGraef 0:d26c1b55cfca 103 #endif /* LWIP_HAVE_LOOPIF */
DieterGraef 0:d26c1b55cfca 104
DieterGraef 0:d26c1b55cfca 105 void
DieterGraef 0:d26c1b55cfca 106 netif_init(void)
DieterGraef 0:d26c1b55cfca 107 {
DieterGraef 0:d26c1b55cfca 108 #if LWIP_HAVE_LOOPIF
DieterGraef 0:d26c1b55cfca 109 ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
DieterGraef 0:d26c1b55cfca 110 IP4_ADDR(&loop_gw, 127,0,0,1);
DieterGraef 0:d26c1b55cfca 111 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
DieterGraef 0:d26c1b55cfca 112 IP4_ADDR(&loop_netmask, 255,0,0,0);
DieterGraef 0:d26c1b55cfca 113
DieterGraef 0:d26c1b55cfca 114 #if NO_SYS
DieterGraef 0:d26c1b55cfca 115 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
DieterGraef 0:d26c1b55cfca 116 #else /* NO_SYS */
DieterGraef 0:d26c1b55cfca 117 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
DieterGraef 0:d26c1b55cfca 118 #endif /* NO_SYS */
DieterGraef 0:d26c1b55cfca 119 netif_set_up(&loop_netif);
DieterGraef 0:d26c1b55cfca 120
DieterGraef 0:d26c1b55cfca 121 #endif /* LWIP_HAVE_LOOPIF */
DieterGraef 0:d26c1b55cfca 122 }
DieterGraef 0:d26c1b55cfca 123
DieterGraef 0:d26c1b55cfca 124 /**
DieterGraef 0:d26c1b55cfca 125 * Add a network interface to the list of lwIP netifs.
DieterGraef 0:d26c1b55cfca 126 *
DieterGraef 0:d26c1b55cfca 127 * @param netif a pre-allocated netif structure
DieterGraef 0:d26c1b55cfca 128 * @param ipaddr IP address for the new netif
DieterGraef 0:d26c1b55cfca 129 * @param netmask network mask for the new netif
DieterGraef 0:d26c1b55cfca 130 * @param gw default gateway IP address for the new netif
DieterGraef 0:d26c1b55cfca 131 * @param state opaque data passed to the new netif
DieterGraef 0:d26c1b55cfca 132 * @param init callback function that initializes the interface
DieterGraef 0:d26c1b55cfca 133 * @param input callback function that is called to pass
DieterGraef 0:d26c1b55cfca 134 * ingress packets up in the protocol layer stack.
DieterGraef 0:d26c1b55cfca 135 *
DieterGraef 0:d26c1b55cfca 136 * @return netif, or NULL if failed.
DieterGraef 0:d26c1b55cfca 137 */
DieterGraef 0:d26c1b55cfca 138 struct netif *
DieterGraef 0:d26c1b55cfca 139 netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
DieterGraef 0:d26c1b55cfca 140 ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
DieterGraef 0:d26c1b55cfca 141 {
DieterGraef 0:d26c1b55cfca 142
DieterGraef 0:d26c1b55cfca 143 LWIP_ASSERT("No init function given", init != NULL);
DieterGraef 0:d26c1b55cfca 144
DieterGraef 0:d26c1b55cfca 145 /* reset new interface configuration state */
DieterGraef 0:d26c1b55cfca 146 ip_addr_set_zero(&netif->ip_addr);
DieterGraef 0:d26c1b55cfca 147 ip_addr_set_zero(&netif->netmask);
DieterGraef 0:d26c1b55cfca 148 ip_addr_set_zero(&netif->gw);
DieterGraef 0:d26c1b55cfca 149 netif->flags = 0;
DieterGraef 0:d26c1b55cfca 150 #if LWIP_DHCP
DieterGraef 0:d26c1b55cfca 151 /* netif not under DHCP control by default */
DieterGraef 0:d26c1b55cfca 152 netif->dhcp = NULL;
DieterGraef 0:d26c1b55cfca 153 #endif /* LWIP_DHCP */
DieterGraef 0:d26c1b55cfca 154 #if LWIP_AUTOIP
DieterGraef 0:d26c1b55cfca 155 /* netif not under AutoIP control by default */
DieterGraef 0:d26c1b55cfca 156 netif->autoip = NULL;
DieterGraef 0:d26c1b55cfca 157 #endif /* LWIP_AUTOIP */
DieterGraef 0:d26c1b55cfca 158 #if LWIP_NETIF_STATUS_CALLBACK
DieterGraef 0:d26c1b55cfca 159 netif->status_callback = NULL;
DieterGraef 0:d26c1b55cfca 160 #endif /* LWIP_NETIF_STATUS_CALLBACK */
DieterGraef 0:d26c1b55cfca 161 #if LWIP_NETIF_LINK_CALLBACK
DieterGraef 0:d26c1b55cfca 162 netif->link_callback = NULL;
DieterGraef 0:d26c1b55cfca 163 #endif /* LWIP_NETIF_LINK_CALLBACK */
DieterGraef 0:d26c1b55cfca 164 #if LWIP_IGMP
DieterGraef 0:d26c1b55cfca 165 netif->igmp_mac_filter = NULL;
DieterGraef 0:d26c1b55cfca 166 #endif /* LWIP_IGMP */
DieterGraef 0:d26c1b55cfca 167 #if ENABLE_LOOPBACK
DieterGraef 0:d26c1b55cfca 168 netif->loop_first = NULL;
DieterGraef 0:d26c1b55cfca 169 netif->loop_last = NULL;
DieterGraef 0:d26c1b55cfca 170 #endif /* ENABLE_LOOPBACK */
DieterGraef 0:d26c1b55cfca 171
DieterGraef 0:d26c1b55cfca 172 /* remember netif specific state information data */
DieterGraef 0:d26c1b55cfca 173 netif->state = state;
DieterGraef 0:d26c1b55cfca 174 netif->num = netif_num++;
DieterGraef 0:d26c1b55cfca 175 netif->input = input;
DieterGraef 0:d26c1b55cfca 176 NETIF_SET_HWADDRHINT(netif, NULL);
DieterGraef 0:d26c1b55cfca 177 #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
DieterGraef 0:d26c1b55cfca 178 netif->loop_cnt_current = 0;
DieterGraef 0:d26c1b55cfca 179 #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
DieterGraef 0:d26c1b55cfca 180
DieterGraef 0:d26c1b55cfca 181 netif_set_addr(netif, ipaddr, netmask, gw);
DieterGraef 0:d26c1b55cfca 182
DieterGraef 0:d26c1b55cfca 183 /* call user specified initialization function for netif */
DieterGraef 0:d26c1b55cfca 184 if (init(netif) != ERR_OK) {
DieterGraef 0:d26c1b55cfca 185 return NULL;
DieterGraef 0:d26c1b55cfca 186 }
DieterGraef 0:d26c1b55cfca 187
DieterGraef 0:d26c1b55cfca 188 /* add this netif to the list */
DieterGraef 0:d26c1b55cfca 189 netif->next = netif_list;
DieterGraef 0:d26c1b55cfca 190 netif_list = netif;
DieterGraef 0:d26c1b55cfca 191 snmp_inc_iflist();
DieterGraef 0:d26c1b55cfca 192
DieterGraef 0:d26c1b55cfca 193 #if LWIP_IGMP
DieterGraef 0:d26c1b55cfca 194 /* start IGMP processing */
DieterGraef 0:d26c1b55cfca 195 if (netif->flags & NETIF_FLAG_IGMP) {
DieterGraef 0:d26c1b55cfca 196 igmp_start(netif);
DieterGraef 0:d26c1b55cfca 197 }
DieterGraef 0:d26c1b55cfca 198 #endif /* LWIP_IGMP */
DieterGraef 0:d26c1b55cfca 199
DieterGraef 0:d26c1b55cfca 200 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
DieterGraef 0:d26c1b55cfca 201 netif->name[0], netif->name[1]));
DieterGraef 0:d26c1b55cfca 202 ip_addr_debug_print(NETIF_DEBUG, ipaddr);
DieterGraef 0:d26c1b55cfca 203 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
DieterGraef 0:d26c1b55cfca 204 ip_addr_debug_print(NETIF_DEBUG, netmask);
DieterGraef 0:d26c1b55cfca 205 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
DieterGraef 0:d26c1b55cfca 206 ip_addr_debug_print(NETIF_DEBUG, gw);
DieterGraef 0:d26c1b55cfca 207 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
DieterGraef 0:d26c1b55cfca 208 return netif;
DieterGraef 0:d26c1b55cfca 209 }
DieterGraef 0:d26c1b55cfca 210
DieterGraef 0:d26c1b55cfca 211 /**
DieterGraef 0:d26c1b55cfca 212 * Change IP address configuration for a network interface (including netmask
DieterGraef 0:d26c1b55cfca 213 * and default gateway).
DieterGraef 0:d26c1b55cfca 214 *
DieterGraef 0:d26c1b55cfca 215 * @param netif the network interface to change
DieterGraef 0:d26c1b55cfca 216 * @param ipaddr the new IP address
DieterGraef 0:d26c1b55cfca 217 * @param netmask the new netmask
DieterGraef 0:d26c1b55cfca 218 * @param gw the new default gateway
DieterGraef 0:d26c1b55cfca 219 */
DieterGraef 0:d26c1b55cfca 220 void
DieterGraef 0:d26c1b55cfca 221 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
DieterGraef 0:d26c1b55cfca 222 ip_addr_t *gw)
DieterGraef 0:d26c1b55cfca 223 {
DieterGraef 0:d26c1b55cfca 224 netif_set_ipaddr(netif, ipaddr);
DieterGraef 0:d26c1b55cfca 225 netif_set_netmask(netif, netmask);
DieterGraef 0:d26c1b55cfca 226 netif_set_gw(netif, gw);
DieterGraef 0:d26c1b55cfca 227 }
DieterGraef 0:d26c1b55cfca 228
DieterGraef 0:d26c1b55cfca 229 /**
DieterGraef 0:d26c1b55cfca 230 * Remove a network interface from the list of lwIP netifs.
DieterGraef 0:d26c1b55cfca 231 *
DieterGraef 0:d26c1b55cfca 232 * @param netif the network interface to remove
DieterGraef 0:d26c1b55cfca 233 */
DieterGraef 0:d26c1b55cfca 234 void
DieterGraef 0:d26c1b55cfca 235 netif_remove(struct netif *netif)
DieterGraef 0:d26c1b55cfca 236 {
DieterGraef 0:d26c1b55cfca 237 if (netif == NULL) {
DieterGraef 0:d26c1b55cfca 238 return;
DieterGraef 0:d26c1b55cfca 239 }
DieterGraef 0:d26c1b55cfca 240
DieterGraef 0:d26c1b55cfca 241 #if LWIP_IGMP
DieterGraef 0:d26c1b55cfca 242 /* stop IGMP processing */
DieterGraef 0:d26c1b55cfca 243 if (netif->flags & NETIF_FLAG_IGMP) {
DieterGraef 0:d26c1b55cfca 244 igmp_stop(netif);
DieterGraef 0:d26c1b55cfca 245 }
DieterGraef 0:d26c1b55cfca 246 #endif /* LWIP_IGMP */
DieterGraef 0:d26c1b55cfca 247 if (netif_is_up(netif)) {
DieterGraef 0:d26c1b55cfca 248 /* set netif down before removing (call callback function) */
DieterGraef 0:d26c1b55cfca 249 netif_set_down(netif);
DieterGraef 0:d26c1b55cfca 250 }
DieterGraef 0:d26c1b55cfca 251
DieterGraef 0:d26c1b55cfca 252 snmp_delete_ipaddridx_tree(netif);
DieterGraef 0:d26c1b55cfca 253
DieterGraef 0:d26c1b55cfca 254 /* is it the first netif? */
DieterGraef 0:d26c1b55cfca 255 if (netif_list == netif) {
DieterGraef 0:d26c1b55cfca 256 netif_list = netif->next;
DieterGraef 0:d26c1b55cfca 257 } else {
DieterGraef 0:d26c1b55cfca 258 /* look for netif further down the list */
DieterGraef 0:d26c1b55cfca 259 struct netif * tmpNetif;
DieterGraef 0:d26c1b55cfca 260 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
DieterGraef 0:d26c1b55cfca 261 if (tmpNetif->next == netif) {
DieterGraef 0:d26c1b55cfca 262 tmpNetif->next = netif->next;
DieterGraef 0:d26c1b55cfca 263 break;
DieterGraef 0:d26c1b55cfca 264 }
DieterGraef 0:d26c1b55cfca 265 }
DieterGraef 0:d26c1b55cfca 266 if (tmpNetif == NULL)
DieterGraef 0:d26c1b55cfca 267 return; /* we didn't find any netif today */
DieterGraef 0:d26c1b55cfca 268 }
DieterGraef 0:d26c1b55cfca 269 snmp_dec_iflist();
DieterGraef 0:d26c1b55cfca 270 /* this netif is default? */
DieterGraef 0:d26c1b55cfca 271 if (netif_default == netif) {
DieterGraef 0:d26c1b55cfca 272 /* reset default netif */
DieterGraef 0:d26c1b55cfca 273 netif_set_default(NULL);
DieterGraef 0:d26c1b55cfca 274 }
DieterGraef 0:d26c1b55cfca 275 #if LWIP_NETIF_REMOVE_CALLBACK
DieterGraef 0:d26c1b55cfca 276 if (netif->remove_callback) {
DieterGraef 0:d26c1b55cfca 277 netif->remove_callback(netif);
DieterGraef 0:d26c1b55cfca 278 }
DieterGraef 0:d26c1b55cfca 279 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
DieterGraef 0:d26c1b55cfca 280 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
DieterGraef 0:d26c1b55cfca 281 }
DieterGraef 0:d26c1b55cfca 282
DieterGraef 0:d26c1b55cfca 283 /**
DieterGraef 0:d26c1b55cfca 284 * Find a network interface by searching for its name
DieterGraef 0:d26c1b55cfca 285 *
DieterGraef 0:d26c1b55cfca 286 * @param name the name of the netif (like netif->name) plus concatenated number
DieterGraef 0:d26c1b55cfca 287 * in ascii representation (e.g. 'en0')
DieterGraef 0:d26c1b55cfca 288 */
DieterGraef 0:d26c1b55cfca 289 struct netif *
DieterGraef 0:d26c1b55cfca 290 netif_find(char *name)
DieterGraef 0:d26c1b55cfca 291 {
DieterGraef 0:d26c1b55cfca 292 struct netif *netif;
DieterGraef 0:d26c1b55cfca 293 u8_t num;
DieterGraef 0:d26c1b55cfca 294
DieterGraef 0:d26c1b55cfca 295 if (name == NULL) {
DieterGraef 0:d26c1b55cfca 296 return NULL;
DieterGraef 0:d26c1b55cfca 297 }
DieterGraef 0:d26c1b55cfca 298
DieterGraef 0:d26c1b55cfca 299 num = name[2] - '0';
DieterGraef 0:d26c1b55cfca 300
DieterGraef 0:d26c1b55cfca 301 for(netif = netif_list; netif != NULL; netif = netif->next) {
DieterGraef 0:d26c1b55cfca 302 if (num == netif->num &&
DieterGraef 0:d26c1b55cfca 303 name[0] == netif->name[0] &&
DieterGraef 0:d26c1b55cfca 304 name[1] == netif->name[1]) {
DieterGraef 0:d26c1b55cfca 305 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
DieterGraef 0:d26c1b55cfca 306 return netif;
DieterGraef 0:d26c1b55cfca 307 }
DieterGraef 0:d26c1b55cfca 308 }
DieterGraef 0:d26c1b55cfca 309 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
DieterGraef 0:d26c1b55cfca 310 return NULL;
DieterGraef 0:d26c1b55cfca 311 }
DieterGraef 0:d26c1b55cfca 312
DieterGraef 0:d26c1b55cfca 313 /**
DieterGraef 0:d26c1b55cfca 314 * Change the IP address of a network interface
DieterGraef 0:d26c1b55cfca 315 *
DieterGraef 0:d26c1b55cfca 316 * @param netif the network interface to change
DieterGraef 0:d26c1b55cfca 317 * @param ipaddr the new IP address
DieterGraef 0:d26c1b55cfca 318 *
DieterGraef 0:d26c1b55cfca 319 * @note call netif_set_addr() if you also want to change netmask and
DieterGraef 0:d26c1b55cfca 320 * default gateway
DieterGraef 0:d26c1b55cfca 321 */
DieterGraef 0:d26c1b55cfca 322 void
DieterGraef 0:d26c1b55cfca 323 netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
DieterGraef 0:d26c1b55cfca 324 {
DieterGraef 0:d26c1b55cfca 325 /* TODO: Handling of obsolete pcbs */
DieterGraef 0:d26c1b55cfca 326 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
DieterGraef 0:d26c1b55cfca 327 #if LWIP_TCP
DieterGraef 0:d26c1b55cfca 328 struct tcp_pcb *pcb;
DieterGraef 0:d26c1b55cfca 329 struct tcp_pcb_listen *lpcb;
DieterGraef 0:d26c1b55cfca 330
DieterGraef 0:d26c1b55cfca 331 /* address is actually being changed? */
DieterGraef 0:d26c1b55cfca 332 if (ipaddr && (ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
DieterGraef 0:d26c1b55cfca 333 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
DieterGraef 0:d26c1b55cfca 334 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
DieterGraef 0:d26c1b55cfca 335 pcb = tcp_active_pcbs;
DieterGraef 0:d26c1b55cfca 336 while (pcb != NULL) {
DieterGraef 0:d26c1b55cfca 337 /* PCB bound to current local interface address? */
DieterGraef 0:d26c1b55cfca 338 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))
DieterGraef 0:d26c1b55cfca 339 #if LWIP_AUTOIP
DieterGraef 0:d26c1b55cfca 340 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
DieterGraef 0:d26c1b55cfca 341 && !ip_addr_islinklocal(&(pcb->local_ip))
DieterGraef 0:d26c1b55cfca 342 #endif /* LWIP_AUTOIP */
DieterGraef 0:d26c1b55cfca 343 ) {
DieterGraef 0:d26c1b55cfca 344 /* this connection must be aborted */
DieterGraef 0:d26c1b55cfca 345 struct tcp_pcb *next = pcb->next;
DieterGraef 0:d26c1b55cfca 346 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
DieterGraef 0:d26c1b55cfca 347 tcp_abort(pcb);
DieterGraef 0:d26c1b55cfca 348 pcb = next;
DieterGraef 0:d26c1b55cfca 349 } else {
DieterGraef 0:d26c1b55cfca 350 pcb = pcb->next;
DieterGraef 0:d26c1b55cfca 351 }
DieterGraef 0:d26c1b55cfca 352 }
DieterGraef 0:d26c1b55cfca 353 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
DieterGraef 0:d26c1b55cfca 354 /* PCB bound to current local interface address? */
DieterGraef 0:d26c1b55cfca 355 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
DieterGraef 0:d26c1b55cfca 356 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
DieterGraef 0:d26c1b55cfca 357 /* The PCB is listening to the old ipaddr and
DieterGraef 0:d26c1b55cfca 358 * is set to listen to the new one instead */
DieterGraef 0:d26c1b55cfca 359 ip_addr_set(&(lpcb->local_ip), ipaddr);
DieterGraef 0:d26c1b55cfca 360 }
DieterGraef 0:d26c1b55cfca 361 }
DieterGraef 0:d26c1b55cfca 362 }
DieterGraef 0:d26c1b55cfca 363 #endif
DieterGraef 0:d26c1b55cfca 364 snmp_delete_ipaddridx_tree(netif);
DieterGraef 0:d26c1b55cfca 365 snmp_delete_iprteidx_tree(0,netif);
DieterGraef 0:d26c1b55cfca 366 /* set new IP address to netif */
DieterGraef 0:d26c1b55cfca 367 ip_addr_set(&(netif->ip_addr), ipaddr);
DieterGraef 0:d26c1b55cfca 368 snmp_insert_ipaddridx_tree(netif);
DieterGraef 0:d26c1b55cfca 369 snmp_insert_iprteidx_tree(0,netif);
DieterGraef 0:d26c1b55cfca 370
DieterGraef 0:d26c1b55cfca 371 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
DieterGraef 0:d26c1b55cfca 372 netif->name[0], netif->name[1],
DieterGraef 0:d26c1b55cfca 373 ip4_addr1_16(&netif->ip_addr),
DieterGraef 0:d26c1b55cfca 374 ip4_addr2_16(&netif->ip_addr),
DieterGraef 0:d26c1b55cfca 375 ip4_addr3_16(&netif->ip_addr),
DieterGraef 0:d26c1b55cfca 376 ip4_addr4_16(&netif->ip_addr)));
DieterGraef 0:d26c1b55cfca 377 }
DieterGraef 0:d26c1b55cfca 378
DieterGraef 0:d26c1b55cfca 379 /**
DieterGraef 0:d26c1b55cfca 380 * Change the default gateway for a network interface
DieterGraef 0:d26c1b55cfca 381 *
DieterGraef 0:d26c1b55cfca 382 * @param netif the network interface to change
DieterGraef 0:d26c1b55cfca 383 * @param gw the new default gateway
DieterGraef 0:d26c1b55cfca 384 *
DieterGraef 0:d26c1b55cfca 385 * @note call netif_set_addr() if you also want to change ip address and netmask
DieterGraef 0:d26c1b55cfca 386 */
DieterGraef 0:d26c1b55cfca 387 void
DieterGraef 0:d26c1b55cfca 388 netif_set_gw(struct netif *netif, ip_addr_t *gw)
DieterGraef 0:d26c1b55cfca 389 {
DieterGraef 0:d26c1b55cfca 390 ip_addr_set(&(netif->gw), gw);
DieterGraef 0:d26c1b55cfca 391 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
DieterGraef 0:d26c1b55cfca 392 netif->name[0], netif->name[1],
DieterGraef 0:d26c1b55cfca 393 ip4_addr1_16(&netif->gw),
DieterGraef 0:d26c1b55cfca 394 ip4_addr2_16(&netif->gw),
DieterGraef 0:d26c1b55cfca 395 ip4_addr3_16(&netif->gw),
DieterGraef 0:d26c1b55cfca 396 ip4_addr4_16(&netif->gw)));
DieterGraef 0:d26c1b55cfca 397 }
DieterGraef 0:d26c1b55cfca 398
DieterGraef 0:d26c1b55cfca 399 /**
DieterGraef 0:d26c1b55cfca 400 * Change the netmask of a network interface
DieterGraef 0:d26c1b55cfca 401 *
DieterGraef 0:d26c1b55cfca 402 * @param netif the network interface to change
DieterGraef 0:d26c1b55cfca 403 * @param netmask the new netmask
DieterGraef 0:d26c1b55cfca 404 *
DieterGraef 0:d26c1b55cfca 405 * @note call netif_set_addr() if you also want to change ip address and
DieterGraef 0:d26c1b55cfca 406 * default gateway
DieterGraef 0:d26c1b55cfca 407 */
DieterGraef 0:d26c1b55cfca 408 void
DieterGraef 0:d26c1b55cfca 409 netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
DieterGraef 0:d26c1b55cfca 410 {
DieterGraef 0:d26c1b55cfca 411 snmp_delete_iprteidx_tree(0, netif);
DieterGraef 0:d26c1b55cfca 412 /* set new netmask to netif */
DieterGraef 0:d26c1b55cfca 413 ip_addr_set(&(netif->netmask), netmask);
DieterGraef 0:d26c1b55cfca 414 snmp_insert_iprteidx_tree(0, netif);
DieterGraef 0:d26c1b55cfca 415 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
DieterGraef 0:d26c1b55cfca 416 netif->name[0], netif->name[1],
DieterGraef 0:d26c1b55cfca 417 ip4_addr1_16(&netif->netmask),
DieterGraef 0:d26c1b55cfca 418 ip4_addr2_16(&netif->netmask),
DieterGraef 0:d26c1b55cfca 419 ip4_addr3_16(&netif->netmask),
DieterGraef 0:d26c1b55cfca 420 ip4_addr4_16(&netif->netmask)));
DieterGraef 0:d26c1b55cfca 421 }
DieterGraef 0:d26c1b55cfca 422
DieterGraef 0:d26c1b55cfca 423 /**
DieterGraef 0:d26c1b55cfca 424 * Set a network interface as the default network interface
DieterGraef 0:d26c1b55cfca 425 * (used to output all packets for which no specific route is found)
DieterGraef 0:d26c1b55cfca 426 *
DieterGraef 0:d26c1b55cfca 427 * @param netif the default network interface
DieterGraef 0:d26c1b55cfca 428 */
DieterGraef 0:d26c1b55cfca 429 void
DieterGraef 0:d26c1b55cfca 430 netif_set_default(struct netif *netif)
DieterGraef 0:d26c1b55cfca 431 {
DieterGraef 0:d26c1b55cfca 432 if (netif == NULL) {
DieterGraef 0:d26c1b55cfca 433 /* remove default route */
DieterGraef 0:d26c1b55cfca 434 snmp_delete_iprteidx_tree(1, netif);
DieterGraef 0:d26c1b55cfca 435 } else {
DieterGraef 0:d26c1b55cfca 436 /* install default route */
DieterGraef 0:d26c1b55cfca 437 snmp_insert_iprteidx_tree(1, netif);
DieterGraef 0:d26c1b55cfca 438 }
DieterGraef 0:d26c1b55cfca 439 netif_default = netif;
DieterGraef 0:d26c1b55cfca 440 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
DieterGraef 0:d26c1b55cfca 441 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
DieterGraef 0:d26c1b55cfca 442 }
DieterGraef 0:d26c1b55cfca 443
DieterGraef 0:d26c1b55cfca 444 /**
DieterGraef 0:d26c1b55cfca 445 * Bring an interface up, available for processing
DieterGraef 0:d26c1b55cfca 446 * traffic.
DieterGraef 0:d26c1b55cfca 447 *
DieterGraef 0:d26c1b55cfca 448 * @note: Enabling DHCP on a down interface will make it come
DieterGraef 0:d26c1b55cfca 449 * up once configured.
DieterGraef 0:d26c1b55cfca 450 *
DieterGraef 0:d26c1b55cfca 451 * @see dhcp_start()
DieterGraef 0:d26c1b55cfca 452 */
DieterGraef 0:d26c1b55cfca 453 void netif_set_up(struct netif *netif)
DieterGraef 0:d26c1b55cfca 454 {
DieterGraef 0:d26c1b55cfca 455 if (!(netif->flags & NETIF_FLAG_UP)) {
DieterGraef 0:d26c1b55cfca 456 netif->flags |= NETIF_FLAG_UP;
DieterGraef 0:d26c1b55cfca 457
DieterGraef 0:d26c1b55cfca 458 #if LWIP_SNMP
DieterGraef 0:d26c1b55cfca 459 snmp_get_sysuptime(&netif->ts);
DieterGraef 0:d26c1b55cfca 460 #endif /* LWIP_SNMP */
DieterGraef 0:d26c1b55cfca 461
DieterGraef 0:d26c1b55cfca 462 NETIF_STATUS_CALLBACK(netif);
DieterGraef 0:d26c1b55cfca 463
DieterGraef 0:d26c1b55cfca 464 if (netif->flags & NETIF_FLAG_LINK_UP) {
DieterGraef 0:d26c1b55cfca 465 #if LWIP_ARP
DieterGraef 0:d26c1b55cfca 466 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
DieterGraef 0:d26c1b55cfca 467 if (netif->flags & (NETIF_FLAG_ETHARP)) {
DieterGraef 0:d26c1b55cfca 468 etharp_gratuitous(netif);
DieterGraef 0:d26c1b55cfca 469 }
DieterGraef 0:d26c1b55cfca 470 #endif /* LWIP_ARP */
DieterGraef 0:d26c1b55cfca 471
DieterGraef 0:d26c1b55cfca 472 #if LWIP_IGMP
DieterGraef 0:d26c1b55cfca 473 /* resend IGMP memberships */
DieterGraef 0:d26c1b55cfca 474 if (netif->flags & NETIF_FLAG_IGMP) {
DieterGraef 0:d26c1b55cfca 475 igmp_report_groups( netif);
DieterGraef 0:d26c1b55cfca 476 }
DieterGraef 0:d26c1b55cfca 477 #endif /* LWIP_IGMP */
DieterGraef 0:d26c1b55cfca 478 }
DieterGraef 0:d26c1b55cfca 479 }
DieterGraef 0:d26c1b55cfca 480 }
DieterGraef 0:d26c1b55cfca 481
DieterGraef 0:d26c1b55cfca 482 /**
DieterGraef 0:d26c1b55cfca 483 * Bring an interface down, disabling any traffic processing.
DieterGraef 0:d26c1b55cfca 484 *
DieterGraef 0:d26c1b55cfca 485 * @note: Enabling DHCP on a down interface will make it come
DieterGraef 0:d26c1b55cfca 486 * up once configured.
DieterGraef 0:d26c1b55cfca 487 *
DieterGraef 0:d26c1b55cfca 488 * @see dhcp_start()
DieterGraef 0:d26c1b55cfca 489 */
DieterGraef 0:d26c1b55cfca 490 void netif_set_down(struct netif *netif)
DieterGraef 0:d26c1b55cfca 491 {
DieterGraef 0:d26c1b55cfca 492 if (netif->flags & NETIF_FLAG_UP) {
DieterGraef 0:d26c1b55cfca 493 netif->flags &= ~NETIF_FLAG_UP;
DieterGraef 0:d26c1b55cfca 494 #if LWIP_SNMP
DieterGraef 0:d26c1b55cfca 495 snmp_get_sysuptime(&netif->ts);
DieterGraef 0:d26c1b55cfca 496 #endif
DieterGraef 0:d26c1b55cfca 497
DieterGraef 0:d26c1b55cfca 498 #if LWIP_ARP
DieterGraef 0:d26c1b55cfca 499 if (netif->flags & NETIF_FLAG_ETHARP) {
DieterGraef 0:d26c1b55cfca 500 etharp_cleanup_netif(netif);
DieterGraef 0:d26c1b55cfca 501 }
DieterGraef 0:d26c1b55cfca 502 #endif /* LWIP_ARP */
DieterGraef 0:d26c1b55cfca 503 NETIF_STATUS_CALLBACK(netif);
DieterGraef 0:d26c1b55cfca 504 }
DieterGraef 0:d26c1b55cfca 505 }
DieterGraef 0:d26c1b55cfca 506
DieterGraef 0:d26c1b55cfca 507 #if LWIP_NETIF_STATUS_CALLBACK
DieterGraef 0:d26c1b55cfca 508 /**
DieterGraef 0:d26c1b55cfca 509 * Set callback to be called when interface is brought up/down
DieterGraef 0:d26c1b55cfca 510 */
DieterGraef 0:d26c1b55cfca 511 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
DieterGraef 0:d26c1b55cfca 512 {
DieterGraef 0:d26c1b55cfca 513 if (netif) {
DieterGraef 0:d26c1b55cfca 514 netif->status_callback = status_callback;
DieterGraef 0:d26c1b55cfca 515 }
DieterGraef 0:d26c1b55cfca 516 }
DieterGraef 0:d26c1b55cfca 517 #endif /* LWIP_NETIF_STATUS_CALLBACK */
DieterGraef 0:d26c1b55cfca 518
DieterGraef 0:d26c1b55cfca 519 #if LWIP_NETIF_REMOVE_CALLBACK
DieterGraef 0:d26c1b55cfca 520 /**
DieterGraef 0:d26c1b55cfca 521 * Set callback to be called when the interface has been removed
DieterGraef 0:d26c1b55cfca 522 */
DieterGraef 0:d26c1b55cfca 523 void
DieterGraef 0:d26c1b55cfca 524 netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
DieterGraef 0:d26c1b55cfca 525 {
DieterGraef 0:d26c1b55cfca 526 if (netif) {
DieterGraef 0:d26c1b55cfca 527 netif->remove_callback = remove_callback;
DieterGraef 0:d26c1b55cfca 528 }
DieterGraef 0:d26c1b55cfca 529 }
DieterGraef 0:d26c1b55cfca 530 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
DieterGraef 0:d26c1b55cfca 531
DieterGraef 0:d26c1b55cfca 532 /**
DieterGraef 0:d26c1b55cfca 533 * Called by a driver when its link goes up
DieterGraef 0:d26c1b55cfca 534 */
DieterGraef 0:d26c1b55cfca 535 void netif_set_link_up(struct netif *netif )
DieterGraef 0:d26c1b55cfca 536 {
DieterGraef 0:d26c1b55cfca 537 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
DieterGraef 0:d26c1b55cfca 538 netif->flags |= NETIF_FLAG_LINK_UP;
DieterGraef 0:d26c1b55cfca 539
DieterGraef 0:d26c1b55cfca 540 #if LWIP_DHCP
DieterGraef 0:d26c1b55cfca 541 if (netif->dhcp) {
DieterGraef 0:d26c1b55cfca 542 dhcp_network_changed(netif);
DieterGraef 0:d26c1b55cfca 543 }
DieterGraef 0:d26c1b55cfca 544 #endif /* LWIP_DHCP */
DieterGraef 0:d26c1b55cfca 545
DieterGraef 0:d26c1b55cfca 546 #if LWIP_AUTOIP
DieterGraef 0:d26c1b55cfca 547 if (netif->autoip) {
DieterGraef 0:d26c1b55cfca 548 autoip_network_changed(netif);
DieterGraef 0:d26c1b55cfca 549 }
DieterGraef 0:d26c1b55cfca 550 #endif /* LWIP_AUTOIP */
DieterGraef 0:d26c1b55cfca 551
DieterGraef 0:d26c1b55cfca 552 if (netif->flags & NETIF_FLAG_UP) {
DieterGraef 0:d26c1b55cfca 553 #if LWIP_ARP
DieterGraef 0:d26c1b55cfca 554 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
DieterGraef 0:d26c1b55cfca 555 if (netif->flags & NETIF_FLAG_ETHARP) {
DieterGraef 0:d26c1b55cfca 556 etharp_gratuitous(netif);
DieterGraef 0:d26c1b55cfca 557 }
DieterGraef 0:d26c1b55cfca 558 #endif /* LWIP_ARP */
DieterGraef 0:d26c1b55cfca 559
DieterGraef 0:d26c1b55cfca 560 #if LWIP_IGMP
DieterGraef 0:d26c1b55cfca 561 /* resend IGMP memberships */
DieterGraef 0:d26c1b55cfca 562 if (netif->flags & NETIF_FLAG_IGMP) {
DieterGraef 0:d26c1b55cfca 563 igmp_report_groups( netif);
DieterGraef 0:d26c1b55cfca 564 }
DieterGraef 0:d26c1b55cfca 565 #endif /* LWIP_IGMP */
DieterGraef 0:d26c1b55cfca 566 }
DieterGraef 0:d26c1b55cfca 567 NETIF_LINK_CALLBACK(netif);
DieterGraef 0:d26c1b55cfca 568 }
DieterGraef 0:d26c1b55cfca 569 }
DieterGraef 0:d26c1b55cfca 570
DieterGraef 0:d26c1b55cfca 571 /**
DieterGraef 0:d26c1b55cfca 572 * Called by a driver when its link goes down
DieterGraef 0:d26c1b55cfca 573 */
DieterGraef 0:d26c1b55cfca 574 void netif_set_link_down(struct netif *netif )
DieterGraef 0:d26c1b55cfca 575 {
DieterGraef 0:d26c1b55cfca 576 if (netif->flags & NETIF_FLAG_LINK_UP) {
DieterGraef 0:d26c1b55cfca 577 netif->flags &= ~NETIF_FLAG_LINK_UP;
DieterGraef 0:d26c1b55cfca 578 NETIF_LINK_CALLBACK(netif);
DieterGraef 0:d26c1b55cfca 579 }
DieterGraef 0:d26c1b55cfca 580 }
DieterGraef 0:d26c1b55cfca 581
DieterGraef 0:d26c1b55cfca 582 #if LWIP_NETIF_LINK_CALLBACK
DieterGraef 0:d26c1b55cfca 583 /**
DieterGraef 0:d26c1b55cfca 584 * Set callback to be called when link is brought up/down
DieterGraef 0:d26c1b55cfca 585 */
DieterGraef 0:d26c1b55cfca 586 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
DieterGraef 0:d26c1b55cfca 587 {
DieterGraef 0:d26c1b55cfca 588 if (netif) {
DieterGraef 0:d26c1b55cfca 589 netif->link_callback = link_callback;
DieterGraef 0:d26c1b55cfca 590 }
DieterGraef 0:d26c1b55cfca 591 }
DieterGraef 0:d26c1b55cfca 592 #endif /* LWIP_NETIF_LINK_CALLBACK */
DieterGraef 0:d26c1b55cfca 593
DieterGraef 0:d26c1b55cfca 594 #if ENABLE_LOOPBACK
DieterGraef 0:d26c1b55cfca 595 /**
DieterGraef 0:d26c1b55cfca 596 * Send an IP packet to be received on the same netif (loopif-like).
DieterGraef 0:d26c1b55cfca 597 * The pbuf is simply copied and handed back to netif->input.
DieterGraef 0:d26c1b55cfca 598 * In multithreaded mode, this is done directly since netif->input must put
DieterGraef 0:d26c1b55cfca 599 * the packet on a queue.
DieterGraef 0:d26c1b55cfca 600 * In callback mode, the packet is put on an internal queue and is fed to
DieterGraef 0:d26c1b55cfca 601 * netif->input by netif_poll().
DieterGraef 0:d26c1b55cfca 602 *
DieterGraef 0:d26c1b55cfca 603 * @param netif the lwip network interface structure
DieterGraef 0:d26c1b55cfca 604 * @param p the (IP) packet to 'send'
DieterGraef 0:d26c1b55cfca 605 * @param ipaddr the ip address to send the packet to (not used)
DieterGraef 0:d26c1b55cfca 606 * @return ERR_OK if the packet has been sent
DieterGraef 0:d26c1b55cfca 607 * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
DieterGraef 0:d26c1b55cfca 608 */
DieterGraef 0:d26c1b55cfca 609 err_t
DieterGraef 0:d26c1b55cfca 610 netif_loop_output(struct netif *netif, struct pbuf *p,
DieterGraef 0:d26c1b55cfca 611 ip_addr_t *ipaddr)
DieterGraef 0:d26c1b55cfca 612 {
DieterGraef 0:d26c1b55cfca 613 struct pbuf *r;
DieterGraef 0:d26c1b55cfca 614 err_t err;
DieterGraef 0:d26c1b55cfca 615 struct pbuf *last;
DieterGraef 0:d26c1b55cfca 616 #if LWIP_LOOPBACK_MAX_PBUFS
DieterGraef 0:d26c1b55cfca 617 u8_t clen = 0;
DieterGraef 0:d26c1b55cfca 618 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
DieterGraef 0:d26c1b55cfca 619 /* If we have a loopif, SNMP counters are adjusted for it,
DieterGraef 0:d26c1b55cfca 620 * if not they are adjusted for 'netif'. */
DieterGraef 0:d26c1b55cfca 621 #if LWIP_SNMP
DieterGraef 0:d26c1b55cfca 622 #if LWIP_HAVE_LOOPIF
DieterGraef 0:d26c1b55cfca 623 struct netif *stats_if = &loop_netif;
DieterGraef 0:d26c1b55cfca 624 #else /* LWIP_HAVE_LOOPIF */
DieterGraef 0:d26c1b55cfca 625 struct netif *stats_if = netif;
DieterGraef 0:d26c1b55cfca 626 #endif /* LWIP_HAVE_LOOPIF */
DieterGraef 0:d26c1b55cfca 627 #endif /* LWIP_SNMP */
DieterGraef 0:d26c1b55cfca 628 SYS_ARCH_DECL_PROTECT(lev);
DieterGraef 0:d26c1b55cfca 629 LWIP_UNUSED_ARG(ipaddr);
DieterGraef 0:d26c1b55cfca 630
DieterGraef 0:d26c1b55cfca 631 /* Allocate a new pbuf */
DieterGraef 0:d26c1b55cfca 632 r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
DieterGraef 0:d26c1b55cfca 633 if (r == NULL) {
DieterGraef 0:d26c1b55cfca 634 LINK_STATS_INC(link.memerr);
DieterGraef 0:d26c1b55cfca 635 LINK_STATS_INC(link.drop);
DieterGraef 0:d26c1b55cfca 636 snmp_inc_ifoutdiscards(stats_if);
DieterGraef 0:d26c1b55cfca 637 return ERR_MEM;
DieterGraef 0:d26c1b55cfca 638 }
DieterGraef 0:d26c1b55cfca 639 #if LWIP_LOOPBACK_MAX_PBUFS
DieterGraef 0:d26c1b55cfca 640 clen = pbuf_clen(r);
DieterGraef 0:d26c1b55cfca 641 /* check for overflow or too many pbuf on queue */
DieterGraef 0:d26c1b55cfca 642 if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
DieterGraef 0:d26c1b55cfca 643 ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
DieterGraef 0:d26c1b55cfca 644 pbuf_free(r);
DieterGraef 0:d26c1b55cfca 645 LINK_STATS_INC(link.memerr);
DieterGraef 0:d26c1b55cfca 646 LINK_STATS_INC(link.drop);
DieterGraef 0:d26c1b55cfca 647 snmp_inc_ifoutdiscards(stats_if);
DieterGraef 0:d26c1b55cfca 648 return ERR_MEM;
DieterGraef 0:d26c1b55cfca 649 }
DieterGraef 0:d26c1b55cfca 650 netif->loop_cnt_current += clen;
DieterGraef 0:d26c1b55cfca 651 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
DieterGraef 0:d26c1b55cfca 652
DieterGraef 0:d26c1b55cfca 653 /* Copy the whole pbuf queue p into the single pbuf r */
DieterGraef 0:d26c1b55cfca 654 if ((err = pbuf_copy(r, p)) != ERR_OK) {
DieterGraef 0:d26c1b55cfca 655 pbuf_free(r);
DieterGraef 0:d26c1b55cfca 656 LINK_STATS_INC(link.memerr);
DieterGraef 0:d26c1b55cfca 657 LINK_STATS_INC(link.drop);
DieterGraef 0:d26c1b55cfca 658 snmp_inc_ifoutdiscards(stats_if);
DieterGraef 0:d26c1b55cfca 659 return err;
DieterGraef 0:d26c1b55cfca 660 }
DieterGraef 0:d26c1b55cfca 661
DieterGraef 0:d26c1b55cfca 662 /* Put the packet on a linked list which gets emptied through calling
DieterGraef 0:d26c1b55cfca 663 netif_poll(). */
DieterGraef 0:d26c1b55cfca 664
DieterGraef 0:d26c1b55cfca 665 /* let last point to the last pbuf in chain r */
DieterGraef 0:d26c1b55cfca 666 for (last = r; last->next != NULL; last = last->next);
DieterGraef 0:d26c1b55cfca 667
DieterGraef 0:d26c1b55cfca 668 SYS_ARCH_PROTECT(lev);
DieterGraef 0:d26c1b55cfca 669 if(netif->loop_first != NULL) {
DieterGraef 0:d26c1b55cfca 670 LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
DieterGraef 0:d26c1b55cfca 671 netif->loop_last->next = r;
DieterGraef 0:d26c1b55cfca 672 netif->loop_last = last;
DieterGraef 0:d26c1b55cfca 673 } else {
DieterGraef 0:d26c1b55cfca 674 netif->loop_first = r;
DieterGraef 0:d26c1b55cfca 675 netif->loop_last = last;
DieterGraef 0:d26c1b55cfca 676 }
DieterGraef 0:d26c1b55cfca 677 SYS_ARCH_UNPROTECT(lev);
DieterGraef 0:d26c1b55cfca 678
DieterGraef 0:d26c1b55cfca 679 LINK_STATS_INC(link.xmit);
DieterGraef 0:d26c1b55cfca 680 snmp_add_ifoutoctets(stats_if, p->tot_len);
DieterGraef 0:d26c1b55cfca 681 snmp_inc_ifoutucastpkts(stats_if);
DieterGraef 0:d26c1b55cfca 682
DieterGraef 0:d26c1b55cfca 683 #if LWIP_NETIF_LOOPBACK_MULTITHREADING
DieterGraef 0:d26c1b55cfca 684 /* For multithreading environment, schedule a call to netif_poll */
DieterGraef 0:d26c1b55cfca 685 tcpip_callback((tcpip_callback_fn)netif_poll, netif);
DieterGraef 0:d26c1b55cfca 686 #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
DieterGraef 0:d26c1b55cfca 687
DieterGraef 0:d26c1b55cfca 688 return ERR_OK;
DieterGraef 0:d26c1b55cfca 689 }
DieterGraef 0:d26c1b55cfca 690
DieterGraef 0:d26c1b55cfca 691 /**
DieterGraef 0:d26c1b55cfca 692 * Call netif_poll() in the main loop of your application. This is to prevent
DieterGraef 0:d26c1b55cfca 693 * reentering non-reentrant functions like tcp_input(). Packets passed to
DieterGraef 0:d26c1b55cfca 694 * netif_loop_output() are put on a list that is passed to netif->input() by
DieterGraef 0:d26c1b55cfca 695 * netif_poll().
DieterGraef 0:d26c1b55cfca 696 */
DieterGraef 0:d26c1b55cfca 697 void
DieterGraef 0:d26c1b55cfca 698 netif_poll(struct netif *netif)
DieterGraef 0:d26c1b55cfca 699 {
DieterGraef 0:d26c1b55cfca 700 struct pbuf *in;
DieterGraef 0:d26c1b55cfca 701 /* If we have a loopif, SNMP counters are adjusted for it,
DieterGraef 0:d26c1b55cfca 702 * if not they are adjusted for 'netif'. */
DieterGraef 0:d26c1b55cfca 703 #if LWIP_SNMP
DieterGraef 0:d26c1b55cfca 704 #if LWIP_HAVE_LOOPIF
DieterGraef 0:d26c1b55cfca 705 struct netif *stats_if = &loop_netif;
DieterGraef 0:d26c1b55cfca 706 #else /* LWIP_HAVE_LOOPIF */
DieterGraef 0:d26c1b55cfca 707 struct netif *stats_if = netif;
DieterGraef 0:d26c1b55cfca 708 #endif /* LWIP_HAVE_LOOPIF */
DieterGraef 0:d26c1b55cfca 709 #endif /* LWIP_SNMP */
DieterGraef 0:d26c1b55cfca 710 SYS_ARCH_DECL_PROTECT(lev);
DieterGraef 0:d26c1b55cfca 711
DieterGraef 0:d26c1b55cfca 712 do {
DieterGraef 0:d26c1b55cfca 713 /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
DieterGraef 0:d26c1b55cfca 714 SYS_ARCH_PROTECT(lev);
DieterGraef 0:d26c1b55cfca 715 in = netif->loop_first;
DieterGraef 0:d26c1b55cfca 716 if (in != NULL) {
DieterGraef 0:d26c1b55cfca 717 struct pbuf *in_end = in;
DieterGraef 0:d26c1b55cfca 718 #if LWIP_LOOPBACK_MAX_PBUFS
DieterGraef 0:d26c1b55cfca 719 u8_t clen = pbuf_clen(in);
DieterGraef 0:d26c1b55cfca 720 /* adjust the number of pbufs on queue */
DieterGraef 0:d26c1b55cfca 721 LWIP_ASSERT("netif->loop_cnt_current underflow",
DieterGraef 0:d26c1b55cfca 722 ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
DieterGraef 0:d26c1b55cfca 723 netif->loop_cnt_current -= clen;
DieterGraef 0:d26c1b55cfca 724 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
DieterGraef 0:d26c1b55cfca 725 while (in_end->len != in_end->tot_len) {
DieterGraef 0:d26c1b55cfca 726 LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
DieterGraef 0:d26c1b55cfca 727 in_end = in_end->next;
DieterGraef 0:d26c1b55cfca 728 }
DieterGraef 0:d26c1b55cfca 729 /* 'in_end' now points to the last pbuf from 'in' */
DieterGraef 0:d26c1b55cfca 730 if (in_end == netif->loop_last) {
DieterGraef 0:d26c1b55cfca 731 /* this was the last pbuf in the list */
DieterGraef 0:d26c1b55cfca 732 netif->loop_first = netif->loop_last = NULL;
DieterGraef 0:d26c1b55cfca 733 } else {
DieterGraef 0:d26c1b55cfca 734 /* pop the pbuf off the list */
DieterGraef 0:d26c1b55cfca 735 netif->loop_first = in_end->next;
DieterGraef 0:d26c1b55cfca 736 LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
DieterGraef 0:d26c1b55cfca 737 }
DieterGraef 0:d26c1b55cfca 738 /* De-queue the pbuf from its successors on the 'loop_' list. */
DieterGraef 0:d26c1b55cfca 739 in_end->next = NULL;
DieterGraef 0:d26c1b55cfca 740 }
DieterGraef 0:d26c1b55cfca 741 SYS_ARCH_UNPROTECT(lev);
DieterGraef 0:d26c1b55cfca 742
DieterGraef 0:d26c1b55cfca 743 if (in != NULL) {
DieterGraef 0:d26c1b55cfca 744 LINK_STATS_INC(link.recv);
DieterGraef 0:d26c1b55cfca 745 snmp_add_ifinoctets(stats_if, in->tot_len);
DieterGraef 0:d26c1b55cfca 746 snmp_inc_ifinucastpkts(stats_if);
DieterGraef 0:d26c1b55cfca 747 /* loopback packets are always IP packets! */
DieterGraef 0:d26c1b55cfca 748 if (ip_input(in, netif) != ERR_OK) {
DieterGraef 0:d26c1b55cfca 749 pbuf_free(in);
DieterGraef 0:d26c1b55cfca 750 }
DieterGraef 0:d26c1b55cfca 751 /* Don't reference the packet any more! */
DieterGraef 0:d26c1b55cfca 752 in = NULL;
DieterGraef 0:d26c1b55cfca 753 }
DieterGraef 0:d26c1b55cfca 754 /* go on while there is a packet on the list */
DieterGraef 0:d26c1b55cfca 755 } while (netif->loop_first != NULL);
DieterGraef 0:d26c1b55cfca 756 }
DieterGraef 0:d26c1b55cfca 757
DieterGraef 0:d26c1b55cfca 758 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
DieterGraef 0:d26c1b55cfca 759 /**
DieterGraef 0:d26c1b55cfca 760 * Calls netif_poll() for every netif on the netif_list.
DieterGraef 0:d26c1b55cfca 761 */
DieterGraef 0:d26c1b55cfca 762 void
DieterGraef 0:d26c1b55cfca 763 netif_poll_all(void)
DieterGraef 0:d26c1b55cfca 764 {
DieterGraef 0:d26c1b55cfca 765 struct netif *netif = netif_list;
DieterGraef 0:d26c1b55cfca 766 /* loop through netifs */
DieterGraef 0:d26c1b55cfca 767 while (netif != NULL) {
DieterGraef 0:d26c1b55cfca 768 netif_poll(netif);
DieterGraef 0:d26c1b55cfca 769 /* proceed to next network interface */
DieterGraef 0:d26c1b55cfca 770 netif = netif->next;
DieterGraef 0:d26c1b55cfca 771 }
DieterGraef 0:d26c1b55cfca 772 }
DieterGraef 0:d26c1b55cfca 773 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
DieterGraef 0:d26c1b55cfca 774 #endif /* ENABLE_LOOPBACK */