LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with injecting raw ethernet frames using TAP interface (https://www.linuxquestions.org/questions/programming-9/need-help-with-injecting-raw-ethernet-frames-using-tap-interface-804346/)

knonaka 04-26-2010 02:57 PM

Need help with injecting raw ethernet frames using TAP interface
 
Hi all, I have UDP packets generated on Machine A that are addressed to Machine B (unicast) that I capture with PCAP. After transporting this raw data to Machine B (via RF modem) I'm trying to reinject the original UDP on Machine B through a tap0 interface. The capture and transport are all working fine. BUT once on the destination machine, the reinjected ethernet packets are NOT being received by a local UDP server. this is my socket creation (removed error checking for brevity) :

Code:

  outputSocket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

  struct ifreq s_ifr;
  strcpy(s_ifr.ifr_name, "tap0");

  // get interface index of interface
  ioctl(outputSocket, SIOCGIFINDEX, &s_ifr);

  struct sockaddr_ll outputServer;
  memset(&outputServer, 0, sizeof (struct sockaddr_ll));

  outputServer.sll_family = AF_PACKET;
  outputServer.sll_protocol = htons(ETH_P_ALL);
  outputServer.sll_ifindex = s_ifr.ifr_ifindex;
  outputServer.sll_halen = ETH_ALEN;  // 6

  // is this needed for writing RAW ethernet out, even as client???
  bind(outputSocket, (struct sockaddr *) & outputServer, sizeof (outputServer));

and this is how I'm writing the byte array to the tap interface :

Code:

  int bytesSent = 0;
  bytesSent = write(outputSocket, (const void *) data, dataSize);

  fprintf(stderr, "sent %d bytes to network\n", bytesSent);

"data" is an unsigned char array that contains the original packet data received from pcap, which includes the ethernet header, ip header, udp header, and payload data.

after the call to write() completes, Wireshark on Machine B sees a correctly formatted (checksums and all) UDP packet when listening to tap0 - ie. no lines highlighted in red. *BUT* - a UDP server i have running on Machine B never receives the reinjected UDP datagram. (I have verified that this server works, by using a simple local udp generator app.)

is there something wrong with how I'm creating the socket and/or writing the data back out to the tap0?

Thanks in advance!
Keith

TimothyEBaldwin 04-29-2010 02:31 AM

You have it backwards - you should not be using a socket. Read the tutorial at: http://backreference.org/2010/03/26/...face-tutorial/


All times are GMT -5. The time now is 04:38 PM.