linux - Use Netfilter to write kernel module to modify source IP error, computer crash -
i want write kernel module uses netfilter modify source ip "100.100.100.100" whiche packet destination ip "192.68.4.103" , protocol tcp. write .c file when install module ,it cause computer crash. how should rewrite it? system ubuntu16.04 . here code wrote:
#include <linux/init.h> #include <linux/module.h> #include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include<linux/inet.h> #include<net/ip.h> #include<net/tcp.h> #include <linux/netdevice.h> #include <linux/inet.h> #include <linux/socket.h> #include <linux/skbuff.h> module_license("gpl"); module_author("linhos"); #define if_name "eno1" //network name got command "ifconfig" struct nf_hook_ops nfho; static unsigned int checkip( unsigned int hooknum, struct sk_buff *__skb, const struct net_device *in, const struct net_device *out, int(*okfn)(struct sk_buff *)){ struct sk_buff *skb; struct net_device *dev; struct iphdr *iph; struct tcphdr *tcph; int tot_len; unsigned int iph_len; int tcph_len; int ret; skb = __skb; if (skb == null) return nf_accept; iph = ip_hdr(skb); if (iph == null) return nf_accept; tot_len = ntohs(iph->tot_len); if (iph->daddr =="192.68.4.103") { //locla ip iph_len = ip_hdrlen(skb); skb_pull(skb, iph_len); skb_reset_transport_header(skb); if (iph->protocol == ipproto_tcp) { tcph = tcp_hdr(skb); tcph_len = tcp_hdrlen(skb); iph->saddr = in_aton("100.100.100.100"); dev = dev_get_by_name(&init_net, if_name); tcph->check = 0; skb->csum = csum_partial((unsigned char *) tcph, tot_len - iph_len, 0); tcph->check = csum_tcpudp_magic(iph->saddr, iph->daddr, ntohs(iph->tot_len) - iph_len, iph->protocol, skb->csum); iph->check = 0; iph->check = ip_fast_csum(iph, iph->ihl); skb->ip_summed = checksum_none; skb->pkt_type = packet_host; skb->dev = dev; skb_push(skb, iph_len); skb_push(skb, eth_alen); ret = dev_queue_xmit(skb); if (ret < 0) { printk(kern_err "dev_queue_xmit() error!\n"); return nf_drop; } return nf_stolen; } skb_push(skb, iph_len); skb_reset_transport_header(skb); } return nf_accept; } static int __initfilter_init(void){ printk("----------------checkip ok!--------------"); nfho.hook = checkip; nfho.pf = af_inet; nfho.hooknum = nf_inet_pre_routing; nfho.priority = nf_ip_pri_first; int ret = nf_register_hook(&nfho); if (ret < 0) { printk(kern_err "can't modify skb hook!"); return ret; } return 0; } static void filter_exit(void) { nf_unregister_hook(&nfho); } module_init(filter_init); module_exit(filter_exit);
could explain me doing wrong?
Comments
Post a Comment