Ping packets on raw sockets

1.8k Views Asked by At

I am trying to receiving ping packets on a veth interface. But I can't see anything on receive side. I can see the packet when I do a TCP dump. Here is my code.

s_int32_t checkingRawSocket(void) {

    int sockfd;
    char ifName[IFNAMSIZ];
    fd_set readfds;
    int m_sd;
    int retVal;
    int sockopt;

    strcpy(ifName, "ve12");

    if ((sockfd = socket(AF_PACKET, SOCK_RAW,htons(IPPROTO_ICMP))) == -1) {
        perror("NOT LISTNER: socket");  
        goto EXIT;     
    }    

    retVal = setsockopt(sockfd, SOL_SOCKET,  SO_REUSEADDR,
                    (char *)&sockopt, sizeof(sockopt));
    if (retVal < 0){
        perror("setsockopt() failed");
        goto EXIT;
    }

    if (setsockopt(sockfd, SOL_SOCKET, 
               SO_BINDTODEVICE, ifName, IFNAMSIZ-1) == -1)  {
        perror("SO_BINDTODEVICE");
        retVal = -1;
        goto EXIT;
    }       
    FD_ZERO(&readfds);
    m_sd = sockfd;
    FD_SET(sockfd, &readfds);

    if(select(m_sd + 1, &readfds, NULL, NULL, 0) < 0){
        perror("SELECT FAILED ");
        goto EXIT;
    }

    printf("I got something after select");

    EXIT:
    return -1;
 }

So I do a ping on ve12p. It never crosses select and reaches the print statement .

ping -I ve12p 1.1.1.1

I checked the veth interfaces, they are up and I can see the packet on tcpdump of ve12.

1

There are 1 best solutions below

0
pughar On

I don't believe IPPROTO_ICMP is a valid protocol when using an AF_PACKET domain. You could either try receiving all protocols by using:

socket(AF_PACKET, SOCK_RAW,htons(ETH_P_ALL))

Or, you could use the correct domain AF_INET for the protocol IPPOROTO_ICMP:

socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)

Edit:

The socket(7) man page states that SO_BINDTODEVICE is not supported for packet sockets and that a normal bind() should be used.