版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://damocles.blogbus.com/logs/12355731.html
Hook | Called... |
---|---|
NF_IP_PRE_ROUTING | After sanity checks, before routing decisions. |
NF_IP_LOCAL_IN | After routing decisions if packet is for this host. |
NF_IP_FORWARD | If the packet is destined for another interface. |
NF_IP_LOCAL_OUT | For packets coming from local processes on their way out. |
NF_IP_POST_ROUTING | Just before outbound packets "hit the wire". |
261 /*我们关心其中的那个returen语句,调用了函数NF_HOOK,这个自然就是netfilter的hook调用。我们发现这其实是一个宏,那么继续深入下去看看:
262 * Deliver IP Packets to the higher protocol layers.
263 */
264 int ip_local_deliver(struct sk_buff *skb)
265 {
266 /*
267 * Reassemble IP fragments.
268 */
269
270 if (skb->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
271 skb = ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER);
272 if (!skb)
273 return 0;
274 }
275
276 return NF_HOOK(PF_INET, NF_IP_LOCAL_IN, skb, skb->dev, NULL,
277 ip_local_deliver_finish);
278 }
246 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) /
247 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
可见它的形参分别是协议类型,钩子类型,skb,进去的device,出去的device以及回调函数指针,它首先会探寻说我们的规则表中有没有对这类情况注册钩子函数进行匹配处理,如果有的话,会调用钩子函数,如果没有的话,则继续执行形参中的回调函数,完成整个过程。可见netfilter是一个很轻量级的,和内核网络代码能轻易剥离的防火墙。我们继续往下看:
182 /**这里出现了一个非常重要的数据结构nf_hooks,我们去看一下到底是怎么样子的
183 * nf_hook_thresh - call a netfilter hook
184 *
185 * Returns 1 if the hook has allowed the packet to pass. The function
186 * okfn must be invoked by the caller in this case. Any other return
187 * value indicates the packet has been consumed by the hook.
188 */
189 static inline int nf_hook_thresh(int pf, unsigned int hook,
190 struct sk_buff **pskb,
191 struct net_device *indev,
192 struct net_device *outdev,
193 int (*okfn)(struct sk_buff *), int thresh,
194 int cond)
195 {
196 if (!cond)
197 return 1;
198 #ifndef CONFIG_NETFILTER_DEBUG
199 if (list_empty(&nf_hooks[pf][hook]))
200 return 1;
201 #endif
202 return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
203 }
142 static int __init iptable_filter_init(void)其中的注册table和注册钩子函数就很清晰了,这些都是在初始化时候完成的。我们继续看nf_register_hooks函数,它调用了nf_register_hook函数。
143 {
144 int ret;
145
146 if (forward < 0 || forward > NF_MAX_VERDICT) {
147 printk("iptables forward must be 0 or 1/n");
148 return -EINVAL;
149 }
150
151 /* Entry 1 is the FORWARD hook */
152 initial_table.entries[1].target.verdict = -forward - 1;
153
154 /* Register table */
155 ret = ipt_register_table(&packet_filter, &initial_table.repl);
156 if (ret < 0)
157 return ret;
158
159 /* Register hooks */
160 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
161 if (ret < 0)
162 goto cleanup_table;
163
164 return ret;
165
166 cleanup_table:
167 ipt_unregister_table(&packet_filter);
168 return ret;
169 }
62 int nf_register_hook(struct nf_hook_ops *reg)这个函数很清楚了,它注册一个数据结构到nf_hook_ops的数据结构到表nf_hooks中的相应位置中去,在list中的位置根据reg的priority的值,应该是数值越小,优先级越高,就越先处理。而nf_hook_ops的内容猜都能猜出来吧,肯定是钩子函数咯。
63 {
64 struct list_head *i;
65
66 spin_lock_bh(&nf_hook_lock);
67 list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
68 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
69 break;
70 }
71 list_add_rcu(®->list, i->prev);
72 spin_unlock_bh(&nf_hook_lock);
73
74 synchronize_net();
75 return 0;
76 }
161 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,我们发现一个变量verdict,这个就是钩子函数对数据包的处理结果,它有以下几种类型,NF_STOP我也不知道是干嘛的!!
162 struct net_device *indev,
163 struct net_device *outdev,
164 int (*okfn)(struct sk_buff *),
165 int hook_thresh)
166 {
167 struct list_head *elem;
168 unsigned int verdict;
169 int ret = 0;
170
171 /* We may already have this, but read-locks nest anyway */
172 rcu_read_lock();
173
174 elem = &nf_hooks[pf][hook];
175 next_hook:
176 verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
177 outdev, &elem, okfn, hook_thresh);
178 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
179 ret = 1;
180 goto unlock;
181 } else if (verdict == NF_DROP) {
182 kfree_skb(*pskb);
183 ret = -EPERM;
184 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
185 NFDEBUG("nf_hook: Verdict = QUEUE./n");
186 if (!nf_queue(pskb, elem, pf, hook, indev, outdev, okfn,
187 verdict >> NF_VERDICT_BITS))
188 goto next_hook;
189 }
190 unlock:
191 rcu_read_unlock();
192 return ret;
193 }
Return Code | Meaning |
---|---|
NF_DROP | Discard the packet. |
NF_ACCEPT | Keep the packet. |
NF_STOLEN | Forget about the packet. |
NF_QUEUE | Queue packet for userspace. |
NF_REPEAT | Call this hook function again. |
117 unsigned int nf_iterate(struct list_head *head,
118 struct sk_buff **skb,
119 int hook,
120 const struct net_device *indev,
121 const struct net_device *outdev,
122 struct list_head **i,
123 int (*okfn)(struct sk_buff *),
124 int hook_thresh)
125 {
126 unsigned int verdict;
127
128 /*
129 * The caller must not block between calls to this
130 * function because of risk of continuing from deleted element.
131 */
132 list_for_each_continue_rcu(*i, head) {
133 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
134
135 if (hook_thresh > elem->priority)
136 continue;
137
138 /* Optimization: we don't need to hold module
139 reference here, since function can't sleep. --RR */
140 verdict = elem->hook(hook, skb, indev, outdev, okfn);
141 if (verdict != NF_ACCEPT) {
142 #ifdef CONFIG_NETFILTER_DEBUG
143 if (unlikely((verdict & NF_VERDICT_MASK)
144 > NF_MAX_VERDICT)) {
145 NFDEBUG("Evil return from %p(%u)./n",
146 elem->hook, hook);
147 continue;
148 }
149 #endif
150 if (verdict != NF_REPEAT)
151 return verdict;
152 *i = (*i)->prev;
153 }
154 }
155 return NF_ACCEPT;
156 }
这下我们应该清楚了,这个迭代就是挨个运行nf_hooks[pf][hook]所指向链表中的钩子函数elem->hook。
如果其中有一个钩子函数没有ACCEPT且不是repeat,就直接跳出循环了,然后返回verdict,如果ACCEPT了,则继续处理下一个钩子函数,直到处理完。
60 struct nf_hook_ops我们看看这个数据结构,再想想前面注册hook时候的情景,应该明白了。它其中定义了pf和hooknum,指定了在nf_hooks表中的元素位置,nf_hookfn则是现实的钩子函数,而priority则指定了它在这个链表中的位置,按照升序排列。那么nf_hookfn是什么时候指定的呢?这个自然是和各个协议相关的。在net/ipv4/netfilter/iptable_filter.c中,我们看到这么一个赋值语句。
61 {
62 struct list_head list;
63
64 /* User fills in from here down. */
65 nf_hookfn *hook;
66 struct module *owner;
67 int pf;
68 int hooknum;
69 /* Hooks are ordered in ascending priority. */
70 int priority;
71 };
114 static struct nf_hook_ops ipt_ops[] = {这是在ipv4的filter中预先注册的,我们知道还有预先注册的像nat和mangle,当然我们也可以自己写模块,实现这个hook函数。像这个例子中,第一个的hook函数就是ipt_hook,属于的协议是ipv4,属于的钩子类型是LOCAL_IN。
115 {
116 .hook = ipt_hook,
117 .owner = THIS_MODULE,
118 .pf = PF_INET,
119 .hooknum = NF_IP_LOCAL_IN,
120 .priority = NF_IP_PRI_FILTER,
121 },
122 {
123 .hook = ipt_hook,
124 .owner = THIS_MODULE,
125 .pf = PF_INET,
126 .hooknum = NF_IP_FORWARD,
127 .priority = NF_IP_PRI_FILTER,
128 },
129 {
130 .hook = ipt_local_out_hook,
131 .owner = THIS_MODULE,
132 .pf = PF_INET,
133 .hooknum = NF_IP_LOCAL_OUT,
134 .priority = NF_IP_PRI_FILTER,
135 },
136 };
215 /* Returns one of the generic firewall policies, like NF_ACCEPT. */做一些基本的注释,其实我不喜欢一篇技术博客长篇累牍贴代码,尤其是Linux内核源代码,更多的是希望能够进行分析。但往往越是讨厌的事情,自己往往又这么做了。好吧,还是来解释一下这个函数吧。
216 unsigned int
217 ipt_do_table(struct sk_buff **pskb,
218 unsigned int hook,
219 const struct net_device *in,
220 const struct net_device *out,
221 struct ipt_table *table,//这个是我们要操作的table,如filter
222 void *userdata)
223 {
224 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
225 u_int16_t offset;
226 struct iphdr *ip;
227 u_int16_t datalen;
228 int hotdrop = 0;
//hotdrop为1时就直接返回NF_DROP了,这是快速扔包的方法
229 /* Initializing verdict to NF_DROP keeps gcc happy. */
230 unsigned int verdict = NF_DROP;
231 const char *indev, *outdev;
232 void *table_base;
233 struct ipt_entry *e, *back;
234 struct xt_table_info *private = table->private;
//xt_table_info中蕴含了整套规则,以及这些规则的偏移量,使寻找变得容易
235
236 /* Initialization */
237 ip = (*pskb)->nh.iph;
238 datalen = (*pskb)->len - ip->ihl * 4;
239 indev = in ? in->name : nulldevname;
240 outdev = out ? out->name : nulldevname;
241 /* We handle fragments by dealing with the first fragment as
242 * if it was a normal packet. All other fragments are treated
243 * normally, except that they will NEVER match rules that ask
244 * things we don't know, ie. tcp syn flag or ports). If the
245 * rule is also a fragment-specific rule, non-fragments won't
246 * match it. */
247 offset = ntohs(ip->frag_off) & IP_OFFSET;
248
249 read_lock_bh(&table->lock);
250 IP_NF_ASSERT(table->valid_hooks & (1 << hook));
251 table_base = (void *)private->entries[smp_processor_id()];
252 e = get_entry(table_base, private->hook_entry[hook]);
//得到这个hook点起始规则的偏移
253
254 /* For return from builtin chain */
255 back = get_entry(table_base, private->underflow[hook]);
//得到这个hook点规则末尾的偏移
256
257 do {
258 IP_NF_ASSERT(e);
259 IP_NF_ASSERT(back);
260 if (ip_packet_match(ip, indev, outdev, &e->ip, offset)) {
261 struct ipt_entry_target *t;
262
263 if (IPT_MATCH_ITERATE(e, do_match,
264 *pskb, in, out,
265 offset, &hotdrop) != 0)
//这个宏用来遍历所有的match,会调用do_match函数
266 goto no_match;
//如果没有匹配的规则,则跳转到no_match
267
268 ADD_COUNTER(e->counters, ntohs(ip->tot_len), 1);
269
270 t = ipt_get_target(e);//获得target
271 IP_NF_ASSERT(t->u.kernel.target);
272 /* Standard target? */
273 if (!t->u.kernel.target->target) {
//当为NULL时,就是standard target,它是没有模块定义target函数的
274 int v;
275
276 v = ((struct ipt_standard_target *)t)->verdict;
277 if (v < 0) {
278 /* Pop from stack? */
279 if (v != IPT_RETURN) {
280 verdict = (unsigned)(-v) - 1;
281 break;
282 }
283 e = back;
284 back = get_entry(table_base,
285 back->comefrom);
286 continue;
287 }
288 if (table_base + v != (void *)e + e->next_offset
289 && !(e->ip.flags & IPT_F_GOTO)) {
290 /* Save old back ptr in next entry */
291 struct ipt_entry *next
292 = (void *)e + e->next_offset;
293 next->comefrom
294 = (void *)back - table_base;
295 /* set back pointer to next entry */
296 back = next;
297 }
298
299 e = get_entry(table_base, v);
300 } else {
301 /* Targets which reenter must return
302 abs. verdicts */
303 #ifdef CONFIG_NETFILTER_DEBUG
304 ((struct ipt_entry *)table_base)->comefrom
305 = 0xeeeeeeec;
306 #endif
307 verdict = t->u.kernel.target->target(pskb,
308 in, out,
309 hook,
310 t->u.kernel.target,
311 t->data,
312 userdata);
//调用模块中定义的target函数,返回一个verdict
313
314 #ifdef CONFIG_NETFILTER_DEBUG
315 if (((struct ipt_entry *)table_base)->comefrom
316 != 0xeeeeeeec
317 && verdict == IPT_CONTINUE) {
318 printk("Target %s reentered!/n",
319 t->u.kernel.target->name);
320 verdict = NF_DROP;
321 }
322 ((struct ipt_entry *)table_base)->comefrom
323 = 0x57acc001;
324 #endif
325 /* Target might have changed stuff. */
326 ip = (*pskb)->nh.iph;
327 datalen = (*pskb)->len - ip->ihl * 4;
328
329 if (verdict == IPT_CONTINUE)
330 e = (void *)e + e->next_offset;
331 else
332 /* Verdict */
333 break;
334 }
335 } else {
336
337 no_match:
338 e = (void *)e + e->next_offset;
//如果没有匹配,则找到下一个ipt_entry
339 }
340 } while (!hotdrop);
341
342 read_unlock_bh(&table->lock);
343
344 #ifdef DEBUG_ALLOW_ALL
345 return NF_ACCEPT;
346 #else
347 if (hotdrop)
348 return NF_DROP;
349 else return verdict;
350 #endif
351 }