LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-26-2024, 02:07 AM   #1
resyfer
LQ Newbie
 
Registered: Mar 2024
Distribution: Endeavor OS
Posts: 3

Rep: Reputation: 0
Can't send UDP IPv4 packets in loadable module


Hi!

I've been trying to send UDP packets to the broadcast address of my network interface from a loadable kernel module.

I've been receiving a -EFAULT error for sending it (I've highlighted in the logs).

socat is able to send it without any issue using the same network interface and port.

Logs:
[ 4462.150380] onekfs: Interface: enp0s3, Broadcast Address: x.x.x.x
[ 4462.150395] onekfs: Broadcast of UDP packets at port 39529
[ 4462.150402] onekfs: UDP Packet Send failed: -14

Code:
Code:
#define ONEKFS_PORT     39529
static struct socket *sd_brdcst_sock = NULL;
static struct sockaddr_in sd_brdcst_sin;

__be32 onekfs_brdcst_addr = 0;

/*
... Here there is code that retrieves the broadcast address in __be32 format (for onekfs_brdcst_addr).
/*

/* Setting up socket */
{
        int sd_err = 0;
        int enable_brodcast = 1;
        sockptr_t enable_brdcst;

        /* Get socket */

        sd_err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0, &sd_brdcst_sock);
        if (sd_err < 0) {
                printk(KERN_ERR "onekfs: Failed to create broadcast socket: %d\n", sd_err);
                goto errout;
        }

        /* Enable broadcast */
        enable_brdcst.is_kernel = 1;
        enable_brdcst.kernel = &enable_brodcast;
        sd_err = sock_setsockopt(sd_brdcst_sock, SOL_SOCKET,
                                 SO_BROADCAST, enable_brdcst,
                                 sizeof(enable_brdcst));
        if (sd_err < 0) {
                printk(KERN_ERR "onekfs: Could not set up broadcast: %d\n", sd_err);
                goto errout_with_socket;
        }

        /* Bind to port */
        memset(&sd_brdcst_sin, 0, sizeof(struct sockaddr_in));

        sd_brdcst_sin.sin_family = AF_INET;
        sd_brdcst_sin.sin_port = htons(ONEKFS_PORT);
        printk(KERN_INFO "onekfs: ADD: %pI4", &onekfs_brdcst_addr);
        sd_brdcst_sin.sin_addr.s_addr = onekfs_brdcst_addr;

        sd_err = sd_brdcst_sock->ops->bind(sd_brdcst_sock,
                                           (struct sockaddr *)&sd_brdcst_sin,
                                           sizeof(struct sockaddr_in));
        if (sd_err < 0) {
                printk(KERN_ERR "onekfs: Failed to bind broadcast socket: %d\n", sd_err);
                goto errout_with_socket;
        }
        /* We're live */
        printk(KERN_INFO "onekfs: Broadcast of UDP packets at port %d\n",
               ONEKFS_PORT);

        return 0;


errout_with_socket:
        sock_release(sd_listener_sock);

errout:
        return sd_err;
}

int broadcast_msg(char *data, ssize_t data_len)
{
        /* Generate Msg */
        struct msghdr msg;
        struct iovec iov[1];
        int ret = 0;

        memset(&msg, 0, sizeof(struct msghdr));
        memset(&iov, 0, sizeof(struct kvec));
        
        iov[0].iov_base = data;
        iov[0].iov_len = data_len;
        msg.msg_name = (void *) &sd_brdcst_sin; 
        msg.msg_namelen = sizeof(struct sockaddr_in);
        iov_iter_init(&msg.msg_iter, WRITE, iov, 1, iov[0].iov_len);
        msg.msg_control = NULL;
        msg.msg_controllen = 0;
        msg.msg_flags = 0;

        /* Broadcast */
        ret = sock_sendmsg(sd_brdcst_sock, &msg);
        if (ret < 0) {
                /* THIS IS THE PLACE OF THE PROBLEM */
                printk(KERN_INFO "onekfs: UDP Packet Send failed: %d\n", ret);
                
                /* Temporary */
                sock_release(sd_brdcst_sock);
                sock_release(sd_listener_sock);
        }

        return ret < 0;
}
 
Old 03-28-2024, 02:51 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
> sd_brdcst_sin.sin_addr.s_addr = onekfs_brdcst_addr;
The above line is wrong, should be
sd_brdcst_sin.sin_addr.s_addr = INADDR_BROADCAST;
 
Old 04-06-2024, 12:36 PM   #3
resyfer
LQ Newbie
 
Registered: Mar 2024
Distribution: Endeavor OS
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by nini09 View Post
> should be
sd_brdcst_sin.sin_addr.s_addr = INADDR_BROADCAST;
Hi,

I had received the broadcast address for the desired address, and INADDR_BROADCAST only seemed to send this to listeners connected to my `lo` interface.
 
Old 04-06-2024, 12:37 PM   #4
resyfer
LQ Newbie
 
Registered: Mar 2024
Distribution: Endeavor OS
Posts: 3

Original Poster
Rep: Reputation: 0
I figured out the problem, `sock_sendmsg` needs to be replaced with `kernel_sendmsg` with the required fields, though I have yet to verify _why_ this works.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I know whether a module is a loadable module or compiled-in module? hahacc Linux - Kernel 3 11-21-2014 01:43 AM
Malformed IPv4 packets fuzzing cause 20% more CPU spike than IPv6 packets fuzzing 0206bharti Linux - Newbie 2 11-04-2014 01:35 AM
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 07:28 PM
encapsulating TCP packets in UDP packets... yoshi95 Programming 3 06-03-2004 02:53 PM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration