Note that there are some explanatory texts on larger screens.

plurals
  1. POmarking packet for sending over raw socket
    primarykey
    data
    text
    <p>I have the following function which send packets over raw socket. </p> <pre><code>#include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/ip.h&gt; #include &lt;netinet/udp.h&gt; #include "pkt-types.h" #include "pkt-log.h" #include "pkt-utils.h" int send_packet_raw (void *data, int size) { log_message (LOG_DEBUG, " inside send_packet_raw"); int sd; struct iphdr *iph = (struct iphdr *) data; struct udphdr *udph = (struct udphdr *) (data + sizeof (struct ip)); struct sockaddr_in sin; // needed for notify kernel to not to build header for this int one = 1; const int *val = &amp;one; // creating a socket if ((sd = socket (PF_INET, SOCK_RAW, IPPROTO_UDP)) &lt; 0) { log_message (LOG_ERROR, " problem creating a socket"); return EXITCODE_SOCK_CREATION_FAILED; } // setting address family sin.sin_family = AF_INET; // setting port sin.sin_port = udph-&gt;dest; // setting ip sin.sin_addr.s_addr = iph-&gt;daddr; // notifying kernel do not fill up the packet structure. if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) &lt; 0) { log_message (LOG_ERROR, "error notifying kernel about raw socket"); return EXITCODE_SOCK_KERN_NOTIF_FAILED; } /* setting socket option to use MARK value */ if (setsockopt (sd, SOL_SOCKET, SO_MARK, val, sizeof (one)) &lt; 0) { log_message (LOG_ERROR, "error notifying kernel about MARK"); return EXITCODE_SOCK_MARK_FAILED; } #ifdef CHECKSUM /* compute checksum */ udph-&gt;check = udp_checksum (data + IP_OFFSET, size - IP_OFFSET, iph-&gt;saddr, iph-&gt;daddr); /* testing purposed */ #else udph-&gt;check = 0x00; #endif /* dscp 101000 means express forwarding */ if (sendto (sd, /* our socket */ data, /* data to send */ size, /* total length of our ip packet */ 0, /* routing flag, normally always zero */ (struct sockaddr *) &amp;sin, /* socket addr */ sizeof (sin)) &lt; 0) { log_message (LOG_ERROR, "sending over raw socket failed"); return EXITCODE_SOCK_SEND_FAILED; } else { /* shutdown the socket */ if(shutdown (sd, 2)) /* shutdown ok */ return EXITCODE_OK; } } </code></pre> <p>Now i'm setting mark from nfq_set_verdict2() from libnetfilter_queue :<a href="http://www.netfilter.org/projects/libnetfilter_queue/doxygen/group__Queue.html" rel="nofollow">http://www.netfilter.org/projects/libnetfilter_queue/doxygen/group__Queue.html</a></p> <pre><code>int nfq_set_verdict2 ( struct nfq_q_handle * qh, u_int32_t id, u_int32_t verdict, u_int32_t mark, u_int32_t data_len, const unsigned char * buf ) nfq_set_verdict2 - like nfq_set_verdict, but you can set the mark. Parameters: qh Netfilter queue handle obtained by call to nfq_create_queue(). id ID assigned to packet by netfilter. verdict verdict to return to netfilter (NF_ACCEPT, NF_DROP) mark mark to put on packet data_len number of bytes of data pointed to by buf buf the buffer that contains the packet data </code></pre> <p>when i receive the packet from netfilter_queue i do something following :</p> <pre><code>nfq_set_verdict(..,NF_DROP,MARK,...); process_packet(); </code></pre> <p>This process_packet() calls send_packet_raw().</p> <p>Associated iptable rules :</p> <pre><code>$iptables -t mangle -A PREROUTING -m mark --mark 0xa -j ACCEPT $iptables -t mangle -A PREROUTING -p udp --dport $PORT -j NFQUEUE $iptables -t mangle -A OUTPUT -m mark --mark 0xa -j ACCEPT $iptables -t mangle -A OUTPUT -p udp --sport $PORT -j NFQUEUE </code></pre> <p>I have also put up some -j LOG rules to see if packets are in-fact matching. but as it seems neither packet goes out or goes in as no log entries are shown. Could not understand how to find the problem here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload