From 43c513ea604c77e71333d8307bdb9b710b091721 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Tue, 9 Apr 2024 06:55:39 +0100 Subject: Add netlink socket --- md/index.md | 6 ++--- md/notes/kernel/netlink_socket.md | 50 +++++++++++++++++++-------------------- md/notes/kernel/topics.md | 12 +++++++++- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/md/index.md b/md/index.md index d431024..9bb36df 100644 --- a/md/index.md +++ b/md/index.md @@ -6,6 +6,7 @@ title: Writeup page +[Kernel programming](notes/kernel/topics.md) [QEMU ARM64](writeup/qemu_arm64.md) [BladeRF quick guide](writeup/bladerf_quick_guide.md) [Writing linux mount utility](writeup/writing_linux_mount_utility.md) @@ -38,9 +39,8 @@ title: Writeup page [Using GDB](writeup/using_gdb.md) [Linux syscall table](notes/syscalls.md) [Using mitmproxy](writeup/mitmproxy.md) -[Using RTLSDR](writeup/rtlsdr_usage.md) +[Using RTLSDR](writeup/rtlsdr_usage.md) - @@ -58,7 +58,7 @@ title: Writeup page [Linux antidebug 3](writeup/linux_antidebug_3.md) [Linux antidebug 4](writeup/linux_antidebug_4.md) [Linux antidebug 5](writeup/linux_antidebug_5.md) -[C C11 standard _Generic keyword](writeup/c_c11_standard_generic_keyword.md) +[C C11 standard \_Generic keyword](writeup/c_c11_standard_generic_keyword.md) [C inline assembler](writeup/c_inline_assembler.md) [Wrapping C++ exceptions, templated and classes in C](writeup/wraping_c_plus_plus_exceptions_templates_and_classes_in_c.md) [Makefile tips](writeup/makefile_tips.md) diff --git a/md/notes/kernel/netlink_socket.md b/md/notes/kernel/netlink_socket.md index 01e9043..e022e52 100644 --- a/md/notes/kernel/netlink_socket.md +++ b/md/notes/kernel/netlink_socket.md @@ -3,11 +3,11 @@ keywords: kernel,linux,netlink,socket # Kernel compile "Hello world" -Compile minimal linux kernel module. +As base start from minimal kernel module ## Files -You need to create to files __Makefile__ and __hello_world.c__. +You need to create to files __Makefile__ and __netlink_socket.c__. __Makefile__ ```Makefile @@ -206,51 +206,51 @@ create utility that connects and interacts with netlink socket. int main(int argc, char **argv) { - struct sockaddr_nl src_addr; - struct sockaddr_nl dest_addr; - struct nlmsghdr *nlh; - struct msghdr msg; - struct iovec iov; - int sock_fd; - int ret; + struct sockaddr_nl src_addr; + struct sockaddr_nl dest_addr; + struct nlmsghdr *nlh; + struct msghdr msg; + struct iovec iov; + int sock_fd; + int ret; - sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_PROTO); + sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_PROTO); if (sock_fd < 0) { printf("socket creation for NETLINK failed...\n"); return -1; } - memset(&src_addr, 0, sizeof(src_addr)); + memset(&src_addr, 0, sizeof(src_addr)); src_addr.nl_family = AF_NETLINK; src_addr.nl_pid = getpid(); bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr)); - nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(PAYLOAD_SIZE)); + nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(PAYLOAD_SIZE)); memset(nlh, 0, NLMSG_SPACE(PAYLOAD_SIZE)); nlh->nlmsg_len = NLMSG_SPACE(PAYLOAD_SIZE); nlh->nlmsg_pid = getpid(); nlh->nlmsg_flags = 0; - strcpy(NLMSG_DATA(nlh), "Test test Test!"); + strcpy(NLMSG_DATA(nlh), "Test test Test!"); - memset(&dest_addr, 0, sizeof(dest_addr)); + memset(&dest_addr, 0, sizeof(dest_addr)); dest_addr.nl_family = AF_NETLINK; dest_addr.nl_pid = 0; // Linux Kernel PID dest_addr.nl_groups = 0; - memset(&iov, 0, sizeof(iov)); - iov.iov_base = (void *)nlh; - iov.iov_len = nlh->nlmsg_len; + memset(&iov, 0, sizeof(iov)); + iov.iov_base = (void *)nlh; + iov.iov_len = nlh->nlmsg_len; - memset(&msg, 0, sizeof(msg)); - msg.msg_name = (void *)&dest_addr; - msg.msg_namelen = sizeof(dest_addr); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; + memset(&msg, 0, sizeof(msg)); + msg.msg_name = (void *)&dest_addr; + msg.msg_namelen = sizeof(dest_addr); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; - ret = sendmsg(sock_fd, &msg, 0); + ret = sendmsg(sock_fd, &msg, 0); printf("Message sent, payload: %s\n", (char *)NLMSG_DATA(nlh)); // Clear netlink packet payload: @@ -260,9 +260,9 @@ int main(int argc, char **argv) { ret = recvmsg(sock_fd, &msg, 0); printf("Message received, payload: %s\n", (char *)NLMSG_DATA(nlh)); - close(sock_fd); + close(sock_fd); - return 0; + return 0; } ``` diff --git a/md/notes/kernel/topics.md b/md/notes/kernel/topics.md index d955193..8682aeb 100644 --- a/md/notes/kernel/topics.md +++ b/md/notes/kernel/topics.md @@ -5,7 +5,10 @@ keywords:blog,projects Linux kernel programming topics, mostly for those who have some programming experience and linux command line expertise. Making notes on all fundamental -kernel API's. +kernel API's and gathering all examples from all over the internet, all is +compiled and test on Raspberry Pi 4 kernel 6.6.20. No other versions of kernel +is checked. With move one to new SoC or new kernel all examples will also update +accordingly. ## Topic @@ -47,7 +50,14 @@ kernel API's. + + + ### Kernel userspace +[Netlink show ip](/notes/kernel/netlink_show_ip.md) + + + -- cgit v1.2.3