發表文章

目前顯示的是 9月, 2009的文章

HOWTO: Linux Netfilter Hook Function Developement

圖片
我是從上研究所以後才開始接觸 Linux 的,那時候為了 強迫 自己盡速上手,把我家一台古董級電腦搬出來,按著「施銘威研究室」的 Linux 系列進行一個口令一個動作的學習(回首那段日子 ... 真是 ...)。而我第一個學的操作就是 iptables 的操作,理由很簡單,因為那時候我待在「Distributed Computing and Network Security Laboratory」,想說不會操作防火牆不是太丟臉了嗎?從此就開始接觸到 Linux Kernel 裡面很重要的一個封包處理機制 Netfilter。 Netfilter 是 Linux Kernel 2.4 和 2.6 的網路封包處理框架,下面是官網的介紹: netfilter is a set of hooks inside the Linux kernel that allows kernel modules to register callback functions with the network stack. A registered callback function is then called back for every packet that traverses the respective hook within the network stack. (from http://www.netfilter.org/ ) 簡單來說,我們可以把 Netfilter 的機制想成下面的圖: 圖中的五個橢圓形指的就是五個 Hook Points。而旁邊一串紅色的小圈圈代表的是在該 Kook Point 上的一串規則。凡是有網路封包經過一 Hook Point 時就會進入該 Hook Point 所掛載的所有規則。 我們利用 linux kernel 2.6.28.7 來搜尋一下 NF_HOOK 這個關鍵字: ip_forward.c: ip_forward, NF_INET_FORWARD ip_input.c: ip_local_deliver, NF_INET_LOCAL_IN ip_input.c: ip_rcv, NF_INET_PRE_ROUTING ip_output.c: ip_mc_output, NF_INET_POST_ROUTING ip_outpu