forked from george7551858/kernel-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kweb.c
224 lines (167 loc) · 5.31 KB
/
kweb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <linux/version.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/stat.h>
#include <net/sock.h>
#define BUFFSIZE (1*1024)
static unsigned short port = 8000;
module_param(port, ushort, S_IRUGO);
static unsigned int KiB = 2*1024;
module_param(KiB, uint, S_IRUGO);
#ifdef KWEB_DEBUG
#define KWEBMSG(_msg,args...) printk( KERN_DEBUG "kweb: " _msg, ## args)
#else
#define KWEBMSG(_msg,args...) /* print nothing */
#endif
int start = 0;
static void connection_handler(struct work_struct * ignore);
void tcp_server(struct socket *csocket);
char *inet_ntoa(struct in_addr in);
int sendmsg(struct socket *csocket, const void *data, size_t datalength, int flags);
struct socket *sock;
static DECLARE_WORK(connection_work, connection_handler);
static void connection_handler(struct work_struct * ignore)
{
int rc,s_status,len = 0;
struct socket *newsock;
struct sockaddr_in locaddr;
struct sockaddr_in peeraddr;
char * client_ip;
KWEBMSG("Socket server work now.\n");
rc = sock_create(PF_INET, SOCK_STREAM, 0, &sock);
if ( rc < 0 ) {
KWEBMSG( "ERROR - Could not create socket\n");
return;
}
memset(&locaddr, 0, sizeof(locaddr));
locaddr.sin_family = AF_INET;
locaddr.sin_port = htons(port);
locaddr.sin_addr.s_addr = htonl(INADDR_ANY);
rc = kernel_bind(sock, (struct sockaddr *)&locaddr, sizeof(locaddr));
if (rc == -EADDRINUSE) {
KWEBMSG( "ERROR - Port %d already in use\n",port);
return;
}
if (rc != 0) {
KWEBMSG( "ERROR - Can't bind to port %d\n",port);
return;
}
rc = kernel_listen(sock, 0);
KWEBMSG("Server listening on port:%d\n",port);
KWEBMSG("Waiting for client's request\n");
do {
rc = wait_event_interruptible_timeout(
#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,35))
*(sock->sk->sk_sleep),
#else
*sk_sleep(sock->sk),
#endif
(s_status = kernel_accept(sock, &newsock, O_NONBLOCK)) >= 0,
5 * HZ
);
/* Always relaunch after 5 sec, so it wouldn't block 'kweb_module_cleanup'*/
if (rc == 0) {
KWEBMSG("Time Out\n");
continue;
}
KWEBMSG("%d,%d\n",rc,s_status);
rc = kernel_getpeername(newsock, (struct sockaddr *)&peeraddr, &len);
client_ip = inet_ntoa(peeraddr.sin_addr);
KWEBMSG("Client IP:%s, Client port:%d\n", client_ip, peeraddr.sin_port);
kfree(client_ip);
tcp_server(newsock);
KWEBMSG("Close client socket\n");
if( newsock != NULL ) sock_release(newsock);
newsock = NULL;
} while (start);
}
void tcp_server(struct socket *csocket)
{
char *request;
struct msghdr msg;
struct kvec iov;
request = kmalloc(BUFFSIZE, GFP_KERNEL);
memset(request, 0, BUFFSIZE);
iov.iov_base = (void *)request;
iov.iov_len = (__kernel_size_t)BUFFSIZE;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = NULL;
msg.msg_controllen = 0;
while(kernel_recvmsg(
csocket, /*Client socket*/
&msg, /*Received message*/
&iov, /*Input s/g array for message data(msg.msg_iov)*/
1, /*Size of input s/g array(msg.msg_iovlen)*/
BUFFSIZE, /*Number of bytes to read*/
0 /*Message flags*/ ))
{
KWEBMSG("Request:%s\n",request);
KWEBMSG("TCP request received\n");
sendmsg(csocket,request,strlen(request),0);
KWEBMSG("TCP sent msg\n");
memset(request, 0, BUFFSIZE);
}
kfree(request);
}
int sendmsg(struct socket *csocket, const void *data, size_t datalength, int flags)
{
struct msghdr msg;
struct kvec iov;
int len;
iov.iov_base = (void *)data;
iov.iov_len = (__kernel_size_t)datalength;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = flags;
len = kernel_sendmsg(csocket, &msg, &iov, 1, datalength);
return len;
}
static int __init kweb_module_init(void)
{
int rv;
start = 1;
rv = schedule_work(&connection_work);
KWEBMSG("schedule_work: %d\n",rv);
KWEBMSG("Kweb module init\n");
return 0;
}
static void __exit kweb_module_cleanup(void)
{
int rv;
start = 0;
rv = cancel_work_sync(&connection_work);
KWEBMSG("cancel_work_sync: %d\n",rv);
rv = flush_work(&connection_work);
KWEBMSG("flush_work: %d\n",rv);
if( sock != NULL ) sock_release(sock);
sock = NULL;
KWEBMSG("Kweb module exit\n");
}
module_init(kweb_module_init);
module_exit(kweb_module_cleanup);
MODULE_DESCRIPTION("Kernel TCP server.");
MODULE_LICENSE("GPL");
char *inet_ntoa(struct in_addr in)
{
char* str_ip = NULL;
u_int32_t int_ip = 0;
str_ip = kmalloc(16 * sizeof(char), GFP_KERNEL);
if (!str_ip)
return NULL;
else
memset(str_ip, 0, 16);
int_ip = in.s_addr;
sprintf(str_ip, "%d.%d.%d.%d", (int_ip ) & 0xFF,
(int_ip >> 8 ) & 0xFF,
(int_ip >> 16) & 0xFF,
(int_ip >> 24) & 0xFF);
return str_ip;
}