Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new network feature: NAPT (widely known as NAT) #6360

Merged
merged 12 commits into from
Aug 28, 2019
Merged

Conversation

d-a-v
Copy link
Collaborator

@d-a-v d-a-v commented Jul 30, 2019

This is brought to us thanks to

My contribution was easy (ref), I take no credit.
Napt is now ported to lwIP-2.1.2 (latest stable) as a real patch in lwip2 so it can be proposed upstream.

It is currently only enabled with lwip2 (no IPv6, with features).

This PR also brings heap optimizations from @earlephilhower and improved locking macros from @mhightower83 .

An example is proposed (filling half the RAM) for a damn simple and basic WiFi range extender.

d-a-v referenced this pull request in NeoCat/esp8266-Arduino Jul 30, 2019
esp8266 can handle 2 network interfaces (Wifi client and softAP)
simultaneously in the WIFI_AP_STA mode.
Currently IP_FORWARD is implemented in lwip to forward packets
between 2 interfaces, but static routing must be setup manually.
This patch adds IP_NAPT config option to enable esp8266 to use
NAPT technique on forwarding packets from softAP clients to the
parent WiFi AP.

To enable NAPT on the interface, ip_napt_enable() must be called
with esp8266's softAP interface address. For example:

  extern "C" void ip_napt_enable(unsigned long addr, int enable);
  ip_napt_enable(IPAddress(192,168,4,1), 1);

This patch also provides port mappings to forward packets from
the parent WiFi network to softAP network. To register and
unregister port mappings, following functions can be used:

  void ip_portmap_add(byte proto, unsigned long maddr, unsigned short mport,
                      unsigned long daddr, unsigned short dport);
  bool ip_portmap_remove(byte proto, unsigned short mport);

For exmaple:

  #define IP_PROTO_TCP     6
  #define IP_PROTO_UDP     17
...
  ip_portmap_add(IP_PROTO_TCP, WiFi.localIP(), 8080,
                 IPAddress(192,168,4,3), 80);
  ip_portmap_add(IP_PROTO_UDP, WiFi.localIP(), 12345,
                 IPAddress(192,168,4,2), 12345);

These map TCP port 8080 on WiFi client address to 192.168.4.3:80, and
UDP port 12345 to 192.168.4.2:12345. To remove the first mapping:

  ip_portmap_remove(IP_PROTO_TCP, 8080);

To apply this change, SDK needs to be recompiled with this patch:
  $ cd tools/sdk/lwip/src
  $ make && make release  #=> intalled to ../../lib/liblwip_gcc.a

Note that this patch is experimental and is provided as is.
@devyte devyte self-requested a review August 27, 2019 04:44
@devyte
Copy link
Collaborator

devyte commented Aug 27, 2019

Just needs a couple of code comments. The rest is just a question (netdump?) and the ides of wrapping all the calls, which just means an enhancement issue should be opened once this PR is merged.

@d-a-v d-a-v requested a review from devyte August 27, 2019 12:45
@d-a-v d-a-v merged commit 06f1865 into esp8266:master Aug 28, 2019
@d-a-v d-a-v deleted the napt branch August 28, 2019 15:51
@Suraj151
Copy link

Suraj151 commented Oct 6, 2019

Platform

Settings in IDE

  • Module: Nodemcu 1.0
  • Flash Mode: not listed
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 115200

Problem Description

i tried napt example with fresh arduino installation but it didn't work as it work with lwip1.4. end device not get internet access of router. i tested it with range extender example.

Debug Messages

NAPT Range extender
Heap on start: 50112
...........
STA: 192.168.137.2 (dns: 192.168.137.1 / (IP unset))
AP: 192.168.0.1
Heap before: 49400
ip_napt_init(1000,10): ret=0 (OK=0)
ip_napt_enable_no(SOFTAP_IF): ret=0 (OK=0)
WiFi Network 'Windows Phoneextender' with same password is now NATed behind 'Windows Phone'
Heap after napt init: 36544

is there any other setting that i missed ?

@Suraj151
Copy link

Suraj151 commented Oct 9, 2019

it works !

i dont know how but when i reinstalled all things with same steps in another pc. then it works as expected.
will try to understand reason and thanks for such awaited feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants