Find the answer to your Linux question:
Results 1 to 6 of 6
Hi all, Friends I have a built an IP packet and I need to inject this packet to a machine on my LAN. Hence I need a different ethernet header. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2007
    Posts
    34

    Red face Please please help me ...injecting packets


    Hi all,
    Friends I have a built an IP packet and I need to inject this packet to a machine on my LAN. Hence I need a different ethernet header. I have used sockets and managed to inject a raw packet on to the wire but what exactly I want is that the kernel should build the ethernet header for me. The IP_HDRINCL option in setsockopt() does exactly this...but I get an error on setsockopt() with errno = 92 ...
    Here is my code ...

    Code:
    inject(u_char *pkt, int nr) 
    { /*open socket*/
     fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (fd == -1) 
    {
     printf("\nSocket failed!!"); 
    } 
    
    ret_val=setsockopt (fd, IPPROTO_IP, IP_HDRINCL,(const void *) val, sizeof (one));
    if(ret_val<0)
    	printf("iphdrincl error ...error no=%d",errno);
    
    /*Select interface*/ 
    memset(&ifr, 0, sizeof(ifr)); 
    strncpy (ifr.ifr_name, iftext, sizeof(ifr.ifr_name) - 1); 
    ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0'; 
    if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) 
    { 
     printf("No such interface: %s\n", iftext); 
     return -1; 
    }
     ioctl(fd, SIOCGIFFLAGS, &ifr); 
    if ( (ifr.ifr_flags & IFF_UP) == 0)
     { printf("Interface %s is down\n", iftext);
     return -1;
     } 
    ioctl(fd, SIOCGIFINDEX, &ifr);
     memset(&sa, 0, sizeof (sa));
     sa.sll_family = AF_PACKET;
     sa.sll_ifindex = ifr.ifr_ifindex;
     sa.sll_protocol = htons(ETH_P_ALL); 
    /*Send*/ 
    c = sendto(fd, pkt, nr, 0, (struct sockaddr *)&sa, sizeof (sa));
     }
    Please help me...any help will be appreciated.

  2. #2
    Just Joined!
    Join Date
    Jan 2007
    Posts
    90
    placing the ethernet header is done at the DataLink Layer (DLL) which is
    into the firmware of the device.
    You cannot order device to send some other frame into the LAN whereas
    the device you bought does say 802.3

  3. #3
    Just Joined!
    Join Date
    Feb 2007
    Posts
    34

    Unhappy

    Yes...I understand that the ethernet 802.3 header is attached at the device itself ...
    Is there no way it can be done...I hope u got my problem...
    Also what do u have to say about the code...will that work??

  4. #4
    Just Joined!
    Join Date
    Feb 2007
    Posts
    34
    Quote Originally Posted by bhupeshchawda
    I hope u got my problem...
    I'll again explain my problem for better understanding...I have a packet :

    DATA->IP_HEADER->TCP_HEADER->ETHERNET_HEADER.

    This ETHERNET_HEADER has destination MAC address of my machine. I need to send this packet to another computer on my LAN...For this I would have to strip out the previous ETHERNET_HEADER and attach a new one based on the MAC address of the destination machine...

    So I pass :
    DATA->IP_HEADER->TCP_HEADER
    to the inject function and expect it to attach a new ethernet header...

    my code works for RAW packets but it does not attach the new Ethernet header...

    Also note : The IP_HDRINCL option in setsockopt() gives an error on setsockopt() with errno = 92 ...

    Please please do help me...any help will be appreciated.

  5. #5
    Just Joined!
    Join Date
    Mar 2007
    Posts
    2
    Quote Originally Posted by bhupeshchawda
    I'll again explain my problem for better understanding...I have a packet :

    DATA->IP_HEADER->TCP_HEADER->ETHERNET_HEADER.

    This ETHERNET_HEADER has destination MAC address of my machine. I need to send this packet to another computer on my LAN...For this I would have to strip out the previous ETHERNET_HEADER and attach a new one based on the MAC address of the destination machine...

    So I pass :
    DATA->IP_HEADER->TCP_HEADER
    to the inject function and expect it to attach a new ethernet header...

    my code works for RAW packets but it does not attach the new Ethernet header...

    Also note : The IP_HDRINCL option in setsockopt() gives an error on setsockopt() with errno = 92 ...

    Please please do help me...any help will be appreciated.

    IP_HDRINCL is just an intimation to Kernel that you will be providing the IP stack. When you say so, why can't u build up etherner header itself?

  6. #6
    Just Joined!
    Join Date
    Jan 2007
    Posts
    90
    The way in which the card works is there is always a
    bios on the card which takes care of putting the 802.3 header.
    If you want to bypass the header, the only way is to re-write the
    BIOS on the card.
    Your program works at the level above layer 2 and you want something
    to work on layer 2. I donnot know if tweaking the kernel even can do you
    any help !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •