mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-27 04:01:36 +01:00
netlink: move docs to README
This commit is contained in:
36
README.adoc
36
README.adoc
@@ -3920,6 +3920,42 @@ Reads to that inode return the sequence: `1`, `10`, `100`, ... `10000000`, `1`,
|
|||||||
|
|
||||||
Bibliography: https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux
|
Bibliography: https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux
|
||||||
|
|
||||||
|
==== netlink sockets
|
||||||
|
|
||||||
|
Netlink sockets offer a socket API for kernel / userland communication:
|
||||||
|
|
||||||
|
....
|
||||||
|
/netlink.sh
|
||||||
|
echo $?
|
||||||
|
....
|
||||||
|
|
||||||
|
Outcome: the test passes:
|
||||||
|
|
||||||
|
....
|
||||||
|
0
|
||||||
|
....
|
||||||
|
|
||||||
|
Sources:
|
||||||
|
|
||||||
|
* link:kernel_module/netlink.c[]
|
||||||
|
* link:kernel_module/netlink.h[]
|
||||||
|
* link:kernel_module/user/netlink.c[]
|
||||||
|
* link:rootfs_overlay/netlink.sh[]
|
||||||
|
|
||||||
|
Launch multiple user requests in parallel to stress our socket:
|
||||||
|
|
||||||
|
....
|
||||||
|
insmod /netlink.ko sleep=1
|
||||||
|
for i in `seq 16`; do /netlink.out & done
|
||||||
|
....
|
||||||
|
|
||||||
|
TODO: what is the advantage over `read`, `write` and `poll`? https://stackoverflow.com/questions/16727212/how-netlink-socket-in-linux-kernel-is-different-from-normal-polling-done-by-appl
|
||||||
|
|
||||||
|
Bibliography:
|
||||||
|
|
||||||
|
* https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module
|
||||||
|
* https://en.wikipedia.org/wiki/Netlink
|
||||||
|
|
||||||
=== Linux kernel asynchronous APIs
|
=== Linux kernel asynchronous APIs
|
||||||
|
|
||||||
In this section we will document asynchronous APIs of Linux kernel, especially kthread-related scheduled events.
|
In this section we will document asynchronous APIs of Linux kernel, especially kthread-related scheduled events.
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ Our kernel modules!
|
|||||||
.. link:timer.c[]
|
.. link:timer.c[]
|
||||||
.. link:work_from_work.c[]
|
.. link:work_from_work.c[]
|
||||||
.. link:workqueue_cheat.c[]
|
.. link:workqueue_cheat.c[]
|
||||||
. Misc
|
|
||||||
.. link:netlink.c[]
|
|
||||||
. Hardening
|
. Hardening
|
||||||
.. link:strlen_overflow.c[]
|
.. link:strlen_overflow.c[]
|
||||||
. Tracing
|
. Tracing
|
||||||
|
|||||||
@@ -1,46 +1,34 @@
|
|||||||
/*
|
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
|
||||||
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/delay.h> /* usleep_range */
|
||||||
#include <linux/jiffies.h>
|
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/netlink.h>
|
#include <linux/netlink.h>
|
||||||
#include <linux/skbuff.h>
|
#include <linux/skbuff.h>
|
||||||
#include <net/sock.h>
|
#include <net/sock.h>
|
||||||
|
|
||||||
/* Socket identifier, matches userland. TODO can be anything?
|
#include "netlink.h"
|
||||||
* 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;
|
struct sock *nl_sk = NULL;
|
||||||
|
|
||||||
|
static u32 count;
|
||||||
|
static u32 sleep;
|
||||||
|
module_param(sleep, int, S_IRUSR | S_IWUSR);
|
||||||
|
|
||||||
static void callback(struct sk_buff *skb)
|
static void callback(struct sk_buff *skb)
|
||||||
{
|
{
|
||||||
char readbuf[1024];
|
char readbuf[9];
|
||||||
size_t readbuflen;
|
size_t readbuflen;
|
||||||
int pid;
|
int pid;
|
||||||
int res;
|
int res;
|
||||||
struct nlmsghdr *nlh;
|
struct nlmsghdr *nlh;
|
||||||
struct sk_buff *skb_out;
|
struct sk_buff *skb_out;
|
||||||
|
|
||||||
/* Read user message. */
|
|
||||||
nlh = (struct nlmsghdr *)skb->data;
|
nlh = (struct nlmsghdr *)skb->data;
|
||||||
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
|
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
|
||||||
|
if (sleep)
|
||||||
/* Add an artificial sleep to see what happens when
|
usleep_range(1000000, 1000001);
|
||||||
* multiple requests come in at the same time.
|
readbuflen = snprintf(readbuf, sizeof(readbuf), "%x", count);
|
||||||
*
|
count++;
|
||||||
* 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;
|
pid = nlh->nlmsg_pid;
|
||||||
skb_out = nlmsg_new(readbuflen, 0);
|
skb_out = nlmsg_new(readbuflen, 0);
|
||||||
if (!skb_out) {
|
if (!skb_out) {
|
||||||
|
|||||||
10
kernel_module/netlink.h
Normal file
10
kernel_module/netlink.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef NETLINK_H
|
||||||
|
#define NETLINK_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
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
|
||||||
|
|
||||||
#include <linux/netlink.h>
|
#include <linux/netlink.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -5,8 +7,9 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../netlink.h"
|
||||||
|
|
||||||
#define MAX_PAYLOAD 1024
|
#define MAX_PAYLOAD 1024
|
||||||
#define NETLINK_USER 31
|
|
||||||
|
|
||||||
/* Some of these structs fields must be zeroed.
|
/* Some of these structs fields must be zeroed.
|
||||||
* We could brute force memset them, but
|
* We could brute force memset them, but
|
||||||
@@ -40,10 +43,10 @@ int main()
|
|||||||
msg.msg_namelen = sizeof(dest_addr);
|
msg.msg_namelen = sizeof(dest_addr);
|
||||||
msg.msg_iov = &iov;
|
msg.msg_iov = &iov;
|
||||||
msg.msg_iovlen = 1;
|
msg.msg_iovlen = 1;
|
||||||
printf("before sendmsg\n");
|
fprintf(stderr, "before sendmsg\n");
|
||||||
sendmsg(sock_fd, &msg, 0);
|
sendmsg(sock_fd, &msg, 0);
|
||||||
printf("after sendmsg\n");
|
fprintf(stderr, "after sendmsg\n");
|
||||||
recvmsg(sock_fd, &msg, 0);
|
recvmsg(sock_fd, &msg, 0);
|
||||||
printf("userland received: %s\n", (char *)NLMSG_DATA(nlh));
|
printf("%s\n", (char *)NLMSG_DATA(nlh));
|
||||||
close(sock_fd);
|
close(sock_fd);
|
||||||
}
|
}
|
||||||
|
|||||||
7
rootfs_overlay/netlink.sh
Executable file
7
rootfs_overlay/netlink.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
insmod /netlink.ko
|
||||||
|
[ "$(/netlink.out)" = 0 ]
|
||||||
|
[ "$(/netlink.out)" = 1 ]
|
||||||
|
[ "$(/netlink.out)" = 2 ]
|
||||||
|
rmmod netlink
|
||||||
@@ -10,6 +10,7 @@ for test in \
|
|||||||
/ioctl.sh \
|
/ioctl.sh \
|
||||||
/kstrto.sh \
|
/kstrto.sh \
|
||||||
/mmap.sh \
|
/mmap.sh \
|
||||||
|
/netlink.sh \
|
||||||
/params.sh \
|
/params.sh \
|
||||||
/procfs.sh \
|
/procfs.sh \
|
||||||
/seq_file.sh \
|
/seq_file.sh \
|
||||||
|
|||||||
Reference in New Issue
Block a user