Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * @file |
| 3 | * |
| 4 | * lwIP network interface abstraction |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * |
| 14 | * 1. Redistributions of source code must retain the above copyright notice, |
| 15 | * this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 17 | * this list of conditions and the following disclaimer in the documentation |
| 18 | * and/or other materials provided with the distribution. |
| 19 | * 3. The name of the author may not be used to endorse or promote products |
| 20 | * derived from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 31 | * OF SUCH DAMAGE. |
| 32 | * |
| 33 | * This file is part of the lwIP TCP/IP stack. |
| 34 | * |
| 35 | * Author: Adam Dunkels <adam@sics.se> |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include "lwip/opt.h" |
| 40 | |
| 41 | #include "lwip/def.h" |
| 42 | #include "lwip/ip_addr.h" |
| 43 | #include "lwip/netif.h" |
| 44 | #include "lwip/tcp.h" |
| 45 | |
| 46 | struct netif *netif_list = NULL; |
| 47 | struct netif *netif_default = NULL; |
| 48 | |
| 49 | /** |
| 50 | * Add a network interface to the list of lwIP netifs. |
| 51 | * |
| 52 | * @param netif a pre-allocated netif structure |
| 53 | * @param ipaddr IP address for the new netif |
| 54 | * @param netmask network mask for the new netif |
| 55 | * @param gw default gateway IP address for the new netif |
| 56 | * @param state opaque data passed to the new netif |
| 57 | * @param init callback function that initializes the interface |
| 58 | * @param input callback function that is called to pass |
| 59 | * ingress packets up in the protocol layer stack. |
| 60 | * |
| 61 | * @return netif, or NULL if failed. |
| 62 | */ |
| 63 | struct netif * |
| 64 | netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, |
| 65 | struct ip_addr *gw, |
| 66 | void *state, |
| 67 | err_t (* init)(struct netif *netif), |
| 68 | err_t (* input)(struct pbuf *p, struct netif *netif)) |
| 69 | { |
| 70 | static s16_t netifnum = 0; |
| 71 | |
| 72 | #if LWIP_DHCP |
| 73 | /* netif not under DHCP control by default */ |
| 74 | netif->dhcp = NULL; |
| 75 | #endif |
| 76 | /* remember netif specific state information data */ |
| 77 | netif->state = state; |
| 78 | netif->num = netifnum++; |
| 79 | netif->input = input; |
| 80 | |
| 81 | netif_set_addr(netif, ipaddr, netmask, gw); |
| 82 | |
| 83 | /* call user specified initialization function for netif */ |
| 84 | if (init(netif) != ERR_OK) { |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | /* add this netif to the list */ |
| 89 | netif->next = netif_list; |
| 90 | netif_list = netif; |
| 91 | LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ", |
| 92 | netif->name[0], netif->name[1])); |
| 93 | ip_addr_debug_print(NETIF_DEBUG, ipaddr); |
| 94 | LWIP_DEBUGF(NETIF_DEBUG, (" netmask ")); |
| 95 | ip_addr_debug_print(NETIF_DEBUG, netmask); |
| 96 | LWIP_DEBUGF(NETIF_DEBUG, (" gw ")); |
| 97 | ip_addr_debug_print(NETIF_DEBUG, gw); |
| 98 | LWIP_DEBUGF(NETIF_DEBUG, ("\n")); |
| 99 | return netif; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask, |
| 104 | struct ip_addr *gw) |
| 105 | { |
| 106 | netif_set_ipaddr(netif, ipaddr); |
| 107 | netif_set_netmask(netif, netmask); |
| 108 | netif_set_gw(netif, gw); |
| 109 | } |
| 110 | |
| 111 | void netif_remove(struct netif * netif) |
| 112 | { |
| 113 | if ( netif == NULL ) return; |
| 114 | |
| 115 | /* is it the first netif? */ |
| 116 | if (netif_list == netif) { |
| 117 | netif_list = netif->next; |
| 118 | } |
| 119 | else { |
| 120 | /* look for netif further down the list */ |
| 121 | struct netif * tmpNetif; |
| 122 | for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) { |
| 123 | if (tmpNetif->next == netif) { |
| 124 | tmpNetif->next = netif->next; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | if (tmpNetif == NULL) |
| 129 | return; /* we didn't find any netif today */ |
| 130 | } |
| 131 | /* this netif is default? */ |
| 132 | if (netif_default == netif) |
| 133 | /* reset default netif */ |
| 134 | netif_default = NULL; |
| 135 | LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") ); |
| 136 | } |
| 137 | |
| 138 | struct netif * |
| 139 | netif_find(char *name) |
| 140 | { |
| 141 | struct netif *netif; |
| 142 | u8_t num; |
| 143 | |
| 144 | if (name == NULL) { |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | num = name[2] - '0'; |
| 149 | |
| 150 | for(netif = netif_list; netif != NULL; netif = netif->next) { |
| 151 | if (num == netif->num && |
| 152 | name[0] == netif->name[0] && |
| 153 | name[1] == netif->name[1]) { |
| 154 | LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1])); |
| 155 | return netif; |
| 156 | } |
| 157 | } |
| 158 | LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1])); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr) |
| 164 | { |
| 165 | /* TODO: Handling of obsolete pcbs */ |
| 166 | /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */ |
| 167 | #if LWIP_TCP |
| 168 | struct tcp_pcb *pcb; |
| 169 | struct tcp_pcb_listen *lpcb; |
| 170 | |
| 171 | /* address is actually being changed? */ |
| 172 | if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) |
| 173 | { |
| 174 | /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */ |
| 175 | LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n")); |
| 176 | pcb = tcp_active_pcbs; |
| 177 | while (pcb != NULL) { |
| 178 | /* PCB bound to current local interface address? */ |
| 179 | if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) { |
| 180 | /* this connection must be aborted */ |
| 181 | struct tcp_pcb *next = pcb->next; |
| 182 | LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb)); |
| 183 | tcp_abort(pcb); |
| 184 | pcb = next; |
| 185 | } else { |
| 186 | pcb = pcb->next; |
| 187 | } |
| 188 | } |
| 189 | for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { |
| 190 | /* PCB bound to current local interface address? */ |
| 191 | if (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr))) { |
| 192 | /* The PCB is listening to the old ipaddr and |
| 193 | * is set to listen to the new one instead */ |
| 194 | ip_addr_set(&(lpcb->local_ip), ipaddr); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | #endif |
| 199 | ip_addr_set(&(netif->ip_addr), ipaddr); |
| 200 | #if 0 /* only allowed for Ethernet interfaces TODO: how can we check? */ |
| 201 | /** For Ethernet network interfaces, we would like to send a |
| 202 | * "gratuitous ARP"; this is an ARP packet sent by a node in order |
| 203 | * to spontaneously cause other nodes to update an entry in their |
| 204 | * ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6. |
| 205 | */ |
| 206 | etharp_query(netif, ipaddr, NULL); |
| 207 | #endif |
| 208 | LWIP_DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
| 209 | netif->name[0], netif->name[1], |
| 210 | ip4_addr1(&netif->ip_addr), |
| 211 | ip4_addr2(&netif->ip_addr), |
| 212 | ip4_addr3(&netif->ip_addr), |
| 213 | ip4_addr4(&netif->ip_addr))); |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | netif_set_gw(struct netif *netif, struct ip_addr *gw) |
| 218 | { |
| 219 | ip_addr_set(&(netif->gw), gw); |
| 220 | LWIP_DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
| 221 | netif->name[0], netif->name[1], |
| 222 | ip4_addr1(&netif->gw), |
| 223 | ip4_addr2(&netif->gw), |
| 224 | ip4_addr3(&netif->gw), |
| 225 | ip4_addr4(&netif->gw))); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | netif_set_netmask(struct netif *netif, struct ip_addr *netmask) |
| 230 | { |
| 231 | ip_addr_set(&(netif->netmask), netmask); |
| 232 | LWIP_DEBUGF(NETIF_DEBUG | DBG_TRACE | DBG_STATE | 3, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", |
| 233 | netif->name[0], netif->name[1], |
| 234 | ip4_addr1(&netif->netmask), |
| 235 | ip4_addr2(&netif->netmask), |
| 236 | ip4_addr3(&netif->netmask), |
| 237 | ip4_addr4(&netif->netmask))); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | netif_set_default(struct netif *netif) |
| 242 | { |
| 243 | netif_default = netif; |
| 244 | LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n", |
| 245 | netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\'')); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Bring an interface up, available for processing |
| 250 | * traffic. |
| 251 | * |
| 252 | * @note: Enabling DHCP on a down interface will make it come |
| 253 | * up once configured. |
| 254 | * |
| 255 | * @see dhcp_start() |
| 256 | */ |
| 257 | void netif_set_up(struct netif *netif) |
| 258 | { |
| 259 | netif->flags |= NETIF_FLAG_UP; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Ask if an interface is up |
| 264 | */ |
| 265 | u8_t netif_is_up(struct netif *netif) |
| 266 | { |
| 267 | return (netif->flags & NETIF_FLAG_UP)?1:0; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Bring an interface down, disabling any traffic processing. |
| 272 | * |
| 273 | * @note: Enabling DHCP on a down interface will make it come |
| 274 | * up once configured. |
| 275 | * |
| 276 | * @see dhcp_start() |
| 277 | */ |
| 278 | void netif_set_down(struct netif *netif) |
| 279 | { |
| 280 | netif->flags &= ~NETIF_FLAG_UP; |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | netif_init(void) |
| 285 | { |
| 286 | netif_list = netif_default = NULL; |
| 287 | } |
| 288 | |