receive (temporarily) from all interfaces with raw packet socket - Linux

This is a discussion on receive (temporarily) from all interfaces with raw packet socket - Linux ; In my application (which runs as root) a raw packet socket is used: fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); I want to listen (and react on) all ethernet messages coming from eth0, so the next things that happens is a bind ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: receive (temporarily) from all interfaces with raw packet socket

  1. receive (temporarily) from all interfaces with raw packet socket


    In my application (which runs as root) a raw packet socket
    is used:
    fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

    I want to listen (and react on) all ethernet messages coming
    from eth0, so the next things that happens is a bind
    to the eth0 interface with:
    err = bind(fd, ...)

    Given that there are other interface(s) (already up and running)
    which receive many (IP) frames frequently, there is a problem
    I do not know how to solve:
    After the raw packet socket creation, just before binding to the
    eth0 interface I receive (IP) frames of the other interfaces. This is
    a linux kernel feature (which seems to be there in all 2.4/2.6
    kernels) which happens to trouble me.
    Is there a way to avoid this ?

  2. Re: receive (temporarily) from all interfaces with raw packet socket

    Norbert van Bolhuis wrote:
    >
    > In my application (which runs as root) a raw packet socket
    > is used:
    > fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    >
    > I want to listen (and react on) all ethernet messages coming
    > from eth0, so the next things that happens is a bind
    > to the eth0 interface with:
    > err = bind(fd, ...)
    >
    > Given that there are other interface(s) (already up and running)
    > which receive many (IP) frames frequently, there is a problem
    > I do not know how to solve:
    > After the raw packet socket creation, just before binding to the
    > eth0 interface I receive (IP) frames of the other interfaces. This is
    > a linux kernel feature (which seems to be there in all 2.4/2.6
    > kernels) which happens to trouble me.
    > Is there a way to avoid this ?


    You're probably re-inventing a wheel..

    Have a look at the libpcap library. It seems to be what you need.

    --

    Tauno Voipio
    tauno voipio (at) iki fi


  3. Re: receive (temporarily) from all interfaces with raw packet socket

    Tauno Voipio wrote:
    > Norbert van Bolhuis wrote:
    >
    >>
    >> In my application (which runs as root) a raw packet socket
    >> is used:
    >> fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    >>
    >> I want to listen (and react on) all ethernet messages coming
    >> from eth0, so the next things that happens is a bind
    >> to the eth0 interface with:
    >> err = bind(fd, ...)
    >>
    >> Given that there are other interface(s) (already up and running)
    >> which receive many (IP) frames frequently, there is a problem
    >> I do not know how to solve:
    >> After the raw packet socket creation, just before binding to the
    >> eth0 interface I receive (IP) frames of the other interfaces. This is
    >> a linux kernel feature (which seems to be there in all 2.4/2.6
    >> kernels) which happens to trouble me.
    >> Is there a way to avoid this ?

    >
    >
    > You're probably re-inventing a wheel..
    >
    > Have a look at the libpcap library. It seems to be what you need.
    >


    thanks for your answer.

    I already did look at libpcap (and "reused" some code), the only difference is
    that my application is doing a few more ioctl's just before binding to the
    eth0 interface.

    At least one ioctl (SIOCGIFINDEX, for getting the interface id) needs to
    be done before binding. This ioctl causes kernel routines to run which means
    it can be interrupted by receive interrupts (for incoming frames on other
    interfaces).

    I checked the v2.4/v2.6 kernel code (see packet_create in af_packet.c and
    dev_add_pack, netif_receive_skb, ptype_all in net/core/dev.c) and I'm pretty
    sure this problem also occurs with libpcap.

  4. Re: receive (temporarily) from all interfaces with raw packet socket

    Norbert van Bolhuis wrote:

    > I want to listen (and react on) all ethernet messages coming
    > from eth0, so the next things that happens is a bind
    > to the eth0 interface with:
    > err = bind(fd, ...)


    > After the raw packet socket creation, just before binding to the
    > eth0 interface I receive (IP) frames of the other interfaces. This is
    > a linux kernel feature (which seems to be there in all 2.4/2.6
    > kernels) which happens to trouble me.
    > Is there a way to avoid this ?


    A couple of ideas:
    1. Look at the sockaddr_ll returned by recvfrom/recvmsg and just skip
    those frames whose sll_ifindex doesn't match the one you're interested
    in.

    2. Use the PACKET_MMAP mechanism (see
    http://pusa.uv.es/~ulisses/packet_mmap/). This will typically give you
    better performance anyway. Since you can make the setsockopt call that
    activates it *after* you've bound to eth0, I don't think you should
    ever get any packets from other interfaces. And in any case, the
    sockaddr_ll is included in the mmap'd buffer anyway.

    GH


+ Reply to Thread