rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. |
| 3 | * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv> |
| 4 | * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without modification, |
| 8 | * are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | * this list of conditions and the following disclaimer in the documentation |
| 14 | * and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 21 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 23 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 27 | * OF SUCH DAMAGE. |
| 28 | * |
| 29 | * This file is part of the lwIP TCP/IP stack. |
| 30 | * |
| 31 | * Author: Adam Dunkels <adam@sics.se> |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #ifndef __NETIF_ETHARP_H__ |
| 36 | #define __NETIF_ETHARP_H__ |
| 37 | |
| 38 | #include "lwip/opt.h" |
| 39 | |
| 40 | #if LWIP_ARP || LWIP_ETHERNET /* don't build if not configured for use in lwipopts.h */ |
| 41 | |
| 42 | #include "lwip/pbuf.h" |
| 43 | #include "lwip/ip_addr.h" |
| 44 | #include "lwip/netif.h" |
| 45 | #include "lwip/ip.h" |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | #ifndef ETHARP_HWADDR_LEN |
| 52 | #define ETHARP_HWADDR_LEN 6 |
| 53 | #endif |
| 54 | |
| 55 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 56 | # include "arch/bpstruct.h" |
| 57 | #endif |
| 58 | PACK_STRUCT_BEGIN |
| 59 | struct eth_addr { |
| 60 | PACK_STRUCT_FIELD(u8_t addr[ETHARP_HWADDR_LEN]); |
| 61 | } PACK_STRUCT_STRUCT; |
| 62 | PACK_STRUCT_END |
| 63 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 64 | # include "arch/epstruct.h" |
| 65 | #endif |
| 66 | |
| 67 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 68 | # include "arch/bpstruct.h" |
| 69 | #endif |
| 70 | PACK_STRUCT_BEGIN |
| 71 | /** Ethernet header */ |
| 72 | struct eth_hdr { |
| 73 | #if ETH_PAD_SIZE |
| 74 | PACK_STRUCT_FIELD(u8_t padding[ETH_PAD_SIZE]); |
| 75 | #endif |
| 76 | PACK_STRUCT_FIELD(struct eth_addr dest); |
| 77 | PACK_STRUCT_FIELD(struct eth_addr src); |
| 78 | PACK_STRUCT_FIELD(u16_t type); |
| 79 | } PACK_STRUCT_STRUCT; |
| 80 | PACK_STRUCT_END |
| 81 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 82 | # include "arch/epstruct.h" |
| 83 | #endif |
| 84 | |
| 85 | #define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE) |
| 86 | |
| 87 | #if ETHARP_SUPPORT_VLAN |
| 88 | |
| 89 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 90 | # include "arch/bpstruct.h" |
| 91 | #endif |
| 92 | PACK_STRUCT_BEGIN |
| 93 | /** VLAN header inserted between ethernet header and payload |
| 94 | * if 'type' in ethernet header is ETHTYPE_VLAN. |
| 95 | * See IEEE802.Q */ |
| 96 | struct eth_vlan_hdr { |
| 97 | PACK_STRUCT_FIELD(u16_t prio_vid); |
| 98 | PACK_STRUCT_FIELD(u16_t tpid); |
| 99 | } PACK_STRUCT_STRUCT; |
| 100 | PACK_STRUCT_END |
| 101 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 102 | # include "arch/epstruct.h" |
| 103 | #endif |
| 104 | |
| 105 | #define SIZEOF_VLAN_HDR 4 |
| 106 | #define VLAN_ID(vlan_hdr) (htons((vlan_hdr)->prio_vid) & 0xFFF) |
| 107 | |
| 108 | #endif /* ETHARP_SUPPORT_VLAN */ |
| 109 | |
| 110 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 111 | # include "arch/bpstruct.h" |
| 112 | #endif |
| 113 | PACK_STRUCT_BEGIN |
| 114 | /** the ARP message, see RFC 826 ("Packet format") */ |
| 115 | struct etharp_hdr { |
| 116 | PACK_STRUCT_FIELD(u16_t hwtype); |
| 117 | PACK_STRUCT_FIELD(u16_t proto); |
| 118 | PACK_STRUCT_FIELD(u8_t hwlen); |
| 119 | PACK_STRUCT_FIELD(u8_t protolen); |
| 120 | PACK_STRUCT_FIELD(u16_t opcode); |
| 121 | PACK_STRUCT_FIELD(struct eth_addr shwaddr); |
| 122 | PACK_STRUCT_FIELD(struct ip_addr2 sipaddr); |
| 123 | PACK_STRUCT_FIELD(struct eth_addr dhwaddr); |
| 124 | PACK_STRUCT_FIELD(struct ip_addr2 dipaddr); |
| 125 | } PACK_STRUCT_STRUCT; |
| 126 | PACK_STRUCT_END |
| 127 | #ifdef PACK_STRUCT_USE_INCLUDES |
| 128 | # include "arch/epstruct.h" |
| 129 | #endif |
| 130 | |
| 131 | #define SIZEOF_ETHARP_HDR 28 |
| 132 | #define SIZEOF_ETHARP_PACKET (SIZEOF_ETH_HDR + SIZEOF_ETHARP_HDR) |
| 133 | |
| 134 | /** 5 seconds period */ |
| 135 | #define ARP_TMR_INTERVAL 5000 |
| 136 | |
| 137 | #define ETHTYPE_ARP 0x0806U |
| 138 | #define ETHTYPE_IP 0x0800U |
| 139 | #define ETHTYPE_VLAN 0x8100U |
| 140 | #define ETHTYPE_PPPOEDISC 0x8863U /* PPP Over Ethernet Discovery Stage */ |
| 141 | #define ETHTYPE_PPPOE 0x8864U /* PPP Over Ethernet Session Stage */ |
| 142 | |
| 143 | /** MEMCPY-like macro to copy to/from struct eth_addr's that are local variables |
| 144 | * or known to be 32-bit aligned within the protocol header. */ |
| 145 | #ifndef ETHADDR32_COPY |
| 146 | #define ETHADDR32_COPY(src, dst) SMEMCPY(src, dst, ETHARP_HWADDR_LEN) |
| 147 | #endif |
| 148 | |
| 149 | /** MEMCPY-like macro to copy to/from struct eth_addr's that are no local |
| 150 | * variables and known to be 16-bit aligned within the protocol header. */ |
| 151 | #ifndef ETHADDR16_COPY |
| 152 | #define ETHADDR16_COPY(src, dst) SMEMCPY(src, dst, ETHARP_HWADDR_LEN) |
| 153 | #endif |
| 154 | |
| 155 | #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */ |
| 156 | |
| 157 | /** ARP message types (opcodes) */ |
| 158 | #define ARP_REQUEST 1 |
| 159 | #define ARP_REPLY 2 |
| 160 | |
| 161 | /** Define this to 1 and define LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type) |
| 162 | * to a filter function that returns the correct netif when using multiple |
| 163 | * netifs on one hardware interface where the netif's low-level receive |
| 164 | * routine cannot decide for the correct netif (e.g. when mapping multiple |
| 165 | * IP addresses to one hardware interface). |
| 166 | */ |
| 167 | #ifndef LWIP_ARP_FILTER_NETIF |
| 168 | #define LWIP_ARP_FILTER_NETIF 0 |
| 169 | #endif |
| 170 | |
| 171 | #if ARP_QUEUEING |
| 172 | /** struct for queueing outgoing packets for unknown address |
| 173 | * defined here to be accessed by memp.h |
| 174 | */ |
| 175 | struct etharp_q_entry { |
| 176 | struct etharp_q_entry *next; |
| 177 | struct pbuf *p; |
| 178 | }; |
| 179 | #endif /* ARP_QUEUEING */ |
| 180 | |
| 181 | #define etharp_init() /* Compatibility define, not init needed. */ |
| 182 | void etharp_tmr(void); |
| 183 | s8_t etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr, |
| 184 | struct eth_addr **eth_ret, ip_addr_t **ip_ret); |
| 185 | err_t etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr); |
| 186 | err_t etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q); |
| 187 | err_t etharp_request(struct netif *netif, ip_addr_t *ipaddr); |
| 188 | /** For Ethernet network interfaces, we might want to send "gratuitous ARP"; |
| 189 | * this is an ARP packet sent by a node in order to spontaneously cause other |
| 190 | * nodes to update an entry in their ARP cache. |
| 191 | * From RFC 3220 "IP Mobility Support for IPv4" section 4.6. */ |
| 192 | #define etharp_gratuitous(netif) etharp_request((netif), &(netif)->ip_addr) |
| 193 | void etharp_cleanup_netif(struct netif *netif); |
| 194 | |
| 195 | #if ETHARP_SUPPORT_STATIC_ENTRIES |
| 196 | err_t etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr); |
| 197 | err_t etharp_remove_static_entry(ip_addr_t *ipaddr); |
| 198 | #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */ |
| 199 | |
| 200 | #if LWIP_AUTOIP |
| 201 | err_t etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr, |
| 202 | const struct eth_addr *ethdst_addr, |
| 203 | const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr, |
| 204 | const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr, |
| 205 | const u16_t opcode); |
| 206 | #endif /* LWIP_AUTOIP */ |
| 207 | |
| 208 | #endif /* LWIP_ARP */ |
| 209 | |
| 210 | err_t ethernet_input(struct pbuf *p, struct netif *netif); |
| 211 | |
| 212 | #define eth_addr_cmp(addr1, addr2) (memcmp((addr1)->addr, (addr2)->addr, ETHARP_HWADDR_LEN) == 0) |
| 213 | |
| 214 | extern const struct eth_addr ethbroadcast, ethzero; |
| 215 | |
| 216 | #endif /* LWIP_ARP || LWIP_ETHERNET */ |
| 217 | |
| 218 | #ifdef __cplusplus |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | #endif /* __NETIF_ARP_H__ */ |