-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammer.c
413 lines (341 loc) · 11.8 KB
/
hammer.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <net/sock.h>
#include <net/tcp.h>
#include <linux/proc_fs.h>
MODULE_LICENSE("free-for-all");
#define MODULE_NAME "hammer"
#define CONTROL_NAME "__control"
// #define DEBUG
// #define HAMMER_OVERWRITE
#ifdef DEBUG
#define D(fmt,arg...) printk(fmt " [%s():%s:%d]\n",##arg,__func__,__FILE__,__LINE__)
#else
#define D(fmt,arg...)
#endif
#ifndef MIN
#define MIN(a,b) ((a) > (b) ? (b) : (a))
#endif
#define CONNECTED 1
#define DISCONNECTED 2
#define PROC_DATA 0
#define PROC_STAT 1
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
static struct proc_dir_entry *root;
static struct proc_dir_entry *control;
static spinlock_t giant;
LIST_HEAD(connections);
struct connection {
struct list_head list;
struct socket *socket;
void *orig_sk_user_data;
void (*orig_sk_data_ready)(struct sock *sk, int bytes);
int state;
struct proc {
char name[128];
struct proc_dir_entry *entry;
} proc[2];
#ifdef HAMMER_OVERWRITE
char output[10240];
#else
char *output;
#endif
ssize_t output_len;
struct stats {
unsigned long long http_code[600];
} stats;
};
// Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6
union http_status_line {
struct u64_u32 {
unsigned long long http_slash_one_dot_one;
union {
u32 space_digit_digit_digit;
struct structed {
unsigned char space;
unsigned char digit_0;
unsigned char digit_1;
unsigned char digit_2;
} __attribute__((packed)) structed;
} u32;
} __attribute__((packed)) u64_u32;
unsigned char u8[12];
};
static union http_status_line HTTP;
static int inet_addr(char* ip, unsigned int *dest, unsigned short *port);
static struct connection *connect_to(char *host);
static void disconnect(struct connection *c);
static int hammer_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb,unsigned int offset, size_t len);
static void hammer_sk_data_ready (struct sock *sk, int bytes);
static struct proc_dir_entry *procify(char *name, void *data,
int (*reader)(char *, char **, off_t, int, int *, void *),
int (*writer)(struct file *, const char *, unsigned long, void *));
static void cleanup_procfs_entries(struct connection *c);
static int control_read(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data);
static int control_write(struct file *file, const char *buffer, unsigned long count, void *data);
static int tail(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data);
static int stats(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data);
static int hammer(struct file *file, const char *buffer, unsigned long count, void *data);
inline static unsigned short three_digits_to_u16(char *digits);
// POC: just show the last 'count' bytes from the buffer
static int tail(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data) {
struct connection *c = (struct connection *) data;
if (!c)
return -ENOENT;
if (count > c->output_len) {
count = c->output_len;
offset = 0;
} else {
offset = c->output_len - count;
}
memcpy(buffer,(c->output + offset),count);
return count;
}
static int stats(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data) {
#define HAMMER_MAX_OUTPUT_STAT_SIZE 512
struct connection *c = (struct connection *) data;
int i,len = 0;
char output[HAMMER_MAX_OUTPUT_STAT_SIZE];
if (!c)
return -ENOENT;
if (offset > 0)
return 0;
if (count > HAMMER_MAX_OUTPUT_STAT_SIZE)
count = HAMMER_MAX_OUTPUT_STAT_SIZE;
memset(output,0,sizeof(output));
for (i = 0; i < sizeof(c->stats.http_code)/sizeof(c->stats.http_code[0]); i++) {
if (c->stats.http_code[i] > 0)
len = snprintf(output,sizeof(output) - 1,"%s%d : %lld\n",output,i,c->stats.http_code[i]);
}
memcpy(buffer,output,len);
return len;
#undef HAMMER_MAX_OUTPUT_STAT_SIZE
}
// uses the fact that *buffer is in the user address space, that is why
// copy_from_user and kernel_sendmsg are not used
static int hammer(struct file *file, const char *buffer, unsigned long count, void *data) {
struct connection *c = (struct connection *) data;
struct msghdr msg;
struct iovec iov;
if (!c)
return -ENOENT;
iov.iov_base = (void *)buffer;
iov.iov_len = (__kernel_size_t) count;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = MSG_DONTWAIT;
return sock_sendmsg(c->socket, &msg, count);
}
static int control_read(char *buffer, char **buffer_location, off_t offset, int count, int *eof, void *data) {
return 0;
}
static int control_write(struct file *file, const char *buffer, unsigned long count, void *data) {
#define HAMMER__MAX_INPUT 512
char parse_me[HAMMER__MAX_INPUT + 1];
char *p;
memset(parse_me,'\0',sizeof(parse_me));
if (count > HAMMER__MAX_INPUT)
count = HAMMER__MAX_INPUT;
if (copy_from_user(parse_me,buffer,count) < 0)
return -EFAULT;
p = strchr(parse_me,'\n');
if (p)
*p = '\0';
connect_to(parse_me);
#undef HAMMER__MAX_INPUT
return count;
}
static int inet_addr(char* ip_port, unsigned int *dest,unsigned short *port) {
int a, b, c, d, e;
char addr[4];
if (sscanf(ip_port, "%d.%d.%d.%d:%d", &a, &b, &c, &d,&e) == 5) {
addr[0] = a;
addr[1] = b;
addr[2] = c;
addr[3] = d;
*dest = *(unsigned int *)addr;
*port = htons(e);
return 0;
}
return -1;
}
static void hammer_sk_data_ready (struct sock *sk, int bytes) {
void (*ready)(struct sock *, int);
struct connection *c = (struct connection *) sk->sk_user_data;
read_descriptor_t desc;
read_lock_bh(&sk->sk_callback_lock);
ready = c->orig_sk_data_ready;
desc.arg.data = c;
desc.error = 0;
desc.count = 1;
tcp_read_sock(sk, &desc, hammer_tcp_data_recv);
read_unlock_bh(&sk->sk_callback_lock);
(*ready)(sk,bytes);
}
static void collect_http_stats(struct connection *c, char *buf, size_t len) {
union http_status_line *status = (union http_status_line *) (buf);
unsigned short code;
if (len < sizeof(HTTP) ||
status->u64_u32.http_slash_one_dot_one != HTTP.u64_u32.http_slash_one_dot_one)
return;
code = three_digits_to_u16(&status->u64_u32.u32.structed.digit_0);
c->stats.http_code[code > 599 ? 0 : code]++;
D("found http packet with code: %d",code);
}
inline static unsigned short three_digits_to_u16(char *digits) {
#define digit_or_zero(d) (((d) >= '0' && (d) <= '9') ? (d) : 0)
return (unsigned short ) (100 * (digit_or_zero(digits[0]) - '0')) + (10 * ( digit_or_zero(digits[1]) - '0')) + (digit_or_zero(digits[2]) - '0');
#undef digit_or_zero
}
static int hammer_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb,unsigned int offset, size_t len) {
struct connection *c = (struct connection *) desc->arg.data;
#ifdef HAMMER_OVERWRITE
size_t to_copy = MIN(len,sizeof(c->output));
#endif
if (len == 0)
return 0;
#ifdef HAMMER_OVERWRITE
skb_copy_bits(skb, offset, c->output,to_copy);
collect_http_stats(c, c->output, to_copy);
c->output_len = to_copy;
#else
c->output = krealloc(c->output, c->output_len + len, GFP_ATOMIC);
if (c->output) {
skb_copy_bits(skb, offset, (c->output + c->output_len), len);
collect_http_stats(c, (c->output + c->output_len), len);
c->output_len += len;
}
#endif
c->proc[PROC_DATA].entry->size += len;
return len;
}
void register_callbacks(struct connection *c) {
struct sock *sk = c->socket->sk;
write_lock_bh(&sk->sk_callback_lock);
c->orig_sk_data_ready = sk->sk_data_ready;
c->orig_sk_user_data = sk->sk_user_data;
sk->sk_data_ready = hammer_sk_data_ready;
sk->sk_user_data = c;
write_unlock_bh(&sk->sk_callback_lock);
}
void unregister_callbacks(struct connection *c) {
struct sock *sk = c->socket->sk;
write_lock_bh(&sk->sk_callback_lock);
sk->sk_data_ready = c->orig_sk_data_ready;
sk->sk_user_data = c->orig_sk_user_data;
write_unlock_bh(&sk->sk_callback_lock);
}
static struct connection * connect_to(char *host) {
struct sockaddr_in si;
int rc = 0;
struct connection *c = kmalloc(sizeof(*c), GFP_KERNEL);
if (!c)
goto bad;
memset(c,0,sizeof(*c));
memset(&si, 0, sizeof(si));
si.sin_family = AF_INET;
if (inet_addr(host,&si.sin_addr.s_addr,&si.sin_port) < 0)
goto bad;
snprintf(c->proc[PROC_DATA].name,sizeof(c->proc[PROC_DATA].name),"c_%s_%p",host,c);
snprintf(c->proc[PROC_STAT].name,sizeof(c->proc[PROC_STAT].name),"s_%s_%p",host,c);
c->proc[PROC_DATA].entry = procify(c->proc[PROC_DATA].name,(void *) c, tail, hammer);
if (!c->proc[PROC_DATA].entry)
goto bad;
c->proc[PROC_STAT].entry = procify(c->proc[PROC_STAT].name,(void *) c, stats, NULL);
if (!c->proc[PROC_STAT].entry)
goto bad;
if ((rc = sock_create(AF_INET,SOCK_STREAM,IPPROTO_TCP,&c->socket)) < 0)
goto bad;
#ifdef SK_CAN_REUSE
c->socket->sk->sk_reuse = SK_CAN_REUSE;
#endif
if ((rc = kernel_connect(c->socket, (struct sockaddr *)&si, sizeof(si),0)) < 0)
goto bad;
spin_lock(&giant);
list_add(&c->list,&connections);
spin_unlock(&giant);
c->state = CONNECTED;
register_callbacks(c);
D("connected to %s",host);
return c;
bad:
if (c) {
if (c->socket)
sock_release(c->socket);
cleanup_procfs_entries(c);
kfree(c);
}
D("error creating socket for %s rc: %d",host,rc);
return NULL;
}
static void cleanup_procfs_entries(struct connection *c) {
int i;
for (i = 0; i < sizeof(c->proc)/sizeof(c->proc[0]); i++)
if (c->proc[i].entry)
remove_proc_entry(c->proc[i].name,root);
}
static void disconnect(struct connection *c) {
if (c->state & DISCONNECTED)
return;
c->state = DISCONNECTED;
unregister_callbacks(c);
#ifdef kernel_sock_shutdown
kernel_sock_shutdown(c->socket, SHUT_RDWR);
#else
c->socket->ops->shutdown(c->socket, SHUT_RDWR);
#endif
}
struct proc_dir_entry *procify(char *name, void *data,
int (*reader)(char *, char **, off_t, int, int *, void *),
int (*writer)(struct file *, const char *, unsigned long, void *)) {
struct proc_dir_entry *p = create_proc_entry(name,0644,root);
if (p != NULL) {
p->read_proc = reader;
p->write_proc = writer;
p->data = data;
p->mode = S_IFREG | S_IRUGO;
p->uid = 0;
p->gid = 0;
p->size = 0;
}
return p;
}
int init_module() {
char *http_status_line = "HTTP/1.1 000";
spin_lock_init(&giant);
memcpy(HTTP.u8,http_status_line,sizeof(HTTP.u8));
root = proc_mkdir(MODULE_NAME, NULL);
if (root == NULL)
return -ENOMEM;
control = procify(CONTROL_NAME,NULL,control_read,control_write);
return 0;
}
void cleanup_module() {
struct list_head *pos,*q;
struct connection *c;
D("cleaning up connections");
spin_lock(&giant);
list_for_each_safe(pos, q, &connections) {
c = list_entry(pos, struct connection, list);
list_del(&c->list);
disconnect(c);
#ifndef HAMMER_OVERWRITE
if (c->output)
kfree(c->output);
#endif
cleanup_procfs_entries(c);
kfree(c);
}
spin_unlock(&giant);
if (control)
remove_proc_entry(CONTROL_NAME,root);
remove_proc_entry(MODULE_NAME, NULL);
}