/* * icmp_error.c * * cc -D__KERNEL__ -DMODULE -DCONFIG_NETFILTER * -I/usr/src/linux/include -O -Wall -g -D__SMP__ -DSMP -c * icmp_error.c -o icmp_error.o */ #include #include #include #include #include #include /* copy_from_user() */ #include #include /* ip_compute_csum() */ #include #include #include #include #include #include #include #include #include #include #define MODULE_NAME "icmp_error" #define MODULE_VERSION "0.1" /* print line of info */ #define linfo(fmt, args...) printk(KERN_INFO MODULE_NAME ": " fmt "\n", ## args) /* print info msgs */ #define binfo(fmt, args...) printk(KERN_INFO MODULE_NAME ": " fmt, ## args) /* print info msg */ #define info(fmt, args...) printk(fmt, ## args) /* * this function is called when packets enter the postrouting * (packets about to hit wire) */ static unsigned int hook_post_routing(unsigned int hook, struct sk_buff **skb, const struct net_device *indev, const struct net_device *outdev, int (*okfn)(struct sk_buff *)) { struct iphdr *iph = (*skb)->nh.iph; unsigned char *type; MOD_INC_USE_COUNT; /* * check for ICMP packets, ignore all others */ if (iph->protocol != IPPROTO_ICMP) goto out; binfo("pr: "); if ((*skb)->h.icmph->type == ICMP_ECHO) { info("ICMP_ECHO "); } else if ((*skb)->h.icmph->type == ICMP_ECHOREPLY) { info("ICMP_ECHOREPLY "); } else { /* goto out; */ ; } type = (*skb)->nh.raw; type += 20; /* assume iphdr is 20 bytes, no options */ info("manual icmp type offset gives: %u ", *type); out: info("\n"); MOD_DEC_USE_COUNT; return NF_ACCEPT; } /* * the hook operations structure * fed to netfilter during initialization */ static struct nf_hook_ops icmp_mon_post_routing_ops = { { NULL, NULL }, hook_post_routing, PF_INET, NF_IP_POST_ROUTING, 0 }; /* = { { NULL, NULL }, hook_post_routing, PF_INET, NF_IP_LOCAL_OUT, 0 }; */ /* = { { NULL, NULL }, hook_post_routing, PF_INET, NF_IP_LOCAL_IN, 0 }; */ static int __init init(void) { /* * create and register the post routing hook */ if (nf_register_hook(&icmp_mon_post_routing_ops) != 0) { linfo("unable to register post routing hooks"); return -ENOMEM; } linfo("%s version %s loaded", MODULE_NAME, MODULE_VERSION); return 0; } static void __exit fini(void) { /* * remove the netfilter hooks */ nf_unregister_hook(&icmp_mon_post_routing_ops); linfo("%s version %s unloaded", MODULE_NAME, MODULE_VERSION); } module_init(init); module_exit(fini); MODULE_AUTHOR("Ola Nordstrom "); MODULE_DESCRIPTION("FOO"); MODULE_LICENSE("GPL"); EXPORT_NO_SYMBOLS;