1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * Description: NAT64 header 15 * Author: NA 16 * Create: 2019 17 */ 18 #ifndef LWIP_HDR_NAT64_H 19 #define LWIP_HDR_NAT64_H 20 21 #include "lwip/opt.h" 22 #include "lwip/lwip_rpl.h" 23 #include "lwip/netif.h" 24 #if LWIP_NAT64 25 #include "lwip/ip.h" 26 #include "lwip/dhcp.h" 27 #include "lwip/prot/tcp.h" 28 #ifndef LWIP_NAT64_ENTRY_SIZE 29 #define LWIP_NAT64_ENTRY_SIZE 255 30 #endif 31 32 #define NAT64_TMR_INTERVAL 1000 33 #define NAT64_TMR_INFINITY 0xfffffffful 34 35 #define NAT64_MBR_EXIT_WAIT_ACK_TIME 3 /* for mesh autolink ack retransmission */ 36 37 #define NAT64_PKT_HANDLED (1) 38 #define NAT64_PKT_NOT_HANDLED (0) 39 40 #define NAT64_RET_FAIL (-1) 41 #define NAT64_RET_OK (0) 42 #define NAT64_RET_ERR (1) 43 #define NAT64_RET_ASSEMBLE (2) 44 45 enum nat64_entry_state_e { 46 NAT64_STATE_INIT, 47 NAT64_STATE_CREATED, 48 NAT64_STATE_DHCP_REQUEST, 49 NAT64_STATE_DHCP_FAIL, 50 #if RPL_CONF_SWITCH_MBR_BY_AUTOLINK 51 NAT64_STATE_TIMEOUT_FOR_MBR, 52 #endif 53 NAT64_STATE_ESTABLISH, 54 NAT64_STATE_DELAY_RELEASE, 55 NAT64_STATE_DIRECT_RELEASE, 56 NAT64_STATE_MAX 57 }; 58 59 typedef struct { 60 linklayer_addr_t mac; 61 #if LWIP_NA_PROXY_UNSOLICITED 62 u8_t na_tries : 4; 63 u8_t na_to : 4; 64 #endif 65 #if !LWIP_NAT64_MIN_SUBSTITUTE 66 ip4_addr_t ip; 67 #endif 68 u32_t lifetime; /* in seconds */ 69 u32_t conn_time; 70 u8_t state : 4; 71 u8_t timeouts : 4; 72 u8_t dao_sn; 73 u8_t mnid; 74 u8_t orig_mnid; 75 u8_t nat64_sync : 1; 76 } nat64_entry_t; 77 78 nat64_entry_t *nat64_get_next_inuse_entry(int *state); 79 typedef err_t (*rpl_nate_cb)(nat64_entry_t *, void *); 80 81 /* init function */ 82 /* 83 * @brief 84 * This API is used to init the nat64 module. 85 * 86 * @param[in] none 87 * 88 * @returns 89 * ERR_OK: On success. 90 * ERR_MEM: On memory alloc fail. 91 */ 92 err_t nat64_init(struct netif *ntf); 93 94 /* 95 * @brief 96 * This API is used to deinit the nat64 module. 97 * 98 * @param[in] none 99 * 100 * @returns 101 * ERR_OK: On success. 102 * ERR_MEM: On memory free fail. 103 */ 104 err_t nat64_deinit(void); 105 106 /* hook function */ 107 /* 108 * @brief 109 * This API is used to hook the ipv4 input packets in two different situations: 110 * 1) when the packet is received by the non-mesh interface in a mesh gate . 111 * 2) when the packet is received by the non-mesh interface in a mesh BR . 112 * 113 * @param 114 * [in] 115 * p : packet received 116 * iphdr : ip header 117 * inp : the interface that receive the packet. 118 * 119 * @returns 120 * 0: Hook has not consumed the packet. 121 * != 0: Hook has consumed the packet. 122 */ 123 int nat64_ip4_input(struct pbuf *p, struct ip_hdr *iphdr, const struct netif *inp); 124 125 /* 126 * @brief 127 * This API is used to hook the ipv6 input packets in two different situations: 128 * 1) when the packet is received by the mesh interface in a mesh gate, if non-mesh interface is ipv4 . 129 * 2) when the packet is received by the mesh interface in a mesh BR, if non-mesh interface is ipv4 . 130 * 131 * @param 132 * [in] 133 * p : packet received 134 * iphdr : ip header 135 * inp : the interface that receive the packet. 136 * 137 * @returns 138 * 0: Hook has not consumed the packet. 139 * != 0: Hook has consumed the packet. 140 */ 141 int nat64_ip6_input(struct pbuf *p, struct ip6_hdr *iphdr, const struct netif *inp); 142 143 /* translate IP function */ 144 /* 145 * @brief 146 * This API is used to translate the ip header from ipv6 to ipv4 147 * 148 * @param 149 * [in] 150 * p6 : packet received 151 * [out] 152 * p4 : packet translated 153 * 154 * @returns 155 * ERR_OK: On success. 156 * ERR_MEM: On memory alloc fail. 157 */ 158 err_t nat64_translate_6to4(struct pbuf *p6, struct pbuf *p4); 159 160 /* 161 * @brief 162 * This API is used to translate the ip header from ipv4 to ipv6 163 * 164 * @param 165 * [in] 166 * p4 : packet received 167 * [out] 168 * p6 : packet translated 169 * 170 * @returns 171 * ERR_OK: On success. 172 * ERR_MEM: On memory alloc fail. 173 */ 174 err_t nat64_translate_4to6(struct pbuf *p4, struct pbuf *p6); 175 176 /* entry api */ 177 /* 178 * @brief 179 * This API is used to add an entry of nat64 in the MBR, when receive DAO with mac: 180 * 181 * @param 182 * [in] 183 * mac: mac address received from DAO 184 * 185 * @returns 186 * != NULL: On success. 187 * NULL: On memory alloc fail. 188 */ 189 err_t nat64_entry_add_new(nat64_entry_t *entry); 190 err_t nat64_entry_remove(nat64_entry_t *entry, u8_t evt_flag); 191 err_t nat64_entry_update(nat64_entry_t *entry, u8_t evt_flag); 192 u8_t nat64_entry_traverse(rpl_nate_cb cb, void *arg); 193 194 nat64_entry_t *nat64_entry_lookup_by_mac(const linklayer_addr_t *mac); 195 196 typedef struct nat64_ipv4_rqst_s { 197 const linklayer_addr_t *lladdr; 198 u32_t lifetime; 199 u32_t *ipv4; 200 u32_t conn_time; 201 u8_t dao_sn; 202 u8_t mnid; 203 } nat64_ipv4_rqst_t; 204 205 int nat64_request_ipv4_addr(const nat64_ipv4_rqst_t *ipv4_rqst); 206 int nat64_delete_ipv4_addr(linklayer_addr_t *lladdr, u8_t mnid); 207 int nat64_delete_entry_by_mnid(u8_t mnid); 208 209 #if LWIP_MMBR && LWIP_NAT64_CHANGE_MSG 210 void nat64_send_change_entry_msg(nat64_entry_t *entry, u8_t type); 211 #endif /* LWIP_MMBR && LWIP_NAT64_CHANGE_MSG */ 212 u8_t nat64_arp_ip4_is_proxy(ip4_addr_t sipaddr, ip4_addr_t dipaddr); 213 struct netif *nat64_netif_get(void); 214 215 int nat64_status_check(void); 216 217 /* timer api */ 218 void nat64_tmr(void); 219 #if LWIP_LOWPOWER 220 u32_t nat64_tmr_tick(void); 221 #endif 222 223 err_t nat64_deinit_netif(const struct netif *ntf); 224 err_t nat64_entry_remove_by_mnid(struct netif *nif, void *arg); 225 226 err_t nat64_entry_mac_to_idx(const u8_t *hwaddr, u8_t hwaddr_len, dhcp_num_t *mac_idx); 227 err_t nat64_entry_idx_to_mac(dhcp_num_t mac_idx, u8_t *hwaddr, u8_t *hwaddr_len); 228 err_t nat64_entry_idx_to_ip6addr(dhcp_num_t mac_idx, ip6_addr_t *ip6addr); 229 230 #if LWIP_NA_PROXY_UNSOLICITED 231 void nat64_proxy_unsolicited_na(const struct netif *netif); 232 #endif 233 234 #endif 235 236 #endif 237