Linux Netfilter discussions
 help / color / mirror / Atom feed
From: Piotr Bratkowski <pioterbrat@o2.pl>
To: netfilter@vger.kernel.org
Subject: Firewall
Date: Thu, 01 Jan 2009 18:42:10 +0100	[thread overview]
Message-ID: <495D0072.40809@o2.pl> (raw)

Hello

I'm trying to write simple firewall. But I have problem with tcp filtering.

I'm using 2.6.27 kernel.

Here is my code which supposed to block everything except www.

#define __KERNEL__
#define MODULE

#include <linux/kernel.h>        /* Kernel */
#include <linux/module.h>        /* Module */
#include <linux/netfilter.h>        /* Netfilter */
#include <linux/netfilter_ipv4.h>    /* Netfiletr for IPv4 */
#include <linux/skbuff.h>        /* Socket Kernel Buffers */
#include <linux/ip.h>            /* IP Header*/
#include <linux/tcp.h>

static struct nf_hook_ops netfilter_ops;            

/* Adres odblokowany: 212.77.100.101 (wp.pl) */
static unsigned char *ip_address = "\xD4\x4D\x64\x65";
unsigned char *port = "\x00\x50";

unsigned int main_hook(unsigned int hooknum,
          struct sk_buff *skb,
          const struct net_device *in,
          const struct net_device *out,
          int (*okfn)(struct sk_buff*))
{

    struct tcphdr *tcpH;
    tcpH=(struct tcphdr *) skb_transport_header(skb);
    //tcpH=tcp_hdr(skb)dr(skb); no difference
    if(tcpH->dest==*(unsigned short*) port)
    return NF_ACCEPT;
    else
    return NF_DROP;

    /*struct iphdr * ipHead;
    ipHead = ip_hdr( skb );
   
    if (ipHead == NULL)
        return NF_DROP;
       
    if (ipHead->saddr == *(unsigned int*)ip_address)
        return NF_ACCEPT;
    else
        return NF_DROP;*/
   
    //return NF_ACCEPT;
}

int init_module()
{
    netfilter_ops.hook        =    main_hook;
    netfilter_ops.hooknum        =    NF_INET_PRE_ROUTING;
    netfilter_ops.pf        =    PF_INET;
    netfilter_ops.priority        =    NF_IP_PRI_FIRST;

    nf_register_hook(&netfilter_ops);
   
return 0;
}

void cleanup_module() {
    nf_unregister_hook(&netfilter_ops);
}





             reply	other threads:[~2009-01-01 17:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-01 17:42 Piotr Bratkowski [this message]
     [not found] <163335.69140.qm@web8705.mail.in.yahoo.com>
2009-01-01 22:10 ` Firewall Piotr Bratkowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=495D0072.40809@o2.pl \
    --to=pioterbrat@o2.pl \
    --cc=netfilter@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox