SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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