Why doesn't the router forward my packet?

I am using a raw socket to send TCP packets from computer B to computer A.

First I create a socket

int s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); 

and let me know that the kernel does not fill in the ip header for me, because my data includes both the ip header and tcp header

 int optval = 1; setsockopt(s, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(optval)); 

and set the destination address

 struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(33333); addr.sin_addr.s_addr = inet_addr("192.168.200.135"); 

and send data

 sendto(s, data, len, 0, (struct sockaddr*)&addr, sizeof(addr)); 

My network environment is as below picture network environment

In case A, two computers are connected by a switch. While in case B, computer B connects to the router, and the router connects to the switch.

My program works fine in case A. I can use a raw socket to get a packet on computer A, and can use wirehark to capture this packet.

In case B, I can ping from computer B to computer A and ssh from B to A. And I can use both the raw socket and wirehark on computer A to capture some TCP packet sent between B and A. However, the packets Sent by my raw socket on computer B cannot be written to A. It seems that the router is not sending my packet. Is there something wrong with my package?

My batch sending program looks like this:

 //data to send char data[48] = { //iphdr: 0x45, 0x00, 0x00, 0x30, //version:4 header_length:5 TOS:0 length:0x30 0x00, 0x01, 0x00, 0x00, //identification:1 flags:0 fragment_offset:0 0x40, 0x06, 0x2f, 0x47, //TTL:0x40 protocol:6(TCP) header_checksum:0x2f47 0xc0, 0xa8, 0x01, 0xa8, //source_IP:192.168.1.168 0xc0, 0xa8, 0xc8, 0x87, //destination_IP:192.168.200.135 //tcphdr: 0x56, 0xce, 0x82, 0x35, //source_port:22222 destination_port:33333 0x00, 0x00, 0x00, 0x00, //sequence_number:0 0x00, 0x00, 0x00, 0x00, //ack_number:0 0x50, 0x00, 0xaa, 0xaa, //header_length:5 window_size:0xaaaa 0xeb, 0xbd, 0x00, 0x00, //checksum:0xebbd urgent_pointer:0 //tcp content: 0x7a, 0x68, 0x73, 0x00, 0x39, 0x30, 0xce, 0x56, }; int len = 48; //open the socket int s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); //don't fill header int optval = 1; setsockopt(s, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(optval)); //destination addr struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("192.168.200.135"); //send sendto(s, data, len, 0, (struct sockaddr*)&addr, sizeof(addr)); 
+4
source share
2 answers
  • Router 192.168.1.1 should be your default router. Check your configuration on machine A.
0
source

Your TCP header is erroneous because it skips flags. For your test, you must at least set the SYN flag. Other things may not match the title.

So, if the router does check the TCP header, it probably drops your packet. Checking the TCP header occurs if your router does not actually route but uses NAT to forward packets, or if there is a firewall that checks for ports or connection states.

0
source

Source: https://habr.com/ru/post/1489867/


All Articles