1/**
2 * @file
3 * netif API (to be used from TCPIP thread)
4 */
5
6/*
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 *    this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 *    this list of conditions and the following disclaimer in the documentation
17 *    and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30 * OF SUCH DAMAGE.
31 *
32 * This file is part of the lwIP TCP/IP stack.
33 *
34 * Author: Adam Dunkels <adam@sics.se>
35 *
36 */
37#ifndef LWIP_HDR_NETIF_H
38#define LWIP_HDR_NETIF_H
39
40#include "lwip/opt.h"
41
42#define ENABLE_LOOPBACK (LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF)
43
44#include "lwip/err.h"
45
46#include "lwip/ip_addr.h"
47
48#include "lwip/def.h"
49#include "lwip/pbuf.h"
50#include "lwip/stats.h"
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/* Throughout this file, IP addresses are expected to be in
57 * the same byte order as in IP_PCB. */
58
59/** Must be the maximum of all used hardware address lengths
60    across all types of interfaces in use.
61    This does not have to be changed, normally. */
62#ifndef NETIF_MAX_HWADDR_LEN
63#define NETIF_MAX_HWADDR_LEN 6U
64#endif
65
66/**
67 * @defgroup netif_flags Flags
68 * @ingroup netif
69 * @{
70 */
71
72/** Whether the network interface is 'up'. This is
73 * a software flag used to control whether this network
74 * interface is enabled and processes traffic.
75 * It must be set by the startup code before this netif can be used
76 * (also for dhcp/autoip).
77 */
78#define NETIF_FLAG_UP           0x01U
79/** If set, the netif has broadcast capability.
80 * Set by the netif driver in its init function. */
81#define NETIF_FLAG_BROADCAST    0x02U
82/** If set, the interface has an active link
83 *  (set by the network interface driver).
84 * Either set by the netif driver in its init function (if the link
85 * is up at that time) or at a later point once the link comes up
86 * (if link detection is supported by the hardware). */
87#define NETIF_FLAG_LINK_UP      0x04U
88/** If set, the netif is an ethernet device using ARP.
89 * Set by the netif driver in its init function.
90 * Used to check input packet types and use of DHCP. */
91#define NETIF_FLAG_ETHARP       0x08U
92/** If set, the netif is an ethernet device. It might not use
93 * ARP or TCP/IP if it is used for PPPoE only.
94 */
95#define NETIF_FLAG_ETHERNET     0x10U
96/** If set, the netif has IGMP capability.
97 * Set by the netif driver in its init function. */
98#define NETIF_FLAG_IGMP         0x20U
99/** If set, the netif has MLD6 capability.
100 * Set by the netif driver in its init function. */
101#define NETIF_FLAG_MLD6         0x40U
102
103/**
104 * @}
105 */
106
107enum lwip_internal_netif_client_data_index
108{
109#if LWIP_DHCP
110   LWIP_NETIF_CLIENT_DATA_INDEX_DHCP,
111#endif
112#if LWIP_AUTOIP
113   LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP,
114#endif
115#if LWIP_IGMP
116   LWIP_NETIF_CLIENT_DATA_INDEX_IGMP,
117#endif
118#if LWIP_IPV6_MLD
119   LWIP_NETIF_CLIENT_DATA_INDEX_MLD6,
120#endif
121   LWIP_NETIF_CLIENT_DATA_INDEX_MAX
122};
123
124#if LWIP_CHECKSUM_CTRL_PER_NETIF
125#define NETIF_CHECKSUM_GEN_IP       0x0001
126#define NETIF_CHECKSUM_GEN_UDP      0x0002
127#define NETIF_CHECKSUM_GEN_TCP      0x0004
128#define NETIF_CHECKSUM_GEN_ICMP     0x0008
129#define NETIF_CHECKSUM_GEN_ICMP6    0x0010
130#define NETIF_CHECKSUM_CHECK_IP     0x0100
131#define NETIF_CHECKSUM_CHECK_UDP    0x0200
132#define NETIF_CHECKSUM_CHECK_TCP    0x0400
133#define NETIF_CHECKSUM_CHECK_ICMP   0x0800
134#define NETIF_CHECKSUM_CHECK_ICMP6  0x1000
135#define NETIF_CHECKSUM_ENABLE_ALL   0xFFFF
136#define NETIF_CHECKSUM_DISABLE_ALL  0x0000
137#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
138
139struct netif;
140
141/** MAC Filter Actions, these are passed to a netif's igmp_mac_filter or
142 * mld_mac_filter callback function. */
143enum netif_mac_filter_action {
144  /** Delete a filter entry */
145  NETIF_DEL_MAC_FILTER = 0,
146  /** Add a filter entry */
147  NETIF_ADD_MAC_FILTER = 1
148};
149
150/** Function prototype for netif init functions. Set up flags and output/linkoutput
151 * callback functions in this function.
152 *
153 * @param netif The netif to initialize
154 */
155typedef err_t (*netif_init_fn)(struct netif *netif);
156/** Function prototype for netif->input functions. This function is saved as 'input'
157 * callback function in the netif struct. Call it when a packet has been received.
158 *
159 * @param p The received packet, copied into a pbuf
160 * @param inp The netif which received the packet
161 */
162typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);
163
164#if LWIP_IPV4
165/** Function prototype for netif->output functions. Called by lwIP when a packet
166 * shall be sent. For ethernet netif, set this to 'etharp_output' and set
167 * 'linkoutput'.
168 *
169 * @param netif The netif which shall send a packet
170 * @param p The packet to send (p->payload points to IP header)
171 * @param ipaddr The IP address to which the packet shall be sent
172 */
173typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p,
174       const ip4_addr_t *ipaddr);
175#endif /* LWIP_IPV4*/
176
177#if LWIP_IPV6
178/** Function prototype for netif->output_ip6 functions. Called by lwIP when a packet
179 * shall be sent. For ethernet netif, set this to 'ethip6_output' and set
180 * 'linkoutput'.
181 *
182 * @param netif The netif which shall send a packet
183 * @param p The packet to send (p->payload points to IP header)
184 * @param ipaddr The IPv6 address to which the packet shall be sent
185 */
186typedef err_t (*netif_output_ip6_fn)(struct netif *netif, struct pbuf *p,
187       const ip6_addr_t *ipaddr);
188#endif /* LWIP_IPV6 */
189
190/** Function prototype for netif->linkoutput functions. Only used for ethernet
191 * netifs. This function is called by ARP when a packet shall be sent.
192 *
193 * @param netif The netif which shall send a packet
194 * @param p The packet to send (raw ethernet packet)
195 */
196typedef err_t (*netif_linkoutput_fn)(struct netif *netif, struct pbuf *p);
197/** Function prototype for netif status- or link-callback functions. */
198typedef void (*netif_status_callback_fn)(struct netif *netif);
199#if LWIP_IPV4 && LWIP_IGMP
200/** Function prototype for netif igmp_mac_filter functions */
201typedef err_t (*netif_igmp_mac_filter_fn)(struct netif *netif,
202       const ip4_addr_t *group, enum netif_mac_filter_action action);
203#endif /* LWIP_IPV4 && LWIP_IGMP */
204#if LWIP_IPV6 && LWIP_IPV6_MLD
205/** Function prototype for netif mld_mac_filter functions */
206typedef err_t (*netif_mld_mac_filter_fn)(struct netif *netif,
207       const ip6_addr_t *group, enum netif_mac_filter_action action);
208#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
209
210#if LWIP_DHCP || LWIP_AUTOIP || LWIP_IGMP || LWIP_IPV6_MLD || (LWIP_NUM_NETIF_CLIENT_DATA > 0)
211u8_t netif_alloc_client_data_id(void);
212/** @ingroup netif_cd
213 * Set client data. Obtain ID from netif_alloc_client_data_id().
214 */
215#define netif_set_client_data(netif, id, data) netif_get_client_data(netif, id) = (data)
216/** @ingroup netif_cd
217 * Get client data. Obtain ID from netif_alloc_client_data_id().
218 */
219#define netif_get_client_data(netif, id)       (netif)->client_data[(id)]
220#endif /* LWIP_DHCP || LWIP_AUTOIP || (LWIP_NUM_NETIF_CLIENT_DATA > 0) */
221
222/** Generic data structure used for all lwIP network interfaces.
223 *  The following fields should be filled in by the initialization
224 *  function for the device driver: hwaddr_len, hwaddr[], mtu, flags */
225struct netif {
226  /** pointer to next in linked list */
227  struct netif *next;
228
229#if LWIP_IPV4
230  /** IP address configuration in network byte order */
231  ip_addr_t ip_addr;
232  ip_addr_t netmask;
233  ip_addr_t gw;
234#endif /* LWIP_IPV4 */
235#if LWIP_IPV6
236  /** Array of IPv6 addresses for this netif. */
237  ip_addr_t ip6_addr[LWIP_IPV6_NUM_ADDRESSES];
238  /** The state of each IPv6 address (Tentative, Preferred, etc).
239   * @see ip6_addr.h */
240  u8_t ip6_addr_state[LWIP_IPV6_NUM_ADDRESSES];
241#endif /* LWIP_IPV6 */
242  /** This function is called by the network device driver
243   *  to pass a packet up the TCP/IP stack. */
244  netif_input_fn input;
245#if LWIP_IPV4
246  /** This function is called by the IP module when it wants
247   *  to send a packet on the interface. This function typically
248   *  first resolves the hardware address, then sends the packet.
249   *  For ethernet physical layer, this is usually etharp_output() */
250  netif_output_fn output;
251#endif /* LWIP_IPV4 */
252  /** This function is called by ethernet_output() when it wants
253   *  to send a packet on the interface. This function outputs
254   *  the pbuf as-is on the link medium. */
255  netif_linkoutput_fn linkoutput;
256#if LWIP_IPV6
257  /** This function is called by the IPv6 module when it wants
258   *  to send a packet on the interface. This function typically
259   *  first resolves the hardware address, then sends the packet.
260   *  For ethernet physical layer, this is usually ethip6_output() */
261  netif_output_ip6_fn output_ip6;
262#endif /* LWIP_IPV6 */
263#if LWIP_NETIF_STATUS_CALLBACK
264  /** This function is called when the netif state is set to up or down
265   */
266  netif_status_callback_fn status_callback;
267#endif /* LWIP_NETIF_STATUS_CALLBACK */
268#if LWIP_NETIF_LINK_CALLBACK
269  /** This function is called when the netif link is set to up or down
270   */
271  netif_status_callback_fn link_callback;
272#endif /* LWIP_NETIF_LINK_CALLBACK */
273#if LWIP_NETIF_REMOVE_CALLBACK
274  /** This function is called when the netif has been removed */
275  netif_status_callback_fn remove_callback;
276#endif /* LWIP_NETIF_REMOVE_CALLBACK */
277  /** This field can be set by the device driver and could point
278   *  to state information for the device. */
279  void *state;
280#ifdef netif_get_client_data
281  void* client_data[LWIP_NETIF_CLIENT_DATA_INDEX_MAX + LWIP_NUM_NETIF_CLIENT_DATA];
282#endif
283#if LWIP_IPV6_AUTOCONFIG
284  /** is this netif enabled for IPv6 autoconfiguration */
285  u8_t ip6_autoconfig_enabled;
286#endif /* LWIP_IPV6_AUTOCONFIG */
287#if LWIP_IPV6_SEND_ROUTER_SOLICIT
288  /** Number of Router Solicitation messages that remain to be sent. */
289  u8_t rs_count;
290#endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
291#if LWIP_NETIF_HOSTNAME
292  /* the hostname for this netif, NULL is a valid value */
293  const char*  hostname;
294#endif /* LWIP_NETIF_HOSTNAME */
295#if LWIP_CHECKSUM_CTRL_PER_NETIF
296  u16_t chksum_flags;
297#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF*/
298  /** maximum transfer unit (in bytes) */
299  u16_t mtu;
300  /** number of bytes used in hwaddr */
301  u8_t hwaddr_len;
302  /** link level hardware address of this interface */
303  u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
304  /** flags (@see @ref netif_flags) */
305  u8_t flags;
306  /** descriptive abbreviation */
307  char name[2];
308  /** number of this interface */
309  u8_t num;
310#if MIB2_STATS
311  /** link type (from "snmp_ifType" enum from snmp_mib2.h) */
312  u8_t link_type;
313  /** (estimate) link speed */
314  u32_t link_speed;
315  /** timestamp at last change made (up/down) */
316  u32_t ts;
317  /** counters */
318  struct stats_mib2_netif_ctrs mib2_counters;
319#endif /* MIB2_STATS */
320#if LWIP_IPV4 && LWIP_IGMP
321  /** This function could be called to add or delete an entry in the multicast
322      filter table of the ethernet MAC.*/
323  netif_igmp_mac_filter_fn igmp_mac_filter;
324#endif /* LWIP_IPV4 && LWIP_IGMP */
325#if LWIP_IPV6 && LWIP_IPV6_MLD
326  /** This function could be called to add or delete an entry in the IPv6 multicast
327      filter table of the ethernet MAC. */
328  netif_mld_mac_filter_fn mld_mac_filter;
329#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
330#if LWIP_NETIF_HWADDRHINT
331  u8_t *addr_hint;
332#endif /* LWIP_NETIF_HWADDRHINT */
333#if ENABLE_LOOPBACK
334  /* List of packets to be queued for ourselves. */
335  struct pbuf *loop_first;
336  struct pbuf *loop_last;
337#if LWIP_LOOPBACK_MAX_PBUFS
338  u16_t loop_cnt_current;
339#endif /* LWIP_LOOPBACK_MAX_PBUFS */
340#endif /* ENABLE_LOOPBACK */
341};
342
343#if LWIP_CHECKSUM_CTRL_PER_NETIF
344#define NETIF_SET_CHECKSUM_CTRL(netif, chksumflags) do { \
345  (netif)->chksum_flags = chksumflags; } while(0)
346#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag) if (((netif) == NULL) || (((netif)->chksum_flags & (chksumflag)) != 0))
347#else /* LWIP_CHECKSUM_CTRL_PER_NETIF */
348#define NETIF_SET_CHECKSUM_CTRL(netif, chksumflags)
349#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
350#endif /* LWIP_CHECKSUM_CTRL_PER_NETIF */
351
352/** The list of network interfaces. */
353extern struct netif *netif_list;
354/** The default network interface. */
355extern struct netif *netif_default;
356
357void netif_init(void);
358
359struct netif *netif_add(struct netif *netif,
360#if LWIP_IPV4
361                        const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
362#endif /* LWIP_IPV4 */
363                        void *state, netif_init_fn init, netif_input_fn input);
364#if LWIP_IPV4
365void netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
366                    const ip4_addr_t *gw);
367#endif /* LWIP_IPV4 */
368void netif_remove(struct netif * netif);
369
370/* Returns a network interface given its name. The name is of the form
371   "et0", where the first two letters are the "name" field in the
372   netif structure, and the digit is in the num field in the same
373   structure. */
374struct netif *netif_find(const char *name);
375
376void netif_set_default(struct netif *netif);
377
378#if LWIP_IPV4
379void netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr);
380void netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask);
381void netif_set_gw(struct netif *netif, const ip4_addr_t *gw);
382/** @ingroup netif_ip4 */
383#define netif_ip4_addr(netif)    ((const ip4_addr_t*)ip_2_ip4(&((netif)->ip_addr)))
384/** @ingroup netif_ip4 */
385#define netif_ip4_netmask(netif) ((const ip4_addr_t*)ip_2_ip4(&((netif)->netmask)))
386/** @ingroup netif_ip4 */
387#define netif_ip4_gw(netif)      ((const ip4_addr_t*)ip_2_ip4(&((netif)->gw)))
388/** @ingroup netif_ip4 */
389#define netif_ip_addr4(netif)    ((const ip_addr_t*)&((netif)->ip_addr))
390/** @ingroup netif_ip4 */
391#define netif_ip_netmask4(netif) ((const ip_addr_t*)&((netif)->netmask))
392/** @ingroup netif_ip4 */
393#define netif_ip_gw4(netif)      ((const ip_addr_t*)&((netif)->gw))
394#endif /* LWIP_IPV4 */
395
396void netif_set_up(struct netif *netif);
397void netif_set_down(struct netif *netif);
398/** @ingroup netif
399 * Ask if an interface is up
400 */
401#define netif_is_up(netif) (((netif)->flags & NETIF_FLAG_UP) ? (u8_t)1 : (u8_t)0)
402
403#if LWIP_NETIF_STATUS_CALLBACK
404void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback);
405#endif /* LWIP_NETIF_STATUS_CALLBACK */
406#if LWIP_NETIF_REMOVE_CALLBACK
407void netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback);
408#endif /* LWIP_NETIF_REMOVE_CALLBACK */
409
410void netif_set_link_up(struct netif *netif);
411void netif_set_link_down(struct netif *netif);
412/** Ask if a link is up */
413#define netif_is_link_up(netif) (((netif)->flags & NETIF_FLAG_LINK_UP) ? (u8_t)1 : (u8_t)0)
414
415#if LWIP_NETIF_LINK_CALLBACK
416void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback);
417#endif /* LWIP_NETIF_LINK_CALLBACK */
418
419#if LWIP_NETIF_HOSTNAME
420/** @ingroup netif */
421#define netif_set_hostname(netif, name) do { if((netif) != NULL) { (netif)->hostname = name; }}while(0)
422/** @ingroup netif */
423#define netif_get_hostname(netif) (((netif) != NULL) ? ((netif)->hostname) : NULL)
424#endif /* LWIP_NETIF_HOSTNAME */
425
426#if LWIP_IGMP
427/** @ingroup netif */
428#define netif_set_igmp_mac_filter(netif, function) do { if((netif) != NULL) { (netif)->igmp_mac_filter = function; }}while(0)
429#define netif_get_igmp_mac_filter(netif) (((netif) != NULL) ? ((netif)->igmp_mac_filter) : NULL)
430#endif /* LWIP_IGMP */
431
432#if LWIP_IPV6 && LWIP_IPV6_MLD
433/** @ingroup netif */
434#define netif_set_mld_mac_filter(netif, function) do { if((netif) != NULL) { (netif)->mld_mac_filter = function; }}while(0)
435#define netif_get_mld_mac_filter(netif) (((netif) != NULL) ? ((netif)->mld_mac_filter) : NULL)
436#define netif_mld_mac_filter(netif, addr, action) do { if((netif) && (netif)->mld_mac_filter) { (netif)->mld_mac_filter((netif), (addr), (action)); }}while(0)
437#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
438
439#if ENABLE_LOOPBACK
440err_t netif_loop_output(struct netif *netif, struct pbuf *p);
441void netif_poll(struct netif *netif);
442#if !LWIP_NETIF_LOOPBACK_MULTITHREADING
443void netif_poll_all(void);
444#endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
445#endif /* ENABLE_LOOPBACK */
446
447err_t netif_input(struct pbuf *p, struct netif *inp);
448
449#if LWIP_IPV6
450/** @ingroup netif_ip6 */
451#define netif_ip_addr6(netif, i)  ((const ip_addr_t*)(&((netif)->ip6_addr[i])))
452/** @ingroup netif_ip6 */
453#define netif_ip6_addr(netif, i)  ((const ip6_addr_t*)ip_2_ip6(&((netif)->ip6_addr[i])))
454void netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6);
455void netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3);
456#define netif_ip6_addr_state(netif, i)  ((netif)->ip6_addr_state[i])
457void netif_ip6_addr_set_state(struct netif* netif, s8_t addr_idx, u8_t state);
458s8_t netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr);
459void netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit);
460err_t netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx);
461#define netif_set_ip6_autoconfig_enabled(netif, action) do { if(netif) { (netif)->ip6_autoconfig_enabled = (action); }}while(0)
462#endif /* LWIP_IPV6 */
463
464#if LWIP_NETIF_HWADDRHINT
465#define NETIF_SET_HWADDRHINT(netif, hint) ((netif)->addr_hint = (hint))
466#else /* LWIP_NETIF_HWADDRHINT */
467#define NETIF_SET_HWADDRHINT(netif, hint)
468#endif /* LWIP_NETIF_HWADDRHINT */
469
470#ifdef __cplusplus
471}
472#endif
473
474#endif /* LWIP_HDR_NETIF_H */
475