camabeh
camabeh

Reputation: 1172

Receive all IPv6 packets

How do I receive all IPv6 packets(TCP, UDP, ICMP,...) I for example on lo interface. I am sending ICMP packets with command ping6 ::1, but none of them are received.

Thank you

#include <linux/if_ether.h>
#include <error.h>
#include <stdlib.h>
#include <sys/types.h>


#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main() {
    int socket_fd;
    char buffer[1480];
    struct sockaddr_in6 sin6;
    struct sockaddr sin;

    socket_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
    setsockopt(socket_fd , SOL_SOCKET , SO_BINDTODEVICE , "lo" , strlen("lo")+ 1 );
    if (socket_fd < 0) {
        perror("Failed to create socket");
    }

    ssize_t data_size;

    // Why am I unable to receve any data?
    data_size = recvfrom(socket_fd, buffer, 1480, 0, &sin, (socklen_t *) &sin);
    return 0;
}

Upvotes: 1

Views: 905

Answers (2)

SKi
SKi

Reputation: 8466

RFC3542 Says the following:

We note that IPPROTO_RAW has no special meaning to an IPv6 raw socket (and the IANA currently reserves the value of 255 when used as a next-header field).

So IPPROTO_RAW is not reserved for sending/receiving IPv6 packets.

With IPv4 you can use IPPROTO_RAW only for sending, not receiving. See man raw(7):

An IPPROTO_RAW socket is send only. If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. Note that packet sockets don't reassemble IP fragments, unlike raw sockets.

You may use the following:

socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))

But that may lead to other problems.

If you just want to monitor the traffic, please check pcap-library.

Upvotes: 1

Dan O
Dan O

Reputation: 6090

you're not bind()ing your socket to an address before you try to read data from it.

When a socket is created with socket(2), it exists in a name space (address family) but has no address assigned to it. bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called "assigning a name to a socket".

see also: how to bind raw socket to specific interface

Upvotes: 0

Related Questions