]> Pileus Git - ~andy/linux/blob - net/core/netfilter.c
[NETFILTER]: Fix nf_debug_ip_local_deliver()
[~andy/linux] / net / core / netfilter.c
1 /* netfilter.c: look after the filters for various protocols. 
2  * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
3  *
4  * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
5  * way.
6  *
7  * Rusty Russell (C)2000 -- This code is GPL.
8  *
9  * February 2000: Modified by James Morris to have 1 queue per protocol.
10  * 15-Mar-2000:   Added NF_REPEAT --RR.
11  * 08-May-2003:   Internal logging interface added by Jozsef Kadlecsik.
12  */
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/netfilter.h>
16 #include <net/protocol.h>
17 #include <linux/init.h>
18 #include <linux/skbuff.h>
19 #include <linux/wait.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/if.h>
23 #include <linux/netdevice.h>
24 #include <linux/inetdevice.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/icmp.h>
28 #include <net/sock.h>
29 #include <net/route.h>
30 #include <linux/ip.h>
31
32 /* In this code, we can be waiting indefinitely for userspace to
33  * service a packet if a hook returns NF_QUEUE.  We could keep a count
34  * of skbuffs queued for userspace, and not deregister a hook unless
35  * this is zero, but that sucks.  Now, we simply check when the
36  * packets come back: if the hook is gone, the packet is discarded. */
37 #ifdef CONFIG_NETFILTER_DEBUG
38 #define NFDEBUG(format, args...)  printk(format , ## args)
39 #else
40 #define NFDEBUG(format, args...)
41 #endif
42
43 /* Sockopts only registered and called from user context, so
44    net locking would be overkill.  Also, [gs]etsockopt calls may
45    sleep. */
46 static DECLARE_MUTEX(nf_sockopt_mutex);
47
48 struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
49 static LIST_HEAD(nf_sockopts);
50 static DEFINE_SPINLOCK(nf_hook_lock);
51
52 /* 
53  * A queue handler may be registered for each protocol.  Each is protected by
54  * long term mutex.  The handler must provide an an outfn() to accept packets
55  * for queueing and must reinject all packets it receives, no matter what.
56  */
57 static struct nf_queue_handler_t {
58         nf_queue_outfn_t outfn;
59         void *data;
60 } queue_handler[NPROTO];
61 static DEFINE_RWLOCK(queue_handler_lock);
62
63 int nf_register_hook(struct nf_hook_ops *reg)
64 {
65         struct list_head *i;
66
67         spin_lock_bh(&nf_hook_lock);
68         list_for_each(i, &nf_hooks[reg->pf][reg->hooknum]) {
69                 if (reg->priority < ((struct nf_hook_ops *)i)->priority)
70                         break;
71         }
72         list_add_rcu(&reg->list, i->prev);
73         spin_unlock_bh(&nf_hook_lock);
74
75         synchronize_net();
76         return 0;
77 }
78
79 void nf_unregister_hook(struct nf_hook_ops *reg)
80 {
81         spin_lock_bh(&nf_hook_lock);
82         list_del_rcu(&reg->list);
83         spin_unlock_bh(&nf_hook_lock);
84
85         synchronize_net();
86 }
87
88 /* Do exclusive ranges overlap? */
89 static inline int overlap(int min1, int max1, int min2, int max2)
90 {
91         return max1 > min2 && min1 < max2;
92 }
93
94 /* Functions to register sockopt ranges (exclusive). */
95 int nf_register_sockopt(struct nf_sockopt_ops *reg)
96 {
97         struct list_head *i;
98         int ret = 0;
99
100         if (down_interruptible(&nf_sockopt_mutex) != 0)
101                 return -EINTR;
102
103         list_for_each(i, &nf_sockopts) {
104                 struct nf_sockopt_ops *ops = (struct nf_sockopt_ops *)i;
105                 if (ops->pf == reg->pf
106                     && (overlap(ops->set_optmin, ops->set_optmax, 
107                                 reg->set_optmin, reg->set_optmax)
108                         || overlap(ops->get_optmin, ops->get_optmax, 
109                                    reg->get_optmin, reg->get_optmax))) {
110                         NFDEBUG("nf_sock overlap: %u-%u/%u-%u v %u-%u/%u-%u\n",
111                                 ops->set_optmin, ops->set_optmax, 
112                                 ops->get_optmin, ops->get_optmax, 
113                                 reg->set_optmin, reg->set_optmax,
114                                 reg->get_optmin, reg->get_optmax);
115                         ret = -EBUSY;
116                         goto out;
117                 }
118         }
119
120         list_add(&reg->list, &nf_sockopts);
121 out:
122         up(&nf_sockopt_mutex);
123         return ret;
124 }
125
126 void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
127 {
128         /* No point being interruptible: we're probably in cleanup_module() */
129  restart:
130         down(&nf_sockopt_mutex);
131         if (reg->use != 0) {
132                 /* To be woken by nf_sockopt call... */
133                 /* FIXME: Stuart Young's name appears gratuitously. */
134                 set_current_state(TASK_UNINTERRUPTIBLE);
135                 reg->cleanup_task = current;
136                 up(&nf_sockopt_mutex);
137                 schedule();
138                 goto restart;
139         }
140         list_del(&reg->list);
141         up(&nf_sockopt_mutex);
142 }
143
144 #ifdef CONFIG_NETFILTER_DEBUG
145 #include <net/ip.h>
146 #include <net/tcp.h>
147 #include <linux/netfilter_ipv4.h>
148
149 static void debug_print_hooks_ip(unsigned int nf_debug)
150 {
151         if (nf_debug & (1 << NF_IP_PRE_ROUTING)) {
152                 printk("PRE_ROUTING ");
153                 nf_debug ^= (1 << NF_IP_PRE_ROUTING);
154         }
155         if (nf_debug & (1 << NF_IP_LOCAL_IN)) {
156                 printk("LOCAL_IN ");
157                 nf_debug ^= (1 << NF_IP_LOCAL_IN);
158         }
159         if (nf_debug & (1 << NF_IP_FORWARD)) {
160                 printk("FORWARD ");
161                 nf_debug ^= (1 << NF_IP_FORWARD);
162         }
163         if (nf_debug & (1 << NF_IP_LOCAL_OUT)) {
164                 printk("LOCAL_OUT ");
165                 nf_debug ^= (1 << NF_IP_LOCAL_OUT);
166         }
167         if (nf_debug & (1 << NF_IP_POST_ROUTING)) {
168                 printk("POST_ROUTING ");
169                 nf_debug ^= (1 << NF_IP_POST_ROUTING);
170         }
171         if (nf_debug)
172                 printk("Crap bits: 0x%04X", nf_debug);
173         printk("\n");
174 }
175
176 static void nf_dump_skb(int pf, struct sk_buff *skb)
177 {
178         printk("skb: pf=%i %s dev=%s len=%u\n", 
179                pf,
180                skb->sk ? "(owned)" : "(unowned)",
181                skb->dev ? skb->dev->name : "(no dev)",
182                skb->len);
183         switch (pf) {
184         case PF_INET: {
185                 const struct iphdr *ip = skb->nh.iph;
186                 __u32 *opt = (__u32 *) (ip + 1);
187                 int opti;
188                 __u16 src_port = 0, dst_port = 0;
189
190                 if (ip->protocol == IPPROTO_TCP
191                     || ip->protocol == IPPROTO_UDP) {
192                         struct tcphdr *tcp=(struct tcphdr *)((__u32 *)ip+ip->ihl);
193                         src_port = ntohs(tcp->source);
194                         dst_port = ntohs(tcp->dest);
195                 }
196         
197                 printk("PROTO=%d %u.%u.%u.%u:%hu %u.%u.%u.%u:%hu"
198                        " L=%hu S=0x%2.2hX I=%hu F=0x%4.4hX T=%hu",
199                        ip->protocol, NIPQUAD(ip->saddr),
200                        src_port, NIPQUAD(ip->daddr),
201                        dst_port,
202                        ntohs(ip->tot_len), ip->tos, ntohs(ip->id),
203                        ntohs(ip->frag_off), ip->ttl);
204
205                 for (opti = 0; opti < (ip->ihl - sizeof(struct iphdr) / 4); opti++)
206                         printk(" O=0x%8.8X", *opt++);
207                 printk("\n");
208         }
209         }
210 }
211
212 void nf_debug_ip_local_deliver(struct sk_buff *skb)
213 {
214         /* If it's a loopback packet, it must have come through
215          * NF_IP_LOCAL_OUT, NF_IP_RAW_INPUT, NF_IP_PRE_ROUTING and
216          * NF_IP_LOCAL_IN.  Otherwise, must have gone through
217          * NF_IP_RAW_INPUT and NF_IP_PRE_ROUTING.  */
218         if (!skb->dev) {
219                 printk("ip_local_deliver: skb->dev is NULL.\n");
220         } else {
221                 if (skb->nf_debug != ((1<<NF_IP_PRE_ROUTING)
222                                       | (1<<NF_IP_LOCAL_IN))) {
223                         printk("ip_local_deliver: bad skb: ");
224                         debug_print_hooks_ip(skb->nf_debug);
225                         nf_dump_skb(PF_INET, skb);
226                 }
227         }
228 }
229
230 void nf_debug_ip_loopback_xmit(struct sk_buff *newskb)
231 {
232         if (newskb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
233                                  | (1 << NF_IP_POST_ROUTING))) {
234                 printk("ip_dev_loopback_xmit: bad owned skb = %p: ", 
235                        newskb);
236                 debug_print_hooks_ip(newskb->nf_debug);
237                 nf_dump_skb(PF_INET, newskb);
238         }
239         /* Clear to avoid confusing input check */
240         newskb->nf_debug = 0;
241 }
242
243 void nf_debug_ip_finish_output2(struct sk_buff *skb)
244 {
245         /* If it's owned, it must have gone through the
246          * NF_IP_LOCAL_OUT and NF_IP_POST_ROUTING.
247          * Otherwise, must have gone through
248          * NF_IP_PRE_ROUTING, NF_IP_FORWARD and NF_IP_POST_ROUTING.
249          */
250         if (skb->sk) {
251                 if (skb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
252                                       | (1 << NF_IP_POST_ROUTING))) {
253                         printk("ip_finish_output: bad owned skb = %p: ", skb);
254                         debug_print_hooks_ip(skb->nf_debug);
255                         nf_dump_skb(PF_INET, skb);
256                 }
257         } else {
258                 if (skb->nf_debug != ((1 << NF_IP_PRE_ROUTING)
259                                       | (1 << NF_IP_FORWARD)
260                                       | (1 << NF_IP_POST_ROUTING))) {
261                         /* Fragments, entunnelled packets, TCP RSTs
262                            generated by ipt_REJECT will have no
263                            owners, but still may be local */
264                         if (skb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
265                                               | (1 << NF_IP_POST_ROUTING))){
266                                 printk("ip_finish_output:"
267                                        " bad unowned skb = %p: ",skb);
268                                 debug_print_hooks_ip(skb->nf_debug);
269                                 nf_dump_skb(PF_INET, skb);
270                         }
271                 }
272         }
273 }
274 #endif /*CONFIG_NETFILTER_DEBUG*/
275
276 /* Call get/setsockopt() */
277 static int nf_sockopt(struct sock *sk, int pf, int val, 
278                       char __user *opt, int *len, int get)
279 {
280         struct list_head *i;
281         struct nf_sockopt_ops *ops;
282         int ret;
283
284         if (down_interruptible(&nf_sockopt_mutex) != 0)
285                 return -EINTR;
286
287         list_for_each(i, &nf_sockopts) {
288                 ops = (struct nf_sockopt_ops *)i;
289                 if (ops->pf == pf) {
290                         if (get) {
291                                 if (val >= ops->get_optmin
292                                     && val < ops->get_optmax) {
293                                         ops->use++;
294                                         up(&nf_sockopt_mutex);
295                                         ret = ops->get(sk, val, opt, len);
296                                         goto out;
297                                 }
298                         } else {
299                                 if (val >= ops->set_optmin
300                                     && val < ops->set_optmax) {
301                                         ops->use++;
302                                         up(&nf_sockopt_mutex);
303                                         ret = ops->set(sk, val, opt, *len);
304                                         goto out;
305                                 }
306                         }
307                 }
308         }
309         up(&nf_sockopt_mutex);
310         return -ENOPROTOOPT;
311         
312  out:
313         down(&nf_sockopt_mutex);
314         ops->use--;
315         if (ops->cleanup_task)
316                 wake_up_process(ops->cleanup_task);
317         up(&nf_sockopt_mutex);
318         return ret;
319 }
320
321 int nf_setsockopt(struct sock *sk, int pf, int val, char __user *opt,
322                   int len)
323 {
324         return nf_sockopt(sk, pf, val, opt, &len, 0);
325 }
326
327 int nf_getsockopt(struct sock *sk, int pf, int val, char __user *opt, int *len)
328 {
329         return nf_sockopt(sk, pf, val, opt, len, 1);
330 }
331
332 static unsigned int nf_iterate(struct list_head *head,
333                                struct sk_buff **skb,
334                                int hook,
335                                const struct net_device *indev,
336                                const struct net_device *outdev,
337                                struct list_head **i,
338                                int (*okfn)(struct sk_buff *),
339                                int hook_thresh)
340 {
341         unsigned int verdict;
342
343         /*
344          * The caller must not block between calls to this
345          * function because of risk of continuing from deleted element.
346          */
347         list_for_each_continue_rcu(*i, head) {
348                 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
349
350                 if (hook_thresh > elem->priority)
351                         continue;
352
353                 /* Optimization: we don't need to hold module
354                    reference here, since function can't sleep. --RR */
355                 verdict = elem->hook(hook, skb, indev, outdev, okfn);
356                 if (verdict != NF_ACCEPT) {
357 #ifdef CONFIG_NETFILTER_DEBUG
358                         if (unlikely(verdict > NF_MAX_VERDICT)) {
359                                 NFDEBUG("Evil return from %p(%u).\n",
360                                         elem->hook, hook);
361                                 continue;
362                         }
363 #endif
364                         if (verdict != NF_REPEAT)
365                                 return verdict;
366                         *i = (*i)->prev;
367                 }
368         }
369         return NF_ACCEPT;
370 }
371
372 int nf_register_queue_handler(int pf, nf_queue_outfn_t outfn, void *data)
373 {      
374         int ret;
375
376         write_lock_bh(&queue_handler_lock);
377         if (queue_handler[pf].outfn)
378                 ret = -EBUSY;
379         else {
380                 queue_handler[pf].outfn = outfn;
381                 queue_handler[pf].data = data;
382                 ret = 0;
383         }
384         write_unlock_bh(&queue_handler_lock);
385
386         return ret;
387 }
388
389 /* The caller must flush their queue before this */
390 int nf_unregister_queue_handler(int pf)
391 {
392         write_lock_bh(&queue_handler_lock);
393         queue_handler[pf].outfn = NULL;
394         queue_handler[pf].data = NULL;
395         write_unlock_bh(&queue_handler_lock);
396         
397         return 0;
398 }
399
400 /* 
401  * Any packet that leaves via this function must come back 
402  * through nf_reinject().
403  */
404 static int nf_queue(struct sk_buff *skb, 
405                     struct list_head *elem, 
406                     int pf, unsigned int hook,
407                     struct net_device *indev,
408                     struct net_device *outdev,
409                     int (*okfn)(struct sk_buff *))
410 {
411         int status;
412         struct nf_info *info;
413 #ifdef CONFIG_BRIDGE_NETFILTER
414         struct net_device *physindev = NULL;
415         struct net_device *physoutdev = NULL;
416 #endif
417
418         /* QUEUE == DROP if noone is waiting, to be safe. */
419         read_lock(&queue_handler_lock);
420         if (!queue_handler[pf].outfn) {
421                 read_unlock(&queue_handler_lock);
422                 kfree_skb(skb);
423                 return 1;
424         }
425
426         info = kmalloc(sizeof(*info), GFP_ATOMIC);
427         if (!info) {
428                 if (net_ratelimit())
429                         printk(KERN_ERR "OOM queueing packet %p\n",
430                                skb);
431                 read_unlock(&queue_handler_lock);
432                 kfree_skb(skb);
433                 return 1;
434         }
435
436         *info = (struct nf_info) { 
437                 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
438
439         /* If it's going away, ignore hook. */
440         if (!try_module_get(info->elem->owner)) {
441                 read_unlock(&queue_handler_lock);
442                 kfree(info);
443                 return 0;
444         }
445
446         /* Bump dev refs so they don't vanish while packet is out */
447         if (indev) dev_hold(indev);
448         if (outdev) dev_hold(outdev);
449
450 #ifdef CONFIG_BRIDGE_NETFILTER
451         if (skb->nf_bridge) {
452                 physindev = skb->nf_bridge->physindev;
453                 if (physindev) dev_hold(physindev);
454                 physoutdev = skb->nf_bridge->physoutdev;
455                 if (physoutdev) dev_hold(physoutdev);
456         }
457 #endif
458
459         status = queue_handler[pf].outfn(skb, info, queue_handler[pf].data);
460         read_unlock(&queue_handler_lock);
461
462         if (status < 0) {
463                 /* James M doesn't say fuck enough. */
464                 if (indev) dev_put(indev);
465                 if (outdev) dev_put(outdev);
466 #ifdef CONFIG_BRIDGE_NETFILTER
467                 if (physindev) dev_put(physindev);
468                 if (physoutdev) dev_put(physoutdev);
469 #endif
470                 module_put(info->elem->owner);
471                 kfree(info);
472                 kfree_skb(skb);
473                 return 1;
474         }
475         return 1;
476 }
477
478 /* Returns 1 if okfn() needs to be executed by the caller,
479  * -EPERM for NF_DROP, 0 otherwise. */
480 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
481                  struct net_device *indev,
482                  struct net_device *outdev,
483                  int (*okfn)(struct sk_buff *),
484                  int hook_thresh)
485 {
486         struct list_head *elem;
487         unsigned int verdict;
488         int ret = 0;
489
490         /* We may already have this, but read-locks nest anyway */
491         rcu_read_lock();
492
493 #ifdef CONFIG_NETFILTER_DEBUG
494         if (unlikely((*pskb)->nf_debug & (1 << hook))) {
495                 printk("nf_hook: hook %i already set.\n", hook);
496                 nf_dump_skb(pf, *pskb);
497         }
498         (*pskb)->nf_debug |= (1 << hook);
499 #endif
500
501         elem = &nf_hooks[pf][hook];
502 next_hook:
503         verdict = nf_iterate(&nf_hooks[pf][hook], pskb, hook, indev,
504                              outdev, &elem, okfn, hook_thresh);
505         if (verdict == NF_ACCEPT || verdict == NF_STOP) {
506                 ret = 1;
507                 goto unlock;
508         } else if (verdict == NF_DROP) {
509                 kfree_skb(*pskb);
510                 ret = -EPERM;
511         } else if (verdict == NF_QUEUE) {
512                 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
513                 if (!nf_queue(*pskb, elem, pf, hook, indev, outdev, okfn))
514                         goto next_hook;
515         }
516 unlock:
517         rcu_read_unlock();
518         return ret;
519 }
520
521 void nf_reinject(struct sk_buff *skb, struct nf_info *info,
522                  unsigned int verdict)
523 {
524         struct list_head *elem = &info->elem->list;
525         struct list_head *i;
526
527         rcu_read_lock();
528
529         /* Release those devices we held, or Alexey will kill me. */
530         if (info->indev) dev_put(info->indev);
531         if (info->outdev) dev_put(info->outdev);
532 #ifdef CONFIG_BRIDGE_NETFILTER
533         if (skb->nf_bridge) {
534                 if (skb->nf_bridge->physindev)
535                         dev_put(skb->nf_bridge->physindev);
536                 if (skb->nf_bridge->physoutdev)
537                         dev_put(skb->nf_bridge->physoutdev);
538         }
539 #endif
540
541         /* Drop reference to owner of hook which queued us. */
542         module_put(info->elem->owner);
543
544         list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
545                 if (i == elem) 
546                         break;
547         }
548   
549         if (elem == &nf_hooks[info->pf][info->hook]) {
550                 /* The module which sent it to userspace is gone. */
551                 NFDEBUG("%s: module disappeared, dropping packet.\n",
552                         __FUNCTION__);
553                 verdict = NF_DROP;
554         }
555
556         /* Continue traversal iff userspace said ok... */
557         if (verdict == NF_REPEAT) {
558                 elem = elem->prev;
559                 verdict = NF_ACCEPT;
560         }
561
562         if (verdict == NF_ACCEPT) {
563         next_hook:
564                 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
565                                      &skb, info->hook, 
566                                      info->indev, info->outdev, &elem,
567                                      info->okfn, INT_MIN);
568         }
569
570         switch (verdict) {
571         case NF_ACCEPT:
572                 info->okfn(skb);
573                 break;
574
575         case NF_QUEUE:
576                 if (!nf_queue(skb, elem, info->pf, info->hook, 
577                               info->indev, info->outdev, info->okfn))
578                         goto next_hook;
579                 break;
580         }
581         rcu_read_unlock();
582
583         if (verdict == NF_DROP)
584                 kfree_skb(skb);
585
586         kfree(info);
587         return;
588 }
589
590 #ifdef CONFIG_INET
591 /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
592 int ip_route_me_harder(struct sk_buff **pskb)
593 {
594         struct iphdr *iph = (*pskb)->nh.iph;
595         struct rtable *rt;
596         struct flowi fl = {};
597         struct dst_entry *odst;
598         unsigned int hh_len;
599
600         /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
601          * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook.
602          */
603         if (inet_addr_type(iph->saddr) == RTN_LOCAL) {
604                 fl.nl_u.ip4_u.daddr = iph->daddr;
605                 fl.nl_u.ip4_u.saddr = iph->saddr;
606                 fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
607                 fl.oif = (*pskb)->sk ? (*pskb)->sk->sk_bound_dev_if : 0;
608 #ifdef CONFIG_IP_ROUTE_FWMARK
609                 fl.nl_u.ip4_u.fwmark = (*pskb)->nfmark;
610 #endif
611                 fl.proto = iph->protocol;
612                 if (ip_route_output_key(&rt, &fl) != 0)
613                         return -1;
614
615                 /* Drop old route. */
616                 dst_release((*pskb)->dst);
617                 (*pskb)->dst = &rt->u.dst;
618         } else {
619                 /* non-local src, find valid iif to satisfy
620                  * rp-filter when calling ip_route_input. */
621                 fl.nl_u.ip4_u.daddr = iph->saddr;
622                 if (ip_route_output_key(&rt, &fl) != 0)
623                         return -1;
624
625                 odst = (*pskb)->dst;
626                 if (ip_route_input(*pskb, iph->daddr, iph->saddr,
627                                    RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
628                         dst_release(&rt->u.dst);
629                         return -1;
630                 }
631                 dst_release(&rt->u.dst);
632                 dst_release(odst);
633         }
634         
635         if ((*pskb)->dst->error)
636                 return -1;
637
638         /* Change in oif may mean change in hh_len. */
639         hh_len = (*pskb)->dst->dev->hard_header_len;
640         if (skb_headroom(*pskb) < hh_len) {
641                 struct sk_buff *nskb;
642
643                 nskb = skb_realloc_headroom(*pskb, hh_len);
644                 if (!nskb) 
645                         return -1;
646                 if ((*pskb)->sk)
647                         skb_set_owner_w(nskb, (*pskb)->sk);
648                 kfree_skb(*pskb);
649                 *pskb = nskb;
650         }
651
652         return 0;
653 }
654 EXPORT_SYMBOL(ip_route_me_harder);
655
656 int skb_ip_make_writable(struct sk_buff **pskb, unsigned int writable_len)
657 {
658         struct sk_buff *nskb;
659
660         if (writable_len > (*pskb)->len)
661                 return 0;
662
663         /* Not exclusive use of packet?  Must copy. */
664         if (skb_shared(*pskb) || skb_cloned(*pskb))
665                 goto copy_skb;
666
667         return pskb_may_pull(*pskb, writable_len);
668
669 copy_skb:
670         nskb = skb_copy(*pskb, GFP_ATOMIC);
671         if (!nskb)
672                 return 0;
673         BUG_ON(skb_is_nonlinear(nskb));
674
675         /* Rest of kernel will get very unhappy if we pass it a
676            suddenly-orphaned skbuff */
677         if ((*pskb)->sk)
678                 skb_set_owner_w(nskb, (*pskb)->sk);
679         kfree_skb(*pskb);
680         *pskb = nskb;
681         return 1;
682 }
683 EXPORT_SYMBOL(skb_ip_make_writable);
684 #endif /*CONFIG_INET*/
685
686 /* Internal logging interface, which relies on the real 
687    LOG target modules */
688
689 #define NF_LOG_PREFIXLEN                128
690
691 static nf_logfn *nf_logging[NPROTO]; /* = NULL */
692 static int reported = 0;
693 static DEFINE_SPINLOCK(nf_log_lock);
694
695 int nf_log_register(int pf, nf_logfn *logfn)
696 {
697         int ret = -EBUSY;
698
699         /* Any setup of logging members must be done before
700          * substituting pointer. */
701         spin_lock(&nf_log_lock);
702         if (!nf_logging[pf]) {
703                 rcu_assign_pointer(nf_logging[pf], logfn);
704                 ret = 0;
705         }
706         spin_unlock(&nf_log_lock);
707         return ret;
708 }               
709
710 void nf_log_unregister(int pf, nf_logfn *logfn)
711 {
712         spin_lock(&nf_log_lock);
713         if (nf_logging[pf] == logfn)
714                 nf_logging[pf] = NULL;
715         spin_unlock(&nf_log_lock);
716
717         /* Give time to concurrent readers. */
718         synchronize_net();
719 }               
720
721 void nf_log_packet(int pf,
722                    unsigned int hooknum,
723                    const struct sk_buff *skb,
724                    const struct net_device *in,
725                    const struct net_device *out,
726                    const char *fmt, ...)
727 {
728         va_list args;
729         char prefix[NF_LOG_PREFIXLEN];
730         nf_logfn *logfn;
731         
732         rcu_read_lock();
733         logfn = rcu_dereference(nf_logging[pf]);
734         if (logfn) {
735                 va_start(args, fmt);
736                 vsnprintf(prefix, sizeof(prefix), fmt, args);
737                 va_end(args);
738                 /* We must read logging before nf_logfn[pf] */
739                 logfn(hooknum, skb, in, out, prefix);
740         } else if (!reported) {
741                 printk(KERN_WARNING "nf_log_packet: can\'t log yet, "
742                        "no backend logging module loaded in!\n");
743                 reported++;
744         }
745         rcu_read_unlock();
746 }
747 EXPORT_SYMBOL(nf_log_register);
748 EXPORT_SYMBOL(nf_log_unregister);
749 EXPORT_SYMBOL(nf_log_packet);
750
751 /* This does not belong here, but locally generated errors need it if connection
752    tracking in use: without this, connection may not be in hash table, and hence
753    manufactured ICMP or RST packets will not be associated with it. */
754 void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
755
756 void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb)
757 {
758         void (*attach)(struct sk_buff *, struct sk_buff *);
759
760         if (skb->nfct && (attach = ip_ct_attach) != NULL) {
761                 mb(); /* Just to be sure: must be read before executing this */
762                 attach(new, skb);
763         }
764 }
765
766 void __init netfilter_init(void)
767 {
768         int i, h;
769
770         for (i = 0; i < NPROTO; i++) {
771                 for (h = 0; h < NF_MAX_HOOKS; h++)
772                         INIT_LIST_HEAD(&nf_hooks[i][h]);
773         }
774 }
775
776 EXPORT_SYMBOL(ip_ct_attach);
777 EXPORT_SYMBOL(nf_ct_attach);
778 EXPORT_SYMBOL(nf_getsockopt);
779 EXPORT_SYMBOL(nf_hook_slow);
780 EXPORT_SYMBOL(nf_hooks);
781 EXPORT_SYMBOL(nf_register_hook);
782 EXPORT_SYMBOL(nf_register_queue_handler);
783 EXPORT_SYMBOL(nf_register_sockopt);
784 EXPORT_SYMBOL(nf_reinject);
785 EXPORT_SYMBOL(nf_setsockopt);
786 EXPORT_SYMBOL(nf_unregister_hook);
787 EXPORT_SYMBOL(nf_unregister_queue_handler);
788 EXPORT_SYMBOL(nf_unregister_sockopt);