ReactOS 0.4.15-dev-7788-g1ad9096
netif.c File Reference
#include "lwip/opt.h"
#include "lwip/def.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/tcp_impl.h"
#include "lwip/snmp.h"
#include "lwip/igmp.h"
#include "netif/etharp.h"
#include "lwip/stats.h"
Include dependency graph for netif.c:

Go to the source code of this file.

Macros

#define NETIF_STATUS_CALLBACK(n)
 
#define NETIF_LINK_CALLBACK(n)
 

Functions

void netif_init (void)
 
struct netifnetif_add (struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
 
void netif_set_addr (struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw)
 
void netif_remove (struct netif *netif)
 
struct netifnetif_find (char *name)
 
void netif_set_ipaddr (struct netif *netif, ip_addr_t *ipaddr)
 
void netif_set_gw (struct netif *netif, ip_addr_t *gw)
 
void netif_set_netmask (struct netif *netif, ip_addr_t *netmask)
 
void netif_set_default (struct netif *netif)
 
void netif_set_up (struct netif *netif)
 
void netif_set_down (struct netif *netif)
 
void netif_set_link_up (struct netif *netif)
 
void netif_set_link_down (struct netif *netif)
 

Variables

struct netifnetif_list
 
struct netifnetif_default
 
static u8_t netif_num
 

Detailed Description

lwIP network interface abstraction

Definition in file netif.c.

Macro Definition Documentation

◆ NETIF_LINK_CALLBACK

#define NETIF_LINK_CALLBACK (   n)

Definition at line 72 of file netif.c.

◆ NETIF_STATUS_CALLBACK

#define NETIF_STATUS_CALLBACK (   n)

Definition at line 66 of file netif.c.

Function Documentation

◆ netif_add()

struct netif * netif_add ( struct netif netif,
ip_addr_t ipaddr,
ip_addr_t netmask,
ip_addr_t gw,
void state,
netif_init_fn  init,
netif_input_fn  input 
)

Add a network interface to the list of lwIP netifs.

Parameters
netifa pre-allocated netif structure
ipaddrIP address for the new netif
netmasknetwork mask for the new netif
gwdefault gateway IP address for the new netif
stateopaque data passed to the new netif
initcallback function that initializes the interface
inputcallback function that is called to pass ingress packets up in the protocol layer stack.
Returns
netif, or NULL if failed.

Definition at line 139 of file netif.c.

141{
142
143 LWIP_ASSERT("No init function given", init != NULL);
144
145 /* reset new interface configuration state */
149 netif->flags = 0;
150#if LWIP_DHCP
151 /* netif not under DHCP control by default */
152 netif->dhcp = NULL;
153#endif /* LWIP_DHCP */
154#if LWIP_AUTOIP
155 /* netif not under AutoIP control by default */
156 netif->autoip = NULL;
157#endif /* LWIP_AUTOIP */
158#if LWIP_NETIF_STATUS_CALLBACK
159 netif->status_callback = NULL;
160#endif /* LWIP_NETIF_STATUS_CALLBACK */
161#if LWIP_NETIF_LINK_CALLBACK
162 netif->link_callback = NULL;
163#endif /* LWIP_NETIF_LINK_CALLBACK */
164#if LWIP_IGMP
165 netif->igmp_mac_filter = NULL;
166#endif /* LWIP_IGMP */
167#if ENABLE_LOOPBACK
168 netif->loop_first = NULL;
169 netif->loop_last = NULL;
170#endif /* ENABLE_LOOPBACK */
171
172 /* remember netif specific state information data */
173 netif->state = state;
174 netif->num = netif_num++;
175 netif->input = input;
177#if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
178 netif->loop_cnt_current = 0;
179#endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
180
181 netif_set_addr(netif, ipaddr, netmask, gw);
182
183 /* call user specified initialization function for netif */
184 if (init(netif) != ERR_OK) {
185 return NULL;
186 }
187
188 /* add this netif to the list */
192
193#if LWIP_IGMP
194 /* start IGMP processing */
195 if (netif->flags & NETIF_FLAG_IGMP) {
196 igmp_start(netif);
197 }
198#endif /* LWIP_IGMP */
199
200 LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
201 netif->name[0], netif->name[1]));
203 LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
205 LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
207 LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
208 return netif;
209}
static int state
Definition: maze.c:121
#define NULL
Definition: types.h:112
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
#define ERR_OK
Definition: err.h:52
#define snmp_inc_iflist()
Definition: snmp.h:253
GLenum GLenum GLenum input
Definition: glext.h:9031
#define NETIF_DEBUG
Definition: lwipopts.h:120
#define ip_addr_set_zero(ipaddr)
Definition: ip_addr.h:168
#define ip_addr_debug_print(debug, ipaddr)
Definition: ip_addr.h:212
struct netif * netif_list
Definition: netif.c:75
void netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw)
Definition: netif.c:221
static u8_t netif_num
Definition: netif.c:78
#define NETIF_FLAG_IGMP
Definition: netif.h:95
#define NETIF_SET_HWADDRHINT(netif, hint)
Definition: netif.h:321
Definition: netif.h:136
u8_t flags
Definition: netif.h:192
char name[2]
Definition: netif.h:194
ip_addr_t gw
Definition: netif.h:143
ip_addr_t netmask
Definition: netif.h:142
void * state
Definition: netif.h:172
netif_input_fn input
Definition: netif.h:147
ip_addr_t ip_addr
Definition: netif.h:141
u8_t num
Definition: netif.h:196
struct netif * next
Definition: netif.h:138
static int init
Definition: wintirpc.c:33

Referenced by default_netif_add(), netif_init(), and TCPRegisterInterface().

◆ netif_find()

struct netif * netif_find ( char name)

Find a network interface by searching for its name

Parameters
namethe name of the netif (like netif->name) plus concatenated number in ascii representation (e.g. 'en0')

Definition at line 290 of file netif.c.

291{
292 struct netif *netif;
293 u8_t num;
294
295 if (name == NULL) {
296 return NULL;
297 }
298
299 num = name[2] - '0';
300
301 for(netif = netif_list; netif != NULL; netif = netif->next) {
302 if (num == netif->num &&
303 name[0] == netif->name[0] &&
304 name[1] == netif->name[1]) {
305 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
306 return netif;
307 }
308 }
309 LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
310 return NULL;
311}
unsigned char u8_t
Definition: cc.h:23
GLuint GLuint num
Definition: glext.h:9618
Definition: name.c:39

◆ netif_init()

void netif_init ( void  )

Definition at line 106 of file netif.c.

107{
108#if LWIP_HAVE_LOOPIF
109 ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
110 IP4_ADDR(&loop_gw, 127,0,0,1);
111 IP4_ADDR(&loop_ipaddr, 127,0,0,1);
112 IP4_ADDR(&loop_netmask, 255,0,0,0);
113
114#if NO_SYS
115 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
116#else /* NO_SYS */
117 netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
118#endif /* NO_SYS */
119 netif_set_up(&loop_netif);
120
121#endif /* LWIP_HAVE_LOOPIF */
122}
#define IP4_ADDR(ipaddr, a, b, c, d)
Definition: ip_addr.h:139
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
err_t ip_input(struct pbuf *p, struct netif *inp)
Definition: ip.c:305
struct netif * netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
Definition: netif.c:139
void netif_set_up(struct netif *netif)
Definition: netif.c:453
err_t tcpip_input(struct pbuf *p, struct netif *inp)
Definition: tcpip.c:164

Referenced by lwip_init().

◆ netif_remove()

void netif_remove ( struct netif netif)

Remove a network interface from the list of lwIP netifs.

Parameters
netifthe network interface to remove

Definition at line 235 of file netif.c.

236{
237 if (netif == NULL) {
238 return;
239 }
240
241#if LWIP_IGMP
242 /* stop IGMP processing */
243 if (netif->flags & NETIF_FLAG_IGMP) {
244 igmp_stop(netif);
245 }
246#endif /* LWIP_IGMP */
247 if (netif_is_up(netif)) {
248 /* set netif down before removing (call callback function) */
250 }
251
253
254 /* is it the first netif? */
255 if (netif_list == netif) {
257 } else {
258 /* look for netif further down the list */
259 struct netif * tmpNetif;
260 for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
261 if (tmpNetif->next == netif) {
262 tmpNetif->next = netif->next;
263 break;
264 }
265 }
266 if (tmpNetif == NULL)
267 return; /* we didn't find any netif today */
268 }
270 /* this netif is default? */
271 if (netif_default == netif) {
272 /* reset default netif */
274 }
275#if LWIP_NETIF_REMOVE_CALLBACK
276 if (netif->remove_callback) {
277 netif->remove_callback(netif);
278 }
279#endif /* LWIP_NETIF_REMOVE_CALLBACK */
280 LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
281}
#define snmp_dec_iflist()
Definition: snmp.h:254
#define snmp_delete_ipaddridx_tree(ni)
Definition: snmp.h:279
struct netif * netif_default
Definition: netif.c:76
void netif_set_down(struct netif *netif)
Definition: netif.c:490
void netif_set_default(struct netif *netif)
Definition: netif.c:430
#define netif_is_up(netif)
Definition: netif.h:282

Referenced by default_netif_remove(), and TCPUnregisterInterface().

◆ netif_set_addr()

void netif_set_addr ( struct netif netif,
ip_addr_t ipaddr,
ip_addr_t netmask,
ip_addr_t gw 
)

Change IP address configuration for a network interface (including netmask and default gateway).

Parameters
netifthe network interface to change
ipaddrthe new IP address
netmaskthe new netmask
gwthe new default gateway

Definition at line 221 of file netif.c.

223{
224 netif_set_ipaddr(netif, ipaddr);
225 netif_set_netmask(netif, netmask);
226 netif_set_gw(netif, gw);
227}
void netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
Definition: netif.c:409
void netif_set_gw(struct netif *netif, ip_addr_t *gw)
Definition: netif.c:388
void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
Definition: netif.c:323

Referenced by netif_add(), and TCPUpdateInterfaceIPInformation().

◆ netif_set_default()

void netif_set_default ( struct netif netif)

Set a network interface as the default network interface (used to output all packets for which no specific route is found)

Parameters
netifthe default network interface

Definition at line 430 of file netif.c.

431{
432 if (netif == NULL) {
433 /* remove default route */
435 } else {
436 /* install default route */
438 }
440 LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
441 netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
442}
#define snmp_delete_iprteidx_tree(dflt, ni)
Definition: snmp.h:281
#define snmp_insert_iprteidx_tree(dflt, ni)
Definition: snmp.h:280

Referenced by default_netif_add(), netif_remove(), and TCPUpdateInterfaceIPInformation().

◆ netif_set_down()

void netif_set_down ( struct netif netif)

Bring an interface down, disabling any traffic processing.

Note
: Enabling DHCP on a down interface will make it come up once configured.
See also
dhcp_start()

Definition at line 490 of file netif.c.

491{
492 if (netif->flags & NETIF_FLAG_UP) {
493 netif->flags &= ~NETIF_FLAG_UP;
494#if LWIP_SNMP
496#endif
497
498#if LWIP_ARP
500 etharp_cleanup_netif(netif);
501 }
502#endif /* LWIP_ARP */
504 }
505}
#define snmp_get_sysuptime(value)
Definition: snmp.h:239
#define NETIF_STATUS_CALLBACK(n)
Definition: netif.c:66
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
#define NETIF_FLAG_UP
Definition: netif.h:69

Referenced by netif_remove(), and TCPUpdateInterfaceIPInformation().

◆ netif_set_gw()

void netif_set_gw ( struct netif netif,
ip_addr_t gw 
)

Change the default gateway for a network interface

Parameters
netifthe network interface to change
gwthe new default gateway
Note
call netif_set_addr() if you also want to change ip address and netmask

Definition at line 388 of file netif.c.

389{
390 ip_addr_set(&(netif->gw), gw);
391 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",
392 netif->name[0], netif->name[1],
396 ip4_addr4_16(&netif->gw)));
397}
#define U16_F
Definition: cc.h:36
#define LWIP_DBG_STATE
Definition: debug.h:59
#define LWIP_DBG_TRACE
Definition: debug.h:57
#define ip4_addr1_16(ipaddr)
Definition: ip_addr.h:226
#define ip4_addr2_16(ipaddr)
Definition: ip_addr.h:227
#define ip_addr_set(dest, src)
Definition: ip_addr.h:164
#define ip4_addr3_16(ipaddr)
Definition: ip_addr.h:228
#define ip4_addr4_16(ipaddr)
Definition: ip_addr.h:229

Referenced by netif_set_addr().

◆ netif_set_ipaddr()

void netif_set_ipaddr ( struct netif netif,
ip_addr_t ipaddr 
)

Change the IP address of a network interface

Parameters
netifthe network interface to change
ipaddrthe new IP address
Note
call netif_set_addr() if you also want to change netmask and default gateway

Definition at line 323 of file netif.c.

324{
325 /* TODO: Handling of obsolete pcbs */
326 /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
327#if LWIP_TCP
328 struct tcp_pcb *pcb;
329 struct tcp_pcb_listen *lpcb;
330
331 /* address is actually being changed? */
332 if (ipaddr && (ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
333 /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
334 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
335 pcb = tcp_active_pcbs;
336 while (pcb != NULL) {
337 /* PCB bound to current local interface address? */
338 if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))
339#if LWIP_AUTOIP
340 /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
341 && !ip_addr_islinklocal(&(pcb->local_ip))
342#endif /* LWIP_AUTOIP */
343 ) {
344 /* this connection must be aborted */
345 struct tcp_pcb *next = pcb->next;
346 LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
347 tcp_abort(pcb);
348 pcb = next;
349 } else {
350 pcb = pcb->next;
351 }
352 }
353 for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
354 /* PCB bound to current local interface address? */
355 if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
356 (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
357 /* The PCB is listening to the old ipaddr and
358 * is set to listen to the new one instead */
359 ip_addr_set(&(lpcb->local_ip), ipaddr);
360 }
361 }
362 }
363#endif
366 /* set new IP address to netif */
367 ip_addr_set(&(netif->ip_addr), ipaddr);
370
371 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",
372 netif->name[0], netif->name[1],
377}
#define snmp_insert_ipaddridx_tree(ni)
Definition: snmp.h:278
#define LWIP_AUTOIP
Definition: lwipopts.h:39
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:198
#define ip_addr_isany(addr1)
Definition: ip_addr.h:200
#define ip_addr_islinklocal(addr1)
Definition: ip_addr.h:210
static unsigned __int64 next
Definition: rand_nt.c:6
struct define * next
Definition: compiler.c:65

Referenced by netif_set_addr().

◆ netif_set_link_down()

void netif_set_link_down ( struct netif netif)

Called by a driver when its link goes down

Definition at line 574 of file netif.c.

575{
577 netif->flags &= ~NETIF_FLAG_LINK_UP;
579 }
580}
#define NETIF_LINK_CALLBACK(n)
Definition: netif.c:72
#define NETIF_FLAG_LINK_UP
Definition: netif.h:84

Referenced by TCPUpdateInterfaceLinkStatus().

◆ netif_set_link_up()

void netif_set_link_up ( struct netif netif)

Called by a driver when its link goes up

Definition at line 535 of file netif.c.

536{
537 if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
539
540#if LWIP_DHCP
541 if (netif->dhcp) {
542 dhcp_network_changed(netif);
543 }
544#endif /* LWIP_DHCP */
545
546#if LWIP_AUTOIP
547 if (netif->autoip) {
548 autoip_network_changed(netif);
549 }
550#endif /* LWIP_AUTOIP */
551
552 if (netif->flags & NETIF_FLAG_UP) {
553#if LWIP_ARP
554 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
556 etharp_gratuitous(netif);
557 }
558#endif /* LWIP_ARP */
559
560#if LWIP_IGMP
561 /* resend IGMP memberships */
562 if (netif->flags & NETIF_FLAG_IGMP) {
563 igmp_report_groups( netif);
564 }
565#endif /* LWIP_IGMP */
566 }
568 }
569}

Referenced by TCPUpdateInterfaceLinkStatus().

◆ netif_set_netmask()

void netif_set_netmask ( struct netif netif,
ip_addr_t netmask 
)

Change the netmask of a network interface

Parameters
netifthe network interface to change
netmaskthe new netmask
Note
call netif_set_addr() if you also want to change ip address and default gateway

Definition at line 409 of file netif.c.

410{
412 /* set new netmask to netif */
413 ip_addr_set(&(netif->netmask), netmask);
415 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",
416 netif->name[0], netif->name[1],
421}

Referenced by netif_set_addr().

◆ netif_set_up()

void netif_set_up ( struct netif netif)

Bring an interface up, available for processing traffic.

Note
: Enabling DHCP on a down interface will make it come up once configured.
See also
dhcp_start()

Definition at line 453 of file netif.c.

454{
455 if (!(netif->flags & NETIF_FLAG_UP)) {
457
458#if LWIP_SNMP
460#endif /* LWIP_SNMP */
461
463
465#if LWIP_ARP
466 /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
467 if (netif->flags & (NETIF_FLAG_ETHARP)) {
468 etharp_gratuitous(netif);
469 }
470#endif /* LWIP_ARP */
471
472#if LWIP_IGMP
473 /* resend IGMP memberships */
474 if (netif->flags & NETIF_FLAG_IGMP) {
475 igmp_report_groups( netif);
476 }
477#endif /* LWIP_IGMP */
478 }
479 }
480}

Referenced by default_netif_add(), netif_init(), and TCPUpdateInterfaceIPInformation().

Variable Documentation

◆ netif_default

struct netif* netif_default

The default network interface.

Definition at line 76 of file netif.c.

Referenced by default_netif_add(), default_netif_remove(), ip_route(), netif_remove(), netif_set_default(), and START_TEST().

◆ netif_list

struct netif* netif_list

The list of network interfaces.

Definition at line 75 of file netif.c.

Referenced by ip_input(), ip_route(), netif_add(), netif_find(), netif_remove(), tcp_teardown(), and test_tcp_init_netif().

◆ netif_num

u8_t netif_num
static

Definition at line 78 of file netif.c.

Referenced by netif_add().