[164] | 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 | #include "lwip/sys.h"
|
---|
| 46 | #if LWIP_DHCP
|
---|
| 47 | struct dhcp;
|
---|
| 48 | #endif
|
---|
| 49 | #if LWIP_AUTOIP
|
---|
| 50 | struct autoip;
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | #ifdef __cplusplus
|
---|
| 54 | extern "C" {
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
| 57 | /* Throughout this file, IP addresses are expected to be in
|
---|
| 58 | * the same byte order as in IP_PCB. */
|
---|
| 59 |
|
---|
| 60 | /** must be the maximum of all used hardware address lengths
|
---|
| 61 | across all types of interfaces in use */
|
---|
| 62 | #define NETIF_MAX_HWADDR_LEN 6U
|
---|
| 63 |
|
---|
| 64 | /** Whether the network interface is 'up'. This is
|
---|
| 65 | * a software flag used to control whether this network
|
---|
| 66 | * interface is enabled and processes traffic.
|
---|
| 67 | * It is set by the startup code (for static IP configuration) or
|
---|
| 68 | * by dhcp/autoip when an address has been assigned.
|
---|
| 69 | */
|
---|
| 70 | #define NETIF_FLAG_UP 0x01U
|
---|
| 71 | /** If set, the netif has broadcast capability.
|
---|
| 72 | * Set by the netif driver in its init function. */
|
---|
| 73 | #define NETIF_FLAG_BROADCAST 0x02U
|
---|
| 74 | /** If set, the netif is one end of a point-to-point connection.
|
---|
| 75 | * Set by the netif driver in its init function. */
|
---|
| 76 | #define NETIF_FLAG_POINTTOPOINT 0x04U
|
---|
| 77 | /** If set, the interface is configured using DHCP.
|
---|
| 78 | * Set by the DHCP code when starting or stopping DHCP. */
|
---|
| 79 | #define NETIF_FLAG_DHCP 0x08U
|
---|
| 80 | /** If set, the interface has an active link
|
---|
| 81 | * (set by the network interface driver).
|
---|
| 82 | * Either set by the netif driver in its init function (if the link
|
---|
| 83 | * is up at that time) or at a later point once the link comes up
|
---|
| 84 | * (if link detection is supported by the hardware). */
|
---|
| 85 | #define NETIF_FLAG_LINK_UP 0x10U
|
---|
| 86 | /** If set, the netif is an ethernet device using ARP.
|
---|
| 87 | * Set by the netif driver in its init function.
|
---|
| 88 | * Used to check input packet types and use of DHCP. */
|
---|
| 89 | #define NETIF_FLAG_ETHARP 0x20U
|
---|
| 90 | /** If set, the netif is an ethernet device. It might not use
|
---|
| 91 | * ARP or TCP/IP if it is used for PPPoE only.
|
---|
| 92 | */
|
---|
| 93 | #define NETIF_FLAG_ETHERNET 0x40U
|
---|
| 94 | /** If set, the netif has IGMP capability.
|
---|
| 95 | * Set by the netif driver in its init function. */
|
---|
| 96 | #define NETIF_FLAG_IGMP 0x80U
|
---|
| 97 |
|
---|
| 98 | /** Function prototype for netif init functions. Set up flags and output/linkoutput
|
---|
| 99 | * callback functions in this function.
|
---|
| 100 | *
|
---|
| 101 | * @param netif The netif to initialize
|
---|
| 102 | */
|
---|
| 103 | typedef err_t (*netif_init_fn)(struct netif *netif);
|
---|
| 104 | /** Function prototype for netif->input functions. This function is saved as 'input'
|
---|
| 105 | * callback function in the netif struct. Call it when a packet has been received.
|
---|
| 106 | *
|
---|
| 107 | * @param p The received packet, copied into a pbuf
|
---|
| 108 | * @param inp The netif which received the packet
|
---|
| 109 | */
|
---|
| 110 | typedef err_t (*netif_input_fn)(struct pbuf *p, struct netif *inp);
|
---|
| 111 | /** Function prototype for netif->output functions. Called by lwIP when a packet
|
---|
| 112 | * shall be sent. For ethernet netif, set this to 'etharp_output' and set
|
---|
| 113 | * 'linkoutput'.
|
---|
| 114 | *
|
---|
| 115 | * @param netif The netif which shall send a packet
|
---|
| 116 | * @param p The packet to send (p->payload points to IP header)
|
---|
| 117 | * @param ipaddr The IP address to which the packet shall be sent
|
---|
| 118 | */
|
---|
| 119 | typedef err_t (*netif_output_fn)(struct netif *netif, struct pbuf *p,
|
---|
| 120 | ip_addr_t *ipaddr);
|
---|
| 121 | /** Function prototype for netif->linkoutput functions. Only used for ethernet
|
---|
| 122 | * netifs. This function is called by ARP when a packet shall be sent.
|
---|
| 123 | *
|
---|
| 124 | * @param netif The netif which shall send a packet
|
---|
| 125 | * @param p The packet to send (raw ethernet packet)
|
---|
| 126 | */
|
---|
| 127 | typedef err_t (*netif_linkoutput_fn)(struct netif *netif, struct pbuf *p);
|
---|
| 128 | /** Function prototype for netif status- or link-callback functions. */
|
---|
| 129 | typedef void (*netif_status_callback_fn)(struct netif *netif);
|
---|
| 130 | /** Function prototype for netif igmp_mac_filter functions */
|
---|
| 131 | typedef err_t (*netif_igmp_mac_filter_fn)(struct netif *netif,
|
---|
| 132 | ip_addr_t *group, u8_t action);
|
---|
| 133 |
|
---|
| 134 | /** Generic data structure used for all lwIP network interfaces.
|
---|
| 135 | * The following fields should be filled in by the initialization
|
---|
| 136 | * function for the device driver: hwaddr_len, hwaddr[], mtu, flags */
|
---|
| 137 | struct netif {
|
---|
| 138 | /** pointer to next in linked list */
|
---|
| 139 | struct netif *next;
|
---|
| 140 |
|
---|
| 141 | /** IP address configuration in network byte order */
|
---|
| 142 | ip_addr_t ip_addr;
|
---|
| 143 | ip_addr_t netmask;
|
---|
| 144 | ip_addr_t gw;
|
---|
| 145 |
|
---|
| 146 | /** This function is called by the network device driver
|
---|
| 147 | * to pass a packet up the TCP/IP stack. */
|
---|
| 148 | netif_input_fn input;
|
---|
| 149 | /** This function is called by the IP module when it wants
|
---|
| 150 | * to send a packet on the interface. This function typically
|
---|
| 151 | * first resolves the hardware address, then sends the packet. */
|
---|
| 152 | netif_output_fn output;
|
---|
| 153 | /** This function is called by the ARP module when it wants
|
---|
| 154 | * to send a packet on the interface. This function outputs
|
---|
| 155 | * the pbuf as-is on the link medium. */
|
---|
| 156 | netif_linkoutput_fn linkoutput;
|
---|
| 157 | #if LWIP_NETIF_STATUS_CALLBACK
|
---|
| 158 | /** This function is called when the netif state is set to up or down
|
---|
| 159 | */
|
---|
| 160 | netif_status_callback_fn status_callback;
|
---|
| 161 | #endif /* LWIP_NETIF_STATUS_CALLBACK */
|
---|
| 162 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
| 163 | /** This function is called when the netif link is set to up or down
|
---|
| 164 | */
|
---|
| 165 | netif_status_callback_fn link_callback;
|
---|
| 166 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
| 167 | #if LWIP_NETIF_REMOVE_CALLBACK
|
---|
| 168 | /** This function is called when the netif has been removed */
|
---|
| 169 | netif_status_callback_fn remove_callback;
|
---|
| 170 | #endif /* LWIP_NETIF_REMOVE_CALLBACK */
|
---|
| 171 | /** This field can be set by the device driver and could point
|
---|
| 172 | * to state information for the device. */
|
---|
| 173 | void *state;
|
---|
| 174 | #if LWIP_DHCP
|
---|
| 175 | /** the DHCP client state information for this netif */
|
---|
| 176 | struct dhcp *dhcp;
|
---|
| 177 | #endif /* LWIP_DHCP */
|
---|
| 178 | #if LWIP_AUTOIP
|
---|
| 179 | /** the AutoIP client state information for this netif */
|
---|
| 180 | struct autoip *autoip;
|
---|
| 181 | #endif
|
---|
| 182 | #if LWIP_NETIF_HOSTNAME
|
---|
| 183 | /* the hostname for this netif, NULL is a valid value */
|
---|
| 184 | char* hostname;
|
---|
| 185 | #endif /* LWIP_NETIF_HOSTNAME */
|
---|
| 186 | /** maximum transfer unit (in bytes) */
|
---|
| 187 | u16_t mtu;
|
---|
| 188 | /** number of bytes used in hwaddr */
|
---|
| 189 | u8_t hwaddr_len;
|
---|
| 190 | /** link level hardware address of this interface */
|
---|
| 191 | u8_t hwaddr[NETIF_MAX_HWADDR_LEN];
|
---|
| 192 | /** flags (see NETIF_FLAG_ above) */
|
---|
| 193 | u8_t flags;
|
---|
| 194 | /** descriptive abbreviation */
|
---|
| 195 | char name[2];
|
---|
| 196 | /** number of this interface */
|
---|
| 197 | u8_t num;
|
---|
| 198 | #if LWIP_SNMP
|
---|
| 199 | /** link type (from "snmp_ifType" enum from snmp.h) */
|
---|
| 200 | u8_t link_type;
|
---|
| 201 | /** (estimate) link speed */
|
---|
| 202 | u32_t link_speed;
|
---|
| 203 | /** timestamp at last change made (up/down) */
|
---|
| 204 | u32_t ts;
|
---|
| 205 | /** counters */
|
---|
| 206 | u32_t ifinoctets;
|
---|
| 207 | u32_t ifinucastpkts;
|
---|
| 208 | u32_t ifinnucastpkts;
|
---|
| 209 | u32_t ifindiscards;
|
---|
| 210 | u32_t ifoutoctets;
|
---|
| 211 | u32_t ifoutucastpkts;
|
---|
| 212 | u32_t ifoutnucastpkts;
|
---|
| 213 | u32_t ifoutdiscards;
|
---|
| 214 | #endif /* LWIP_SNMP */
|
---|
| 215 | #if LWIP_IGMP
|
---|
| 216 | /** This function could be called to add or delete a entry in the multicast
|
---|
| 217 | filter table of the ethernet MAC.*/
|
---|
| 218 | netif_igmp_mac_filter_fn igmp_mac_filter;
|
---|
| 219 | #endif /* LWIP_IGMP */
|
---|
| 220 | #if LWIP_NETIF_HWADDRHINT
|
---|
| 221 | u8_t *addr_hint;
|
---|
| 222 | #endif /* LWIP_NETIF_HWADDRHINT */
|
---|
| 223 | #if ENABLE_LOOPBACK
|
---|
| 224 | /* List of packets to be queued for ourselves. */
|
---|
| 225 | struct pbuf *loop_first;
|
---|
| 226 | struct pbuf *loop_last;
|
---|
| 227 | #if LWIP_LOOPBACK_MAX_PBUFS
|
---|
| 228 | u16_t loop_cnt_current;
|
---|
| 229 | #endif /* LWIP_LOOPBACK_MAX_PBUFS */
|
---|
| 230 | #endif /* ENABLE_LOOPBACK */
|
---|
| 231 | };
|
---|
| 232 |
|
---|
| 233 | #if LWIP_SNMP
|
---|
| 234 | #define NETIF_INIT_SNMP(netif, type, speed) \
|
---|
| 235 | /* use "snmp_ifType" enum from snmp.h for "type", snmp_ifType_ethernet_csmacd by example */ \
|
---|
| 236 | (netif)->link_type = (type); \
|
---|
| 237 | /* your link speed here (units: bits per second) */ \
|
---|
| 238 | (netif)->link_speed = (speed); \
|
---|
| 239 | (netif)->ts = 0; \
|
---|
| 240 | (netif)->ifinoctets = 0; \
|
---|
| 241 | (netif)->ifinucastpkts = 0; \
|
---|
| 242 | (netif)->ifinnucastpkts = 0; \
|
---|
| 243 | (netif)->ifindiscards = 0; \
|
---|
| 244 | (netif)->ifoutoctets = 0; \
|
---|
| 245 | (netif)->ifoutucastpkts = 0; \
|
---|
| 246 | (netif)->ifoutnucastpkts = 0; \
|
---|
| 247 | (netif)->ifoutdiscards = 0
|
---|
| 248 | #else /* LWIP_SNMP */
|
---|
| 249 | #define NETIF_INIT_SNMP(netif, type, speed)
|
---|
| 250 | #endif /* LWIP_SNMP */
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | /** The list of network interfaces. */
|
---|
| 254 | extern struct netif *netif_list;
|
---|
| 255 | /** The default network interface. */
|
---|
| 256 | extern struct netif *netif_default;
|
---|
| 257 |
|
---|
| 258 | void netif_init(void);
|
---|
| 259 |
|
---|
| 260 | struct netif *netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
|
---|
| 261 | ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input);
|
---|
| 262 |
|
---|
| 263 | void
|
---|
| 264 | netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
|
---|
| 265 | ip_addr_t *gw);
|
---|
| 266 | void netif_remove(struct netif * netif);
|
---|
| 267 |
|
---|
| 268 | /* Returns a network interface given its name. The name is of the form
|
---|
| 269 | "et0", where the first two letters are the "name" field in the
|
---|
| 270 | netif structure, and the digit is in the num field in the same
|
---|
| 271 | structure. */
|
---|
| 272 | struct netif *netif_find(char *name);
|
---|
| 273 |
|
---|
| 274 | void netif_set_default(struct netif *netif);
|
---|
| 275 |
|
---|
| 276 | void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr);
|
---|
| 277 | void netif_set_netmask(struct netif *netif, ip_addr_t *netmask);
|
---|
| 278 | void netif_set_gw(struct netif *netif, ip_addr_t *gw);
|
---|
| 279 |
|
---|
| 280 | void netif_set_up(struct netif *netif);
|
---|
| 281 | void netif_set_down(struct netif *netif);
|
---|
| 282 | /** Ask if an interface is up */
|
---|
| 283 | #define netif_is_up(netif) (((netif)->flags & NETIF_FLAG_UP) ? (u8_t)1 : (u8_t)0)
|
---|
| 284 |
|
---|
| 285 | #if LWIP_NETIF_STATUS_CALLBACK
|
---|
| 286 | void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback);
|
---|
| 287 | #endif /* LWIP_NETIF_STATUS_CALLBACK */
|
---|
| 288 | #if LWIP_NETIF_REMOVE_CALLBACK
|
---|
| 289 | void netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback);
|
---|
| 290 | #endif /* LWIP_NETIF_REMOVE_CALLBACK */
|
---|
| 291 |
|
---|
| 292 | void netif_set_link_up(struct netif *netif);
|
---|
| 293 | void netif_set_link_down(struct netif *netif);
|
---|
| 294 | /** Ask if a link is up */
|
---|
| 295 | #define netif_is_link_up(netif) (((netif)->flags & NETIF_FLAG_LINK_UP) ? (u8_t)1 : (u8_t)0)
|
---|
| 296 |
|
---|
| 297 | #if LWIP_NETIF_LINK_CALLBACK
|
---|
| 298 | void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback);
|
---|
| 299 | #endif /* LWIP_NETIF_LINK_CALLBACK */
|
---|
| 300 |
|
---|
| 301 | #if LWIP_NETIF_HOSTNAME
|
---|
| 302 | #define netif_set_hostname(netif, name) do { if((netif) != NULL) { (netif)->hostname = name; }}while(0)
|
---|
| 303 | #define netif_get_hostname(netif) (((netif) != NULL) ? ((netif)->hostname) : NULL)
|
---|
| 304 | #endif /* LWIP_NETIF_HOSTNAME */
|
---|
| 305 |
|
---|
| 306 | #if LWIP_IGMP
|
---|
| 307 | #define netif_set_igmp_mac_filter(netif, function) do { if((netif) != NULL) { (netif)->igmp_mac_filter = function; }}while(0)
|
---|
| 308 | #define netif_get_igmp_mac_filter(netif) (((netif) != NULL) ? ((netif)->igmp_mac_filter) : NULL)
|
---|
| 309 | #endif /* LWIP_IGMP */
|
---|
| 310 |
|
---|
| 311 | #if ENABLE_LOOPBACK
|
---|
| 312 | err_t netif_loop_output(struct netif *netif, struct pbuf *p, ip_addr_t *dest_ip);
|
---|
| 313 | void netif_poll(struct netif *netif);
|
---|
| 314 | #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
|
---|
| 315 | void netif_poll_all(void);
|
---|
| 316 | #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
|
---|
| 317 | #endif /* ENABLE_LOOPBACK */
|
---|
| 318 |
|
---|
| 319 | #if LWIP_NETIF_HWADDRHINT
|
---|
| 320 | #define NETIF_SET_HWADDRHINT(netif, hint) ((netif)->addr_hint = (hint))
|
---|
| 321 | #else /* LWIP_NETIF_HWADDRHINT */
|
---|
| 322 | #define NETIF_SET_HWADDRHINT(netif, hint)
|
---|
| 323 | #endif /* LWIP_NETIF_HWADDRHINT */
|
---|
| 324 |
|
---|
| 325 | #ifdef __cplusplus
|
---|
| 326 | }
|
---|
| 327 | #endif
|
---|
| 328 |
|
---|
| 329 | #endif /* __LWIP_NETIF_H__ */
|
---|