Hendrik Lipka / NetServicesMin

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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