Attempt to work around kernel bug
[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 struct proc_dir_entry *proc_entry;
44
45 static int address_in_root(unsigned int haddr) {
46     struct net_device *dev;
47     struct net *net = &init_net;
48
49     for_each_netdev(net, dev) {
50             unsigned int ifhaddr = inet_select_addr(dev,0,0);
51             //printk(KERN_CRIT "Checking address: %u",ifhaddr);
52             if (haddr == ifhaddr) return 1;
53     }
54     return 0;
55 }
56
57 static int inet_bind_entry(struct socket *sock, struct sockaddr *uaddr, int addr_len) {
58     struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
59     unsigned int snum = addr->sin_addr.s_addr;
60         //printk(KERN_CRIT "Checking address %d",snum);
61     if (address_in_root(snum)) {
62         put_net(sock_net(sock->sk));
63         sock_net_set(sock->sk, get_net(&init_net)); 
64         //printk(KERN_CRIT "Rewiring netns");
65     }
66     jprobe_return();
67     return 0;
68 }
69
70 static struct file *do_last_probe(struct nameidata *nd, struct path *path,
71                          const struct open_flags *op, const char *pathname) {
72     
73     
74     jprobe_return();
75
76 }
77
78 static struct jprobe net_probe = {
79         .entry = (kprobe_opcode_t *) inet_bind_entry
80 };
81
82
83 static void __exit transforward_exit(void)
84 {
85         unregister_jprobe(&net_probe);
86         printk("Transforward: Stopped transforward.\n");
87 }
88
89 int once_only=0;
90
91 static int init_probes(void)
92 {
93     int ret = 0;
94         printk("Transforward: starting transforward version %s.\n",
95                VERSION_STR);
96           net_probe.kp.addr = 
97                   (kprobe_opcode_t *) kallsyms_lookup_name("inet_bind");
98           if (!net_probe.kp.addr) {
99                   printk("Couldn't find %s to plant kretprobe\n", "inet_bind");
100                   return -1;
101           }
102   
103           if ((ret = register_jprobe(&net_probe)) <0) {
104                   printk("register_jprobe failed, returned %u\n", ret);
105                   return -1;
106           }
107
108         return ret;
109 }
110
111 int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data) {            
112         if (!once_only) {
113                 once_only=1;
114                 if (init_probes()==-1)
115                         printk(KERN_CRIT "Could not install procprotect probes. Reload module to retry.");
116                 else 
117                         printk(KERN_CRIT "Activated transforward module");
118         }
119     return count;
120 }
121 static const struct file_operations transforward_fops = {
122     .owner = THIS_MODULE,
123     .write = procfile_write
124 };
125
126 static int __init transforward_init(void)
127 {
128     int ret = 0;
129         printk("Transforward: starting transforward version %s.\n",
130                VERSION_STR);
131
132     proc_entry = proc_create("transforward", 0644, NULL, &transforward_fops);
133         return ret;
134 }
135
136 module_init(transforward_init);
137 module_exit(transforward_exit);