標籤:des style blog http io color ar os 使用
新版本的ipset
上周在兒童醫院給小小看病等待叫號的間隙,收到了Netfilter郵件清單的推送訊息,一覽了ipset最新的6.23版本的新特性,很多正是我目前所需要的,特別是timeout和skbinfo參數的支援,具體的詳情請自行查看manual,如果不想看那麼多,我這裡簡單的貼一下:
timeout
All set types supports the optional timeout parameter when creating a set and adding entries. The value of the timeout
parameter for the create command means the default timeout value (in seconds) for new entries. If a set is created with
timeout support, then the same timeout option can be used to specify non-default timeout values when adding entries. Zero
timeout value means the entry is added permanent to the set. The timeout value of already added elements can be changed
by readding the element using the -exist option. Example:
ipset create test hash:ip timeout 300
ipset add test 192.168.0.1 timeout 60
ipset -exist add test 192.168.0.1 timeout 600
...
skbinfo, skbmark, skbprio, skbqueue
All set types support the optional skbinfo extension. This extension allow to store the metainfo (firewall mark, tc class
and hardware queue) with every entry and map it to packets by usage of SET netfilter target with --map-set option. skb┅\
mark option format: MARK or MARK/MASK, where MARK and MASK are 32bit hex numbers with 0x prefix. If only mark is speci‐
fied mask 0xffffffff are used. skbprio option has tc class format: MAJOR:MINOR, where major and minor numbers are hex
without 0x prefix. skbqueue option is just decimal number.
ipset create foo hash:ip skbinfo
ipset add foo skbmark 0x1111/0xff00ffff skbprio 1:10 skbqueue 10
...
nomatch
The hash set types which can store net type of data (i.e. hash:*net*) support the optional nomatch option when adding
entries. When matching elements in the set, entries marked as nomatch are skipped as if those were not added to the set,
which makes possible to build up sets with exceptions. See the example at hash type hash:net below.
When elements are tested by ipset, the nomatch flags are taken into account. If one wants to test the existence of an
element marked with nomatch in a set, then the flag must be specified too.
...
編譯
總之,相比較老版本的4.5,確實增加了不少新的東西,於是就迫不及待地下載,編譯,試用,一般而言,這些步驟都是例行的,都不會遇到什麼特別大的困難,特別是看了其README之後:
0. You need the source tree of your kernel (version >= 2.6.32)
and it have to be configured with ip6tables support enabled,
modules compiled. For kernel versions < 2.6.39 please apply
the netlink.patch against your kernel tree, which adds the
new subsystem identifier for ipset.
而我的核心就是2.6.32版本的,雖然比較老了,但沒有辦法。不過既然明說了支援2.6.32,那就放心了,除了README之外,其網站上也明確說明支援2.6.32核心:
For the new branch
linux kernel source code (version >= 2.6.32)
source of ipset: ipset-6.23.tar.bz2 (md5sum)
於是開始例行的工作:
tar xjvf ipset-6.23.tar.bz2
cd ipset-6.23
./configure
報告說沒有為核心打netlink.patch,不過這不是什麼事兒,但是在這個點上,我提出了質疑:
質疑:明明只是更新了標頭檔,為何非要給核心源碼樹打patch啊?
我感到這個動作不太合乎常規,為了不依賴源碼樹,我將這個補丁打到了編譯所需的2.6.32的核心標頭檔中,於是configure順利通過,但是遇到了另一個問題,提示我沒有安裝libmnl,由於我這是一個乾淨的環境,所以在Netfilter網站下載了次新版的libmnl源碼,例行安裝libmnl。這一步對於我而言並非必須的,像我這樣天天折騰Netfilter的,怎麼可能不安裝libmnl啊...
接來下就是make了,順利通過,然後make modules,報錯一大堆,然後我感到,這個ipset-6.23根本TMD就不支援2.6.32,所有的文檔說明都是TMD在胡扯!報錯的是下面這個檔案:
ipset-6.23/kernel/net/netfilter/xt_set.c
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c: In function ‘set_match_v0_checkentry‘:
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:99: error: ‘const struct xt_mtchk_param‘ has no member named ‘net‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:108: error: ‘const struct xt_mtchk_param‘ has no member named ‘net‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c: In function ‘set_match_v0_destroy‘:
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:123: error: ‘const struct xt_mtdtor_param‘ has no member named ‘net‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c: In function ‘set_match_v1_checkentry‘:
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:148: error: ‘const struct xt_mtchk_param‘ has no member named ‘net‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:157: error: ‘const struct xt_mtchk_param‘ has no member named ‘net‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c: In function ‘set_match_v1_destroy‘:
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:169: error: ‘const struct xt_mtdtor_param‘ has no member named ‘net‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c: In function ‘set_target_v0‘:
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c:231: error: ‘const struct xt_match_param‘ has no member named ‘targinfo‘
/usr/src/ipset-6.23/kernel/net/netfilter/xt_set.c: In function ‘set_target_v0_checkentry‘:
我在這個源檔案中發現了下面的宏定義:
#ifdef HAVE_CHECKENTRY_BOOL#define CHECK_OK 1#define CHECK_FAIL(err) 0#define CONST const#define FTYPE bool#define XT_PAR_NET(par) NULL#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */#define CHECK_OK 0#define CHECK_FAIL(err) (err)#define CONST#define FTYPE int#define XT_PAR_NET(par) (par)->net#endif
很顯然,根據注釋,我應該定義HAVE_CHECKENTRY_BOOL,但是這個宏的定義應該自動化才合理,完全不應該去手工幹預,在configure檔案中,發現了下面的定義語句:
if test -f $ksourcedir/net/netfilter/xt_state.c && $GREP -q ‘bool state_mt_check‘ $ksourcedir/net/netfilter/xt_state.c; then { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5$as_echo "yes" >&6; } HAVE_CHECKENTRY_BOOL=defineelse { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5$as_echo "no" >&6; } HAVE_CHECKENTRY_BOOL=undeffi注意$ksourcedir/net/netfilter/xt_state.c這個萬惡的語句,我頓時火冒三丈,指定源碼檔案就是為了在這個檔案中尋找match check回呼函數的傳回值類型,作者難道不能通過核心的版本號碼還區分嗎?難道對於一個固定的核心版本,match check的傳回值規範不是固定的嗎?對於2.6.32核心,下面的調用難道可以改變嗎?:
if (par->target->checkentry != NULL && !par->target->checkentry(par)) return -EINVAL;
我很生氣,但我不能強求開源軟體一定怎麼怎麼樣,就像比爾.蓋茨曾經說的那樣,得不到報酬的程式員是寫不出一流的軟體的,這一點我稍微有點信了。好吧,我指定一定源碼檔案,讓configure的過程去定義那個萬惡的HAVE_CHECKENTRY_BOOL宏,值得注意的是,除了那個宏之外,起到相同的旨在不同核心版本間適配作用的還有一個宏:HAVE_XT_TARGET_PARAM,它的作用是在沒有定義xt_action_param結構體的低版本核心中將其定義為xt_target_param,在必要的時候強轉成xt_match_param。定義了這兩個宏之後,xt_set編譯通過,但是ipset核心模組本身卻報錯了,而這個ipset核心模組本身是要比xt_set更重要的,要知道xt_set只是一個和iptables聯動時所用的模組,即便真的無法適配,自己寫一個應該也不難,然而對於ipset本身的核心模組,如果要自己寫,那就相當於自己實現ipset-6.23本身了...還好,這次的新錯誤不多:
CC [M] /usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.o
/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.c: In function ‘call_ad‘:
/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.c:1444: error: ‘SIZE_MAX‘ undeclared (first use in this function)
/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.c:1444: error: (Each undeclared identifier is reported only once
/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.c:1444: error: for each function it appears in.)
/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.c:1444: warning: type defaults to ‘int‘ in declaration of ‘_min1‘
/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.c:1444: warning: comparison of distinct pointer types lacks a cast
make[4]: *** [/usr/src/ipset-6.23/kernel/net/netfilter/ipset/ip_set_core.o] 錯誤 1
說的是SIZE_MAX沒有定義,這個常量在3.5以上的高版本核心上才有,2.6.32如果不手工定義的話根據不能編譯通過,雖然手工定義一下並不難,但是越發覺得ipset-6.23的編譯文檔和支援版本的說明文檔就是在不負責任地胡說八道,簡直就是在扯淡!!我找到了另外一篇文檔,上面明確了一個多少還能說得過去的事實:
2 Supported Configurations
* iptables >= 1.4.3
* kernel-source >= 2.6.29
For ipset-6 you need:
* libmnl
* Linux kernel >= 2.6.35
起碼和ipset-6.23源碼中的注釋能對得上,除此之外也是在胡扯。
這個事實太噁心人了啊!我定義了SIZE_MAX之後,編譯就可以順利通過了,雖然有一些類型轉換的警告,但是忽略它們並不會有什麼大不了的後果。於是我覺得為了讓ipset-6.23內建的文檔更好地服務福士,有必要做一個真正的patch,事實上我確實這麼做了,我想做的事情有以下的目標:
1.編譯ipset-6.23僅僅依賴核心標頭檔而不再依賴源碼;
2.依賴核心版本而適配資料結構和宏定義而不是在核心源碼中找特徵值。
由於我的目標僅僅是在2.6.32核心上編譯成功,因為我並沒有測試2.6.33/34/35/36以及3.0/1/2/3/4/5/6/7/8...但我相信,3.5以上的核心版本上是一定可以成功編譯的。ipset-6.23的打包者可能根本就沒有在低版本比如2.6.32核心上進行測試,這個工作以及緊隨其後的修正工作也許本來就是留給我這樣的人的,再者說,2.6.32這個版本也許使用的人本來就不多,不提供全訪問的支援也是理所當然。我並沒有怪作者和打包者的意思,也許在修正過程中確實有些衝動,所以在此澄清。
這讓人想起了時尚這個名詞,這是在19世紀末粗放的工業化達到頂峰時誕生的一個詞,人們普遍認為,進步是必然的,最新的就是最好的,變化的向前的,速度是加快的,保持時尚的方式就是站在潮流最前端。這個理念被普遍信奉和傳承,一直到Linux 3.X核心時代...如果你還在用2.6.9核心,並且發現了它的一個大bug,沒人會理你的,站在潮流最前端的衝浪者會說:世界在進步,為何不試試3.17版本的核心呢?!
直接進入ipset-6.23目錄,執行patch -p1 < ../ipset-6.23.patch即可,而ipset-6.23.patch的內容如下:
diff -Nur ipset-6.23/kernel/include/linux/netfilter/ipset/ip_set.h ipset-6.23.new/kernel/include/linux/netfilter/ipset/ip_set.h--- ipset-6.23/kernel/include/linux/netfilter/ipset/ip_set.h 2014-09-23 19:18:34.000000000 +0800+++ ipset-6.23.new/kernel/include/linux/netfilter/ipset/ip_set.h 2014-11-13 16:27:15.000000000 +0800@@ -26,6 +26,9 @@ #define IP_SET_MODULE_DESC(a, b, c) _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c)) +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,35)+#define SIZE_MAX (~(size_t)0)+#endif /* Set features */ enum ip_set_feature { IPSET_TYPE_IP_FLAG = 0,diff -Nur ipset-6.23/kernel/net/netfilter/xt_set.c ipset-6.23.new/kernel/net/netfilter/xt_set.c--- ipset-6.23/kernel/net/netfilter/xt_set.c 2014-09-23 19:18:34.000000000 +0800+++ ipset-6.23.new/kernel/net/netfilter/xt_set.c 2014-11-13 16:26:50.000000000 +0800@@ -28,12 +28,18 @@ MODULE_ALIAS("ipt_SET"); MODULE_ALIAS("ip6t_SET"); -#ifdef HAVE_CHECKENTRY_BOOL+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,35) #define CHECK_OK 1 #define CHECK_FAIL(err) 0 #define CONST const #define FTYPE bool+/* Only confirm version 2.6.32 :) */+#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,32)+/* netns is not supported completly */+#define XT_PAR_NET(par) (&init_net)+#else #define XT_PAR_NET(par) NULL+#endif #else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35) */ #define CHECK_OK 0 #define CHECK_FAIL(err) (err)@@ -217,7 +223,7 @@ /* Revision 0 interface: backward compatible with netfilter/iptables */ -#ifdef HAVE_XT_TARGET_PARAM+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,35) #undef xt_action_param #define xt_action_param xt_target_param #define CAST_TO_MATCH (const struct xt_match_param *)
打上這個patch,在2.6.32核心上便可以直接編譯ipset-6.23軟體包了。
編譯後記:1.關於命名空間ipset-6.23的HAVE_CHECKENTRY_BOOL宏將XT_PAR_NET定義成了NULL,而XT_PAR_NET這個宏取出的是net結構體:
struct net { ... struct net_generic *gen;}值得注意的是gen欄位:
struct net_generic { unsigned int len; struct rcu_head rcu; void *ptr[0];};我們看一下它的注釋:
/* * Generic net pointers are to be used by modules to put some private * stuff on the struct net without explicit struct net modification * * The rules are simple: * 1. register the ops with register_pernet_gen_device to get the id * of your private pointer; * 2. call net_assign_generic() to put the private data on the struct * net (most preferably this should be done in the ->init callback * of the ops registered); * 3. do not change this pointer while the net is alive; * 4. do not try to have any private reference on the net_generic object. * * After accomplishing all of the above, the private pointer can be * accessed with the net_generic() call. */
ipset就是將自己的資料放在了gen欄位裡面,如果net由於HAVE_CHECKENTRY_BOOL宏置為NULL了,那麼皮之不存,毛將焉附?即便編譯通過,能用嗎?事實上,由於在取值的時候,並沒有判斷net是否為空白:
static inline struct ip_set_net *ip_set_pernet(struct net *net){ return net_generic(net, ip_set_net_id);}這樣就會導致panic崩潰。因此這個原始的代碼根本就不可能在HAVE_CHECKENTRY_BOOL宏被定義了的時候使用。因為net為空白,如果判斷了,核心不會崩潰,但是卻取不到任何資料,如果沒有判斷,核心就會崩潰。因此這個代碼本身可以說是錯誤的!
我們知道,2.6.32核心可能對net命名空間支援的還不夠完善,但是不管怎樣,init_net是存在的,因此我將XT_PAR_NET定義成了&init_net。
2.我是不是該提交一個patch我怕被罵,而且我也不想罵人,所以這件事交給遠方的朋友去做了。在我看來,很多犯錯誤的人都是執迷不悟的,輕輕說一句就可以展開罵戰。只會編程的人是惹不起的。別的不說,反正我是看著README操作的,上面寫了>=2.6.32的都可以,然而我就是沒法編譯,confiure裡面的辦法真的很噁心。反正就是寫的不對!!!照著做就是TMD不行!!!
於Linux-2.6.32核心上編譯ipset-6.23的坎坷經曆