mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
netlink
This commit is contained in:
@@ -2439,7 +2439,7 @@ Can also be activated with the `panic_on_warn` boot parameter.
|
|||||||
|
|
||||||
==== CONFIG_PROC_EVENTS
|
==== CONFIG_PROC_EVENTS
|
||||||
|
|
||||||
Logs proc events such as process creation to a link:https://en.wikipedia.org/wiki/Netlink[netlink socket].
|
Logs proc events such as process creation to a link:kernel_module/netlink.c[netlink socket].
|
||||||
|
|
||||||
We then have a userland program that listens to the events and prints them out:
|
We then have a userland program that listens to the events and prints them out:
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ shift "$(($OPTIND - 1))"
|
|||||||
for arch in x86_64 arm aarch64; do
|
for arch in x86_64 arm aarch64; do
|
||||||
./build -a "$arch" -klq
|
./build -a "$arch" -klq
|
||||||
if "$gem5"; then
|
if "$gem5"; then
|
||||||
./build -a "$arch" -g -Gkl
|
./build -a "$arch" -Gkl
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
@@ -10,37 +10,39 @@
|
|||||||
.. link:oops.c[]
|
.. link:oops.c[]
|
||||||
.. link:warn_on.c[]
|
.. link:warn_on.c[]
|
||||||
.. link:warn_on.c[]
|
.. link:warn_on.c[]
|
||||||
. Module utils
|
. Modules
|
||||||
.. link:params.c[]
|
.. link:params.c[]
|
||||||
.. link:vermagic.c[]
|
.. link:vermagic.c[]
|
||||||
.. link:vermagic_fail.c[]
|
.. link:vermagic_fail.c[]
|
||||||
.. link:module_info.c[]
|
.. link:module_info.c[]
|
||||||
|
.. Module dependencies
|
||||||
|
... link:dep.c[]
|
||||||
|
... link:dep2.c[]
|
||||||
. Pseudo filesystems
|
. Pseudo filesystems
|
||||||
|
.. link:anonymous_inode.c[]
|
||||||
|
.. link:character_device.c[]
|
||||||
|
.. link:character_device_create.c[]
|
||||||
.. link:debugfs.c[]
|
.. link:debugfs.c[]
|
||||||
.. link:procfs.c[]
|
|
||||||
.. link:sysfs.c[]
|
|
||||||
.. link:fops.c[]
|
.. link:fops.c[]
|
||||||
.. link:ioctl.c[]
|
.. link:ioctl.c[]
|
||||||
.. link:poll.c[]
|
|
||||||
.. link:mmap.c[]
|
.. link:mmap.c[]
|
||||||
.. link:anonymous_inode.c[]
|
.. link:poll.c[]
|
||||||
|
.. link:procfs.c[]
|
||||||
.. link:seq_file.c[]
|
.. link:seq_file.c[]
|
||||||
.. link:seq_file_inode.c[]
|
.. link:seq_file_inode.c[]
|
||||||
|
.. link:sysfs.c[]
|
||||||
. Asynchronous
|
. Asynchronous
|
||||||
.. link:workqueue_cheat.c[]
|
.. link:irq.c[]
|
||||||
.. link:sleep.c[]
|
|
||||||
.. link:kthread.c[]
|
.. link:kthread.c[]
|
||||||
.. link:kthreads.c[]
|
.. link:kthreads.c[]
|
||||||
.. link:schedule.c[]
|
.. link:schedule.c[]
|
||||||
|
.. link:sleep.c[]
|
||||||
.. link:timer.c[]
|
.. link:timer.c[]
|
||||||
.. link:work_from_work.c[]
|
.. link:work_from_work.c[]
|
||||||
.. link:irq.c[]
|
.. link:workqueue_cheat.c[]
|
||||||
. Module dependencies
|
. Misc
|
||||||
.. link:dep.c[]
|
|
||||||
.. link:dep2.c[]
|
|
||||||
.. link:character_device.c[]
|
|
||||||
.. link:character_device_create.c[]
|
|
||||||
.. link:virt_to_phys.c[]
|
.. link:virt_to_phys.c[]
|
||||||
|
.. link:netlink.c[]
|
||||||
. Utilities
|
. Utilities
|
||||||
.. link:kstrto.c[]
|
.. link:kstrto.c[]
|
||||||
. Arch
|
. Arch
|
||||||
|
|||||||
78
kernel_module/netlink.c
Normal file
78
kernel_module/netlink.c
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
https://en.wikipedia.org/wiki/Netlink
|
||||||
|
|
||||||
|
https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <linux/delay.h> /* usleep_range */
|
||||||
|
#include <linux/jiffies.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/netlink.h>
|
||||||
|
#include <linux/skbuff.h>
|
||||||
|
#include <net/sock.h>
|
||||||
|
|
||||||
|
/* Socket identifier, matches userland. TODO can be anything?
|
||||||
|
* Is there a more scalable way to do it? E.g. ioctl device,
|
||||||
|
* kernel generates one on the fly, then give it back and connect?
|
||||||
|
* https://stackoverflow.com/questions/32898173/can-i-have-more-than-32-netlink-sockets-in-kernelspace */
|
||||||
|
#define NETLINK_USER 31
|
||||||
|
|
||||||
|
struct sock *nl_sk = NULL;
|
||||||
|
|
||||||
|
static void callback(struct sk_buff *skb)
|
||||||
|
{
|
||||||
|
char readbuf[1024];
|
||||||
|
size_t readbuflen;
|
||||||
|
int pid;
|
||||||
|
int res;
|
||||||
|
struct nlmsghdr *nlh;
|
||||||
|
struct sk_buff *skb_out;
|
||||||
|
|
||||||
|
/* Read user message. */
|
||||||
|
nlh = (struct nlmsghdr *)skb->data;
|
||||||
|
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
|
||||||
|
|
||||||
|
/* Add an artificial sleep to see what happens when
|
||||||
|
* multiple requests come in at the same time.
|
||||||
|
*
|
||||||
|
* Try this out (it works):
|
||||||
|
* for i in `seq 16`; do /netlink.out & done */
|
||||||
|
usleep_range(1000000, 1000001);
|
||||||
|
|
||||||
|
/* Reply with jiffies. */
|
||||||
|
readbuflen = snprintf(readbuf, sizeof(readbuf), "%llu", (unsigned long long)jiffies);
|
||||||
|
pid = nlh->nlmsg_pid;
|
||||||
|
skb_out = nlmsg_new(readbuflen, 0);
|
||||||
|
if (!skb_out) {
|
||||||
|
pr_err("nlmsg_new\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, readbuflen, 0);
|
||||||
|
NETLINK_CB(skb_out).dst_group = 0;
|
||||||
|
strncpy(nlmsg_data(nlh), readbuf, readbuflen);
|
||||||
|
res = nlmsg_unicast(nl_sk, skb_out, pid);
|
||||||
|
if (res < 0)
|
||||||
|
pr_info("nlmsg_unicast\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int myinit(void)
|
||||||
|
{
|
||||||
|
struct netlink_kernel_cfg cfg = {
|
||||||
|
.input = callback,
|
||||||
|
};
|
||||||
|
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
|
||||||
|
if (!nl_sk) {
|
||||||
|
pr_err("netlink_kernel_create\n");
|
||||||
|
return -10;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void myexit(void)
|
||||||
|
{
|
||||||
|
netlink_kernel_release(nl_sk);
|
||||||
|
}
|
||||||
|
|
||||||
|
module_init(myinit);
|
||||||
|
module_exit(myexit);
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
@@ -25,5 +25,6 @@ These programs can also be compiled and used on host.
|
|||||||
... link:ring0.c[]
|
... link:ring0.c[]
|
||||||
. Module tests
|
. Module tests
|
||||||
.. link:anonymous_inode.c[]
|
.. link:anonymous_inode.c[]
|
||||||
.. link:poll.c[]
|
|
||||||
.. link:ioctl.c[]
|
.. link:ioctl.c[]
|
||||||
|
.. link:netlink.c[]
|
||||||
|
.. link:poll.c[]
|
||||||
|
|||||||
49
kernel_module/user/netlink.c
Normal file
49
kernel_module/user/netlink.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include <linux/netlink.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define MAX_PAYLOAD 1024
|
||||||
|
#define NETLINK_USER 31
|
||||||
|
|
||||||
|
/* Some of these structs fields must be zeroed.
|
||||||
|
* We could brute force memset them, but
|
||||||
|
* TODO determine exactly which, and move into main. */
|
||||||
|
int sock_fd;
|
||||||
|
struct iovec iov;
|
||||||
|
struct msghdr msg;
|
||||||
|
struct nlmsghdr *nlh;
|
||||||
|
struct sockaddr_nl src_addr, dest_addr;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
|
||||||
|
if (sock_fd < 0) {
|
||||||
|
perror("socket");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
src_addr.nl_family = AF_NETLINK;
|
||||||
|
src_addr.nl_pid = getpid();
|
||||||
|
bind(sock_fd, (struct sockaddr *)&src_addr, sizeof(src_addr));
|
||||||
|
dest_addr.nl_family = AF_NETLINK;
|
||||||
|
nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
|
||||||
|
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
|
||||||
|
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
|
||||||
|
nlh->nlmsg_pid = getpid();
|
||||||
|
nlh->nlmsg_flags = 0;
|
||||||
|
strcpy(NLMSG_DATA(nlh), "user request");
|
||||||
|
iov.iov_base = (void *)nlh;
|
||||||
|
iov.iov_len = nlh->nlmsg_len;
|
||||||
|
msg.msg_name = (void *)&dest_addr;
|
||||||
|
msg.msg_namelen = sizeof(dest_addr);
|
||||||
|
msg.msg_iov = &iov;
|
||||||
|
msg.msg_iovlen = 1;
|
||||||
|
printf("before sendmsg\n");
|
||||||
|
sendmsg(sock_fd, &msg, 0);
|
||||||
|
printf("after sendmsg\n");
|
||||||
|
recvmsg(sock_fd, &msg, 0);
|
||||||
|
printf("userland received: %s\n", (char *)NLMSG_DATA(nlh));
|
||||||
|
close(sock_fd);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user