1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  *
31  */
32 #ifndef __LWIP_NETIF_H__
33 #define __LWIP_NETIF_H__
34 
35 #include "lwip/opt.h"
36 
37 #define ENABLE_LOOPBACK (LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF)
38 
39 #include "lwip/err.h"
40 
41 #include "lwip/ip_addr.h"
42 
43 #include "lwip/def.h"
44 #include "lwip/pbuf.h"
45 #if LWIP_DHCP
46 struct dhcp;
47 #endif
48 #if LWIP_AUTOIP
49 struct autoip;
50 #endif
51 
52 #ifdef __cplusplus
53 extern "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 #define NETIF_MAX_HWADDR_LEN 6U
62 
63 /** The size of a fully constructed netif name which the
64  * netif can be identified by in APIs. Composed of
65  * 2 chars, 3 (max) digits, and 1 \0
66  */
67 #ifndef NETIF_NAMESIZE
68 #define NETIF_NAMESIZE 6
69 #endif
70 
71 /** Whether the network interface is 'up'. This is
72  * a software flag used to control whether this network
73  * interface is enabled and processes traffic.
74  * It is set by the startup code (for static IP configuration) or
75  * by dhcp/autoip when an address has been assigned.
76  */
77 #define NETIF_FLAG_UP           0x01U
78 /** If set, the netif has broadcast capability.
79  * Set by the netif driver in its init function. */
80 #define NETIF_FLAG_BROADCAST    0x02U
81 /** If set, the netif is one end of a point-to-point connection.
82  * Set by the netif driver in its init function. */
83 #define NETIF_FLAG_POINTTOPOINT 0x04U
84 /** If set, the interface is configured using DHCP.
85  * Set by the DHCP code when starting or stopping DHCP. */
86 #define NETIF_FLAG_DHCP         0x08U
87 /** If set, the interface has an active link
88  *  (set by the network interface driver).
89  * Either set by the netif driver in its init function (if the link
90  * is up at that time) or at a later point once the link comes up
91  * (if link detection is supported by the hardware). */
92 #define NETIF_FLAG_LINK_UP      0x10U
93 /** If set, the netif is an ethernet device using ARP.
94  * Set by the netif driver in its init function.
95  * Used to check input packet types and use of DHCP. */
96 #define NETIF_FLAG_ETHARP       0x20U
97 /** If set, the netif is an ethernet device. It might not use
98  * ARP or TCP/IP if it is used for PPPoE only.
99  */
100 #define NETIF_FLAG_ETHERNET     0x40U
101 /** If set, the netif has IGMP capability.
102  * Set by the netif driver in its init function. */
103 #define NETIF_FLAG_IGMP         0x80U
104 
105 /** Function prototype for netif init functions. Set up flags and output/linkoutput
106  * callback functions in this function.
107  *
108  * @param netif The netif to initialize
109  */
110 typedef err_t (*netif_init_fn)(struct netif *netif);
111 /** Function prototype for netif->input functions. This function is saved as 'input'
112  * callback function in the netif struct. Call it when a packet has been received.
113  *
114  * @param p The received packet, copied into a pbuf
115  * @param inp The netif which received the packet
116  */
117 typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);
118 /** Function prototype for netif->output functions. Called by lwIP when a packet
119  * shall be sent. For ethernet netif, set this to 'etharp_output' and set
120  * 'linkoutput'.
121  *
122  * @param netif The netif which shall send a packet
123  * @param p The packet to send (p->payload points to IP header)
124  * @param ipaddr The IP address to which the packet shall be sent
125  */
126 typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p,
127        ip_addr_t *ipaddr);
128 /** Function prototype for netif->linkoutput functions. Only used for ethernet
129  * netifs. This function is called by ARP when a packet shall be sent.
130  *
131  * @param netif The netif which shall send a packet
132  * @param p The packet to send (raw ethernet packet)
133  */
134 typedef err_t (*netif_linkoutput_fn)(struct netif *netif, struct pbuf *p);
135 /** Function prototype for netif status- or link-callback functions. */
136 typedef void (*netif_status_callback_fn)(struct netif *netif);
137 /** Function prototype for netif igmp_mac_filter functions */
138 typedef err_t (*netif_igmp_mac_filter_fn)(struct netif *netif,
139        ip_addr_t *group, u8_t action);
140 
141 /** Generic data structure used for all lwIP network interfaces.
142  *  The following fields should be filled in by the initialization
143  *  function for the device driver: hwaddr_len, hwaddr[], mtu, flags */
144 struct netif {
145   /** pointer to next in linked list */
146   struct netif *next;
147 
148   /** IP address configuration in network byte order */
149   ip_addr_t ip_addr;
150   ip_addr_t netmask;
151   ip_addr_t gw;
152 
153   /** This function is called by the network device driver
154    *  to pass a packet up the TCP/IP stack. */
155   netif_input_fn input;
156   /** This function is called by the IP module when it wants
157    *  to send a packet on the interface. This function typically
158    *  first resolves the hardware address, then sends the packet. */
159   netif_output_fn output;
160   /** This function is called by the ARP module when it wants
161    *  to send a packet on the interface. This function outputs
162    *  the pbuf as-is on the link medium. */
163   netif_linkoutput_fn linkoutput;
164 #if LWIP_NETIF_STATUS_CALLBACK
165   /** This function is called when the netif state is set to up or down
166    */
167   netif_status_callback_fn status_callback;
168 #endif /* LWIP_NETIF_STATUS_CALLBACK */
169 #if LWIP_NETIF_LINK_CALLBACK
170   /** This function is called when the netif link is set to up or down
171    */
172   netif_status_callback_fn link_callback;
173 #endif /* LWIP_NETIF_LINK_CALLBACK */
174 #if LWIP_NETIF_REMOVE_CALLBACK
175   /** This function is called when the netif has been removed */
176   netif_status_callback_fn remove_callback;
177 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
178   /** This field can be set by the device driver and could point
179    *  to state information for the device. */
180   void *state;
181 #if LWIP_DHCP
182   /** the DHCP client state information for this netif */
183   struct dhcp *dhcp;
184 #endif /* LWIP_DHCP */
185 #if LWIP_AUTOIP
186   /** the AutoIP client state information for this netif */
187   struct autoip *autoip;
188 #endif
189 #if LWIP_NETIF_HOSTNAME
190   /* the hostname for this netif, NULL is a valid value */
191   char*  hostname;
192 #endif /* LWIP_NETIF_HOSTNAME */
193   /** maximum transfer unit (in bytes) */
194   u16_t mtu;
195   /** number of bytes used in hwaddr */
196   u8_t hwaddr_len;
197   /** link level hardware address of this interface */
198   u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
199   /** flags (see NETIF_FLAG_ above) */
200   u8_t flags;
201   /** descriptive abbreviation */
202   char name[NETIF_NAMESIZE];
203   /** number of this interface */
204   u8_t num;
205 #if LWIP_SNMP
206   /** link type (from "snmp_ifType" enum from snmp.h) */
207   u8_t link_type;
208   /** (estimate) link speed */
209   u32_t link_speed;
210   /** timestamp at last change made (up/down) */
211   u32_t ts;
212   /** counters */
213   u32_t ifinoctets;
214   u32_t ifinucastpkts;
215   u32_t ifinnucastpkts;
216   u32_t ifindiscards;
217   u32_t ifoutoctets;
218   u32_t ifoutucastpkts;
219   u32_t ifoutnucastpkts;
220   u32_t ifoutdiscards;
221 #endif /* LWIP_SNMP */
222 #if LWIP_IGMP
223   /** This function could be called to add or delete a entry in the multicast
224       filter table of the ethernet MAC.*/
225   netif_igmp_mac_filter_fn igmp_mac_filter;
226 #endif /* LWIP_IGMP */
227 #if LWIP_NETIF_HWADDRHINT
228   u8_t *addr_hint;
229 #endif /* LWIP_NETIF_HWADDRHINT */
230 #if ENABLE_LOOPBACK
231   /* List of packets to be queued for ourselves. */
232   struct pbuf *loop_first;
233   struct pbuf *loop_last;
234 #if LWIP_LOOPBACK_MAX_PBUFS
235   u16_t loop_cnt_current;
236 #endif /* LWIP_LOOPBACK_MAX_PBUFS */
237 #endif /* ENABLE_LOOPBACK */
238 };
239 
240 #if LWIP_SNMP
241 #define NETIF_INIT_SNMP(netif, type, speed) \
242   /* use "snmp_ifType" enum from snmp.h for "type", snmp_ifType_ethernet_csmacd by example */ \
243   (netif)->link_type = (type);    \
244   /* your link speed here (units: bits per second) */  \
245   (netif)->link_speed = (speed);  \
246   (netif)->ts = 0;              \
247   (netif)->ifinoctets = 0;      \
248   (netif)->ifinucastpkts = 0;   \
249   (netif)->ifinnucastpkts = 0;  \
250   (netif)->ifindiscards = 0;    \
251   (netif)->ifoutoctets = 0;     \
252   (netif)->ifoutucastpkts = 0;  \
253   (netif)->ifoutnucastpkts = 0; \
254   (netif)->ifoutdiscards = 0
255 #else /* LWIP_SNMP */
256 #define NETIF_INIT_SNMP(netif, type, speed)
257 #endif /* LWIP_SNMP */
258 
259 
260 /** The list of network interfaces. */
261 extern struct netif *netif_list;
262 /** The default network interface. */
263 extern struct netif *netif_default;
264 
265 void netif_init(void);
266 
267 struct netif *netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
268       ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input);
269 
270 void
271 netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
272       ip_addr_t *gw);
273 void netif_remove(struct netif * netif);
274 
275 /* Returns a network interface given its name. The name is of the form
276    "et0", where the first two letters are the "name" field in the
277    netif structure, and the digit is in the num field in the same
278    structure. */
279 struct netif *netif_find(char *name);
280 
281 void netif_set_default(struct netif *netif);
282 
283 void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr);
284 void netif_set_netmask(struct netif *netif, ip_addr_t *netmask);
285 void netif_set_gw(struct netif *netif, ip_addr_t *gw);
286 
287 void netif_set_up(struct netif *netif);
288 void netif_set_down(struct netif *netif);
289 /** Ask if an interface is up */
290 #define netif_is_up(netif) (((netif)->flags & NETIF_FLAG_UP) ? (u8_t)1 : (u8_t)0)
291 
292 #if LWIP_NETIF_STATUS_CALLBACK
293 void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback);
294 #endif /* LWIP_NETIF_STATUS_CALLBACK */
295 #if LWIP_NETIF_REMOVE_CALLBACK
296 void netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback);
297 #endif /* LWIP_NETIF_REMOVE_CALLBACK */
298 
299 void netif_set_link_up(struct netif *netif);
300 void netif_set_link_down(struct netif *netif);
301 /** Ask if a link is up */
302 #define netif_is_link_up(netif) (((netif)->flags & NETIF_FLAG_LINK_UP) ? (u8_t)1 : (u8_t)0)
303 
304 #if LWIP_NETIF_LINK_CALLBACK
305 void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback);
306 #endif /* LWIP_NETIF_LINK_CALLBACK */
307 
308 #if LWIP_NETIF_HOSTNAME
309 #define netif_set_hostname(netif, name) do { if((netif) != NULL) { (netif)->hostname = name; }}while(0)
310 #define netif_get_hostname(netif) (((netif) != NULL) ? ((netif)->hostname) : NULL)
311 #endif /* LWIP_NETIF_HOSTNAME */
312 
313 #if LWIP_IGMP
314 #define netif_set_igmp_mac_filter(netif, function) do { if((netif) != NULL) { (netif)->igmp_mac_filter = function; }}while(0)
315 #define netif_get_igmp_mac_filter(netif) (((netif) != NULL) ? ((netif)->igmp_mac_filter) : NULL)
316 #endif /* LWIP_IGMP */
317 
318 #if ENABLE_LOOPBACK
319 err_t netif_loop_output(struct netif *netif, struct pbuf *p, ip_addr_t *dest_ip);
320 void netif_poll(struct netif *netif);
321 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
322 void netif_poll_all(void);
323 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
324 #endif /* ENABLE_LOOPBACK */
325 
326 #if LWIP_NETIF_HWADDRHINT
327 #define NETIF_SET_HWADDRHINT(netif, hint) ((netif)->addr_hint = (hint))
328 #else /* LWIP_NETIF_HWADDRHINT */
329 #define NETIF_SET_HWADDRHINT(netif, hint)
330 #endif /* LWIP_NETIF_HWADDRHINT */
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif /* __LWIP_NETIF_H__ */
337