Kernel Newbies archive mirror
 help / color / mirror / Atom feed
From: Jaydeep Das <jaydeepjd.8914@gmail.com>
To: kernelnewbies@kernelnewbies.org
Subject: EFAULT for sending UDP from LKM
Date: Sun, 24 Mar 2024 00:15:53 +0530	[thread overview]
Message-ID: <CACaPSoufsW=Wxu=LqitMEArOwXh4OwbL8jm+FOSxxHcv17B76A@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 4512 bytes --]

Hey everyone,

I am trying to develop a linux kernel module. One of the task that it has
is to send a UDP packet to the broadcast address of
a network interface. I have attached the dmesg logs below. For some reason
it is able to bind to the address but it is not able
to send the packet. It shows EFAULT. Please advise.

Thank you in advance.

16 static struct socket *sd_listener_sock = NULL;
17 static struct sockaddr_in sd_listener_sin;
18 static struct socket *sd_brdcst_sock = NULL;
19 static struct sockaddr_in sd_brdcst_sin;
...
106 static int __init onekfs_sd_brdcst_init(void)
107 {
108         int sd_err = 0;
109         int enable_brodcast = 1;
110         sockptr_t enable_brdcst;
111
112         /* Get socket */
113
114         sd_err = sock_create_kern(&init_net, AF_INET, SOCK_DGRAM, 0,
&sd_brdcst_sock);
115         if (sd_err < 0) {
116                 printk(KERN_ERR "onekfs: Failed to create broadcast
socket: %d\n", sd_err);
117                 goto errout;
118         }
119
120         /* Enable broadcast */
121         enable_brdcst.is_kernel = 1;
122         enable_brdcst.kernel = &enable_brodcast;
123         sd_err = sock_setsockopt(sd_brdcst_sock, SOL_SOCKET,
124                                  SO_BROADCAST, enable_brdcst,
125                                  sizeof(enable_brdcst));
126         if (sd_err < 0) {
127                 printk(KERN_ERR "onekfs: Could not set up broadcast:
%d\n", sd_err);
128                 goto errout_with_socket;
129         }
130
131         /* Bind to port */
132         memset(&sd_brdcst_sin, 0, sizeof(struct sockaddr_in));
133
134         sd_brdcst_sin.sin_family = AF_INET;
135         sd_brdcst_sin.sin_port = htons(ONEKFS_PORT + 1);
136         sd_brdcst_sin.sin_addr.s_addr = htonl(onekfs_brdcst_addr);
137         // sd_brdcst_sin.sin_addr.s_addr = onekfs_brdcst_addr;
138
139         sd_err = sd_brdcst_sock->ops->bind(sd_brdcst_sock,
140                                            (struct sockaddr
*)&sd_brdcst_sin,
141                                            sizeof(struct sockaddr_in));
142         if (sd_err < 0) {
143                 printk(KERN_ERR "onekfs: Failed to bind broadcast
socket: %d\n", sd_err);
144                 goto errout_with_socket;
145         }
146
147         /* We're live */
148         printk(KERN_INFO "onekfs: Broadcast of UDP packets at port
%d\n",
149                ONEKFS_PORT + 1);
150
151         return 0;
152
153
154 errout_with_socket:
155         sock_release(sd_listener_sock);
156
157 errout:
158         return sd_err;
159 }
...
183 int broadcast_msg(char *data, ssize_t data_len)
184 {
185         /* Generate Msg */
186         struct msghdr* msg = kzalloc(sizeof (struct msghdr),
GFP_KERNEL);
187         struct iovec* iov = kzalloc(sizeof (struct iovec), GFP_KERNEL);
188         int ret;
189
190         // memset(&msg, 0, sizeof(struct msghdr));
191         // memset(&iov, 0, sizeof(struct kvec));
192
193         char *pk_data = kzalloc(PROJECT_NAME_LEN + data_len,
GFP_KERNEL);
194         if (!pk_data) {
195                 printk(KERN_WARNING "onekfs:"
196                        " Could not get memory for payload\n");
197                 return 1;
198         }
199
200         sprintf(pk_data, "onekfs%s", data);
201
202         iov->iov_base = pk_data;
203         iov->iov_len = PROJECT_NAME_LEN + data_len;
204         msg->msg_name = (void *)&sd_brdcst_sin;
205         msg->msg_namelen = sizeof(sd_brdcst_sin);
206         msg->msg_inq = PROJECT_NAME_LEN + data_len;
207         iov_iter_init(&msg->msg_iter, WRITE, iov, 1, iov->iov_len);
208         /* Broadcast */
209
210         printk(KERN_INFO "onekfs: Broadcast Socket type: %d\n",
sd_brdcst_sock->type);
211         __be32 temp = (sd_brdcst_sin.sin_addr.s_addr);
...
215         ret = sock_sendmsg(sd_brdcst_sock, msg);
217         */* TODO: -14 (Bad Address error) */*
218         if (ret < 0){
219                 printk(KERN_INFO "onekfs: UDP Packet Send failed:
%d\n", ret);
220                 sock_release(sd_brdcst_sock);
221                 sock_release(sd_listener_sock);
222                 kfree(msg);
223                 kfree(iov);
224         }
225
226         kfree(pk_data);
227
228         return ret < 0;
229 }

*Logs*:





*[ 4952.707575] onekfs: Interface: enp0s3, Broadcast Address: x.x.x.x[
4952.707626] onekfs: Broadcast of UDP packets at port 39530[ 4952.707639]
onekfs: Broadcast Addr: x.x.x.x[ 4952.707665] onekfs: UDP Packet Send
failed: -14[ 4952.707678] onekfs: Loaded*

[-- Attachment #1.2: Type: text/html, Size: 6374 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

                 reply	other threads:[~2024-03-23 18:48 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACaPSoufsW=Wxu=LqitMEArOwXh4OwbL8jm+FOSxxHcv17B76A@mail.gmail.com' \
    --to=jaydeepjd.8914@gmail.com \
    --cc=kernelnewbies@kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).