📄 kaodv6.c
字号:
/***************************************************************************** * * Copyright (C) 2001 Uppsala University and Ericsson AB. * Copyright (C) 2003 Simon Fraser University and NewMIC * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Authors: Erik Nordström, <erik.nordstrom@it.uu.se> * : Peter Lee <peter.lee@shaw.ca> * *****************************************************************************/#include <linux/config.h>#ifdef CONFIG_MODVERSIONS#define MODVERSIONS#include <linux/modversions.h>#include <linux/version.h>#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,18))#define USE_OLD_ROUTE_ME_HARDER#endif#else#define USE_OLD_ROUTE_ME_HARDER#endif#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/errno.h>#include <linux/fs.h>#include <linux/wrapper.h>#include <linux/string.h>#include <linux/if_ether.h>#include <linux/kmod.h>#include <linux/ctype.h>#include <linux/skbuff.h>#include <linux/netfilter.h>//PL: left the <linux/netfilter_ipv4.h> for ip_route_me_harder#include <linux/netfilter_ipv4.h>#include <linux/netfilter_ipv6.h>#include <linux/ipv6.h>#include <net/ip.h>#include <net/dst.h>#include <net/route.h>#include <net/ip6_route.h>#include <linux/udp.h>#include <net/neighbour.h>static struct nf_hook_ops nf_hook1, nf_hook2, nf_hook3;#define AODV_PORT 654#define MAX_INTERFACES 10unsigned int ifindices[MAX_INTERFACES];int nif = 0;/* This function is taken from the kernel ip_queue.c source file. It * seem to have been moved to net/core/netfilter.c in later kernel * versions (verified for 2.4.18). There it is called * ip_route_me_harder(). Old version is kept here for compatibility. */#ifdef USE_OLD_ROUTE_ME_HARDER/* With a chainsaw... */static int route6_me_harder(struct sk_buff *skb){ struct ipv6hdr *iph = skb->nh.ipv6h; struct dst_entry *dst; struct flowi fl; fl.proto = iph->nexthdr; fl.fl6_dst = &iph->daddr; fl.fl6_src = &iph->saddr; fl.oif = skb->sk ? skb->sk->bound_dev_if : 0; fl.fl6_flowlabel = 0; fl.uli_u.ports.dport = 0; fl.uli_u.ports.sport = 0; dst = ip6_route_output(skb->sk, &fl); if (dst->error) { if (net_ratelimit()) printk(KERN_DEBUG "route6_me_harder: No more route.\n"); return -EINVAL; } /* Drop old route. */ dst_release(skb->dst); skb->dst = dst; return 0;}/*static int route_me_harder(struct sk_buff *skb){ struct iphdr *iph = skb->nh.iph; struct rtable *rt; struct rt_key key = { dst:iph->daddr, src:iph->saddr, oif:skb->sk ? skb->sk->bound_dev_if : 0, tos:RT_TOS(iph->tos) | RTO_CONN,#ifdef CONFIG_IP_ROUTE_FWMARK fwmark:skb->nfmark#endif }; if (ip_route_output_key(&rt, &key) != 0) return -EINVAL; // Drop old route. dst_release(skb->dst); skb->dst = &rt->u.dst; return 0;}*/#endif//PL: flowi structure is taken out from /usr/src/linux/include/net/flow.h/*ZJH*struct flowi { int proto; //{TCP, UDP, ICMP} union { struct { __u32 daddr; __u32 saddr; } ip4_u; struct { struct in6_addr * daddr; struct in6_addr * saddr; __u32 flowlabel; } ip6_u; } nl_u;#define fl6_dst nl_u.ip6_u.daddr#define fl6_src nl_u.ip6_u.saddr#define fl6_flowlabel nl_u.ip6_u.flowlabel#define fl4_dst nl_u.ip4_u.daddr#define fl4_src nl_u.ip4_u.saddr int oif; union { struct { __u16 sport; __u16 dport; } ports; struct { __u8 type; __u8 code; } icmpt; unsigned long data; } uli_u;};*ZJH*/#define FLOWR_NODECISION 0 /* rule not appliable to flow */#define FLOWR_SELECT 1 /* flow must follow this rule */#define FLOWR_CLEAR 2 /* priority level clears flow */#define FLOWR_ERROR 3//PL: Locally defined the route6_me_harderstatic int route6_me_harder(struct sk_buff *skb){ struct ipv6hdr *iph = skb->nh.ipv6h; struct dst_entry *dst; struct flowi fl; fl.proto = iph->nexthdr; fl.fl6_dst = &iph->daddr; fl.fl6_src = &iph->saddr; fl.oif = skb->sk ? skb->sk->bound_dev_if : 0; fl.fl6_flowlabel = 0; fl.uli_u.ports.dport = 0; fl.uli_u.ports.sport = 0; dst = (struct dst_entry *) ip6_route_output(skb->sk, &fl);/*ZJH*/ if (dst->error) { if (net_ratelimit()) printk(KERN_DEBUG "route6_me_harder: No more route.\n"); //PL: debug printk("route6_me_harder: ip6_route_output has problem, dst->error != 0\n"); return -EINVAL; } /* Drop old route. */ dst_release(skb->dst); skb->dst = dst; return 0;}unsigned int nf_aodv_hook(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *)){ int i; /* We are only interested in IP packets */ //PL: //if ((*skb)->nh.iph == NULL) if ((*skb)->nh.ipv6h == NULL) goto accept; else { //PL: debug /* int j; printk("src address : "); for(j=0; j<16; j++) { printk("%x", (*skb)->nh.ipv6h->saddr.s6_addr[j]); } printk("\n"); printk("dst address : "); for(j=0; j<16; j++) { printk("%x", (*skb)->nh.ipv6h->daddr.s6_addr[j]); } printk("\n"); printk("hooknum = %d\n", hooknum); */ } /* We want AODV control messages to go through directly to the * AODV socket.... */ /* if ((*skb)->nh.iph && (*skb)->nh.iph->protocol == IPPROTO_UDP) if ((*skb)->sk) if ((*skb)->sk->dport == htons(AODV_PORT) || (*skb)->sk->sport == htons(AODV_PORT)) goto accept; */ //PL: I'm not sure about this (This is the code causing core dump!!) /* if ((*skb)->h.uh != NULL) { if ((*skb)->h.uh->source == htons(AODV_PORT) || (*skb)->h.uh->dest == htons(AODV_PORT)) goto accept; } */ //PL: use the original code except change it to ipv6 and // (*skb)->nh.ipv6h == NULL is already checked at the beginning if ((*skb)->nh.ipv6h->nexthdr == IPPROTO_UDP) if ((*skb)->sk) if ((*skb)->sk->dport == htons(AODV_PORT) || (*skb)->sk->sport == htons(AODV_PORT)) goto accept; /* Check which hook the packet is on... */ switch (hooknum){ //PL: //case NF_IP_PRE_ROUTING: case NF_IP6_PRE_ROUTING: //PL: debug //printk("nf_aodv_hook: NF_IP6_PRE_ROUTING is called\n"); /* Loop through all AODV enabled interfaces and see if the packet * is bound to any of them. */ for (i = 0; i < nif; i++) if (ifindices[i] == in->ifindex) { (*skb)->nfmark = 3; goto queue; } break; //PL: //case NF_IP_LOCAL_OUT: case NF_IP6_LOCAL_OUT: //PL: debug //printk("nf_aodv_hook: NF_IP6_LOCAL_OUT is called\n"); for (i = 0; i < nif; i++) if (ifindices[i] == out->ifindex) { (*skb)->nfmark = 4; goto queue; } break; //PL: //case NF_IP_POST_ROUTING: case NF_IP6_POST_ROUTING: /* Re-route all packets before sending on interface. This will make sure queued packets are routed on a newly installed route (after a successful RREQ-cycle). FIXME: Make sure only "buffered" packets are re-routed. But how? */ //PL: debug /* printk("nf_aodv_hook: NF_IP6_POST_ROUTING is called, nfmark = %ld\n", (*skb)->nfmark); */ if ((*skb)->nfmark == 3 || (*skb)->nfmark == 4) {#ifdef USE_OLD_ROUTE_ME_HARDER //PL: route6_me_harder((*skb));#else //PL: Need to solve the implicit declaration warning route6_me_harder((*skb));#endif } return NF_ACCEPT; default:;/*ZJH*/ } accept: //PL: debug //printk("nf_aodv_hook: NF_ACCEPT\n"); (*skb)->nfmark = 2; return NF_ACCEPT; queue: //PL: debug //printk("nf_aodv_hook: NF_QUEUE\n"); return NF_QUEUE;}/* * Called when the module is inserted in the kernel. */char *ifname[MAX_INTERFACES] = { "eth0" };MODULE_PARM(ifname, "1-" __MODULE_STRING(MAX_INTERFACES) "s");int init_module(){ struct net_device *dev = NULL; int i; EXPORT_NO_SYMBOLS; nf_hook1.list.next = NULL; nf_hook1.list.prev = NULL; nf_hook1.hook = nf_aodv_hook; //PL: //nf_hook1.pf = PF_INET; nf_hook1.pf = PF_INET6; //nf_hook1.hooknum = NF_IP_PRE_ROUTING; nf_hook1.hooknum = NF_IP6_PRE_ROUTING; nf_register_hook(&nf_hook1); nf_hook2.list.next = NULL; nf_hook2.list.prev = NULL; nf_hook2.hook = nf_aodv_hook; //PL: //nf_hook2.pf = PF_INET; nf_hook2.pf = PF_INET6; //nf_hook2.hooknum = NF_IP_LOCAL_OUT; nf_hook2.hooknum = NF_IP6_LOCAL_OUT; nf_register_hook(&nf_hook2); nf_hook3.list.next = NULL; nf_hook3.list.prev = NULL; nf_hook3.hook = nf_aodv_hook; //PL: //nf_hook3.pf = PF_INET; nf_hook3.pf = PF_INET6; //nf_hook3.hooknum = NF_IP_POST_ROUTING; nf_hook3.hooknum = NF_IP6_POST_ROUTING; nf_register_hook(&nf_hook3); for (i = 0; i < MAX_INTERFACES; i++) { if (!ifname[i]) break; dev = dev_get_by_name(ifname[i]); if (!dev) { printk("kaodv: No device %s available, ignoring!\n", ifname[i]); dev_put(dev); continue; } ifindices[nif++] = dev->ifindex; dev_put(dev); } return 0;}/* * Called when removing the module from memory... */void cleanup_module(){ nf_unregister_hook(&nf_hook1); nf_unregister_hook(&nf_hook2); nf_unregister_hook(&nf_hook3);}MODULE_DESCRIPTION("AODV kernel support. © Uppsala University & Ericsson AB");MODULE_AUTHOR("Erik Nordström");#ifdef MODULE_LICENSEMODULE_LICENSE("GPL");#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -