linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Listening on a TCP socket from a Kernel Module
@ 2021-05-16 23:20 louisrossberg
  2021-05-17  1:55 ` Neal Cardwell
  0 siblings, 1 reply; 3+ messages in thread
From: louisrossberg @ 2021-05-16 23:20 UTC (permalink / raw)
  Cc: linux-kernel

Hello, can somebody point me in the right direction for what I would use to listen on a TCP socket from the kernel? I am working on a kernel module and have spent the past day looking through include/net and include/linux for something that would allow me to do so. I know TCP listening is typically done in userspace, but it should be possible at the kernel level right? tcp_diag looks promising, but it seems like that is mainly for monitoring sockets, and I'm not sure if I would be able to provide responses from it.

Louis Rossberg,
Warped Technologies


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Listening on a TCP socket from a Kernel Module
  2021-05-16 23:20 Listening on a TCP socket from a Kernel Module louisrossberg
@ 2021-05-17  1:55 ` Neal Cardwell
  2021-05-17 12:50   ` David Laight
  0 siblings, 1 reply; 3+ messages in thread
From: Neal Cardwell @ 2021-05-17  1:55 UTC (permalink / raw)
  To: louisrossberg; +Cc: linux-kernel, Netdev

On Sun, May 16, 2021 at 7:20 PM louisrossberg
<louisrossberg@protonmail.com> wrote:
>
> Hello, can somebody point me in the right direction for
> what I would use to listen on a TCP socket from the
> kernel? I am working on a kernel module and have spent the
> past day looking through include/net and include/linux for
> something that would allow me to do so. I know TCP
> listening is typically done in userspace, but it should be
> possible at the kernel level right?  tcp_diag looks
> promising, but it seems like that is mainly for monitoring
> sockets, and I'm not sure if I would be able to provide
> responses from it.
>
> Louis Rossberg,
> Warped Technologies

Perhaps kernel_listen() and related functions (kernel_bind(),
kernel_accept(), etc.) in net/socket.c might do the trick for your use
case? Looking at how the callers of these functions structure their
code might give you enough to go on.

(I'm adding the Linux kernel networking mailing list to this thread as well.)

best regards,
neal

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: Listening on a TCP socket from a Kernel Module
  2021-05-17  1:55 ` Neal Cardwell
@ 2021-05-17 12:50   ` David Laight
  0 siblings, 0 replies; 3+ messages in thread
From: David Laight @ 2021-05-17 12:50 UTC (permalink / raw)
  To: 'Neal Cardwell', louisrossberg; +Cc: linux-kernel, Netdev

From: Neal Cardwell
> Sent: 17 May 2021 02:55
> 
> On Sun, May 16, 2021 at 7:20 PM louisrossberg
> <louisrossberg@protonmail.com> wrote:
> >
> > Hello, can somebody point me in the right direction for
> > what I would use to listen on a TCP socket from the
> > kernel? I am working on a kernel module and have spent the
> > past day looking through include/net and include/linux for
> > something that would allow me to do so. I know TCP
> > listening is typically done in userspace, but it should be
> > possible at the kernel level right?  tcp_diag looks
> > promising, but it seems like that is mainly for monitoring
> > sockets, and I'm not sure if I would be able to provide
> > responses from it.
> >
> > Louis Rossberg,
> > Warped Technologies
> 
> Perhaps kernel_listen() and related functions (kernel_bind(),
> kernel_accept(), etc.) in net/socket.c might do the trick for your use
> case? Looking at how the callers of these functions structure their
> code might give you enough to go on.

They should work.
There are a couple of issues though:
- There is no getsockopt() support in current kernels.
- You may need to use __sock_create() rather than sock_create_kern()
  in order to hold a reference to the network namespace.
It may be possible to use the wakeup callbacks that select/poll use.
But it is probably safer to use blocking operations from a separate
kernel thread.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-17 12:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-16 23:20 Listening on a TCP socket from a Kernel Module louisrossberg
2021-05-17  1:55 ` Neal Cardwell
2021-05-17 12:50   ` David Laight

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 NNTP newsgroup(s).