Skip to content

Commit e05eb91

Browse files
committed
netlink: move docs to README
1 parent 3d4e0c0 commit e05eb91

File tree

7 files changed

+72
-29
lines changed

7 files changed

+72
-29
lines changed

README.adoc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3920,6 +3920,42 @@ Reads to that inode return the sequence: `1`, `10`, `100`, ... `10000000`, `1`,
39203920

39213921
Bibliography: https://stackoverflow.com/questions/4508998/what-is-an-anonymous-inode-in-linux
39223922

3923+
==== netlink sockets
3924+
3925+
Netlink sockets offer a socket API for kernel / userland communication:
3926+
3927+
....
3928+
/netlink.sh
3929+
echo $?
3930+
....
3931+
3932+
Outcome: the test passes:
3933+
3934+
....
3935+
0
3936+
....
3937+
3938+
Sources:
3939+
3940+
* link:kernel_module/netlink.c[]
3941+
* link:kernel_module/netlink.h[]
3942+
* link:kernel_module/user/netlink.c[]
3943+
* link:rootfs_overlay/netlink.sh[]
3944+
3945+
Launch multiple user requests in parallel to stress our socket:
3946+
3947+
....
3948+
insmod /netlink.ko sleep=1
3949+
for i in `seq 16`; do /netlink.out & done
3950+
....
3951+
3952+
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
3953+
3954+
Bibliography:
3955+
3956+
* https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module
3957+
* https://en.wikipedia.org/wiki/Netlink
3958+
39233959
=== Linux kernel asynchronous APIs
39243960

39253961
In this section we will document asynchronous APIs of Linux kernel, especially kthread-related scheduled events.

kernel_module/README.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Our kernel modules!
99
.. link:timer.c[]
1010
.. link:work_from_work.c[]
1111
.. link:workqueue_cheat.c[]
12-
. Misc
13-
.. link:netlink.c[]
1412
. Hardening
1513
.. link:strlen_overflow.c[]
1614
. Tracing

kernel_module/netlink.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,34 @@
1-
/*
2-
https://en.wikipedia.org/wiki/Netlink
3-
4-
https://stackoverflow.com/questions/3299386/how-to-use-netlink-socket-to-communicate-with-a-kernel-module
5-
*/
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
62

73
#include <linux/delay.h> /* usleep_range */
8-
#include <linux/jiffies.h>
94
#include <linux/module.h>
105
#include <linux/netlink.h>
116
#include <linux/skbuff.h>
127
#include <net/sock.h>
138

14-
/* Socket identifier, matches userland. TODO can be anything?
15-
* Is there a more scalable way to do it? E.g. ioctl device,
16-
* kernel generates one on the fly, then give it back and connect?
17-
* https://stackoverflow.com/questions/32898173/can-i-have-more-than-32-netlink-sockets-in-kernelspace */
18-
#define NETLINK_USER 31
9+
#include "netlink.h"
1910

2011
struct sock *nl_sk = NULL;
2112

13+
static u32 count;
14+
static u32 sleep;
15+
module_param(sleep, int, S_IRUSR | S_IWUSR);
16+
2217
static void callback(struct sk_buff *skb)
2318
{
24-
char readbuf[1024];
19+
char readbuf[9];
2520
size_t readbuflen;
2621
int pid;
2722
int res;
2823
struct nlmsghdr *nlh;
2924
struct sk_buff *skb_out;
3025

31-
/* Read user message. */
3226
nlh = (struct nlmsghdr *)skb->data;
3327
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
34-
35-
/* Add an artificial sleep to see what happens when
36-
* multiple requests come in at the same time.
37-
*
38-
* Try this out (it works):
39-
* for i in `seq 16`; do /netlink.out & done */
40-
usleep_range(1000000, 1000001);
41-
42-
/* Reply with jiffies. */
43-
readbuflen = snprintf(readbuf, sizeof(readbuf), "%llu", (unsigned long long)jiffies);
28+
if (sleep)
29+
usleep_range(1000000, 1000001);
30+
readbuflen = snprintf(readbuf, sizeof(readbuf), "%x", count);
31+
count++;
4432
pid = nlh->nlmsg_pid;
4533
skb_out = nlmsg_new(readbuflen, 0);
4634
if (!skb_out) {

kernel_module/netlink.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef NETLINK_H
2+
#define NETLINK_H
3+
4+
/* Socket identifier, matches userland. TODO can be anything?
5+
* Is there a more scalable way to do it? E.g. ioctl device,
6+
* kernel generates one on the fly, then give it back and connect?
7+
* https://stackoverflow.com/questions/32898173/can-i-have-more-than-32-netlink-sockets-in-kernelspace */
8+
#define NETLINK_USER 31
9+
10+
#endif

kernel_module/user/netlink.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
2+
13
#include <linux/netlink.h>
24
#include <stdio.h>
35
#include <stdlib.h>
46
#include <string.h>
57
#include <sys/socket.h>
68
#include <unistd.h>
79

10+
#include "../netlink.h"
11+
812
#define MAX_PAYLOAD 1024
9-
#define NETLINK_USER 31
1013

1114
/* Some of these structs fields must be zeroed.
1215
* We could brute force memset them, but
@@ -40,10 +43,10 @@ int main()
4043
msg.msg_namelen = sizeof(dest_addr);
4144
msg.msg_iov = &iov;
4245
msg.msg_iovlen = 1;
43-
printf("before sendmsg\n");
46+
fprintf(stderr, "before sendmsg\n");
4447
sendmsg(sock_fd, &msg, 0);
45-
printf("after sendmsg\n");
48+
fprintf(stderr, "after sendmsg\n");
4649
recvmsg(sock_fd, &msg, 0);
47-
printf("userland received: %s\n", (char *)NLMSG_DATA(nlh));
50+
printf("%s\n", (char *)NLMSG_DATA(nlh));
4851
close(sock_fd);
4952
}

rootfs_overlay/netlink.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
set -e
3+
insmod /netlink.ko
4+
[ "$(/netlink.out)" = 0 ]
5+
[ "$(/netlink.out)" = 1 ]
6+
[ "$(/netlink.out)" = 2 ]
7+
rmmod netlink

rootfs_overlay/test_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ for test in \
1010
/ioctl.sh \
1111
/kstrto.sh \
1212
/mmap.sh \
13+
/netlink.sh \
1314
/params.sh \
1415
/procfs.sh \
1516
/seq_file.sh \

0 commit comments

Comments
 (0)