russdill / lwip-nat

lwIP DNAT support
11 stars 5 forks source link

Compiler warnings and invalid checksum. #5

Closed ToraNova closed 3 years ago

ToraNova commented 3 years ago

Hi Russ,

Thank you for open-sourcing this wonderful add-on project for lwip. I was trying to use this on a STM32H7 to do some traffic forwarding from a PPPoS interface to an ethernet interface. While compiling this library, I've encountered several warnings from my compiler (arm-none-eabi-gcc (Arch Repository) 10.3.0).

lib/nat/nat_proto_ip4.c: In function 'icmp4_ip4_prerouting_nat':
lib/nat/nat_proto_ip4.c:144:17: warning: taking address of packed member of 'struct ip_hdr' may result in an unaligned pointer value [-Waddress-of-packed-member]
  144 |   update_chksum(&iphdr->_chksum, &iphdr->dest, &pcb->ip.local_ip, 2);
      |                 ^~~~~~~~~~~~~~~
lib/nat/nat_proto_ip4.c:148:17: warning: taking address of packed member of 'struct ip_hdr' may result in an unaligned pointer value [-Waddress-of-packed-member]
  148 |   update_chksum(&iphdr->_chksum, &iphdr->src, &pcb->nat_local_ip, 2);
      |                 ^~~~~~~~~~~~~~~
lib/nat/nat_proto_ip4.c: In function 'ip4_prerouting_nat':
lib/nat/nat_proto_ip4.c:234:17: warning: taking address of packed member of 'struct ip_hdr' may result in an unaligned pointer value [-Waddress-of-packed-member]
  234 |   update_chksum(&iphdr->_chksum, &iphdr->src, &pcb->ip.local_ip, 2);
      |                 ^~~~~~~~~~~~~~~
lib/nat/nat_proto_ip4.c:238:17: warning: taking address of packed member of 'struct ip_hdr' may result in an unaligned pointer value [-Waddress-of-packed-member]
  238 |   update_chksum(&iphdr->_chksum, &iphdr->dest, &pcb->nat_local_ip, 2);
      |                 ^~~~~~~~~~~~~~~

Ignoring these, I have the following setup.

PC1   (PPPoS interface using pppd)
10.0.0.1
    |
    |
    |
10.0.0.2
STM32H743
192.168.7.1
    |
    |
    |
192.168.7.2
PC2   (Ethernet interface)

From PC1, I can ping 10.0.0.2; likewise from PC2, I can ping 192.168.7.1. Everything looks ok from here. I set PC2's default route as 192.168.7.1 and then tried to ping 10.0.0.1 from PC2. I fired up wireshark monitoring my PPP interface on PC1 and see that it received the ICMP packets, however, the checksum is invalid (0x0000).

I would like to know if these are caused by the warning message. I am not too familiar about packing/unpacking of C-struct. Could you let me know what's wrong with it?

Additional information: Snippet on how I initialized the NAT-rule using the library.

struct nat_rule *rule = &usb2ppp_rule;
rule->inp = &usbet_netif;
rule->outp = &pppos_netif;
err = nat_rule_add(rule);
if (err != ERR_OK){
logger_printf("nat setup failed.\n");
}
ToraNova commented 3 years ago

after looking at lwip's source, I notice adding the following line in lwipopts.h solves my problem.

#define LWIP_CHECKSUM_CTRL_PER_NETIF    0
#define CHECKSUM_GEN_IP         0
#define CHECKSUM_GEN_UDP                0
#define CHECKSUM_GEN_TCP                0
#define CHECKSUM_GEN_ICMP               0