Repository navigation
Expand file tree
/
Copy pathkmod_comp.c
More file actions
434 lines (339 loc) · 12 KB
/
Copy pathkmod_comp.c
File metadata and controls
434 lines (339 loc) · 12 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright 2013,2016 Didier Barvaux
* Copyright 2013,2014 Mikhail Gruzdev
* Copyright 2009,2010 Thales Communications
* Copyright 2013,2014 Viveris Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file kmod_netfilter
* @brief Export the ROHC library to the Linux kernel
* @author Suho CHOI <aiek261247@gmail.com>
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/net_namespace.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/uaccess.h>
#include <linux/proc_fs.h>
#include "config.h"
#include "rohc.h"
#include "rohc_comp.h"
#include "rohc_decomp.h"
#define BUFFER_SIZE 10241
struct rohc_init {
struct rohc_comp *compressor;
struct rohc_decomp *decompressor;
unsigned char *rohc_packet_out; // comp ROHC packet
unsigned char *rohc_packet_in; // ROHC packet to decomp
unsigned char *feedback_to_send_buf; // feedback to send decomp
unsigned char *rcvd_feedback_buf; // comp feedback rcvd
unsigned char *ip_pkt_out; // decomp IP packet
unsigned char *ip_pkt_in; // comp IP packet
struct rohc_buf feedback_to_send; // feedback to send decomp with the ROHC by
size_t rohc_out_size; // comp ROHC packet
size_t ip_out_size; // decomp IP packet
};
static struct rohc_init rinit;
static struct nf_hook_ops nfho;
static int gen_false_random_num(const struct rohc_comp *const comp,
void *const user_context);
static void rohc_print_traces(void *const priv_ctxt __attribute__((unused)),
const rohc_trace_level_t level,
const rohc_trace_entity_t entity,
const int profile,
const char *const format,
...)
{
va_list args;
va_start(args, format);
vprintk(format, args);
va_end(args);
}
int rohc_comp_init(struct rohc_init *rcouple,
struct sk_buff *skb,
struct iphdr *ih) {
pr_info("ROHC_COMP_INIT\n");
memset(rcouple, 0, sizeof(struct rohc_init));
rcouple->rohc_packet_out = kzalloc(BUFFER_SIZE, GFP_KERNEL);
if(rcouple->rohc_packet_out == NULL)
goto free_comp;
rcouple->rcvd_feedback_buf = kzalloc(BUFFER_SIZE, GFP_KERNEL);
if(rcouple->rcvd_feedback_buf == NULL)
return NF_ACCEPT;
rcouple->feedback_to_send_buf = kzalloc(BUFFER_SIZE, GFP_KERNEL);
if(rcouple->feedback_to_send_buf == NULL)
return NF_ACCEPT;
struct rohc_buf rohc_packet = rohc_buf_init_empty(rcouple->rohc_packet_out, BUFFER_SIZE);
struct rohc_buf ip_packet = rohc_buf_init_full(skb->data, ntohs(ih->tot_len), 0);
rohc_status_t status;
rcouple->rohc_out_size = 0;
rcouple->compressor = rohc_comp_new2(ROHC_SMALL_CID, ROHC_SMALL_CID_MAX,
gen_false_random_num, NULL);
if (rcouple->compressor == NULL) {
pr_info("failed create the ROHC compressor\n");
goto free_comp;
}
if(!rohc_comp_set_traces_cb2(rcouple->compressor, rohc_print_traces, NULL)) {
pr_info("cannot set trace callback for compressor\n");
goto free_comp;
}
if(!rohc_comp_set_features(rcouple->compressor, ROHC_COMP_FEATURE_DUMP_PACKETS)) {
pr_info("failed to enable packet dumps\n");
goto free_comp;
}
if(!rohc_comp_enable_profile(rcouple->compressor, ROHC_PROFILE_TCP)) {
pr_info("failed to enable the TCP profile\n");
goto free_comp;
}
rcouple->feedback_to_send.time.sec = 0;
rcouple->feedback_to_send.time.nsec = 0;
rcouple->feedback_to_send.data = rcouple->feedback_to_send_buf;
rcouple->feedback_to_send.max_len = BUFFER_SIZE;
rcouple->feedback_to_send.offset = 0;
rcouple->feedback_to_send.len = 0;
rohc_buf_append_buf(&rohc_packet, rcouple->feedback_to_send);
rohc_buf_pull(&rohc_packet, rcouple->feedback_to_send.len);
status = rohc_compress4(rcouple->compressor, ip_packet, &rohc_packet);
if (status == ROHC_STATUS_OK) {
pr_info("ROHC Compression\n");
}
else {
pr_info("Compression failed\n");
goto free_comp;
}
rohc_buf_push(&rohc_packet, rcouple->feedback_to_send.len);
rcouple->rohc_out_size = rohc_packet.len;
pr_info("Compression Packet len = %u", rcouple->rohc_out_size);
rohc_buf_reset(&rcouple->feedback_to_send);
return 0;
free_pkt_out:
kfree(rcouple->rohc_packet_out);
free_rcvd_feedback:
kfree(rcouple->rcvd_feedback_buf);
free_feedback_send:
kfree(rcouple->feedback_to_send_buf);
free_comp:
rohc_comp_free(rcouple->compressor);
return NF_ACCEPT;
}
int rohc_decomp_init(struct rohc_init *rcouple,
struct sk_buff *skb,
struct iphdr *ih) {
pr_info("ROHC_DECOMP_INIT\n");
rcouple->rohc_packet_in = kzalloc(BUFFER_SIZE, GFP_KERNEL);
if(rcouple->rohc_packet_in == NULL)
goto free_pkt_in;
//struct rohc_buf rohc_packet = rohc_buf_init_full(skb->data, ntohs(ih->tot_len), 0);
struct rohc_buf rohc_packet = rohc_buf_init_full(rcouple->rohc_packet_out,
ntohs(ih->tot_len), 0);
struct rohc_buf ip_packet = rohc_buf_init_empty(rcouple->rohc_packet_in, BUFFER_SIZE);
struct rohc_buf rcvd_feedback = rohc_buf_init_empty(rcouple->rcvd_feedback_buf,
BUFFER_SIZE);
struct rohc_buf *feedback_to_send = &rcouple->feedback_to_send;
struct rohc_comp *comp_associated = rcouple->compressor;
rohc_status_t status;
rcouple->ip_out_size = 0;
rcouple->decompressor = rohc_decomp_new2(ROHC_SMALL_CID, ROHC_SMALL_CID_MAX,
ROHC_O_MODE);
if (rcouple->decompressor == NULL) {
pr_info("failed create the ROHC compressor\n");
goto free_decomp;
}
if(!rohc_decomp_set_traces_cb2(rcouple->decompressor, rohc_print_traces, NULL)) {
pr_info("cannot set trace callback for compressor\n");
goto free_decomp;
}
if(!rohc_decomp_set_features(rcouple->decompressor, ROHC_DECOMP_FEATURE_DUMP_PACKETS)) {
pr_info("failed to enable packet dumps\n");
goto free_decomp;
}
if(!rohc_decomp_enable_profile(rcouple->decompressor, ROHC_PROFILE_TCP)) {
pr_info("failed to enable the TCP profile\n");
goto free_decomp;
}
status = rohc_decompress3(rcouple->decompressor, rohc_packet, &ip_packet,
&rcvd_feedback, feedback_to_send);
if(status == ROHC_STATUS_OK) {
pr_info("ROHC Decompression\n");
}
else {
pr_info("ROHC decomp failed\n");
goto free_decomp;
}
rcouple->ip_out_size = ip_packet.len;
pr_info("Decompression Packet len = %u", rcouple->ip_out_size);
if(!rohc_comp_deliver_feedback2(comp_associated, rcvd_feedback)) {
pr_info("failed to deliver received feedback to comp.\n");
goto free_decomp;
}
return 0;
free_pkt_in:
kfree(rcouple->rohc_packet_in);
free_decomp:
rohc_decomp_free(rcouple->decompressor);
return NF_ACCEPT;
}
static unsigned int hook_init (void *priv,
struct sk_buff *skb,
const struct nf_hook_state *state) {
struct iphdr *iph;
struct tcphdr *tph;
struct iphdr *ih;
iph = ip_hdr(skb);
tph = tcp_hdr(skb);
ih = skb_header_pointer(skb, iph->frag_off, sizeof(iph), &iph);
if (ih == NULL) {
pr_info("TRUNCATED\n");
return NF_ACCEPT;
}
if (iph->protocol == IPPROTO_TCP) {
int i;
pr_info("Origin IP LEN=%u TTL=%u ID=%u DATA=%u",
ntohs(ih->tot_len), ih->ttl, ntohs(ih->id), skb->data);
pr_info("ROHC COMP / DECOMP INIT Start\n");
i = rohc_comp_init(&rinit, skb, ih);
if (i != 0) {
pr_info("failed to init ROHC Compressor\n");
return NF_ACCEPT;
}
i = rohc_decomp_init(&rinit, skb, ih);
if (i != 0) {
pr_info("failed to init ROHC Decompressor\n");
return NF_ACCEPT;
}
return NF_ACCEPT;
}
return NF_ACCEPT;
}
static int gen_false_random_num(const struct rohc_comp *const comp,
void *const user_context) {
return 0;
}
static int my_comp(void) {
nfho.hook = hook_init;
//nfho.hooknum = NF_INET_POST_ROUTING; // hook in ip_finish_output()
nfho.hooknum = NF_INET_LOCAL_IN;
nfho.pf = NFPROTO_IPV4;
nfho.priority = NF_IP_PRI_FIRST;
nfho.priv = NULL;
nf_register_net_hook(&init_net, &nfho);
return 0;
}
static void my_comp_exit(void) {
nf_unregister_net_hook(&init_net, &nfho);
}
module_init(my_comp);
module_exit(my_comp_exit);
MODULE_VERSION(PACKAGE_VERSION PACKAGE_REVNO);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Didier Barvaux, Mikhail Gruzdev, Thales Communications, Viveris Technologies");
MODULE_DESCRIPTION(PACKAGE_NAME
", version " PACKAGE_VERSION PACKAGE_REVNO " (" PACKAGE_URL ")");
/*
* General API
*/
EXPORT_SYMBOL_GPL(rohc_version);
EXPORT_SYMBOL_GPL(rohc_get_mode_descr);
EXPORT_SYMBOL_GPL(rohc_get_profile_descr);
EXPORT_SYMBOL_GPL(rohc_profile_is_rohcv1);
EXPORT_SYMBOL_GPL(rohc_profile_is_rohcv2);
EXPORT_SYMBOL_GPL(rohc_profile_get_other_version);
EXPORT_SYMBOL_GPL(rohc_get_packet_descr);
EXPORT_SYMBOL_GPL(rohc_get_ext_descr);
EXPORT_SYMBOL_GPL(rohc_get_packet_type);
EXPORT_SYMBOL_GPL(rohc_packet_is_ir);
EXPORT_SYMBOL_GPL(rohc_packet_carry_static_info);
EXPORT_SYMBOL_GPL(rohc_packet_carry_crc_7_or_8);
EXPORT_SYMBOL_GPL(rohc_buf_is_malformed);
EXPORT_SYMBOL_GPL(rohc_buf_is_empty);
EXPORT_SYMBOL_GPL(rohc_buf_push);
EXPORT_SYMBOL_GPL(rohc_buf_pull);
EXPORT_SYMBOL_GPL(rohc_buf_avail_len);
EXPORT_SYMBOL_GPL(rohc_buf_data_at);
EXPORT_SYMBOL_GPL(rohc_buf_data);
EXPORT_SYMBOL_GPL(rohc_buf_prepend);
EXPORT_SYMBOL_GPL(rohc_buf_append);
EXPORT_SYMBOL_GPL(rohc_buf_append_buf);
EXPORT_SYMBOL_GPL(rohc_buf_reset);
/*
* Compression API
*/
/* general */
EXPORT_SYMBOL_GPL(rohc_comp_new2);
EXPORT_SYMBOL_GPL(rohc_comp_free);
EXPORT_SYMBOL_GPL(rohc_compress4);
EXPORT_SYMBOL_GPL(rohc_comp_pad);
EXPORT_SYMBOL_GPL(rohc_comp_force_contexts_reinit);
/* segment */
EXPORT_SYMBOL_GPL(rohc_comp_get_segment2);
/* feedback */
EXPORT_SYMBOL_GPL(rohc_comp_deliver_feedback2);
/* statistics */
EXPORT_SYMBOL_GPL(rohc_comp_get_state_descr);
EXPORT_SYMBOL_GPL(rohc_comp_get_general_info);
EXPORT_SYMBOL_GPL(rohc_comp_get_last_packet_info2);
/* configuration */
EXPORT_SYMBOL_GPL(rohc_comp_profile_enabled);
EXPORT_SYMBOL_GPL(rohc_comp_enable_profile);
EXPORT_SYMBOL_GPL(rohc_comp_disable_profile);
EXPORT_SYMBOL_GPL(rohc_comp_enable_profiles);
EXPORT_SYMBOL_GPL(rohc_comp_disable_profiles);
EXPORT_SYMBOL_GPL(rohc_comp_set_mrru);
EXPORT_SYMBOL_GPL(rohc_comp_get_mrru);
EXPORT_SYMBOL_GPL(rohc_comp_get_max_cid);
EXPORT_SYMBOL_GPL(rohc_comp_get_cid_type);
EXPORT_SYMBOL_GPL(rohc_comp_set_wlsb_window_width);
EXPORT_SYMBOL_GPL(rohc_comp_set_reorder_ratio);
EXPORT_SYMBOL_GPL(rohc_comp_set_periodic_refreshes);
EXPORT_SYMBOL_GPL(rohc_comp_set_periodic_refreshes_time);
EXPORT_SYMBOL_GPL(rohc_comp_set_traces_cb2);
EXPORT_SYMBOL_GPL(rohc_comp_set_features);
/* RTP-specific configuration */
EXPORT_SYMBOL_GPL(rohc_comp_set_rtp_detection_cb);
/*
* Decompression API
*/
/* general */
EXPORT_SYMBOL_GPL(rohc_decomp_new2);
EXPORT_SYMBOL_GPL(rohc_decomp_free);
EXPORT_SYMBOL_GPL(rohc_decompress3);
/* statistics */
EXPORT_SYMBOL_GPL(rohc_decomp_get_state_descr);
EXPORT_SYMBOL_GPL(rohc_decomp_get_general_info);
EXPORT_SYMBOL_GPL(rohc_decomp_get_context_info);
EXPORT_SYMBOL_GPL(rohc_decomp_get_last_packet_info);
/* configuration */
EXPORT_SYMBOL_GPL(rohc_decomp_profile_enabled);
EXPORT_SYMBOL_GPL(rohc_decomp_enable_profile);
EXPORT_SYMBOL_GPL(rohc_decomp_disable_profile);
EXPORT_SYMBOL_GPL(rohc_decomp_enable_profiles);
EXPORT_SYMBOL_GPL(rohc_decomp_disable_profiles);
EXPORT_SYMBOL_GPL(rohc_decomp_get_cid_type);
EXPORT_SYMBOL_GPL(rohc_decomp_get_max_cid);
EXPORT_SYMBOL_GPL(rohc_decomp_set_mrru);
EXPORT_SYMBOL_GPL(rohc_decomp_get_mrru);
EXPORT_SYMBOL_GPL(rohc_decomp_set_rate_limits);
EXPORT_SYMBOL_GPL(rohc_decomp_get_rate_limits);
EXPORT_SYMBOL_GPL(rohc_decomp_set_prtt);
EXPORT_SYMBOL_GPL(rohc_decomp_get_prtt);
EXPORT_SYMBOL_GPL(rohc_decomp_set_traces_cb2);
EXPORT_SYMBOL_GPL(rohc_decomp_set_features);