For some reason, I need to put/inject a packet to an interface. I've made the content of the packet in the local machine. Assume that there's no other machine in the network to send the packet to local machine's network interface. So the local machine should do the job. If I send the packet to the interface, it's be put to the interface for going out of the machine and since the destination MAC address of the packet and the destination IP of the packet belong to the local machine, the packet will go nowhere. I think such a packet is called an egress packet, while I need the same packet be received by the interface or pretend for the kernel that it has been received by the interface. I think such a packet is called an ingress packet. OS is ubuntu 20.04 and the code is in C. An example of such a code follows. I've examined it between two VM's and it works perfectly, but what can I do when I want to do it with only one machine?
In the following example, I respond the ping (ICMP) packets. When I run it between two VM's it works properly, but when I send the generated response to the only interface in a single machine, the ping can't get its response.
int main(int argc, const char *argv[])
{
u8 buf[1000], *dest = NULL;
int len, destLen = 0;
int sdRaw = OpenRawSocket(argv[1], NULL, NULL);
while (true)
{
len = recv(sdRaw, buf, sizeof(buf), 0);
CHECK(len > 0, "%s", strerror(errno));
if (*(u16 *)(buf + 0x0c) == 8 &&
buf[0x17] == 1)
{
if (destLen < len)
{
printf("destLen: %d --> %d\n", destLen, len);
dest = realloc(dest, len);
destLen = len;
}
memcpy(dest + 12, buf + 12, len - 12);
memcpy(dest, buf + 6, 6);
memcpy(dest + 6, buf, 6);
*(u16 *)(dest + 0x14) = 0;
*(u32 *)(dest + 0x1a) = *(u32 *)(buf + 0x1e);
*(u32 *)(dest + 0x1e) = *(u32 *)(buf + 0x1a);
FixChecksums(dest, len);
CHECK(send(sdRaw, dest, len, 0) == len, "%s", strerror(errno));
}
}
}
Please don't ask about the usefulness of the code. I'm going to simplify the issue with a simple code, but the problem will be resolved if this simple issue will be overcomed.
What I have tried:
It's weird that all questions are forced to have a portion for the questioner to describe what they've tried. Some questions are just questions, not some trial and effort issues. They may be just in the beginning of a problem and don't know the first step.