ipv4_nat.c 40 KB

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