Make kernel version default to the latest one we support in the Makefile
[transforward.git] / transforward.c
1 #include <linux/module.h>
2 #include <linux/moduleparam.h>
3 #include <linux/types.h>
4 #include <linux/kernel.h>
5 #include <linux/fs_struct.h>
6 #include <linux/fs.h>
7 #include <linux/mm.h>
8 #include <linux/reboot.h>
9 #include <linux/delay.h>
10 #include <linux/proc_fs.h>
11 #include <asm/uaccess.h>
12 #include <linux/sysrq.h>
13 #include <linux/timer.h>
14 #include <linux/time.h>
15 #include <linux/lglock.h>
16 #include <linux/init.h>
17 #include <linux/idr.h>
18 #include <linux/namei.h>
19 #include <linux/bitops.h>
20 #include <linux/mount.h>
21 #include <linux/dcache.h>
22 #include <linux/spinlock.h>
23 #include <linux/completion.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/kprobes.h>
27 #include <linux/kallsyms.h>
28 #include <linux/nsproxy.h>
29 #include <net/sock.h>
30 #include <linux/inetdevice.h>
31
32 #define VERSION_STR "0.0.1"
33
34 #ifndef CONFIG_X86_64
35 #error "This code does not support your architecture"
36 #endif
37
38 MODULE_AUTHOR("Sapan Bhatia <sapanb@cs.princeton.edu>");
39 MODULE_DESCRIPTION("Transparent port forwarding for LXC.");
40 MODULE_LICENSE("GPL");
41 MODULE_VERSION(VERSION_STR);
42
43 static int address_in_root(unsigned int haddr) {
44     struct net_device *dev;
45     struct net *net = &init_net;
46
47     for_each_netdev(net, dev) {
48             unsigned int ifhaddr = inet_select_addr(dev,0,0);
49             //printk(KERN_CRIT "Checking address: %u",ifhaddr);
50             if (haddr == ifhaddr) return 1;
51     }
52     return 0;
53 }
54
55 static int inet_bind_entry(struct socket *sock, struct sockaddr *uaddr, int addr_len) {
56     struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
57     unsigned int snum = addr->sin_addr.s_addr;
58         //printk(KERN_CRIT "Checking address %d",snum);
59     if (address_in_root(snum)) {
60         put_net(sock_net(sock->sk));
61         sock_net_set(sock->sk, get_net(&init_net)); 
62         //printk(KERN_CRIT "Rewiring netns");
63     }
64     jprobe_return();
65     return 0;
66 }
67
68 static struct file *do_last_probe(struct nameidata *nd, struct path *path,
69                          const struct open_flags *op, const char *pathname) {
70     
71     
72     jprobe_return();
73
74 }
75
76 static struct jprobe net_probe = {
77         .entry = (kprobe_opcode_t *) inet_bind_entry
78 };
79
80
81 static void __exit transforward_exit(void)
82 {
83         unregister_jprobe(&net_probe);
84         printk("Transforward: Stopped transforward.\n");
85 }
86
87
88
89 static int __init transforward_init(void)
90 {
91     int ret = 0;
92         printk("Transforward: starting transforward version %s.\n",
93                VERSION_STR);
94           net_probe.kp.addr = 
95                   (kprobe_opcode_t *) kallsyms_lookup_name("inet_bind");
96           if (!net_probe.kp.addr) {
97                   printk("Couldn't find %s to plant kretprobe\n", "inet_bind");
98                   return -1;
99           }
100   
101           if ((ret = register_jprobe(&net_probe)) <0) {
102                   printk("register_jprobe failed, returned %u\n", ret);
103                   return -1;
104           }
105           //printk("Planted jprobe at %p, handler addr %p\n",
106                  //net_probe.kp.addr, net_probe.entry);
107
108         return ret;
109 }
110
111 module_init(transforward_init);
112 module_exit(transforward_exit);