OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP/] [core/] [netif.c] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/**
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
#include "lwip/snmp.h"
46
 
47
struct netif *netif_list = NULL;
48
struct netif *netif_default = NULL;
49
 
50
/**
51
 * Add a network interface to the list of lwIP netifs.
52
 *
53
 * @param netif a pre-allocated netif structure
54
 * @param ipaddr IP address for the new netif
55
 * @param netmask network mask for the new netif
56
 * @param gw default gateway IP address for the new netif
57
 * @param state opaque data passed to the new netif
58
 * @param init callback function that initializes the interface
59
 * @param input callback function that is called to pass
60
 * ingress packets up in the protocol layer stack.
61
 *
62
 * @return netif, or NULL if failed.
63
 */
64
struct netif *
65
netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
66
  struct ip_addr *gw,
67
  void *state,
68
  err_t (* init)(struct netif *netif),
69
  err_t (* input)(struct pbuf *p, struct netif *netif))
70
{
71
  static s16_t netifnum = 0;
72
 
73
  /* reset new interface configuration state */
74
  netif->ip_addr.addr = 0;
75
  netif->netmask.addr = 0;
76
  netif->gw.addr = 0;
77
  netif->flags = 0;
78
#if LWIP_DHCP
79
  /* netif not under DHCP control by default */
80
  netif->dhcp = NULL;
81
#endif
82
  /* remember netif specific state information data */
83
  netif->state = state;
84
  netif->num = netifnum++;
85
  netif->input = input;
86
 
87
  netif_set_addr(netif, ipaddr, netmask, gw);
88
 
89
  /* call user specified initialization function for netif */
90
  if (init(netif) != ERR_OK) {
91
    return NULL;
92
  }
93
 
94
  /* add this netif to the list */
95
  netif->next = netif_list;
96
  netif_list = netif;
97
  snmp_inc_iflist();
98
 
99
  LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
100
    netif->name[0], netif->name[1]));
101
  ip_addr_debug_print(NETIF_DEBUG, ipaddr);
102
  LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
103
  ip_addr_debug_print(NETIF_DEBUG, netmask);
104
  LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
105
  ip_addr_debug_print(NETIF_DEBUG, gw);
106
  LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
107
  return netif;
108
}
109
 
110
void
111
netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask,
112
    struct ip_addr *gw)
113
{
114
  netif_set_ipaddr(netif, ipaddr);
115
  netif_set_netmask(netif, netmask);
116
  netif_set_gw(netif, gw);
117
}
118
 
119
void netif_remove(struct netif * netif)
120
{
121
  if ( netif == NULL ) return;
122
 
123
  snmp_delete_ipaddridx_tree(netif);
124
 
125
  /*  is it the first netif? */
126
  if (netif_list == netif) {
127
    netif_list = netif->next;
128
    snmp_dec_iflist();
129
  }
130
  else {
131
    /*  look for netif further down the list */
132
    struct netif * tmpNetif;
133
    for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
134
      if (tmpNetif->next == netif) {
135
        tmpNetif->next = netif->next;
136
        snmp_dec_iflist();
137
        break;
138
      }
139
    }
140
    if (tmpNetif == NULL)
141
      return; /*  we didn't find any netif today */
142
  }
143
  /* this netif is default? */
144
  if (netif_default == netif)
145
    /* reset default netif */
146
    netif_default = NULL;
147
  LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
148
}
149
 
150
struct netif *
151
netif_find(char *name)
152
{
153
  struct netif *netif;
154
  u8_t num;
155
 
156
  if (name == NULL) {
157
    return NULL;
158
  }
159
 
160
  num = name[2] - '0';
161
 
162
  for(netif = netif_list; netif != NULL; netif = netif->next) {
163
    if (num == netif->num &&
164
       name[0] == netif->name[0] &&
165
       name[1] == netif->name[1]) {
166
      LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
167
      return netif;
168
    }
169
  }
170
  LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
171
  return NULL;
172
}
173
 
174
void
175
netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
176
{
177
  /* TODO: Handling of obsolete pcbs */
178
  /* See:  http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
179
#if LWIP_TCP
180
  struct tcp_pcb *pcb;
181
  struct tcp_pcb_listen *lpcb;
182
 
183
  /* address is actually being changed? */
184
  if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
185
  {
186
    /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
187
    LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: netif address being changed\n"));
188
    pcb = tcp_active_pcbs;
189
    while (pcb != NULL) {
190
      /* PCB bound to current local interface address? */
191
      if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
192
        /* this connection must be aborted */
193
        struct tcp_pcb *next = pcb->next;
194
        LWIP_DEBUGF(NETIF_DEBUG | 1, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
195
        tcp_abort(pcb);
196
        pcb = next;
197
      } else {
198
        pcb = pcb->next;
199
      }
200
    }
201
    for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
202
      /* PCB bound to current local interface address? */
203
      if (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr))) {
204
        /* The PCB is listening to the old ipaddr and
205
         * is set to listen to the new one instead */
206
        ip_addr_set(&(lpcb->local_ip), ipaddr);
207
      }
208
    }
209
  }
210
#endif
211
  snmp_delete_ipaddridx_tree(netif);
212
  snmp_delete_iprteidx_tree(0,netif);
213
  /* set new IP address to netif */
214
  ip_addr_set(&(netif->ip_addr), ipaddr);
215
  snmp_insert_ipaddridx_tree(netif);
216
  snmp_insert_iprteidx_tree(0,netif);
217
 
218
#if 0 /* only allowed for Ethernet interfaces TODO: how can we check? */
219
  /** For Ethernet network interfaces, we would like to send a
220
   *  "gratuitous ARP"; this is an ARP packet sent by a node in order
221
   *  to spontaneously cause other nodes to update an entry in their
222
   *  ARP cache. From RFC 3220 "IP Mobility Support for IPv4" section 4.6.
223
   */
224
  etharp_query(netif, ipaddr, NULL);
225
#endif
226
  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",
227
    netif->name[0], netif->name[1],
228
    ip4_addr1(&netif->ip_addr),
229
    ip4_addr2(&netif->ip_addr),
230
    ip4_addr3(&netif->ip_addr),
231
    ip4_addr4(&netif->ip_addr)));
232
}
233
 
234
void
235
netif_set_gw(struct netif *netif, struct ip_addr *gw)
236
{
237
  ip_addr_set(&(netif->gw), gw);
238
  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",
239
    netif->name[0], netif->name[1],
240
    ip4_addr1(&netif->gw),
241
    ip4_addr2(&netif->gw),
242
    ip4_addr3(&netif->gw),
243
    ip4_addr4(&netif->gw)));
244
}
245
 
246
void
247
netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
248
{
249
  snmp_delete_iprteidx_tree(0, netif);
250
  /* set new netmask to netif */
251
  ip_addr_set(&(netif->netmask), netmask);
252
  snmp_insert_iprteidx_tree(0, netif);
253
  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",
254
    netif->name[0], netif->name[1],
255
    ip4_addr1(&netif->netmask),
256
    ip4_addr2(&netif->netmask),
257
    ip4_addr3(&netif->netmask),
258
    ip4_addr4(&netif->netmask)));
259
}
260
 
261
void
262
netif_set_default(struct netif *netif)
263
{
264
  if (netif == NULL)
265
  {
266
    /* remove default route */
267
    snmp_delete_iprteidx_tree(1, netif);
268
  }
269
  else
270
  {
271
    /* install default route */
272
    snmp_insert_iprteidx_tree(1, netif);
273
  }
274
  netif_default = netif;
275
  LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
276
           netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
277
}
278
 
279
/**
280
 * Bring an interface up, available for processing
281
 * traffic.
282
 *
283
 * @note: Enabling DHCP on a down interface will make it come
284
 * up once configured.
285
 *
286
 * @see dhcp_start()
287
 */
288
void netif_set_up(struct netif *netif)
289
{
290
  netif->flags |= NETIF_FLAG_UP;
291
#if LWIP_SNMP
292
  snmp_get_sysuptime(&netif->ts);
293
#endif
294
}
295
 
296
/**
297
 * Ask if an interface is up
298
 */
299
u8_t netif_is_up(struct netif *netif)
300
{
301
  return (netif->flags & NETIF_FLAG_UP)?1:0;
302
}
303
 
304
/**
305
 * Bring an interface down, disabling any traffic processing.
306
 *
307
 * @note: Enabling DHCP on a down interface will make it come
308
 * up once configured.
309
 *
310
 * @see dhcp_start()
311
 */
312
void netif_set_down(struct netif *netif)
313
{
314
  netif->flags &= ~NETIF_FLAG_UP;
315
#if LWIP_SNMP
316
  snmp_get_sysuptime(&netif->ts);
317
#endif
318
}
319
 
320
void
321
netif_init(void)
322
{
323
  netif_list = netif_default = NULL;
324
}
325
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.