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