1 /* netfilter.c: look after the filters for various protocols.
2 * Heavily influenced by the old firewall.c by David Bonn and Alan Cox.
4 * Thanks to Rob `CmdrTaco' Malda for not influencing this code in any
7 * Rusty Russell (C)2000 -- This code is GPL.
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.
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>
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>
29 #include <net/route.h>
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)
40 #define NFDEBUG(format, args...)
43 /* Sockopts only registered and called from user context, so
44 net locking would be overkill. Also, [gs]etsockopt calls may
46 static DECLARE_MUTEX(nf_sockopt_mutex);
48 struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
49 static LIST_HEAD(nf_sockopts);
50 static spinlock_t nf_hook_lock = SPIN_LOCK_UNLOCKED;
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.
57 static struct nf_queue_handler_t {
58 nf_queue_outfn_t outfn;
60 } queue_handler[NPROTO];
61 static rwlock_t queue_handler_lock = RW_LOCK_UNLOCKED;
63 int nf_register_hook(struct nf_hook_ops *reg)
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)
72 list_add_rcu(®->list, i->prev);
73 spin_unlock_bh(&nf_hook_lock);
79 void nf_unregister_hook(struct nf_hook_ops *reg)
81 spin_lock_bh(&nf_hook_lock);
82 list_del_rcu(®->list);
83 spin_unlock_bh(&nf_hook_lock);
88 /* Do exclusive ranges overlap? */
89 static inline int overlap(int min1, int max1, int min2, int max2)
91 return max1 > min2 && min1 < max2;
94 /* Functions to register sockopt ranges (exclusive). */
95 int nf_register_sockopt(struct nf_sockopt_ops *reg)
100 if (down_interruptible(&nf_sockopt_mutex) != 0)
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);
120 list_add(®->list, &nf_sockopts);
122 up(&nf_sockopt_mutex);
126 void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
128 /* No point being interruptible: we're probably in cleanup_module() */
130 down(&nf_sockopt_mutex);
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);
140 list_del(®->list);
141 up(&nf_sockopt_mutex);
144 #ifdef CONFIG_NETFILTER_DEBUG
147 #include <linux/netfilter_ipv4.h>
149 static void debug_print_hooks_ip(unsigned int nf_debug)
151 if (nf_debug & (1 << NF_IP_PRE_ROUTING)) {
152 printk("PRE_ROUTING ");
153 nf_debug ^= (1 << NF_IP_PRE_ROUTING);
155 if (nf_debug & (1 << NF_IP_LOCAL_IN)) {
157 nf_debug ^= (1 << NF_IP_LOCAL_IN);
159 if (nf_debug & (1 << NF_IP_FORWARD)) {
161 nf_debug ^= (1 << NF_IP_FORWARD);
163 if (nf_debug & (1 << NF_IP_LOCAL_OUT)) {
164 printk("LOCAL_OUT ");
165 nf_debug ^= (1 << NF_IP_LOCAL_OUT);
167 if (nf_debug & (1 << NF_IP_POST_ROUTING)) {
168 printk("POST_ROUTING ");
169 nf_debug ^= (1 << NF_IP_POST_ROUTING);
172 printk("Crap bits: 0x%04X", nf_debug);
176 void nf_dump_skb(int pf, struct sk_buff *skb)
178 printk("skb: pf=%i %s dev=%s len=%u\n",
180 skb->sk ? "(owned)" : "(unowned)",
181 skb->dev ? skb->dev->name : "(no dev)",
185 const struct iphdr *ip = skb->nh.iph;
186 __u32 *opt = (__u32 *) (ip + 1);
188 __u16 src_port = 0, dst_port = 0;
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);
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),
202 ntohs(ip->tot_len), ip->tos, ntohs(ip->id),
203 ntohs(ip->frag_off), ip->ttl);
205 for (opti = 0; opti < (ip->ihl - sizeof(struct iphdr) / 4); opti++)
206 printk(" O=0x%8.8X", *opt++);
212 void nf_debug_ip_local_deliver(struct sk_buff *skb)
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. */
219 printk("ip_local_deliver: skb->dev is NULL.\n");
221 else if (strcmp(skb->dev->name, "lo") == 0) {
222 if (skb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
223 | (1 << NF_IP_POST_ROUTING)
224 | (1 << NF_IP_PRE_ROUTING)
225 | (1 << NF_IP_LOCAL_IN))) {
226 printk("ip_local_deliver: bad loopback skb: ");
227 debug_print_hooks_ip(skb->nf_debug);
228 nf_dump_skb(PF_INET, skb);
232 if (skb->nf_debug != ((1<<NF_IP_PRE_ROUTING)
233 | (1<<NF_IP_LOCAL_IN))) {
234 printk("ip_local_deliver: bad non-lo skb: ");
235 debug_print_hooks_ip(skb->nf_debug);
236 nf_dump_skb(PF_INET, skb);
241 void nf_debug_ip_loopback_xmit(struct sk_buff *newskb)
243 if (newskb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
244 | (1 << NF_IP_POST_ROUTING))) {
245 printk("ip_dev_loopback_xmit: bad owned skb = %p: ",
247 debug_print_hooks_ip(newskb->nf_debug);
248 nf_dump_skb(PF_INET, newskb);
250 /* Clear to avoid confusing input check */
251 newskb->nf_debug = 0;
254 void nf_debug_ip_finish_output2(struct sk_buff *skb)
256 /* If it's owned, it must have gone through the
257 * NF_IP_LOCAL_OUT and NF_IP_POST_ROUTING.
258 * Otherwise, must have gone through
259 * NF_IP_PRE_ROUTING, NF_IP_FORWARD and NF_IP_POST_ROUTING.
262 if (skb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
263 | (1 << NF_IP_POST_ROUTING))) {
264 printk("ip_finish_output: bad owned skb = %p: ", skb);
265 debug_print_hooks_ip(skb->nf_debug);
266 nf_dump_skb(PF_INET, skb);
269 if (skb->nf_debug != ((1 << NF_IP_PRE_ROUTING)
270 | (1 << NF_IP_FORWARD)
271 | (1 << NF_IP_POST_ROUTING))) {
272 /* Fragments, entunnelled packets, TCP RSTs
273 generated by ipt_REJECT will have no
274 owners, but still may be local */
275 if (skb->nf_debug != ((1 << NF_IP_LOCAL_OUT)
276 | (1 << NF_IP_POST_ROUTING))){
277 printk("ip_finish_output:"
278 " bad unowned skb = %p: ",skb);
279 debug_print_hooks_ip(skb->nf_debug);
280 nf_dump_skb(PF_INET, skb);
285 #endif /*CONFIG_NETFILTER_DEBUG*/
287 /* Call get/setsockopt() */
288 static int nf_sockopt(struct sock *sk, int pf, int val,
289 char __user *opt, int *len, int get)
292 struct nf_sockopt_ops *ops;
295 if (down_interruptible(&nf_sockopt_mutex) != 0)
298 list_for_each(i, &nf_sockopts) {
299 ops = (struct nf_sockopt_ops *)i;
302 if (val >= ops->get_optmin
303 && val < ops->get_optmax) {
305 up(&nf_sockopt_mutex);
306 ret = ops->get(sk, val, opt, len);
310 if (val >= ops->set_optmin
311 && val < ops->set_optmax) {
313 up(&nf_sockopt_mutex);
314 ret = ops->set(sk, val, opt, *len);
320 up(&nf_sockopt_mutex);
324 down(&nf_sockopt_mutex);
326 if (ops->cleanup_task)
327 wake_up_process(ops->cleanup_task);
328 up(&nf_sockopt_mutex);
332 int nf_setsockopt(struct sock *sk, int pf, int val, char __user *opt,
335 return nf_sockopt(sk, pf, val, opt, &len, 0);
338 int nf_getsockopt(struct sock *sk, int pf, int val, char __user *opt, int *len)
340 return nf_sockopt(sk, pf, val, opt, len, 1);
343 static unsigned int nf_iterate(struct list_head *head,
344 struct sk_buff **skb,
346 const struct net_device *indev,
347 const struct net_device *outdev,
348 struct list_head **i,
349 int (*okfn)(struct sk_buff *),
353 * The caller must not block between calls to this
354 * function because of risk of continuing from deleted element.
356 list_for_each_continue_rcu(*i, head) {
357 struct nf_hook_ops *elem = (struct nf_hook_ops *)*i;
359 if (hook_thresh > elem->priority)
362 /* Optimization: we don't need to hold module
363 reference here, since function can't sleep. --RR */
364 switch (elem->hook(hook, skb, indev, outdev, okfn)) {
378 #ifdef CONFIG_NETFILTER_DEBUG
383 NFDEBUG("Evil return from %p(%u).\n",
391 int nf_register_queue_handler(int pf, nf_queue_outfn_t outfn, void *data)
395 write_lock_bh(&queue_handler_lock);
396 if (queue_handler[pf].outfn)
399 queue_handler[pf].outfn = outfn;
400 queue_handler[pf].data = data;
403 write_unlock_bh(&queue_handler_lock);
408 /* The caller must flush their queue before this */
409 int nf_unregister_queue_handler(int pf)
411 write_lock_bh(&queue_handler_lock);
412 queue_handler[pf].outfn = NULL;
413 queue_handler[pf].data = NULL;
414 write_unlock_bh(&queue_handler_lock);
420 * Any packet that leaves via this function must come back
421 * through nf_reinject().
423 static int nf_queue(struct sk_buff *skb,
424 struct list_head *elem,
425 int pf, unsigned int hook,
426 struct net_device *indev,
427 struct net_device *outdev,
428 int (*okfn)(struct sk_buff *))
431 struct nf_info *info;
432 #ifdef CONFIG_BRIDGE_NETFILTER
433 struct net_device *physindev = NULL;
434 struct net_device *physoutdev = NULL;
437 /* QUEUE == DROP if noone is waiting, to be safe. */
438 read_lock(&queue_handler_lock);
439 if (!queue_handler[pf].outfn) {
440 read_unlock(&queue_handler_lock);
445 info = kmalloc(sizeof(*info), GFP_ATOMIC);
448 printk(KERN_ERR "OOM queueing packet %p\n",
450 read_unlock(&queue_handler_lock);
455 *info = (struct nf_info) {
456 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
458 /* If it's going away, ignore hook. */
459 if (!try_module_get(info->elem->owner)) {
460 read_unlock(&queue_handler_lock);
465 /* Bump dev refs so they don't vanish while packet is out */
466 if (indev) dev_hold(indev);
467 if (outdev) dev_hold(outdev);
469 #ifdef CONFIG_BRIDGE_NETFILTER
470 if (skb->nf_bridge) {
471 physindev = skb->nf_bridge->physindev;
472 if (physindev) dev_hold(physindev);
473 physoutdev = skb->nf_bridge->physoutdev;
474 if (physoutdev) dev_hold(physoutdev);
478 status = queue_handler[pf].outfn(skb, info, queue_handler[pf].data);
479 read_unlock(&queue_handler_lock);
482 /* James M doesn't say fuck enough. */
483 if (indev) dev_put(indev);
484 if (outdev) dev_put(outdev);
485 #ifdef CONFIG_BRIDGE_NETFILTER
486 if (physindev) dev_put(physindev);
487 if (physoutdev) dev_put(physoutdev);
489 module_put(info->elem->owner);
497 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
498 struct net_device *indev,
499 struct net_device *outdev,
500 int (*okfn)(struct sk_buff *),
503 struct list_head *elem;
504 unsigned int verdict;
507 /* We may already have this, but read-locks nest anyway */
510 #ifdef CONFIG_NETFILTER_DEBUG
511 if (skb->nf_debug & (1 << hook)) {
512 printk("nf_hook: hook %i already set.\n", hook);
513 nf_dump_skb(pf, skb);
515 skb->nf_debug |= (1 << hook);
518 elem = &nf_hooks[pf][hook];
520 verdict = nf_iterate(&nf_hooks[pf][hook], &skb, hook, indev,
521 outdev, &elem, okfn, hook_thresh);
522 if (verdict == NF_QUEUE) {
523 NFDEBUG("nf_hook: Verdict = QUEUE.\n");
524 if (!nf_queue(skb, elem, pf, hook, indev, outdev, okfn))
543 void nf_reinject(struct sk_buff *skb, struct nf_info *info,
544 unsigned int verdict)
546 struct list_head *elem = &info->elem->list;
551 /* Release those devices we held, or Alexey will kill me. */
552 if (info->indev) dev_put(info->indev);
553 if (info->outdev) dev_put(info->outdev);
554 #ifdef CONFIG_BRIDGE_NETFILTER
555 if (skb->nf_bridge) {
556 if (skb->nf_bridge->physindev)
557 dev_put(skb->nf_bridge->physindev);
558 if (skb->nf_bridge->physoutdev)
559 dev_put(skb->nf_bridge->physoutdev);
563 /* Drop reference to owner of hook which queued us. */
564 module_put(info->elem->owner);
566 list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
571 if (elem == &nf_hooks[info->pf][info->hook]) {
572 /* The module which sent it to userspace is gone. */
573 NFDEBUG("%s: module disappeared, dropping packet.\n",
578 /* Continue traversal iff userspace said ok... */
579 if (verdict == NF_REPEAT) {
584 if (verdict == NF_ACCEPT) {
586 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
588 info->indev, info->outdev, &elem,
589 info->okfn, INT_MIN);
598 if (!nf_queue(skb, elem, info->pf, info->hook,
599 info->indev, info->outdev, info->okfn))
605 if (verdict == NF_DROP)
613 /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
614 int ip_route_me_harder(struct sk_buff **pskb)
616 struct iphdr *iph = (*pskb)->nh.iph;
618 struct flowi fl = {};
619 struct dst_entry *odst;
622 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
623 * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook.
625 if (inet_addr_type(iph->saddr) == RTN_LOCAL) {
626 fl.nl_u.ip4_u.daddr = iph->daddr;
627 fl.nl_u.ip4_u.saddr = iph->saddr;
628 fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
629 fl.oif = (*pskb)->sk ? (*pskb)->sk->sk_bound_dev_if : 0;
630 #ifdef CONFIG_IP_ROUTE_FWMARK
631 fl.nl_u.ip4_u.fwmark = (*pskb)->nfmark;
633 fl.proto = iph->protocol;
634 if (ip_route_output_key(&rt, &fl) != 0)
637 /* Drop old route. */
638 dst_release((*pskb)->dst);
639 (*pskb)->dst = &rt->u.dst;
641 /* non-local src, find valid iif to satisfy
642 * rp-filter when calling ip_route_input. */
643 fl.nl_u.ip4_u.daddr = iph->saddr;
644 if (ip_route_output_key(&rt, &fl) != 0)
648 if (ip_route_input(*pskb, iph->daddr, iph->saddr,
649 RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
650 dst_release(&rt->u.dst);
653 dst_release(&rt->u.dst);
657 if ((*pskb)->dst->error)
660 /* Change in oif may mean change in hh_len. */
661 hh_len = (*pskb)->dst->dev->hard_header_len;
662 if (skb_headroom(*pskb) < hh_len) {
663 struct sk_buff *nskb;
665 nskb = skb_realloc_headroom(*pskb, hh_len);
669 skb_set_owner_w(nskb, (*pskb)->sk);
677 int skb_ip_make_writable(struct sk_buff **pskb, unsigned int writable_len)
679 struct sk_buff *nskb;
682 if (writable_len > (*pskb)->len)
685 /* Not exclusive use of packet? Must copy. */
686 if (skb_shared(*pskb) || skb_cloned(*pskb))
689 /* Alexey says IP hdr is always modifiable and linear, so ok. */
690 if (writable_len <= (*pskb)->nh.iph->ihl*4)
693 iplen = writable_len - (*pskb)->nh.iph->ihl*4;
695 /* DaveM says protocol headers are also modifiable. */
696 switch ((*pskb)->nh.iph->protocol) {
699 if (skb_copy_bits(*pskb, (*pskb)->nh.iph->ihl*4,
700 &hdr, sizeof(hdr)) != 0)
702 if (writable_len <= (*pskb)->nh.iph->ihl*4 + hdr.doff*4)
707 if (writable_len<=(*pskb)->nh.iph->ihl*4+sizeof(struct udphdr))
712 <= (*pskb)->nh.iph->ihl*4 + sizeof(struct icmphdr))
715 /* Insert other cases here as desired */
719 nskb = skb_copy(*pskb, GFP_ATOMIC);
722 BUG_ON(skb_is_nonlinear(nskb));
724 /* Rest of kernel will get very unhappy if we pass it a
725 suddenly-orphaned skbuff */
727 skb_set_owner_w(nskb, (*pskb)->sk);
733 return pskb_may_pull(*pskb, writable_len);
735 EXPORT_SYMBOL(skb_ip_make_writable);
736 #endif /*CONFIG_INET*/
738 /* Internal logging interface, which relies on the real
739 LOG target modules */
741 #define NF_LOG_PREFIXLEN 128
743 static nf_logfn *nf_logging[NPROTO]; /* = NULL */
744 static int reported = 0;
745 static spinlock_t nf_log_lock = SPIN_LOCK_UNLOCKED;
747 int nf_log_register(int pf, nf_logfn *logfn)
751 /* Any setup of logging members must be done before
752 * substituting pointer. */
754 spin_lock(&nf_log_lock);
755 if (!nf_logging[pf]) {
756 nf_logging[pf] = logfn;
759 spin_unlock(&nf_log_lock);
763 void nf_log_unregister(int pf, nf_logfn *logfn)
765 spin_lock(&nf_log_lock);
766 if (nf_logging[pf] == logfn)
767 nf_logging[pf] = NULL;
768 spin_unlock(&nf_log_lock);
770 /* Give time to concurrent readers. */
774 void nf_log_packet(int pf,
775 unsigned int hooknum,
776 const struct sk_buff *skb,
777 const struct net_device *in,
778 const struct net_device *out,
779 const char *fmt, ...)
782 char prefix[NF_LOG_PREFIXLEN];
786 logfn = nf_logging[pf];
789 vsnprintf(prefix, sizeof(prefix), fmt, args);
791 /* We must read logging before nf_logfn[pf] */
792 smp_read_barrier_depends();
793 logfn(hooknum, skb, in, out, prefix);
794 } else if (!reported) {
795 printk(KERN_WARNING "nf_log_packet: can\'t log yet, "
796 "no backend logging module loaded in!\n");
801 EXPORT_SYMBOL(nf_log_register);
802 EXPORT_SYMBOL(nf_log_unregister);
803 EXPORT_SYMBOL(nf_log_packet);
805 /* This does not belong here, but ipt_REJECT needs it if connection
806 tracking in use: without this, connection may not be in hash table,
807 and hence manufactured ICMP or RST packets will not be associated
809 void (*ip_ct_attach)(struct sk_buff *, struct nf_ct_info *);
811 void __init netfilter_init(void)
815 for (i = 0; i < NPROTO; i++) {
816 for (h = 0; h < NF_MAX_HOOKS; h++)
817 INIT_LIST_HEAD(&nf_hooks[i][h]);
821 EXPORT_SYMBOL(ip_ct_attach);
822 EXPORT_SYMBOL(ip_route_me_harder);
823 EXPORT_SYMBOL(nf_getsockopt);
824 EXPORT_SYMBOL(nf_hook_slow);
825 EXPORT_SYMBOL(nf_hooks);
826 EXPORT_SYMBOL(nf_register_hook);
827 EXPORT_SYMBOL(nf_register_queue_handler);
828 EXPORT_SYMBOL(nf_register_sockopt);
829 EXPORT_SYMBOL(nf_reinject);
830 EXPORT_SYMBOL(nf_setsockopt);
831 EXPORT_SYMBOL(nf_unregister_hook);
832 EXPORT_SYMBOL(nf_unregister_queue_handler);
833 EXPORT_SYMBOL(nf_unregister_sockopt);