Udp packet caught by tcpdump but not received by socket

I wrote a rawudp program to send an udp packet through a raw socket, following the web page http://www.tenouk.com/Module43a.html . Then I wrote an udp server to listen for udp packets on this port. Codes are as follows:

... sd = socket(AF_INET, SOCK_DGRAM, 0); bind(sd, (struct sockaddr *)&ipaddr, sizeof(ipaddr)); size = recvfrom(sd, msgbuf.text, 2000, 0, (struct sockaddr *)&sin, &sin_len); ... // print the recevied udp packet 

When I used rawudp to send the udp packet to the udp server, it is ok if the source IP address and destination IP address are different. But the udp server could not receive the udp packet if the source IP address and destination IP address are the same.

I tried using tcpdump to catch the udp package. And I found that tcpdump can show the udp packet when using the same source and destination IP address. It seemed that the IP address of the source and destination was the same, the udp packet was removed somewhere, and therefore the socket was not received in the udp server.

Below are some of the logs (the IP address was updated in the logs). The only difference between the two cases was the source IP address (192.168.0.26 compared to 192.168.0.226).

I am stuck on this issue. Can anybody help me. Thanks in advance.

udp client side (rawudp):

 case 1> rawudp 192.168.0.26 18321 192.168.0.226 19702 test.bin # using different source and destination ip addresses size of ip header: 20 size of udp header: 8 read 69 bytes from file test.bin successfully. socket() - using SOCK_RAW socket and UDP protocol is OK. setsockopt() is OK. total length of IP packet: 97 0000: 4500 6100 0000 4000 4011 CD8F C0A8 001A 0010: C0A8 00E2 4791 4CF6 004D 0000 0104 0401 0020: FF00 0105 084E 0600 5225 1183 0406 0501 0030: 5211 3840 0D05 2735 2109 02C0 0023 0101 0040: 8080 448D 30C0 0300 9005 093C 5E56 8791 0050: 4B2D B7C0 082A 0000 2900 0000 0412 0C95 0060: 00 sendto() is OK. case 2> rawudp 192.168.0.226 18321 192.168.0.226 19702 test.bin # using same source and destination ip addresses size of ip header: 20 size of udp header: 8 read 69 bytes from file test.bin successfully. socket() - using SOCK_RAW socket and UDP protocol is OK. setsockopt() is OK. total length of IP packet: 97 0000: 4500 6100 0000 4000 4011 CCC7 C0A8 00E2 0010: C0A8 00E2 4791 4CF6 004D 0000 0104 0401 0020: FF00 0105 084E 0600 5225 1183 0406 0501 0030: 5211 3840 0D05 2735 2109 02C0 0023 0101 0040: 8080 448D 30C0 0300 9005 093C 5E56 8791 0050: 4B2D B7C0 082A 0000 2900 0000 0412 0C95 0060: 00 sendto() is OK. 

udp server side:

 case 1> udp server receive 69 bytes from ip address 192.168.0.26 port 18321, sin_len 16 02:13:24.252841 IP 192.168.0.26.18321 > 192.168.0.226.19702: UDP, length 69 0x0000: 4500 0061 0000 4000 4011 0198 c0a8 001a E..a..@. @....... 0x0010: c0a8 00e2 4791 4cf6 004d 0000 0104 0401 ....GL.M...... 0x0020: ff00 0105 084e 0600 5225 1183 0406 0501 .....N..R%...... 0x0030: 5211 3840 0d05 2735 2109 02c0 0023 0101 R.8@.. '5!....#.. 0x0040: 8080 448d 30c0 0300 9005 093c 5e56 8791 ..D.0......<^V.. 0x0050: 4b2d b7c0 082a 0000 2900 0000 0412 0c95 K-...*..)....... 0x0060: 00 . 02:13:39.500469 IP 192.168.0.226.18321 > 192.168.0.226.19702: UDP, length 69 0x0000: 4500 0061 0000 4000 4011 00d0 c0a8 00e2 E..a..@. @....... 0x0010: c0a8 00e2 4791 4cf6 004d 0000 0104 0401 ....GL.M...... 0x0020: ff00 0105 084e 0600 5225 1183 0406 0501 .....N..R%...... 0x0030: 5211 3840 0d05 2735 2109 02c0 0023 0101 R.8@.. '5!....#.. 0x0040: 8080 448d 30c0 0300 9005 093c 5e56 8791 ..D.0......<^V.. 0x0050: 4b2d b7c0 082a 0000 2900 0000 0412 0c95 K-...*..)....... 0x0060: 00 

When I start the rawudp and udp server on the same host, the update server can receive the udp package from rawudp when I set the source and destination IP addresses. But if I run rawudp on a different host than the udp server, the udp server could not get the service pack from rawudp when I set these IP addresses. In both cases, tcpdump can catch the udp packet on the udp server side.

In the latter case, only the udp packet with the same IP address of the source and destination is affected. If I distinguished them, the update server could always receive the udp package. I am not sure if the kernel can delete a packet with the same local IP address if the packet is not received from the lo interface.

+6
source share

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


All Articles