Created
November 27, 2010 03:07
-
-
Save llj098/717521 to your computer and use it in GitHub Desktop.
a software switch written with the netfilter framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj-m += switch.o | |
all: | |
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules | |
clean: | |
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/slab.h> | |
#include <linux/list.h> | |
#include <linux/ip.h> | |
#include <linux/netdevice.h> | |
#include <linux/netfilter.h> | |
#include <linux/netfilter_ipv4.h> | |
#include <linux/skbuff.h> | |
#include <linux/tcp.h> | |
#include <linux/udp.h> | |
#include <linux/string.h> | |
#include <linux/ipv6.h> | |
#include <linux/inet.h> | |
MODULE_LICENSE("GPL"); | |
MODULE_AUTHOR("lijin liu<llj098@gmail.com>"); | |
MODULE_DESCRIPTION("Software switch"); | |
static struct nf_hook_ops netfilter_ops_in;/* IP PRE ROUTING */ | |
struct tcphdr *tcp_header; | |
struct sk_buff *sock_buff; | |
struct iphdr *ip_header; | |
int sadd,dadd,bit1,bit2,bit3,bit4; | |
struct net_device *dev; | |
char *in_face = "eth0"; | |
char *out_face = "eth1"; | |
void log_ip(int sadd,int dadd) | |
{ | |
int b1,b2,b3,b4; | |
b1 = 255 & sadd; | |
b2 = (0xff00 & sadd) >> 8; | |
b3 = (0xff0000 & sadd) >> 16; | |
b4 = (0xff000000 &sadd) >>24; | |
printk("sw:Source IP: %d.%d.%d.%d",b1,b2,b3,b4); | |
b1 = 255 & dadd; | |
b2 = (0xff00 & dadd) >> 8; | |
b3 = (0xff0000 & dadd) >> 16; | |
b4 = (0xff000000 & dadd) >>24; | |
printk(" sw: Destination IP: %d.%d.%d.%d",b1,b2,b3,b4); | |
} | |
unsigned int main_hook(unsigned int hooknum, | |
const struct sk_buff *skb, | |
const struct net_device *in, | |
const struct net_device *out, | |
int(*okfn)(struct sk_buff*)) | |
{ | |
printk("sw:----------------------------------\n"); | |
sock_buff = skb_copy(skb,GFP_ATOMIC); | |
ip_header = (struct iphdr*)(sock_buff->network_header); | |
log_ip(ip_header->saddr,ip_header->daddr); | |
printk("sw: dev->name is %s\n",sock_buff->dev); | |
for_each_netdev(&init_net,dev){ | |
if(strcmp(dev->name,in_face) == 0){ | |
printk("sw: in face\n"); | |
sock_buff->dev = out_face; | |
break; | |
} | |
else if(strcmp(dev->name,out_face) ==0){ | |
printk("sw: out face\n"); | |
sock_buff->dev = in_face; | |
break; | |
} | |
} | |
printk("sw:----------------------------------\n"); | |
return NF_ACCEPT; | |
} | |
/*check the package wether should pass though*/ | |
int package_passthough(struct sk_buff *skbuff) | |
{ | |
return 1; | |
} | |
int init_module(void) | |
{ | |
netfilter_ops_in.hook = main_hook; | |
netfilter_ops_in.pf = PF_INET; | |
netfilter_ops_in.hooknum= NF_INET_PRE_ROUTING; | |
netfilter_ops_in.priority=NF_IP_PRI_FIRST; | |
nf_register_hook(&netfilter_ops_in); | |
printk(KERN_INFO "sw: init_module() called\n"); | |
return 0; | |
} | |
void cleanup_module(void) | |
{ | |
printk(KERN_INFO "sw: cleanup_module() called\n"); | |
nf_unregister_hook(&netfilter_ops_in); | |
printk(KERN_INFO "sw: hook unregisted, quit called\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment