extending iptables kernel modules - linux

I want to write a kernel module that adds some features to linux iptables. Before starting to code my own module, I want to do some tests and try to use some modules and try to add them to my freshly installed Ubuntu 15.5 machine. It provides the libipt_xx.c , ipt_xx.c and ipt_xx.h, about which I read in some tutorial that they should past the file in that way:
ipt_xx.c -> /usr/src/linux-headers-4.2.0-16/net/ipv4/netfilter/
ipt_xx.h -> /usr/src/linux-headers-4.2.0-16/include/uapi/linux/netfilter_ipv4
libipt_xx.c -> /usr/local/src/iptables-x.x.x/extensions/
But, I can't find the /usr/local/src/iptables-x.x.x/ directory. When I execute iptables -V , I have iptables v1.4.21 as output, so it means I should have a folder like iptables-1.4.21.
Thanks for help.

Some of the files that you mention are part of the Linux kernel source. You get them when you download the Linux kernel source code from one of multiple places. These files are compiled into the kernel, either statically or in the form of modules. These are the files located in:
net/ipv4/netfilter/
include/uapi/linux/netfilter_ipv4/
Other files are part of iptables source code which is a user space application. You can get the source code from Netfilter Git repository. These are the files located in:
iptables-x.x.x/extensions/

Related

Command to extract Linux source code in Yocto

I am working on Yocto, and I added meta-intel layer to my bblayers.conf..
I need to make modifications to Linux source code, so I need to look at what all files are present..
Can you please provide the bitbake command to get the location of linux source code.
I tried bitbake -c unpack linux-intel and went to build/tmp/work/ and did not find source code..
Certain components which are used as source for multiple recipes are extracted into a shared place as a build optimization and linux kernel is one of such components so you can run
bitbake -cshared_workdir virtual/kernel
then the sources will be in
<TMPDIR>/work-shared/<MACHINE>/kernel-source
If you then want to modify sources then
there are couple of ways to do kernel development
Devtool
Tradition Kernel Development

Manually building a Kernel source from Yocto build

I have a Yocto build for i.mx6 and I want to modify its Kernel. I figured that if I copy Kernel source outside the Yocto project and make my modifications without dealing with patches, I can speed things up significantly. But the thing is, the Kernel source I have to use is already patched and I want to fetch and continue working from there. I will work on the already-patched source files and re-arranging them is a painful process.
For starting point, my patches work fine, and I can get a working image using bitbake fsl-image-multimedia-full command. The Kernel source I want to use is created after this process.
I have tried copying the source under ..../tmp/work-shared/imx6qsabresd/kernel-source. Although make zImage and make modules finished without any trouble, manual building was not successful with an error in a dtsi file (Unable to parse...). Of course, I have checked the file and there was no syntax error.
Also, I checked the kernel source files I copied and it seems that the patches are successfully implemented.
Am I doing something wrong with the patches? With my manual build routine, I can build unpatched kernel source with no errors. I am sure that there are experienced Yocto users here that have their own workarounds to make this process shorter. So, any help is appreciated. Thanks in advance.
You can also edit files in tmp/work-shared/<machine>/kernel-source then compile modified kernel with bitbake -C compile virtual/kernel
My favorite method of doing kernel development in a Yocto project is to create an SDK and build the kernel outside of the Yocto system. This allows more rapid builds because make will only build new changes, whereas a kernel build within Yocto always starts from scratch.
Here are some of my notes on compiling the Linux kernel outside of the Yocto system. The exact paths for this will depend on your exact configuration and software versions. In your case, IMAGE_NAME=fsl-image-multimedia-full
Run bitbake -c populate_sdk ${IMAGE_NAME}. You will get a
self-extracting and self-installing shell script.
Run the shell script (for me it was
tmp/deploy/sdk/${NAME}-glibc-i686-${IMAGE_NAME}-cortexa9hf-neon-toolchain-1.0.0.sh),
and agree to the default SDK location (for me it was
usr/local/oecore-i686).
Source the scripts generated by the install script. I use the
following helper script to load the SDK so I don't have to keep
track of the paths involved. You need to source this in each time
you want to use the SDK.
enable_sdk.sh:
#!/bin/bash
if [[ "$0" = "$BASH_SOURCE" ]]
then
echo "Error: you must source this script."
exit 1
fi
source /usr/local/oecore-i686/environment-setup-corei7-32-${NAME}-linux
source /usr/local/oecore-i686/environment-setup-cortexa9hf-neon-${NAME}-linux-gnueabi
Copy the defconfig file from your Yocto directory to your kernel
directory (checked out somewhere outside of the Yocto tree) as
.config.
Run make oldconfig in your kernel directory so that the Linux
kernel build system picks up the existing .config.
Note: you may have to answer questions about config options that
are not set in the .config file.
Note: running make menuconfig will fail when the SDK is enabled,
because the SDK does not have the ncurses libraries set up
correctly. For this command, run it in a new terminal that has not
enabled the SDK so that it uses the local ncurses-dev packages you
have installed.
Run make -jN to build the kernel.
To run the new kernel, copy the zImage and ${NAME}.dtb files to
your NFS/TFTP share or boot device. I use another script to speed
up the process.
update_kernel.sh:
#!/bin/bash
set -x
sudo cp /path-to-linux-source/arch/arm/boot/dts/${NAME}.dtb /srv/nfs/${DEVICE}/boot/
sudo cp /path-to-linux-source/arch/arm/boot/zImage /srv/nfs/${DEVICE}/boot/
set +x
You can also point Yocto to your local Linux repo in your .bb
file. This is useful for making sure your kernel changes still
build correctly within Yocto.
SRC_URI = "git:///path-to-linux-source/.git/;branch=${KBRANCH};protocol=file"
UPDATE: Over a year later, I realize that I completely missed the question about broken patches. Without more information, I can't be sure what went wrong copying the kernel source from Yocto to an external build. I'd suggest opening a Bitbake devshell for the kernel and doing a diff with the external directory after manually applying patches to see what went wrong, or just copy the source from inside the devshell to your external directory.
https://www.yoctoproject.org/docs/1.4.2/dev-manual/dev-manual.html#platdev-appdev-devshell
When debugging certain commands or even when just editing packages, devshell can be a useful tool. When you invoke devshell, source files are extracted into your working directory and patches are applied.
Since it can't parse it, there seems to be a problem with patch. How do you patch the device tree? Are you patching it in the .bb file?
If so, check your patch for possible syntax errors, it's very easy to overlook the syntax errors in device tree. You can remove the patch and do it manually from bitbake -c devshell <kernel-name>
If not, please try to do it there and check again. Please share results if any of these helps you.

Procedure to compile the linux kernel for an embedded device - installing headers files in the system

I am compiling a linux kernel (4.9.15) in my host machine and installing it in an embedded device. All works fine but I have a question about the proper way to install all the include files in the system (/usr/include/linux with updated version.h)
This is how I proceed:
I compile the sources in my host machine.
My compilation script generates boot.tar.gz, modules-4.9.15.tar.gz, linux-4.9.15.tar.gz and linux-headers-4.9.15.tar.gz and copy them to the embedded system
In the embedded system I extract boot.tar.gz (containing System.map-4.9.15, config-4.9.15, initrd.img-4.9.15 and vmlinuz-4.9.15) to /boot folder, the modules-4.9.15.tar.gz to /lib/modules and the source and headers files to /usr/src/
I update the /lib/modules/4.9.15 "source" and "build" links to point to /usr/src/4.9.15 folder
In /usr/src/linux-4.9.15 folder I do make install_headers
I update grub with update-grub2 and reboot
My doubt is how to update the system's /usr/include/linux/ folder. I thought that executing make headers_install would do it but it just installs the include files in the sources folder. Should I copy the generated /usr/src/linux-4.9.15/usr/include/linux folder to the system's /usr/include/linux manually? Is that the proper way to do it?
Any suggestion to do the process in a better way?
Thanks!

Reaching the root directory of linux kernel source on ubuntu?

I am running ubuntu 13.10 on linux kernel version 3.11.0-12.I have to add a system call in this but i am facing a problem. The very first step says that I have to change my current working directory to kernel directory.
I used the command " cd linux-3.11.0-12 " but it is showing that no such file or directory exists. Please tell me where am i going wrong and how do I correct this mistake.
Wait, you want to add a system call to the Linux kernel, but you don't know how to reach the source code? Are you sure you are able to modify, configure, build, install and boot the Linux kernel?
Assuming yes, you need to get the source code of Linux first (for example, by cloning https://github.com/torvalds/linux or just downloading the version you are interested in), extract it somewhere and then cd to the path where you extracted it. Then you can begin modifying to your heart's content.
Perhaps this blog post could help you.
To get the source of the installed kernel on ubuntu, you can use the command [for ubuntu 13.04+ ]
apt-get source linux-image-`uname -r`
and should be typically be placed under /usr/src
Reference:
[1] https://help.ubuntu.com/community/Kernel/Compile
[2] https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

How do I get the correct .config file for compiling the Linux kernel source specific to my hardware?

I tried using make defconfig to compile the kernel, but as expected, it failed to boot. I was wondering what .config file do kernel vendors like Canonical for Ubuntu use, that the kernel is able to boot right out-of-the-box. Of course, I am still a beginner and configuring the various parameters, is a little out of my league currently.
Specifically,I am looking to load a basic "hello, world!" module to my running kernel 2.6.32.41. For that, I would need to compile kernels source against the same .config file that was used for the running kernel.
If your running kernel was compiled with the CONFIG_IKCONFIG_PROC option, you can get the config in /proc/config.gz:
$ zcat /proc/config.gz >my_config
Copy my_config into your kernel build directory as .config and run make config to be prompted for configuration options that are absent from your config file (this will only happen if you are using a kernel source that is newer than your running kernel). You should then be able to compile a new kernel with the same features as your current one.
Distros typically use their own kernel configuration, where most of the drivers are compiled as modules to be dynamically loaded when the corresponding hardware is requested. Also the kernel needs to be booted with relevant boot options (like the one specifying the root filesystem). Your defconfig kernel probably failed to boot because of that.
I don't know about getting the one that's "correct for your hardware", but you can use the config that Ubuntu gives you by looking in /boot/ for a file starting with the name config. There may be more than one, in which case use the command uname -r to tell which kernel you're currently running, and then you can use the appropriate config.
option1:
source code of your booted system
cd /usr/src/linux-headers-3.2.0-29;
this will generate .config
sudo make oldconfig;
vi .config
option2:
zcat /proc/config.gz > my_config
option3:
echo /boot/config* > my_config
"defconfig" is usually pegged at the commonly used hardware - x86, or x86_64, and perhaps not so recent chipset or motherboard. Sometimes, like my Lenovo laptop, only the latest kernel source, and with enabling some config option, after googling through the bugzilla database, will it work.
Like what Jeff Welling said, to get the config in use, u can look under /boot directory. Same for my Fedora Core too. But if u want to compile a basic program as a "kernel module", and by that it simply means "loadable kernel module", u don't need to compile the kernel source. U just need the kernel headers for that current version. For example, "apt-cache search" in Ubuntu 10.04 returns several possible option:
linux-headers-2.6.38 - Header files related to Linux kernel, specifically,
linux-libc-dev - Linux Kernel Headers for development
Ubuntu normally patched the stock kernel (from kernel.org) to have their own kernel. If u have downloaded the stock kernel, and attempt to use the /boot's config file (or sometimes u can find the currently loaded config as /proc/config.gz, like the Backtrack's Ubuntu, which is based on 10.04 LTS), then u may need to do a "make oldconfig" with the current config file named as ".config". "make oldconfig" will then use the .config to generate a new .config that is compatible with the kernel source.

Resources