Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:11:28+00:00 2026-06-04T11:11:28+00:00

I have the following function which send packets over raw socket. #include <unistd.h> #include

  • 0

I have the following function which send packets over raw socket.

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

#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 = &one;
  // creating a socket
  if ((sd = socket (PF_INET, SOCK_RAW, IPPROTO_UDP)) < 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->dest;
  // setting ip
  sin.sin_addr.s_addr = iph->daddr;
  // notifying kernel do not fill up the packet structure.
  if (setsockopt (sd, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 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)) < 0)
  {
    log_message (LOG_ERROR, "error notifying kernel about MARK");
    return EXITCODE_SOCK_MARK_FAILED;
  }
  #ifdef CHECKSUM
  /* compute checksum */
  udph->check = udp_checksum (data + IP_OFFSET, size - IP_OFFSET, iph->saddr, iph->daddr);
  /* testing purposed */
  #else
  udph->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 *) &sin, /* socket addr */
              sizeof (sin)) < 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;
  }
}

Now i’m setting mark from nfq_set_verdict2() from libnetfilter_queue :http://www.netfilter.org/projects/libnetfilter_queue/doxygen/group__Queue.html

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

when i receive the packet from netfilter_queue i do something following :

nfq_set_verdict(..,NF_DROP,MARK,...);
process_packet();

This process_packet() calls send_packet_raw().

Associated iptable rules :

$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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:11:29+00:00Added an answer on June 4, 2026 at 11:11 am

    Not exactly sure what the question was, but

    nfq_set_verdict(..,NF_DROP,MARK,...);
    process_packet();
    

    looks bad. I wouldn’t call NF_DROP before processing the packet.
    I have written a couple of tunneling programs, and I first process the packet, put it in my buffer, the issue a NF_DROP. After this I can reissue the packets from buffer using the raw socket. So:

    process_packet();    
    nfq_set_verdict(..,NF_DROP,MARK,...);
    

    would be better. At least copy the packet data before issuing the verdict.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
I have the following function which creates a std::vector of iterators into another container:
I have the following C++ function definition, which I am trying to call through
I have a for-loop which performs the following function: Take a M by 8
I have added the following fine function to my status bar to show which
I have the following jQuery which I need adapting: $(document).ready(function(){ $(.rss-popup a).hover(function() { $(this).next(em).stop(true,
I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
I have the following loop inside an function init(); which is executed onload, I
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have the following code, in which Boost.Local uses a function callback to load

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.