changtimwu / changtimwu.github.com

Tim's testing/practice notes
7 stars 2 forks source link

Filter out outgoing packets on a raw socket #54

Open changtimwu opened 7 years ago

changtimwu commented 7 years ago

Reference neither Raw Socket promiscuous mode not sniffing what I write nor C raw socket receive or sniff incoming packets only gives decent solution.

changtimwu commented 7 years ago
changtimwu commented 7 years ago

kernel trace

    /* Never send packets back to the socket
         * they originated from - MvS (miquels@drinkel.ow.org) */
changtimwu commented 7 years ago

RECV_TYPE

Openwrt adds a new socket option called PACKET_RECV_TYPE. Here is the patch It's mainly for ead. example usage of PACKET_RECV_TYPE.

We make use of it.

int v=0;
v = PACKET_MASK_ANY & ~(1<<PACKET_OUTGOING) & ~(1 << PACKET_LOOPBACK);
setsockopt( raw_sock, SOL_PACKET, PACKET_RECV_TYPE, &v, sizeof(v));

Excellent! It works well.