ipv4_nat.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /**
  2. * NAT - NAT implementation for lwIP supporting TCP/UDP and ICMP.
  3. * Copyright (c) 2009 Christian Walter, ?Embedded Solutions, Vienna 2009.
  4. *
  5. * Copyright (c) 2010 lwIP project ;-)
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  22. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  24. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  27. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. *
  30. * This file is part of the lwIP TCP/IP stack.
  31. */
  32. /*
  33. * File : ipv4_nat.c
  34. * This file is part of RT-Thread RTOS
  35. * COPYRIGHT (C) 2015, RT-Thread Development Team
  36. *
  37. * This program is free software; you can redistribute it and/or modify
  38. * it under the terms of the GNU General Public License as published by
  39. * the Free Software Foundation; either version 2 of the License, or
  40. * (at your option) any later version.
  41. *
  42. * This program is distributed in the hope that it will be useful,
  43. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. * GNU General Public License for more details.
  46. *
  47. * You should have received a copy of the GNU General Public License along
  48. * with this program; if not, write to the Free Software Foundation, Inc.,
  49. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  50. *
  51. * Change Logs:
  52. * Date Author Notes
  53. * 2015-01-26 Hichard porting to RT-Thread
  54. * 2015-01-27 Bernard code cleanup for lwIP in RT-Thread
  55. */
  56. /*
  57. * TODOS:
  58. * - we should decide if we want to use static tables for NAT or a linked
  59. * list.
  60. * - we should allocate icmp ping id if multiple clients are sending
  61. * ping requests.
  62. * - maybe we could hash the identifiers for TCP, ICMP and UDP and use
  63. * a single table structure. This would reduce the code amount although
  64. * it will cost performance.
  65. * - NAT code must check for broadcast addresses and NOT forward
  66. * them.
  67. *
  68. * - netif_remove must notify NAT code when a NAT'ed interface is removed
  69. * - allocate NAT entries from a new memp pool instead of the heap
  70. * - let ttl be ticks, not seconds
  71. *
  72. * HOWTO USE:
  73. *
  74. * Shows how to create NAT between a PPP interface and an internal NIC.
  75. * In this case the network 213.129.231.168/29 is nat'ed when packets
  76. * are sent to the destination network 10.0.0.0/24 (untypical example -
  77. * most users will have the other way around).
  78. *
  79. * Step 1) Execute when network interfaces are ready.
  80. *
  81. * new_nat_entry.out_if = (struct netif *)&PPP_IF;
  82. * new_nat_entry.in_if = (struct netif *)&EMAC_if;
  83. * IP4_ADDR(&new_nat_entry.source_net, 213, 129, 231, 168);
  84. * IP4_ADDR(&new_nat_entry.source_netmask, 255, 255, 255, 248);
  85. * IP4_ADDR(&new_nat_entry.dest_net, 10, 0, 0, 0);
  86. * IP4_ADDR(&new_nat_entry.source_netmask, 255, 0, 0, 0);
  87. * ip_nat_add(&new_nat_entry);
  88. */
  89. #include "ipv4_nat.h"
  90. #include "lwip/opt.h"
  91. #ifdef LWIP_USING_NAT
  92. #include "lwip/ip.h"
  93. #include "lwip/inet.h"
  94. #include "lwip/netif.h"
  95. #include "lwip/ip_addr.h"
  96. #include "lwip/icmp.h"
  97. #include "lwip/tcp_impl.h"
  98. #include "lwip/udp.h"
  99. #include "lwip/mem.h"
  100. #include "lwip/sys.h"
  101. #include "lwip/timers.h"
  102. #include "netif/etharp.h"
  103. #include <limits.h>
  104. #include <string.h>
  105. /** Define this to enable debug output of this module */
  106. #ifndef LWIP_NAT_DEBUG
  107. #define LWIP_NAT_DEBUG LWIP_DBG_OFF
  108. #endif
  109. #define LWIP_NAT_TTL_INFINITE (INT_MAX)
  110. #define LWIP_NAT_DEFAULT_TTL_SECONDS (128)
  111. #define LWIP_NAT_FORWARD_HEADER_SIZE_MIN (sizeof(struct eth_hdr))
  112. #define LWIP_NAT_DEFAULT_STATE_TABLES_ICMP (4)
  113. #define LWIP_NAT_DEFAULT_STATE_TABLES_TCP (32)
  114. #define LWIP_NAT_DEFAULT_STATE_TABLES_UDP (32)
  115. #define LWIP_NAT_DEFAULT_TCP_SOURCE_PORT (40000)
  116. #define LWIP_NAT_DEFAULT_UDP_SOURCE_PORT (40000)
  117. #define IPNAT_ENTRY_RESET(x) do { \
  118. (x)->ttl = 0; \
  119. } while(0)
  120. typedef struct ip_nat_conf
  121. {
  122. struct ip_nat_conf *next;
  123. ip_nat_entry_t entry;
  124. } ip_nat_conf_t;
  125. typedef struct ip_nat_entry_common
  126. {
  127. s32_t ttl; /* @todo: do we really need this to be signed?? */
  128. ip_addr_t source;
  129. ip_addr_t dest;
  130. ip_nat_conf_t *cfg;
  131. } ip_nat_entry_common_t;
  132. typedef struct ip_nat_entries_icmp
  133. {
  134. ip_nat_entry_common_t common;
  135. u16_t id;
  136. u16_t seqno;
  137. } ip_nat_entries_icmp_t;
  138. typedef struct ip_nat_entries_tcp
  139. {
  140. ip_nat_entry_common_t common;
  141. u16_t nport;
  142. u16_t sport;
  143. u16_t dport;
  144. } ip_nat_entries_tcp_t;
  145. typedef struct ip_nat_entries_udp
  146. {
  147. ip_nat_entry_common_t common;
  148. u16_t nport;
  149. u16_t sport;
  150. u16_t dport;
  151. } ip_nat_entries_udp_t;
  152. typedef union u_nat_entry
  153. {
  154. ip_nat_entry_common_t *cmn;
  155. ip_nat_entries_tcp_t *tcp;
  156. ip_nat_entries_icmp_t *icmp;
  157. ip_nat_entries_udp_t *udp;
  158. } nat_entry_t;
  159. static ip_nat_conf_t *ip_nat_cfg = NULL;
  160. static ip_nat_entries_icmp_t ip_nat_icmp_table[LWIP_NAT_DEFAULT_STATE_TABLES_ICMP];
  161. static ip_nat_entries_tcp_t ip_nat_tcp_table[LWIP_NAT_DEFAULT_STATE_TABLES_TCP];
  162. static ip_nat_entries_udp_t ip_nat_udp_table[LWIP_NAT_DEFAULT_STATE_TABLES_UDP];
  163. /* ----------------------- Static functions (COMMON) --------------------*/
  164. static void ip_nat_chksum_adjust(u8_t *chksum, const u8_t *optr, s16_t olen, const u8_t *nptr, s16_t nlen);
  165. static void ip_nat_cmn_init(ip_nat_conf_t *nat_config, const struct ip_hdr *iphdr,
  166. ip_nat_entry_common_t *nat_entry);
  167. static ip_nat_conf_t *ip_nat_shallnat(const struct ip_hdr *iphdr);
  168. static void ip_nat_reset_state(ip_nat_conf_t *cfg);
  169. /* ----------------------- Static functions (DEBUG) ---------------------*/
  170. #if defined(LWIP_DEBUG) && (LWIP_NAT_DEBUG & LWIP_DBG_ON)
  171. static void ip_nat_dbg_dump(const char *msg, const struct ip_hdr *iphdr);
  172. static void ip_nat_dbg_dump_ip(const ip_addr_t *addr);
  173. static void ip_nat_dbg_dump_icmp_nat_entry(const char *msg, const ip_nat_entries_icmp_t *nat_entry);
  174. static void ip_nat_dbg_dump_tcp_nat_entry(const char *msg, const ip_nat_entries_tcp_t *nat_entry);
  175. static void ip_nat_dbg_dump_udp_nat_entry(const char *msg, const ip_nat_entries_udp_t *nat_entry);
  176. static void ip_nat_dbg_dump_init(ip_nat_conf_t *ip_nat_cfg_new);
  177. static void ip_nat_dbg_dump_remove(ip_nat_conf_t *cur);
  178. #else /* defined(LWIP_DEBUG) && (LWIP_NAT_DEBUG & LWIP_DBG_ON) */
  179. #define ip_nat_dbg_dump(msg, iphdr)
  180. #define ip_nat_dbg_dump_ip(addr)
  181. #define ip_nat_dbg_dump_icmp_nat_entry(msg, nat_entry)
  182. #define ip_nat_dbg_dump_tcp_nat_entry(msg, nat_entry)
  183. #define ip_nat_dbg_dump_udp_nat_entry(msg, nat_entry)
  184. #define ip_nat_dbg_dump_init(ip_nat_cfg_new)
  185. #define ip_nat_dbg_dump_remove(cur)
  186. #endif /* defined(LWIP_DEBUG) && (LWIP_NAT_DEBUG & LWIP_DBG_ON) */
  187. /* ----------------------- Static functions (TCP) -----------------------*/
  188. static ip_nat_entries_tcp_t *ip_nat_tcp_lookup_incoming(const struct ip_hdr *iphdr, const struct tcp_hdr *tcphdr);
  189. static ip_nat_entries_tcp_t *ip_nat_tcp_lookup_outgoing(ip_nat_conf_t *nat_config,
  190. const struct ip_hdr *iphdr, const struct tcp_hdr *tcphdr,
  191. u8_t allocate);
  192. /* ----------------------- Static functions (UDP) -----------------------*/
  193. static ip_nat_entries_udp_t *ip_nat_udp_lookup_incoming(const struct ip_hdr *iphdr, const struct udp_hdr *udphdr);
  194. static ip_nat_entries_udp_t *ip_nat_udp_lookup_outgoing(ip_nat_conf_t *nat_config,
  195. const struct ip_hdr *iphdr, const struct udp_hdr *udphdr,
  196. u8_t allocate);
  197. /**
  198. * Timer callback function that calls ip_nat_tmr() and reschedules itself.
  199. *
  200. * @param arg unused argument
  201. */
  202. static void
  203. nat_timer(void *arg)
  204. {
  205. LWIP_UNUSED_ARG(arg);
  206. LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: nat_timer()\n"));
  207. ip_nat_tmr();
  208. sys_timeout(LWIP_NAT_TMR_INTERVAL_SEC * 1000, nat_timer, NULL);
  209. }
  210. /** Initialize this module */
  211. void
  212. ip_nat_init(void)
  213. {
  214. int i;
  215. extern void lwip_ip_input_set_hook(int (*hook)(struct pbuf *p, struct netif *inp));
  216. /* @todo: this can be omitted since we trust static variables
  217. to be initialized to zero */
  218. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_ICMP; i++) {
  219. IPNAT_ENTRY_RESET(&ip_nat_icmp_table[i].common);
  220. }
  221. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_TCP; i++) {
  222. IPNAT_ENTRY_RESET(&ip_nat_tcp_table[i].common);
  223. }
  224. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_UDP; i++) {
  225. IPNAT_ENTRY_RESET(&ip_nat_udp_table[i].common);
  226. }
  227. /* we must lock scheduler to protect following code */
  228. rt_enter_critical();
  229. /* add a lwip timer for NAT */
  230. sys_timeout(LWIP_NAT_TMR_INTERVAL_SEC * 1000, nat_timer, NULL);
  231. /* un-protect */
  232. rt_exit_critical();
  233. }
  234. /** Allocate a new ip_nat_conf_t item */
  235. static ip_nat_conf_t*
  236. ip_nat_alloc(void)
  237. {
  238. ip_nat_conf_t *ret = (ip_nat_conf_t*)mem_malloc(sizeof(ip_nat_conf_t));
  239. return ret;
  240. }
  241. /** Free a removed ip_nat_conf_t item */
  242. static void
  243. ip_nat_free(ip_nat_conf_t *item)
  244. {
  245. LWIP_ASSERT("item != NULL", item != NULL);
  246. mem_free(item);
  247. }
  248. /** Add a new NAT entry
  249. *
  250. * @param new_entry pointer to a structure used to initialize the entry
  251. * @return ERR_OK if succeeded
  252. */
  253. err_t
  254. ip_nat_add(const ip_nat_entry_t *new_entry)
  255. {
  256. err_t err = ERR_VAL;
  257. ip_nat_conf_t *cur = ip_nat_cfg;
  258. ip_nat_conf_t *ip_nat_cfg_new = ip_nat_alloc();
  259. LWIP_ASSERT("new_entry != NULL", new_entry != NULL);
  260. if (ip_nat_cfg_new != NULL) {
  261. SMEMCPY(&ip_nat_cfg_new->entry, new_entry, sizeof(ip_nat_entry_t));
  262. ip_nat_cfg_new->next = NULL;
  263. ip_nat_dbg_dump_init(ip_nat_cfg_new);
  264. if (ip_nat_cfg == NULL) {
  265. ip_nat_cfg = ip_nat_cfg_new;
  266. } else {
  267. /* @todo: do we really need to enqueue the new entry at the end?? */
  268. while (cur->next != NULL) {
  269. cur = cur->next;
  270. }
  271. cur->next = ip_nat_cfg_new;
  272. }
  273. err = ERR_OK;
  274. } else {
  275. err = ERR_MEM;
  276. }
  277. return err;
  278. }
  279. /** Remove a NAT entry previously added by 'ip_nat_add()'.
  280. *
  281. * @param remove_entry describes the entry to remove
  282. */
  283. void
  284. ip_nat_remove(const ip_nat_entry_t *remove_entry)
  285. {
  286. ip_nat_conf_t *cur = ip_nat_cfg;
  287. ip_nat_conf_t *next;
  288. ip_nat_conf_t *previous = NULL;
  289. while (cur != NULL) {
  290. /* Remove the NAT interfaces */
  291. if ((cur->entry.source_net.addr == remove_entry->source_net.addr) &&
  292. (cur->entry.source_netmask.addr == remove_entry->source_netmask.addr) &&
  293. (cur->entry.dest_net.addr == remove_entry->dest_net.addr) &&
  294. (cur->entry.dest_netmask.addr == remove_entry->dest_netmask.addr) &&
  295. (cur->entry.out_if == remove_entry->out_if) &&
  296. (cur->entry.in_if == remove_entry->in_if))
  297. {
  298. ip_nat_dbg_dump_remove(cur);
  299. ip_nat_reset_state(cur);
  300. next = cur->next;
  301. if (cur == ip_nat_cfg) {
  302. ip_nat_cfg = next;
  303. } else {
  304. LWIP_ASSERT("NULL != previous", NULL != previous);
  305. previous->next = next;
  306. }
  307. /* free 'cur' or there will be a memory leak */
  308. ip_nat_free(cur);
  309. return;
  310. } else {
  311. previous = cur;
  312. cur = cur->next;
  313. }
  314. }
  315. }
  316. /** Reset a NAT configured entry to be reused.
  317. * Effectively calls IPNAT_ENTRY_RESET() on 'cfg'.
  318. *
  319. * @param cfg NAT entry to reset
  320. */
  321. static void
  322. ip_nat_reset_state(ip_nat_conf_t *cfg)
  323. {
  324. int i;
  325. /* @todo: optimize this!!!
  326. why do we search for it anyway, if we have the pointer??? */
  327. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_ICMP; i++) {
  328. if(ip_nat_icmp_table[i].common.cfg == cfg) {
  329. IPNAT_ENTRY_RESET(&ip_nat_icmp_table[i].common);
  330. }
  331. }
  332. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_TCP; i++) {
  333. if(ip_nat_tcp_table[i].common.cfg == cfg) {
  334. IPNAT_ENTRY_RESET(&ip_nat_tcp_table[i].common);
  335. }
  336. }
  337. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_UDP; i++) {
  338. if(ip_nat_udp_table[i].common.cfg == cfg) {
  339. IPNAT_ENTRY_RESET(&ip_nat_udp_table[i].common);
  340. }
  341. }
  342. }
  343. /** Check if this packet should be routed or should be translated
  344. *
  345. * @param iphdr the IP header to check
  346. * @return - a NAT entry if the packet shall be translated,
  347. * - NULL if the packet shall be routed normally
  348. */
  349. static ip_nat_conf_t *
  350. ip_nat_shallnat(const struct ip_hdr *iphdr)
  351. {
  352. ip_nat_conf_t *nat_config = ip_nat_cfg;
  353. for (nat_config = ip_nat_cfg; nat_config != NULL; nat_config = nat_config->next) {
  354. if (ip_addr_netcmp(&(iphdr->dest), &(nat_config->entry.dest_net),
  355. &(nat_config->entry.dest_netmask)) ||
  356. ip_addr_netcmp(&(iphdr->src), &(nat_config->entry.source_net),
  357. &(nat_config->entry.source_netmask))) {
  358. break;
  359. }
  360. }
  361. return nat_config;
  362. }
  363. /** Check if the IP header can be hidden and if the remaining packet
  364. * is long enough. p->payload is reset to the IP header on return.
  365. *
  366. * @param p received packet, p->payload pointing to IP header
  367. * @param min_size minimum p->tot_len after hiding IP header
  368. * @return a pointer to the next header (after IP header),
  369. * NULL if hiding IP header fails or the packet is too short
  370. */
  371. static void*
  372. ip_nat_check_header(struct pbuf *p, u16_t min_size)
  373. {
  374. void *ret = NULL;
  375. struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
  376. s16_t iphdr_len = IPH_HL(iphdr) * 4;
  377. if(!pbuf_header(p, -iphdr_len)) {
  378. if(p->tot_len >= min_size) {
  379. ret = p->payload;
  380. }
  381. /* Restore pbuf payload pointer from previous header check. */
  382. pbuf_header(p, iphdr_len);
  383. }
  384. return ret;
  385. }
  386. /** Input processing: check if a received packet belongs to a NAT entry
  387. * and if so, translated it and send it on.
  388. *
  389. * @param p received packet
  390. * @return 1 if the packet has been consumed (it was a NAT packet),
  391. * 0 if the packet has not been consumed (no NAT packet)
  392. */
  393. u8_t
  394. ip_nat_input(struct pbuf *p)
  395. {
  396. struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
  397. struct tcp_hdr *tcphdr;
  398. struct udp_hdr *udphdr;
  399. struct icmp_echo_hdr *icmphdr;
  400. nat_entry_t nat_entry;
  401. err_t err;
  402. u8_t consumed = 0;
  403. int i;
  404. struct pbuf *q = NULL;
  405. nat_entry.cmn = NULL;
  406. ip_nat_dbg_dump("ip_nat_in: checking nat for", iphdr);
  407. switch (IPH_PROTO(iphdr)) {
  408. case IP_PROTO_TCP:
  409. tcphdr = (struct tcp_hdr*)ip_nat_check_header(p, sizeof(struct tcp_hdr));
  410. if (tcphdr == NULL) {
  411. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_input: short tcp packet (%" U16_F " bytes) discarded\n", p->tot_len));
  412. } else {
  413. nat_entry.tcp = ip_nat_tcp_lookup_incoming(iphdr, tcphdr);
  414. if (nat_entry.tcp != NULL) {
  415. /* Refresh TCP entry */
  416. nat_entry.tcp->common.ttl = LWIP_NAT_DEFAULT_TTL_SECONDS;
  417. tcphdr->dest = nat_entry.tcp->sport;
  418. /* Adjust TCP checksum for changed destination port */
  419. ip_nat_chksum_adjust((u8_t *)&(tcphdr->chksum),
  420. (u8_t *)&(nat_entry.tcp->nport), 2, (u8_t *)&(tcphdr->dest), 2);
  421. /* Adjust TCP checksum for changing dest IP address */
  422. ip_nat_chksum_adjust((u8_t *)&(tcphdr->chksum),
  423. (u8_t *)&(nat_entry.cmn->cfg->entry.out_if->ip_addr.addr), 4,
  424. (u8_t *)&(nat_entry.cmn->source.addr), 4);
  425. consumed = 1;
  426. }
  427. }
  428. break;
  429. case IP_PROTO_UDP:
  430. udphdr = (struct udp_hdr *)ip_nat_check_header(p, sizeof(struct udp_hdr));
  431. if (udphdr == NULL) {
  432. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  433. ("ip_nat_input: short udp packet (%" U16_F " bytes) discarded\n",
  434. p->tot_len));
  435. } else {
  436. nat_entry.udp = ip_nat_udp_lookup_incoming(iphdr, udphdr);
  437. if (nat_entry.udp != NULL) {
  438. /* Refresh UDP entry */
  439. nat_entry.udp->common.ttl = LWIP_NAT_DEFAULT_TTL_SECONDS;
  440. udphdr->dest = nat_entry.udp->sport;
  441. /* Adjust UDP checksum for changed destination port */
  442. ip_nat_chksum_adjust((u8_t *)&(udphdr->chksum),
  443. (u8_t *)&(nat_entry.udp->nport), 2, (u8_t *)&(udphdr->dest), 2);
  444. /* Adjust UDP checksum for changing dest IP address */
  445. ip_nat_chksum_adjust((u8_t *)&(udphdr->chksum),
  446. (u8_t *)&(nat_entry.cmn->cfg->entry.out_if->ip_addr.addr), 4,
  447. (u8_t *)&(nat_entry.cmn->source.addr), 4);
  448. consumed = 1;
  449. }
  450. }
  451. break;
  452. case IP_PROTO_ICMP:
  453. icmphdr = (struct icmp_echo_hdr *)ip_nat_check_header(p, sizeof(struct icmp_echo_hdr));
  454. if (icmphdr == NULL) {
  455. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  456. ("ip_nat_out: short icmp echo reply packet (%" U16_F " bytes) discarded\n",
  457. p->tot_len));
  458. } else {
  459. if (ICMP_ER == ICMPH_TYPE(icmphdr)) {
  460. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_ICMP; i++) {
  461. nat_entry.icmp = &ip_nat_icmp_table[i];
  462. if ((nat_entry.icmp->common.ttl) &&
  463. (iphdr->src.addr == nat_entry.icmp->common.dest.addr) &&
  464. (nat_entry.icmp->id == icmphdr->id) &&
  465. (nat_entry.icmp->seqno == icmphdr->seqno)) {
  466. ip_nat_dbg_dump_icmp_nat_entry("found existing nat entry: ", nat_entry.icmp);
  467. consumed = 1;
  468. IPNAT_ENTRY_RESET(nat_entry.cmn);
  469. break;
  470. }
  471. }
  472. }
  473. }
  474. break;
  475. default:
  476. break;
  477. }
  478. if(consumed) {
  479. /* packet consumed, send it out on in_if */
  480. struct netif *in_if;
  481. /* check if the pbuf has room for link headers */
  482. if (pbuf_header(p, PBUF_LINK_HLEN)) {
  483. /* pbuf has no room for link headers, allocate an extra pbuf */
  484. q = pbuf_alloc(PBUF_LINK, 0, PBUF_RAM);
  485. if (q == NULL) {
  486. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_input: no pbuf for outgoing header\n"));
  487. // rt_kprintf("ip_nat_input: no pbuf for outgoing header\n");
  488. /* @todo: stats? */
  489. pbuf_free(p);
  490. p = NULL;
  491. return 1;
  492. } else {
  493. pbuf_cat(q, p);
  494. }
  495. } else {
  496. /* restore p->payload to IP header */
  497. if (pbuf_header(p, -PBUF_LINK_HLEN)) {
  498. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_input: restoring header failed\n"));
  499. // rt_kprintf("ip_nat_input: restoring header failed\n");
  500. /* @todo: stats? */
  501. pbuf_free(p);
  502. p = NULL;
  503. return 1;
  504. }
  505. else q = p;
  506. }
  507. /* if we come here, q is the pbuf to send (either points to p or to a chain) */
  508. in_if = nat_entry.cmn->cfg->entry.in_if;
  509. iphdr->dest.addr = nat_entry.cmn->source.addr;
  510. ip_nat_chksum_adjust((u8_t *) & IPH_CHKSUM(iphdr),
  511. (u8_t *) & (nat_entry.cmn->cfg->entry.out_if->ip_addr.addr), 4,
  512. (u8_t *) & (iphdr->dest.addr), 4);
  513. ip_nat_dbg_dump("ip_nat_input: packet back to source after nat: ", iphdr);
  514. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_input: sending packet on interface ("));
  515. ip_nat_dbg_dump_ip(&(in_if->ip_addr));
  516. LWIP_DEBUGF(LWIP_NAT_DEBUG, (")\n"));
  517. err = in_if->output(in_if, q, (ip_addr_t *)&(iphdr->dest));
  518. if(err != ERR_OK) {
  519. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  520. ("ip_nat_input: failed to send rewritten packet. link layer returned %d\n",
  521. err));
  522. // rt_kprintf("ip_nat_input: failed to send rewritten packet. link layer returned %d\n", err);
  523. }
  524. /* now that q (and/or p) is sent (or not), give up the reference to it
  525. this frees the input pbuf (p) as we have consumed it. */
  526. pbuf_free(q);
  527. }
  528. return consumed;
  529. }
  530. /** Check if one NAT entry timed out */
  531. static void
  532. ip_nat_check_timeout(ip_nat_entry_common_t *nat_entry)
  533. {
  534. if(nat_entry->ttl > 0) {
  535. if(nat_entry->ttl != LWIP_NAT_TTL_INFINITE) {
  536. /* this is not a 'no-timeout' entry */
  537. if(nat_entry->ttl > LWIP_NAT_TMR_INTERVAL_SEC) {
  538. nat_entry->ttl -= LWIP_NAT_TMR_INTERVAL_SEC;
  539. } else {
  540. nat_entry->ttl = 0;
  541. }
  542. }
  543. }
  544. }
  545. /** The NAT timer function, to be called at an interval of
  546. * LWIP_NAT_TMR_INTERVAL_SEC seconds.
  547. */
  548. void
  549. ip_nat_tmr(void)
  550. {
  551. int i;
  552. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_tmr: removing old entries\n"));
  553. for(i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_ICMP; i++) {
  554. ip_nat_check_timeout((ip_nat_entry_common_t *) & ip_nat_icmp_table[i]);
  555. }
  556. for(i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_TCP; i++) {
  557. ip_nat_check_timeout((ip_nat_entry_common_t *) & ip_nat_tcp_table[i]);
  558. }
  559. for(i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_UDP; i++) {
  560. ip_nat_check_timeout((ip_nat_entry_common_t *) & ip_nat_udp_table[i]);
  561. }
  562. }
  563. /** Check if we want to perform NAT with this packet. If so, send it out on
  564. * the correct interface.
  565. *
  566. * @param p the packet to test/send
  567. * @return 1: the packet has been sent using NAT,
  568. * 0: the packet did not belong to a NAT entry
  569. */
  570. u8_t
  571. ip_nat_out(struct pbuf *p)
  572. {
  573. u8_t sent = 0;
  574. err_t err;
  575. struct ip_hdr *iphdr = p->payload;
  576. struct icmp_echo_hdr *icmphdr;
  577. struct tcp_hdr *tcphdr;
  578. struct udp_hdr *udphdr;
  579. ip_nat_conf_t *nat_config;
  580. nat_entry_t nat_entry;
  581. int i;
  582. nat_entry.cmn = NULL;
  583. ip_nat_dbg_dump("ip_nat_out: checking nat for", iphdr);
  584. /* Check if this packet should be routed or should be translated */
  585. nat_config = ip_nat_shallnat(iphdr);
  586. if (nat_config != NULL ) {
  587. if (nat_config->entry.out_if == NULL) {
  588. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_out: no external interface for nat table entry\n"));
  589. } else {
  590. switch (IPH_PROTO(iphdr))
  591. {
  592. case IP_PROTO_TCP:
  593. tcphdr = (struct tcp_hdr *)ip_nat_check_header(p, sizeof(struct tcp_hdr));
  594. if (tcphdr == NULL) {
  595. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  596. ("ip_nat_out: short tcp packet (%" U16_F " bytes) discarded\n", p->tot_len));
  597. } else {
  598. nat_entry.tcp = ip_nat_tcp_lookup_outgoing(nat_config, iphdr, tcphdr, 1);
  599. if (nat_entry.tcp != NULL) {
  600. /* Adjust TCP checksum for changing source port */
  601. tcphdr->src = nat_entry.tcp->nport;
  602. ip_nat_chksum_adjust((u8_t *)&(tcphdr->chksum),
  603. (u8_t *)&(nat_entry.tcp->sport), 2, (u8_t *)&(tcphdr->src), 2);
  604. /* Adjust TCP checksum for changing source IP address */
  605. ip_nat_chksum_adjust((u8_t *)&(tcphdr->chksum),
  606. (u8_t *)&(nat_entry.cmn->source.addr), 4,
  607. (u8_t *)&(nat_entry.cmn->cfg->entry.out_if->ip_addr.addr), 4);
  608. }
  609. }
  610. break;
  611. case IP_PROTO_UDP:
  612. udphdr = (struct udp_hdr *)ip_nat_check_header(p, sizeof(struct udp_hdr));
  613. if (udphdr == NULL) {
  614. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  615. ("ip_nat_out: short udp packet (%" U16_F " bytes) discarded\n", p->tot_len));
  616. } else {
  617. nat_entry.udp = ip_nat_udp_lookup_outgoing(nat_config, iphdr, udphdr, 1);
  618. if (nat_entry.udp != NULL) {
  619. /* Adjust UDP checksum for changing source port */
  620. udphdr->src = nat_entry.udp->nport;
  621. ip_nat_chksum_adjust((u8_t *)&(udphdr->chksum),
  622. (u8_t *)&(nat_entry.udp->sport), 2, (u8_t *) & (udphdr->src), 2);
  623. /* Adjust UDP checksum for changing source IP address */
  624. ip_nat_chksum_adjust((u8_t *)&(udphdr->chksum),
  625. (u8_t *)&(nat_entry.cmn->source.addr), 4,
  626. (u8_t *)&(nat_entry.cmn->cfg->entry.out_if->ip_addr.addr), 4);
  627. }
  628. }
  629. break;
  630. case IP_PROTO_ICMP:
  631. icmphdr = (struct icmp_echo_hdr *)ip_nat_check_header(p, sizeof(struct icmp_echo_hdr));
  632. if(icmphdr == NULL) {
  633. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  634. ("ip_nat_out: short icmp echo packet (%" U16_F " bytes) discarded\n", p->tot_len));
  635. } else {
  636. if (ICMPH_TYPE(icmphdr) == ICMP_ECHO) {
  637. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_ICMP; i++) {
  638. if (!ip_nat_icmp_table[i].common.ttl) {
  639. nat_entry.icmp = &ip_nat_icmp_table[i];
  640. ip_nat_cmn_init(nat_config, iphdr, nat_entry.cmn);
  641. nat_entry.icmp->id = icmphdr->id;
  642. nat_entry.icmp->seqno = icmphdr->seqno;
  643. ip_nat_dbg_dump_icmp_nat_entry(" ip_nat_out: created new NAT entry ", nat_entry.icmp);
  644. break;
  645. }
  646. }
  647. if (NULL == nat_entry.icmp)
  648. {
  649. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_out: no more NAT entries for ICMP available\n"));
  650. }
  651. }
  652. }
  653. break;
  654. default:
  655. break;
  656. }
  657. if (nat_entry.cmn != NULL) {
  658. struct netif *out_if = nat_entry.cmn->cfg->entry.out_if;
  659. /* Exchange the IP source address with the address of the interface
  660. * where the packet will be sent.
  661. */
  662. /* @todo: check nat_config->entry.out_if agains nat_entry.cmn->cfg->entry.out_if */
  663. iphdr->src.addr = nat_config->entry.out_if->ip_addr.addr;
  664. ip_nat_chksum_adjust((u8_t *) & IPH_CHKSUM(iphdr),
  665. (u8_t *) & (nat_entry.cmn->source.addr), 4, (u8_t *) & iphdr->src.addr, 4);
  666. ip_nat_dbg_dump("ip_nat_out: rewritten packet", iphdr);
  667. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_out: sending packet on interface ("));
  668. ip_nat_dbg_dump_ip(&(out_if->ip_addr));
  669. LWIP_DEBUGF(LWIP_NAT_DEBUG, (")\n"));
  670. err = out_if->output(out_if, p, (ip_addr_t *)&(iphdr->dest));
  671. if (err != ERR_OK) {
  672. LWIP_DEBUGF(LWIP_NAT_DEBUG,
  673. ("ip_nat_out: failed to send rewritten packet. link layer returned %d\n", err));
  674. // rt_kprintf("ip_nat_out: failed to send rewritten packet. link layer returned %d\n", err);
  675. } else {
  676. sent = 1;
  677. }
  678. }
  679. }
  680. }
  681. return sent;
  682. }
  683. /** Initialize common parts of a NAT entry
  684. *
  685. * @param nat_config NAT config entry
  686. * @param iphdr IP header from which to initialize the entry
  687. * @param nat_entry entry to initialize
  688. */
  689. static void
  690. ip_nat_cmn_init(ip_nat_conf_t *nat_config, const struct ip_hdr *iphdr, ip_nat_entry_common_t *nat_entry)
  691. {
  692. LWIP_ASSERT("NULL != nat_entry", NULL != nat_entry);
  693. LWIP_ASSERT("NULL != nat_config", NULL != nat_config);
  694. LWIP_ASSERT("NULL != iphdr", NULL != iphdr);
  695. nat_entry->cfg = nat_config;
  696. nat_entry->dest = *((ip_addr_t *)&iphdr->dest);
  697. nat_entry->source = *((ip_addr_t *)&iphdr->src);
  698. nat_entry->ttl = LWIP_NAT_DEFAULT_TTL_SECONDS;
  699. }
  700. /**
  701. * This function checks for incoming packets if we already have a NAT entry.
  702. * If yes a pointer to the NAT entry is returned. Otherwise NULL.
  703. *
  704. * @param nat_config NAT configuration.
  705. * @param iphdr The IP header.
  706. * @param udphdr The UDP header.
  707. * @return A pointer to an existing NAT entry or
  708. * NULL if none is found.
  709. */
  710. static ip_nat_entries_udp_t *
  711. ip_nat_udp_lookup_incoming(const struct ip_hdr *iphdr, const struct udp_hdr *udphdr)
  712. {
  713. int i;
  714. ip_nat_entries_udp_t *nat_entry = NULL;
  715. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_UDP; i++) {
  716. if (ip_nat_udp_table[i].common.ttl) {
  717. if ((iphdr->src.addr == ip_nat_udp_table[i].common.dest.addr) &&
  718. (udphdr->src == ip_nat_udp_table[i].dport) &&
  719. (udphdr->dest == ip_nat_udp_table[i].nport)) {
  720. nat_entry = &ip_nat_udp_table[i];
  721. ip_nat_dbg_dump_udp_nat_entry("ip_nat_udp_lookup_incoming: found existing nat entry: ",
  722. nat_entry);
  723. break;
  724. }
  725. }
  726. }
  727. return nat_entry;
  728. }
  729. /**
  730. * This function checks if we already have a NAT entry for this UDP connection.
  731. * If yes the a pointer to this NAT entry is returned.
  732. *
  733. * @param iphdr The IP header.
  734. * @param udphdr The UDP header.
  735. * @param allocate If no existing NAT entry is found and this flag is true
  736. * a NAT entry is allocated.
  737. */
  738. static ip_nat_entries_udp_t *
  739. ip_nat_udp_lookup_outgoing(ip_nat_conf_t *nat_config, const struct ip_hdr *iphdr,
  740. const struct udp_hdr *udphdr, u8_t allocate)
  741. {
  742. int i;
  743. nat_entry_t nat_entry;
  744. int last_free = -1;
  745. nat_entry.cmn = NULL;
  746. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_UDP; i++) {
  747. if (ip_nat_udp_table[i].common.ttl) {
  748. if ((iphdr->src.addr == ip_nat_udp_table[i].common.source.addr) &&
  749. (iphdr->dest.addr == ip_nat_udp_table[i].common.dest.addr) &&
  750. (udphdr->src == ip_nat_udp_table[i].sport) &&
  751. (udphdr->dest == ip_nat_udp_table[i].dport)) {
  752. nat_entry.udp = &ip_nat_udp_table[i];
  753. ip_nat_dbg_dump_udp_nat_entry("ip_nat_udp_lookup_outgoing: found existing nat entry: ",
  754. nat_entry.udp);
  755. break;
  756. }
  757. } else {
  758. last_free = i;
  759. }
  760. }
  761. if (nat_entry.cmn == NULL) {
  762. if (allocate) {
  763. if (last_free != -1) {
  764. nat_entry.udp = &ip_nat_udp_table[last_free];
  765. nat_entry.udp->nport = htons((u16_t) (LWIP_NAT_DEFAULT_UDP_SOURCE_PORT + i));
  766. nat_entry.udp->sport = udphdr->src;
  767. nat_entry.udp->dport = udphdr->dest;
  768. ip_nat_cmn_init(nat_config, iphdr, nat_entry.cmn);
  769. ip_nat_dbg_dump_udp_nat_entry("ip_nat_udp_lookup_outgoing: created new nat entry: ",
  770. nat_entry.udp);
  771. } else {
  772. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_udp_lookup_outgoing: no more NAT entries available\n"));
  773. // rt_kprintf("ip_nat_udp_lookup_outgoing: no more NAT entries available\n");
  774. }
  775. }
  776. }
  777. return nat_entry.udp;
  778. }
  779. /**
  780. * This function checks for incoming packets if we already have a NAT entry.
  781. * If yes a pointer to the NAT entry is returned. Otherwise NULL.
  782. *
  783. * @param nat_config NAT configuration.
  784. * @param iphdr The IP header.
  785. * @param tcphdr The TCP header.
  786. * @return A pointer to an existing NAT entry or NULL if none is found.
  787. */
  788. static ip_nat_entries_tcp_t *
  789. ip_nat_tcp_lookup_incoming(const struct ip_hdr *iphdr, const struct tcp_hdr *tcphdr)
  790. {
  791. int i;
  792. ip_nat_entries_tcp_t *nat_entry = NULL;
  793. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_TCP; i++) {
  794. if (ip_nat_tcp_table[i].common.ttl) {
  795. if ((iphdr->src.addr == ip_nat_tcp_table[i].common.dest.addr) &&
  796. (tcphdr->src == ip_nat_tcp_table[i].dport) &&
  797. (tcphdr->dest == ip_nat_tcp_table[i].nport)) {
  798. nat_entry = &ip_nat_tcp_table[i];
  799. ip_nat_dbg_dump_tcp_nat_entry("ip_nat_tcp_lookup_incoming: found existing nat entry: ",
  800. nat_entry);
  801. break;
  802. }
  803. }
  804. }
  805. return nat_entry;
  806. }
  807. /**
  808. * This function checks if we already have a NAT entry for this TCP connection.
  809. * If yes the a pointer to this NAT entry is returned.
  810. *
  811. * @param iphdr The IP header.
  812. * @param tcphdr The TCP header.
  813. * @param allocate If no existing NAT entry is found and this flag is true
  814. * a NAT entry is allocated.
  815. */
  816. static ip_nat_entries_tcp_t *
  817. ip_nat_tcp_lookup_outgoing(ip_nat_conf_t *nat_config, const struct ip_hdr *iphdr,
  818. const struct tcp_hdr *tcphdr, u8_t allocate)
  819. {
  820. int i;
  821. nat_entry_t nat_entry;
  822. int last_free = -1;
  823. nat_entry.cmn = NULL;
  824. for (i = 0; i < LWIP_NAT_DEFAULT_STATE_TABLES_TCP; i++) {
  825. if (ip_nat_tcp_table[i].common.ttl) {
  826. if ((iphdr->src.addr == ip_nat_tcp_table[i].common.source.addr) &&
  827. (iphdr->dest.addr == ip_nat_tcp_table[i].common.dest.addr) &&
  828. (tcphdr->src == ip_nat_tcp_table[i].sport) &&
  829. (tcphdr->dest == ip_nat_tcp_table[i].dport)) {
  830. nat_entry.tcp = &ip_nat_tcp_table[i];
  831. ip_nat_dbg_dump_tcp_nat_entry("ip_nat_tcp_lookup_outgoing: found existing nat entry: ",
  832. nat_entry.tcp);
  833. break;
  834. }
  835. } else {
  836. last_free = i;
  837. }
  838. }
  839. if (nat_entry.cmn == NULL) {
  840. if (allocate) {
  841. if (last_free != -1) {
  842. nat_entry.tcp = &ip_nat_tcp_table[last_free];
  843. nat_entry.tcp->nport = htons((u16_t) (LWIP_NAT_DEFAULT_TCP_SOURCE_PORT + i));
  844. nat_entry.tcp->sport = tcphdr->src;
  845. nat_entry.tcp->dport = tcphdr->dest;
  846. ip_nat_cmn_init(nat_config, iphdr, nat_entry.cmn);
  847. ip_nat_dbg_dump_tcp_nat_entry("ip_nat_tcp_lookup_outgoing: created new nat entry: ",
  848. nat_entry.tcp);
  849. } else {
  850. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_udp_lookup_outgoing: no more NAT entries available\n"));
  851. // rt_kprintf("ip_nat_udp_lookup_outgoing: no more NAT entries available\n");
  852. }
  853. }
  854. }
  855. return nat_entry.tcp;
  856. }
  857. /** Adjusts the checksum of a NAT'ed packet without having to completely recalculate it
  858. * @todo: verify this works for little- and big-endian
  859. *
  860. * @param chksum points to the chksum in the packet
  861. * @param optr points to the old data in the packet
  862. * @param olen length of old data
  863. * @param nptr points to the new data in the packet
  864. * @param nlen length of new data
  865. */
  866. static void
  867. ip_nat_chksum_adjust(u8_t *chksum, const u8_t *optr, s16_t olen, const u8_t *nptr, s16_t nlen)
  868. {
  869. s32_t x, oldval, newval;
  870. LWIP_ASSERT("NULL != chksum", NULL != chksum);
  871. LWIP_ASSERT("NULL != optr", NULL != optr);
  872. LWIP_ASSERT("NULL != nptr", NULL != nptr);
  873. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_chksum_adjust: chksum=%p, optr=%p, olen=%" U16_F ", nptr=%p, nlen=%" U16_F "\n",
  874. chksum, optr, olen, nptr, nlen));
  875. x = chksum[0] * 256 + chksum[1];
  876. x = ~x & 0xFFFF;
  877. while (olen) {
  878. oldval = optr[0] * 256 + optr[1];
  879. optr += 2;
  880. x -= oldval & 0xffff;
  881. if (x <= 0) {
  882. x--;
  883. x &= 0xffff;
  884. }
  885. olen -= 2;
  886. }
  887. while (nlen) {
  888. newval = nptr[0] * 256 + nptr[1];
  889. nptr += 2;
  890. x += newval & 0xffff;
  891. if (x & 0x10000) {
  892. x++;
  893. x &= 0xffff;
  894. }
  895. nlen -= 2;
  896. }
  897. x = ~x & 0xFFFF;
  898. chksum[0] = x / 256;
  899. chksum[1] = x & 0xff;
  900. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_chksum_adjust: chksum = 0x%x\n", *((u16_t *) chksum)));
  901. }
  902. #if defined(LWIP_DEBUG) && (LWIP_NAT_DEBUG & LWIP_DBG_ON)
  903. /**
  904. * This function dumps an IP address
  905. *
  906. * @param addr IP address
  907. */
  908. static void
  909. ip_nat_dbg_dump_ip(const ip_addr_t *addr)
  910. {
  911. LWIP_ASSERT("NULL != addr", NULL != addr);
  912. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("%" U16_F ".%" U16_F ".%" U16_F ".%" U16_F,
  913. ip4_addr1(addr), ip4_addr2(addr), ip4_addr3(addr), ip4_addr4(addr)));
  914. }
  915. /**
  916. * This function dumps an IP header
  917. *
  918. * @param msg a message to print
  919. * @param iphdr IP header
  920. */
  921. static void
  922. ip_nat_dbg_dump(const char *msg, const struct ip_hdr *iphdr)
  923. {
  924. LWIP_ASSERT("NULL != msg", NULL != msg);
  925. LWIP_ASSERT("NULL != iphdr", NULL != iphdr);
  926. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("%s: IP: (", msg));
  927. ip_nat_dbg_dump_ip((ip_addr_t *)&(iphdr->src));
  928. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  929. ip_nat_dbg_dump_ip((ip_addr_t *)&(iphdr->dest));
  930. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" id=%" U16_F ", chksum=%" U16_F ")\n",
  931. ntohs(IPH_ID(iphdr)), ntohs(IPH_CHKSUM(iphdr))));
  932. }
  933. /**
  934. * This function dumps an ICMP echo reply/recho request nat entry.
  935. *
  936. * @param msg a message to print
  937. * @param nat_entry the ICMP NAT entry to print
  938. */
  939. static void
  940. ip_nat_dbg_dump_icmp_nat_entry(const char *msg, const ip_nat_entries_icmp_t *nat_entry)
  941. {
  942. LWIP_ASSERT("NULL != msg", NULL != msg);
  943. LWIP_ASSERT("NULL != nat_entry", NULL != nat_entry);
  944. LWIP_ASSERT("NULL != nat_entry->common.cfg", NULL != nat_entry->common.cfg);
  945. LWIP_ASSERT("NULL != nat_entry->common.cfg->entry.out_if",
  946. NULL != nat_entry->common.cfg->entry.out_if);
  947. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("%s", msg));
  948. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ICMP : ("));
  949. ip_nat_dbg_dump_ip(&(nat_entry->common.source));
  950. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  951. ip_nat_dbg_dump_ip(&(nat_entry->common.dest));
  952. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" id=%" U16_F, ntohs(nat_entry->id)));
  953. LWIP_DEBUGF(LWIP_NAT_DEBUG, (", seq=%" U16_F, ntohs(nat_entry->seqno)));
  954. LWIP_DEBUGF(LWIP_NAT_DEBUG, (") mapped at ("));
  955. ip_nat_dbg_dump_ip(&(nat_entry->common.cfg->entry.out_if->ip_addr));
  956. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  957. ip_nat_dbg_dump_ip(&(nat_entry->common.dest));
  958. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" id=%" U16_F, ntohs(nat_entry->id)));
  959. LWIP_DEBUGF(LWIP_NAT_DEBUG, (", seq=%" U16_F, ntohs(nat_entry->seqno)));
  960. LWIP_DEBUGF(LWIP_NAT_DEBUG, (")\n"));
  961. }
  962. /**
  963. * This function dumps an TCP nat entry.
  964. *
  965. * @param msg a message to print
  966. * @param nat_entry the TCP NAT entry to print
  967. */
  968. static void
  969. ip_nat_dbg_dump_tcp_nat_entry(const char *msg, const ip_nat_entries_tcp_t *nat_entry)
  970. {
  971. LWIP_ASSERT("NULL != msg", NULL != msg);
  972. LWIP_ASSERT("NULL != nat_entry", NULL != nat_entry);
  973. LWIP_ASSERT("NULL != nat_entry->common.cfg", NULL != nat_entry->common.cfg);
  974. LWIP_ASSERT("NULL != nat_entry->common.cfg->entry.out_if",
  975. NULL != nat_entry->common.cfg->entry.out_if);
  976. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("%s", msg));
  977. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("TCP : ("));
  978. ip_nat_dbg_dump_ip(&(nat_entry->common.source));
  979. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->sport)));
  980. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  981. ip_nat_dbg_dump_ip(&(nat_entry->common.dest));
  982. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->dport)));
  983. LWIP_DEBUGF(LWIP_NAT_DEBUG, (") mapped at ("));
  984. ip_nat_dbg_dump_ip(&(nat_entry->common.cfg->entry.out_if->ip_addr));
  985. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->nport)));
  986. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  987. ip_nat_dbg_dump_ip(&(nat_entry->common.dest));
  988. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->dport)));
  989. LWIP_DEBUGF(LWIP_NAT_DEBUG, (")\n"));
  990. }
  991. /**
  992. * This function dumps a UDP NAT entry.
  993. *
  994. * @param msg a message to print
  995. * @param nat_entry the UDP NAT entry to print
  996. */
  997. static void
  998. ip_nat_dbg_dump_udp_nat_entry(const char *msg, const ip_nat_entries_udp_t *nat_entry)
  999. {
  1000. LWIP_ASSERT("NULL != msg", NULL != msg);
  1001. LWIP_ASSERT("NULL != nat_entry", NULL != nat_entry);
  1002. LWIP_ASSERT("NULL != nat_entry->common.cfg", NULL != nat_entry->common.cfg);
  1003. LWIP_ASSERT("NULL != nat_entry->common.cfg->entry.out_if",
  1004. NULL != nat_entry->common.cfg->entry.out_if);
  1005. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("%s", msg));
  1006. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("UDP : ("));
  1007. ip_nat_dbg_dump_ip(&(nat_entry->common.source));
  1008. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->sport)));
  1009. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  1010. ip_nat_dbg_dump_ip(&(nat_entry->common.dest));
  1011. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->dport)));
  1012. LWIP_DEBUGF(LWIP_NAT_DEBUG, (") mapped at ("));
  1013. ip_nat_dbg_dump_ip(&(nat_entry->common.cfg->entry.out_if->ip_addr));
  1014. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->nport)));
  1015. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  1016. ip_nat_dbg_dump_ip(&(nat_entry->common.dest));
  1017. LWIP_DEBUGF(LWIP_NAT_DEBUG, (":%" U16_F, ntohs(nat_entry->dport)));
  1018. LWIP_DEBUGF(LWIP_NAT_DEBUG, (")\n"));
  1019. }
  1020. /** Prints some info when creating a new NAT entry */
  1021. static void
  1022. ip_nat_dbg_dump_init(ip_nat_conf_t *ip_nat_cfg_new)
  1023. {
  1024. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_init: added new NAT interface\n"));
  1025. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_init: "));
  1026. ip_nat_dbg_dump_ip(&(ip_nat_cfg_new->entry.source_net));
  1027. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("/"));
  1028. ip_nat_dbg_dump_ip(&(ip_nat_cfg_new->entry.source_netmask));
  1029. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("@"));
  1030. ip_nat_dbg_dump_ip(&(ip_nat_cfg_new->entry.in_if->ip_addr));
  1031. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  1032. ip_nat_dbg_dump_ip(&(ip_nat_cfg_new->entry.dest_net));
  1033. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("/"));
  1034. ip_nat_dbg_dump_ip(&(ip_nat_cfg_new->entry.dest_netmask));
  1035. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("@"));
  1036. ip_nat_dbg_dump_ip(&(ip_nat_cfg_new->entry.out_if->ip_addr));
  1037. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("\n"));
  1038. }
  1039. /** Prints some info when removing a NAT entry */
  1040. static void
  1041. ip_nat_dbg_dump_remove(ip_nat_conf_t *cur)
  1042. {
  1043. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_remove: removing existing NAT interface\n"));
  1044. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("ip_nat_remove: "));
  1045. ip_nat_dbg_dump_ip(&(cur->entry.source_net));
  1046. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("/"));
  1047. ip_nat_dbg_dump_ip(&(cur->entry.source_netmask));
  1048. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("@"));
  1049. ip_nat_dbg_dump_ip(&(cur->entry.in_if->ip_addr));
  1050. LWIP_DEBUGF(LWIP_NAT_DEBUG, (" --> "));
  1051. ip_nat_dbg_dump_ip(&(cur->entry.dest_net));
  1052. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("/"));
  1053. ip_nat_dbg_dump_ip(&(cur->entry.dest_netmask));
  1054. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("@"));
  1055. ip_nat_dbg_dump_ip(&(cur->entry.out_if->ip_addr));
  1056. LWIP_DEBUGF(LWIP_NAT_DEBUG, ("\n"));
  1057. }
  1058. #endif /* defined(LWIP_DEBUG) && (LWIP_NAT_DEBUG & LWIP_DBG_ON) */
  1059. #endif /* IP_NAT */