Research on iptablesbpf module

Source: Internet
Author: User
The previous article mentioned two new things on Netfilter. one of them is the bpfmatch module, and the other nftables ball has not been released. even if it is released, it is not as attractive as bpfmatch, after all, Berkeley's stuff should not be underestimated! Although nftables also borrowed the idea of BPF, it can be seen that only at the code level, the previous article mentioned two new things on Netfilter, one of which is the bpfmatch module, another nftables ball has not been released yet. even if it is released, it is not as attractive as bpfmatch. after all, Berkeley's things should not be underestimated! Although nftables also borrowed the idea of BPF, it can be seen that it is only at the code level. On the contrary, the bpfmatch module is IptablesQualitative Leap, comprehensive state machine, JIT (timely compilation technology), it can complete packet matching judgment in a short period of time, according to the author's helper said compared to the traditional IptablesIn terms of hard-coded match, the matching execution efficiency of the match set based on the state machine must be improved X times in a timely manner! I just heard that I didn't know each other.

I had no time to try this item carefully. I started to make trouble in the middle of the night last night, and I couldn't sleep any more. I thought of the bpf module of iptables, so I couldn't help but give it a try, however, in order to configure an iptables rule for successfully using the bpf module, there are a lot of work in the early stage. The following is an example. I will detail each step as follows:
1. download and install the latest Debian7 release (this can be directly upgraded in the current version, but you have long wanted to have a new environment. The network speed is very fast this night. download it)
2. download the latest kernel source and the latest iptables source
3. build a compilation tool suite
4. Compile the latest kernel, driver, and iptables
5. Compile the iptables rules that can take effect to use the bpf module.

In these five steps, step 1 takes a relatively long time. I downloaded all the images, about 14 GB in total. In fact, if I select network installation, it will save more time and I don't care, it was late at night. during the download process, I was not idle. I just bought a half set of "The story of the Romans" Yesterday (because of the tight money, I bought half a set first, I have read more than half of the winners during the download process. I am immersed in the XX project of Sura's entry into Rome. the download is complete. The download in step 1 is not a problem. I will not give the specific link. all you want to know about it. let's announce the version:
Kernel: 3.9.6
Iptables: 1.4.19.1

The next thing can't be taken over, but we still need to bring a few more before the details, 3rd is necessary, because since we chose Debian, we like its tool set, otherwise, you can just release one version. Step 1 seems to be just a successful result, and then this step takes the most time.

Debian has a set of tools for Compiling kernel, called kernel-package. it can package the final generated kernel image, driver, initrd image, and development headers into a deb package, it can be directly installed on the Debian release. without this package, you will no doubt spend a lot of time compiling the kernel and driver. initrd alone won't be able to do it all at once, even if you encapsulate all of these into shell, you should always pay attention to shell execution. at least it is impossible to press enter to read another article titled "The winner's Myth, thanks to the use of kernel-package, during the compilation process, I have read the winner's myth! The Debian tool is specially designed for developers on Debian, so even if there is a problem, it has been announced to the Debian community by others and has been corrected for a long time. Building this toolkit is simple:
Apt-get installkernel-package
In addition, to use menuconfig to configure the kernel, install the following:
Apt-get installlibncurses5-dev

After necessary tools are built, the rest is to compile the kernel. the direct goal of this round is to play the bpf module. Therefore, remember to enable BPF configuration when configuring the kernel in menuconfig, it is in the "bpf" match support in the CoreNetfilter Configuration. if you want to use the config file of Debian 7, you can see the style of the kernel-package, note that the preceding operations must be completed in steps:
Step 1: cp/boot/config-3.2.0-4-amd64/usr/src/linux-3.9.6/. config
Step 2: make menuconfig
Step 3: make-kpkg -- initrd kernel-image & make-kpkgkernel-headers & find/usr/src/-type deb-name *. deb | xargs-I dpkg-I {}& & reboot
Step 4: Find a book or a movie, but in order to allow the computer to compile at full speed, it is still psychologically comforting to change the computer (Note: Psychological !);
Now that the new kernel is compiled, the uname has been upgraded to 3.9.6, and the kernel-header is also in place under/usr/src, /lib/modules/3.9.6/build is in place, and the rest is to compile iptables. this is very simple and can be divided into several steps:
Step 1: configure -- enable-nfbpf-compiler; this enable is particularly important. In fact, this is the option that I will re-add to compile after I write iptables rules that fail to succeed multiple times, in the beginning, I directly configure and thought that I could pass the test by playing tricks. The result was a tragedy when I wrote an iptables test!
Step 2: make & make install

The preparation is finished now. the time is half past eight A.M.. after breakfast, I want to buy food for lunch. (because my wife brought her daughter to early education today, I stayed for cooking, because my work is organized step by step, it is relatively slow. for a simple lunch, it will take one step and one step from a.m ...), iptables testing is conducted in the afternoon...
Finally, you can write the following rules subconsciously:
Iptables-a input-m bpf -- bytecode 'dumb'-jDROP

In fact, my requirement is very simple. I just want to set bytecode to icmp, but I don't know the specific syntax and instructions of bpf. The principle is that I can't understand it, but I have actually implemented it, I am too lazy to check the manual. since I don't want to check the manual, it is very difficult to write a translation program by myself. for example, I want to translate tcpport1234 into bytecode conforming to the bpf syntax, so I thought from TcpdumpI tried to pull out the code. However, this kind of behavior is a little tricky. I have never been very concerned about the code, and now I have been overwhelmed by the code. Actually, like TcpdumpIn such a mature program, there must be some parameters that can print bpf code. this is really my intuition. most people who have played GNU code know it, when writing this code, the author must have printed these things. at the end of the release, it was left as a debugging information. since the code is open-source, why don't code users be more convenient? This is also true. man tcpdump will find that the options d, dd, and ddd are exactly for this purpose. here are these things. I will take matching icmp as an example:
@ First look at the decimal code
Root @ zhaoya:/usr/src/linux-3.9.6/net/netfilter # tcpdump-I any icmp-ddd
40 0 0 14
21 0 3 2048
48 0 0 25
21 0 1 1
6 0 65535
6 0 0 0
@ Although meaningless, let's take a look at the hexadecimal code. at least we know that each row is a 4-tuple {operation code, matching interpretation, matching judgment, parameter}
Root @ zhaoya:/usr/src/linux-3.9.6/net/netfilter # tcpdump-I any icmp-dd
{0x28, 0, 0, 0x0000000e },
{0x15, 0, 3, 0x00000800 },
{0x30, 0, 0, 0x00000019 },
{0x15, 0, 1, 0x00000001 },
{0x6, 0, 0, 0x0000ffff },
{0x6, 0, 0, 0x00000000 },
@ Finally, let's take a look at its mnemonic code, similar to assembly instructions.
Root @ zhaoya:/usr/src/linux-3.9.6/net/netfilter # tcpdump-I any icmp-d
(000) ldh [14]
(001) jeq #0x800 jt 2 jf 5 # ipprotocol
(002) ldb [25]
(003) jeq #0x1 jt 4 jf 5 # ICMP protocol
(004) ret #65535.
(005) ret #0

Now that I have this, of course I was overjoyed. so I quickly tested and google the bytecode syntax, which is fairly simple:
Code = $ (tcpdump-I any icmp-ddd | tr '\ n '',')
That's why the above "dumb eye" is filled by code. take a picture and press enter to ping the gateway! Dmesg does not report any error, so I want to get it. it is a problem with bytecode! But where can I find the correct one? Disappointed, I really have a desire to write a parser myself. The Kitchen opened the range hood and stabilized my mood. google, why do other people do what they do! Google found that iptables originally had a tool named nfbpf-compiler. in utils, I re-compiled it, in fact, in the help of the bpf module of iptables, a format is clearly written, that is:
-- Bytecode 'nfbpf _ compiler RAW '
I am so excited that I have not read the documents carefully. it is a sin! So in this format, we will replace it with 'icmp '. if it succeeds, the ping will fail, so I want to see how the code generated through nfbpf_compiler is different from the code generated by tcpdump, the tool generates the following:
Root @ zhaoya:/usr/src/linux-3.9.6/net/netfilter # nfbpf_compile RAW 'icmp'
7, 48 0 0, 84 0 0, 21 0 3 64, 48 0 0 9, 21 0 1, 6 0 0 65535,6 0 00

What's the difference? I noticed the RAW parameter. I vaguely remember that RAW is a linktype. when I studied tcpdump, I seemed to have noticed it because someone asked me what type of EN10MB was, and I didn't know it at the time, when I checked the information, I found that it is actually a link layer type, and it is tied to ATM and so on (these previous network layer protocols have degraded to the link layer in the TCP/IP world ), so what is RAW, in fact, is similar to the RAW of socket, it refers to a bare IP, that is, it does not have a link layer or ignore the link layer. At this point, I finally understand, tcpdump eth series network card translation code is clearly EN10MB, that is, the Ethernet protocol, through tcpdump-I eth0-L to see what linktype it can use, disappointing! So I want to use lo for an experiment, but lo is the same as eth0. how can lo simulate an Ethernet card! On Mac, lo0 is NULL, which is reasonable! See details! Let's take a look at the nfbpf_compiler's use of EN10MB:
Root @ zhaoya:/usr/src/linux-3.9.6/net/netfilter # nfbpf_compile EN10MB 'icmp'
6, 40 0 0 12, 21 0 3 2048,48 0 0 23,21 0 1, 6 0 0 65535,6 0 00
The result is similar to that of tcpdump!

In turn, let's think about why bpf match cannot use NE10MBcode? I have read the post of xt_bpfpatch. The author's example is "EN10MB! Why cannot I release the version? Isn't it because there are too many linktypes that can be easily panic if not processed ?? It seems that the IP code at the waist of the hourglass is easy to write! In any case, the problem is finally solved. to sum up the following lessons:

1. Before you start playing new things, you must read the document and read the manual. now you are no longer stuck in the code. this is an improvement. but when there is a problem, you don't have to look at the code to solve it.

2. pay attention to the parameter options during compilation. sometimes it is not a program bug, but because you automatically give up the function you want during compilation.

3. in other words, do not do what others have already done (bpf compilation program is half written ...), it's not the late adolescence that I wrote the sorting algorithm 10 years ago. now I want to seek money, and if I want to practice it, no one else will practice it!

Finally, unlike what I mentioned in this article, this process is not miserable. this is because we all like Tragedies. after reading a tragedy, you will find that, maybe it is a comedy with black humor. Of course, it seems miserable to be woken up by her daughter's noisy voice in the middle of the night, but some children know, is it really miserable? It may also be a kind of happiness. if you have no children, you will know in the future...
The winner's myth is over today. tomorrow morning at, there will be a great journey to Rome! Another interesting thing is the ufw on Debian!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.