Fix ip checksum calculation

master
Jiajie Chen 6 years ago
parent 67890aeb42
commit 0dc44ae9cb

@ -45,12 +45,14 @@ ucore:
biscuit: biscuit:
ifneq ($(shell uname)-$(arch), Darwin-riscv64) ifneq ($(shell uname)-$(arch), Darwin-riscv64)
ifneq ($(shell uname)-$(arch), Darwin-aarch64)
@echo Building biscuit programs @echo Building biscuit programs
@mkdir -p biscuit/build @mkdir -p biscuit/build
@cd biscuit/build && cmake $(cmake_build_args) .. && make @cd biscuit/build && cmake $(cmake_build_args) .. && make
@rm -rf $(out_dir)/biscuit && mkdir -p $(out_dir)/biscuit @rm -rf $(out_dir)/biscuit && mkdir -p $(out_dir)/biscuit
@cp $(biscuit_bin_path)/* $(out_dir)/biscuit @cp $(biscuit_bin_path)/* $(out_dir)/biscuit
endif endif
endif
$(busybox): $(busybox):
ifeq ($(arch), x86_64) ifeq ($(arch), x86_64)

@ -32,7 +32,8 @@ pub fn main() {
color_text!("!", 97), color_text!("!", 97),
); );
let text = "reguler \x1b[4munderline\x1b[24m \x1b[7mreverse\x1b[27m \x1b[9mstrikethrough\x1b[29m"; let text =
"reguler \x1b[4munderline\x1b[24m \x1b[7mreverse\x1b[27m \x1b[9mstrikethrough\x1b[29m";
println!("\x1b[47m{}\x1b[0m", color_text!(text, 30)); println!("\x1b[47m{}\x1b[0m", color_text!(text, 30));
for i in 31..38 { for i in 31..38 {
println!("{}", color_text!(text, i)); println!("{}", color_text!(text, i));

@ -7,7 +7,6 @@ extern crate rcore_user;
use rcore_user::syscall::*; use rcore_user::syscall::*;
// IMPORTANT: Must define main() like this // IMPORTANT: Must define main() like this
#[no_mangle] #[no_mangle]
pub fn main() { pub fn main() {

@ -4,9 +4,9 @@
#[macro_use] #[macro_use]
extern crate rcore_user; extern crate rcore_user;
use rcore_user::syscall::{sys_socket, sys_read, sys_setsockopt, sys_sendto, sys_ioctl};
use core::mem::size_of;
use core::default::Default; use core::default::Default;
use core::mem::size_of;
use rcore_user::syscall::{sys_ioctl, sys_read, sys_sendto, sys_setsockopt, sys_socket};
#[repr(C)] #[repr(C)]
#[derive(Default)] #[derive(Default)]
@ -33,7 +33,7 @@ pub struct SockAddrIn {
#[derive(Default)] #[derive(Default)]
pub struct SockAddrHa { pub struct SockAddrHa {
sha_family: u16, sha_family: u16,
sha_data: [u8; 14] sha_data: [u8; 14],
} }
#[repr(C)] #[repr(C)]
@ -43,7 +43,7 @@ struct ArpReq {
arp_ha: SockAddrHa, arp_ha: SockAddrHa,
arp_flags: u32, arp_flags: u32,
arp_netmask: SockAddrIn, arp_netmask: SockAddrIn,
arp_dev: [u8; 16] arp_dev: [u8; 16],
} }
// IMPORTANT: Must define main() like this // IMPORTANT: Must define main() like this
@ -59,12 +59,18 @@ pub unsafe fn main() {
let mut buffer = [0u8; 2048]; let mut buffer = [0u8; 2048];
// set header included // set header included
let included = 1u32; let included = 1u32;
sys_setsockopt(capture_fd as usize, 0, 3, &included as *const u32 as usize, 4); sys_setsockopt(
capture_fd as usize,
0,
3,
&included as *const u32 as usize,
4,
);
let mut addr: SockAddrLl = Default::default(); let mut addr: SockAddrLl = Default::default();
// packet // packet
addr.sll_family = 17; addr.sll_family = 17;
const ETHER_HEADER_LEN: usize = 6+6+2; const ETHER_HEADER_LEN: usize = 6 + 6 + 2;
// ethertype 0x0800 ipv4 // ethertype 0x0800 ipv4
buffer[12] = 0x08; buffer[12] = 0x08;
buffer[13] = 0x00; buffer[13] = 0x00;
@ -74,17 +80,21 @@ pub unsafe fn main() {
arp.arp_pa.sin_family = 2; arp.arp_pa.sin_family = 2;
arp.arp_dev[0..9].copy_from_slice(&[b'e', b'n', b'p', b'0', b's', b'4', b'f', b'0', 0]); arp.arp_dev[0..9].copy_from_slice(&[b'e', b'n', b'p', b'0', b's', b'4', b'f', b'0', 0]);
loop { loop {
let len = sys_read(capture_fd as usize, buffer.as_mut_ptr().add(ETHER_HEADER_LEN), buffer.len()) as usize; let len = sys_read(
capture_fd as usize,
buffer.as_mut_ptr().add(ETHER_HEADER_LEN),
buffer.len(),
) as usize;
println!("Got packet of len {}", len); println!("Got packet of len {}", len);
if ETHER_HEADER_LEN + len > 20 && buffer[ETHER_HEADER_LEN + 0] == 0x45 { if ETHER_HEADER_LEN + len > 20 && buffer[ETHER_HEADER_LEN + 0] == 0x45 {
// ipv4 // ipv4
let ttl = buffer[ETHER_HEADER_LEN + 8]; let ttl = buffer[ETHER_HEADER_LEN + 8];
if ttl > 1 { if ttl > 1 {
println!("ttl {}", ttl); println!("ttl {}", ttl);
let dst_ip = (buffer[ETHER_HEADER_LEN + 19] as u32) << 24 | let dst_ip = (buffer[ETHER_HEADER_LEN + 19] as u32) << 24
(buffer[ETHER_HEADER_LEN + 18] as u32) << 16 | | (buffer[ETHER_HEADER_LEN + 18] as u32) << 16
(buffer[ETHER_HEADER_LEN + 17] as u32) << 8 | | (buffer[ETHER_HEADER_LEN + 17] as u32) << 8
(buffer[ETHER_HEADER_LEN + 16] as u32); | (buffer[ETHER_HEADER_LEN + 16] as u32);
println!("dst_ip {:#X}", dst_ip); println!("dst_ip {:#X}", dst_ip);
arp.arp_pa.sin_addr = dst_ip; arp.arp_pa.sin_addr = dst_ip;
// SIOCGARP // SIOCGARP
@ -96,11 +106,19 @@ pub unsafe fn main() {
buffer[0..6].copy_from_slice(&[0, 0x16, 0x31, 0xFF, 0xA4, 0x9F]); buffer[0..6].copy_from_slice(&[0, 0x16, 0x31, 0xFF, 0xA4, 0x9F]);
buffer[6..12].copy_from_slice(&arp.arp_ha.sha_data[0..6]); buffer[6..12].copy_from_slice(&arp.arp_ha.sha_data[0..6]);
buffer[ETHER_HEADER_LEN + 8] = ttl - 1; buffer[ETHER_HEADER_LEN + 8] = ttl - 1;
let checksum = (buffer[ETHER_HEADER_LEN + 10] as u16) << 8 | (buffer[ETHER_HEADER_LEN + 11] as u16); let checksum = (buffer[ETHER_HEADER_LEN + 10] as u16) << 8
buffer[ETHER_HEADER_LEN + 10] = ((checksum + 1) >> 8) as u8; | (buffer[ETHER_HEADER_LEN + 11] as u16);
buffer[ETHER_HEADER_LEN + 11] = checksum as u8; buffer[ETHER_HEADER_LEN + 10] = ((checksum + 0x0100) >> 8) as u8;
buffer[ETHER_HEADER_LEN + 11] = (checksum + 0x0100) as u8;
addr.sll_ifindex = 0; addr.sll_ifindex = 0;
sys_sendto(packet as usize, buffer.as_ptr(), len as usize + ETHER_HEADER_LEN, 0, &addr as *const SockAddrLl as usize, size_of::<SockAddrLl>()); sys_sendto(
packet as usize,
buffer.as_ptr(),
len as usize + ETHER_HEADER_LEN,
0,
&addr as *const SockAddrLl as usize,
size_of::<SockAddrLl>(),
);
} }
} }
} }

@ -10,7 +10,7 @@ use alloc::vec::Vec;
use core::ptr; use core::ptr;
use rcore_user::io::get_line; use rcore_user::io::get_line;
use rcore_user::syscall::{sys_exec, sys_vfork, sys_wait, sys_getcwd, sys_chdir, sys_access}; use rcore_user::syscall::{sys_access, sys_chdir, sys_exec, sys_getcwd, sys_vfork, sys_wait};
// IMPORTANT: Must define main() like this // IMPORTANT: Must define main() like this
#[no_mangle] #[no_mangle]

@ -5,7 +5,7 @@
#[macro_use] #[macro_use]
extern crate rcore_user; extern crate rcore_user;
use rcore_user::syscall::{sys_arch_prctl, sys_vfork, sys_getpid, sys_sleep}; use rcore_user::syscall::{sys_arch_prctl, sys_getpid, sys_sleep, sys_vfork};
fn set_tls(tls: usize, pid: usize) { fn set_tls(tls: usize, pid: usize) {
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]

Loading…
Cancel
Save