Onenet

Dependents:   K64F_eCompass_OneNET_JW

Committer:
robert_jw
Date:
Mon Jun 20 01:40:20 2016 +0000
Revision:
0:b2805b6888dc
ADS

Who changed what in which revision?

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