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 8526499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:17:22+00:00 2026-06-11T08:17:22+00:00

Even if a similar topic already exists, I noticed that it dates back two

  • 0

Even if a similar topic already exists, I noticed that it dates back two years, thus I guess it’s more appropriate to open a fresh one…

I’m trying to figure out how to send UDP packets from the Linux Kernel (3.3.4), in order to monitor the behavior of the random number generator (/drivers/char/random.c). So far, I’ve managed to monitor a few things owing to the sock_create and sock_sendmsg functions. You can find the typical piece of code I use at the end of this message. (You might also want to download the complete modified random.c file here.)

By inserting this code inside the appropriate random.c functions, I’m able to send a UDP packet for each access to /dev/random and /dev/urandom, and each keyboard/mouse events used by the random number generator to harvest entropy. However it doesn’t work at all when I try to monitor the disk events: it generates a kernel panic during boot.

Consequently, here’s my main question: Have you any idea why my code causes so much trouble when inserted in the disk events function? (add_disk_randomness)

Alternatively, I’ve read about the netpoll API, which is supposed to handle this kind of UDP-in-kernel problems. Unfortunately I haven’t found any relevant documentation apart from an quite interesting but outdated Red Hat presentation from 2005. Do you think I should rather use this API? If yes, have you got any example?

Any help would be appreciated.
Thanks in advance.

PS: It’s my first question here, so please don’t hesitate to tell me if I’m doing something wrong, I’ll keep it in mind for future 🙂


#include <linux/net.h>
#include <linux/in.h>
#include <linux/netpoll.h>
#define MESSAGE_SIZE 1024
#define INADDR_SEND ((unsigned long int)0x0a00020f) //10.0.2.15
static bool sock_init;
static struct socket *sock;
static struct sockaddr_in sin;
static struct msghdr msg;
static struct iovec iov;

[...]

int error, len;
mm_segment_t old_fs;
char message[MESSAGE_SIZE];

if (sock_init == false)
{
  /* Creating socket */
  error = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
  if (error<0)
    printk(KERN_DEBUG "Can't create socket. Error %d\n",error);

  /* Connecting the socket */
  sin.sin_family = AF_INET;
  sin.sin_port = htons(1764);
  sin.sin_addr.s_addr = htonl(INADDR_SEND);
  error = sock->ops->connect(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr), 0);
  if (error<0)
    printk(KERN_DEBUG "Can't connect socket. Error %d\n",error);

  /* Preparing message header */
  msg.msg_flags = 0;
  msg.msg_name = &sin;
  msg.msg_namelen  = sizeof(struct sockaddr_in);
  msg.msg_control = NULL;
  msg.msg_controllen = 0;
  msg.msg_iov = &iov;
  msg.msg_control = NULL;
  sock_init = true;
}

/* Sending a message */
sprintf(message,"EXTRACT / Time: %llu / InputPool: %4d / BlockingPool: %4d / NonblockingPool: %4d / Request: %4d\n",
  get_cycles(),
  input_pool.entropy_count,
  blocking_pool.entropy_count,
  nonblocking_pool.entropy_count,
  nbytes*8);
iov.iov_base = message;
len = strlen(message);
iov.iov_len = len;
msg.msg_iovlen = len;
old_fs = get_fs();
set_fs(KERNEL_DS);
error = sock_sendmsg(sock,&msg,len);
set_fs(old_fs);
  • 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-11T08:17:23+00:00Added an answer on June 11, 2026 at 8:17 am

    I solved my problem a few months ago. Here’s the solution I used.

    The standard packet-sending API (sock_create, connect, …) cannot be used in a few contexts (interruptions). Using it in the wrong place leads to a KP.

    The netpoll API is more “low-level” and works in every context. However, there are several conditions :

    • Ethernet devices
    • IP network
    • UDP only (no TCP)
    • Different computers for sending and receiving packets (You can’t send to yourself.)

    Make sure to respect them, because you won’t get any error message if there’s a problem. It will just silently fail 🙂 Here’s a bit of code.

    Declaration

    #include <linux/netpoll.h>
    #define MESSAGE_SIZE 1024
    #define INADDR_LOCAL ((unsigned long int)0xc0a80a54) //192.168.10.84
    #define INADDR_SEND ((unsigned long int)0xc0a80a55) //192.168.10.85
    static struct netpoll* np = NULL;
    static struct netpoll np_t;
    

    Initialization

    np_t.name = "LRNG";
    strlcpy(np_t.dev_name, "eth0", IFNAMSIZ);
    np_t.local_ip = htonl(INADDR_LOCAL);
    np_t.remote_ip = htonl(INADDR_SEND);
    np_t.local_port = 6665;
    np_t.remote_port = 6666;
    memset(np_t.remote_mac, 0xff, ETH_ALEN);
    netpoll_print_options(&np_t);
    netpoll_setup(&np_t);
    np = &np_t;
    

    Use

    char message[MESSAGE_SIZE];
    sprintf(message,"%d\n",42);
    int len = strlen(message);
    netpoll_send_udp(np,message,len);
    

    Hope it can help someone.

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

Sidebar

Related Questions

Why two object's hash code is not same even though they have similar values.
I learned that even though seemingly similar, Eulerian path can be solved in linear
I have found many similar posts and even tried to find out how to
Even though it's not part of HTTP 1.1/RFC2616 webapps that wish to force a
Even so the jar files exists and are in the correct folder, I get
After all what I've read on this topic, I know that there is no
I have found couple of similar topic about this but none of them solve
I can't seem to find a similar topic on Stack Overflow regarding this, so
I've searched thoroughly for a similar topic and couldn't find one. Here's hoping someone
To start with, I tried to search on stackoverflow for the similar topic. I

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.