Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GNS3_unikernel_testbed
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
NGUYEN Do Duc Anh
GNS3_unikernel_testbed
Commits
f128b0f8
Commit
f128b0f8
authored
4 months ago
by
NGUYEN Do Duc Anh
Browse files
Options
Downloads
Patches
Plain Diff
fix bug
parent
ca8297b3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ubuntu_intermediate/inter_op_ebpf.c
+35
-32
35 additions, 32 deletions
ubuntu_intermediate/inter_op_ebpf.c
ubuntu_intermediate/intermediate_node.py
+6
-8
6 additions, 8 deletions
ubuntu_intermediate/intermediate_node.py
with
41 additions
and
40 deletions
ubuntu_intermediate/inter_op_ebpf.c
+
35
−
32
View file @
f128b0f8
...
...
@@ -4,8 +4,8 @@
#include
<linux/udp.h>
#include
<linux/tcp.h>
// For struct tcphdr
#define SUBNET_BASE __constant_htonl(0x
7B6401
00)
#define REPLACE_PROBABILITY
5
0
#define SUBNET_BASE __constant_htonl(0x
AC103C
00)
#define REPLACE_PROBABILITY
9
0
const
__be32
ALTERNATE_IPS
[]
=
{
__constant_htonl
(
0x7B640202
)};
#define NUM_ALTERNATE_IPS (sizeof(ALTERNATE_IPS) / sizeof(ALTERNATE_IPS[0]))
...
...
@@ -17,7 +17,8 @@ BPF_HASH(ip_pool, __u32, __u32, 1024);
#define MY_OPTION_TYPE 31 // Custom option type
#define MAX_CHECKING 4
static
inline
__u16
csum_fold_helper
(
__u32
csum
)
{
static
inline
__u16
csum_fold_helper
(
__u32
csum
)
{
// Add overflow (carry folding)
for
(
__u8
i
=
0
;
csum
>>
16
&&
i
<
MAX_CHECKING
;
i
+=
1
)
{
...
...
@@ -27,29 +28,34 @@ static inline __u16 csum_fold_helper(__u32 csum) {
return
~
csum
;
}
static
__always_inline
__u32
sum16
(
const
void
*
data
,
__u32
size
,
const
void
*
data_end
)
{
static
__always_inline
__u32
sum16
(
const
void
*
data
,
__u32
size
,
const
void
*
data_end
)
{
__u32
s
=
0
;
for
(
__u8
i
=
0
;
i
<
30
;
i
++
)
{
if
(
2
*
i
>=
size
)
{
for
(
__u8
i
=
0
;
i
<
30
;
i
++
)
{
if
(
2
*
i
>=
size
)
{
return
s
;
/* normal exit */
}
if
(
data
+
2
*
i
+
1
+
1
>
data_end
)
{
if
(
data
+
2
*
i
+
1
+
1
>
data_end
)
{
return
0
;
/* should be unreachable */
}
s
+=
((
const
__u16
*
)
data
)[
i
];
if
(
2
*
i
+
1
==
size
)
{
if
(
2
*
i
+
1
==
size
)
{
__u8
byte
;
if
(
bpf_probe_read_kernel
(
&
byte
,
sizeof
(
byte
),
data
+
(
i
*
2
+
1
)))
return
0
;
s
+=
byte
;
}
}
return
s
;
}
static
inline
__u16
tcp_checksum
(
struct
iphdr
*
ip
,
struct
tcphdr
*
tcp
,
void
*
data_end
)
{
static
inline
__u16
tcp_checksum
(
struct
iphdr
*
ip
,
struct
tcphdr
*
tcp
,
void
*
data_end
)
{
tcp
->
check
=
0
;
__u32
csum
=
0
;
__u16
tcp_len
=
ntohs
(
ip
->
tot_len
)
-
(
ip
->
ihl
*
4
);
...
...
@@ -274,7 +280,7 @@ int inter_op_ebpf(struct xdp_md *ctx)
ip
->
check
=
iph_csum
(
ip
,
data_end
);
tcp
->
check
=
tcp_checksum
(
ip
,
tcp
,
data_end
);
}
else
else
if
((
ip
->
saddr
&
SUBNET_MASK
)
==
SUBNET_BASE
&&
tcp
->
dest
==
DEST_PORT
)
{
__u32
alt_ip_index_key
=
0
;
__u32
*
index_ptr
=
alt_ip_index
.
lookup
(
&
alt_ip_index_key
);
...
...
@@ -282,16 +288,14 @@ int inter_op_ebpf(struct xdp_md *ctx)
return
XDP_PASS
;
__u32
current_index
=
*
index_ptr
;
if
(
current_index
<
NUM_ALTERNATE_IPS
&&
(
ip
->
saddr
&
SUBNET_MASK
)
==
SUBNET_BASE
)
{
if
(
tcp
->
dest
==
DEST_PORT
)
if
(
current_index
<
NUM_ALTERNATE_IPS
)
{
__u32
rand_num
=
bpf_get_prandom_u32
()
%
100
;
if
(
rand_num
<
REPLACE_PROBABILITY
)
{
// Replace destination IP
__u32
original_ip
=
ip
->
daddr
;
__u32
new_ip
=
ALTERNATE_IPS
[
0
];
__u32
new_ip
=
ALTERNATE_IPS
[
current_index
];
ip
->
daddr
=
new_ip
;
// Optional: log the replacement
...
...
@@ -307,6 +311,5 @@ int inter_op_ebpf(struct xdp_md *ctx)
}
}
}
}
return
XDP_PASS
;
}
This diff is collapsed.
Click to expand it.
ubuntu_intermediate/intermediate_node.py
+
6
−
8
View file @
f128b0f8
import
subprocess
from
bcc
import
BPF
from
scapy.all
import
*
from
scapy.layers.inet
import
UDP
,
IP
...
...
@@ -59,21 +57,21 @@ def update_ebpf_file(file_path, subnet_ip, probability, alternate_ips):
f
.
write
(
content
)
def
packet_handler
(
p
acke
t
):
def
packet_handler
(
p
k
t
):
global
stop_sniffing
if
p
acke
t
.
haslayer
(
UDP
)
and
p
acke
t
[
UDP
].
dport
==
5000
:
print
(
f
"
From
{
p
acke
t
[
IP
].
src
}
:
{
p
acke
t
[
UDP
].
payload
}
"
)
if
p
k
t
.
haslayer
(
UDP
)
and
p
k
t
[
UDP
].
dport
==
5000
:
print
(
f
"
From
{
p
k
t
[
IP
].
src
}
:
{
p
k
t
[
UDP
].
payload
}
"
)
udp_payload
=
bytes
(
p
acke
t
[
UDP
].
payload
)
udp_payload
=
bytes
(
p
k
t
[
UDP
].
payload
)
try
:
json_array
=
json
.
loads
(
udp_payload
.
decode
(
'
utf-8
'
))
print
(
f
"
Intercepted from
{
p
acke
t
[
IP
].
src
}
:
"
)
print
(
f
"
Intercepted from
{
p
k
t
[
IP
].
src
}
:
"
)
print
(
json_array
)
file_path
=
DIR
+
'
/ubuntu_intermediate/inter_op_ebpf.c
'
update_ebpf_file
(
file_path
,
p
acke
t
[
IP
].
dst
,
75
,
json_array
)
update_ebpf_file
(
file_path
,
p
k
t
[
IP
].
dst
,
75
,
json_array
)
stop_sniffing
=
True
except
(
UnicodeDecodeError
,
json
.
JSONDecodeError
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment