Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
icmp.c
Go to the documentation of this file.
1#include "icmp.h"
2#include "libk/serial.h"
3#include <str.h>
4#include "net/ethernet.h"
5#include "net/ip_type.h"
6#include "net/ipv4.h"
7#include "net/netbuff.h"
8#include "net/socket.h"
9#include "netutils.h"
10
11void handle_icmp(netdev_t* dev, struct ipv4_header* ip, uint8_t mac_dst[6]) {
12
13 uint8_t ihl = ip->version_ihl & 0x0F;
14 uint16_t ip_hdr_len = ihl * 4;
15
16 uint16_t total_len = vxNtohs(ip->total_length);
17 if (total_len < ip_hdr_len + 8)
18 return; // minimal ICMP
19
20 struct icmp_header* icmp =
21 (struct icmp_header*) ((uint8_t*) ip + ip_hdr_len);
22
23 uint16_t icmp_len = total_len - ip_hdr_len;
24
25 // echo request
26 char ip_buf[16];
27 vxInetNtoa(ip->src_ip, ip_buf);
28
29 uint16_t frag_field = vxNtohs(ip->flags_fragment);
30
31 // 1. Cek bit "More Fragments" (MF)
32 // Bit ini ada di posisi 0x2000
33 bool has_more_fragments = (frag_field & 0x2000);
34
35 // 2. Cek "Fragment Offset"
36 // Offset ada di 13 bit bawah (0x1FFF)
37 uint16_t fragment_offset = (frag_field & 0x1FFF);
38
39 if (has_more_fragments || fragment_offset > 0) {
40 LOG2_WARN("ICMP", "packet ini terfragmentasi");
41 // TODO: handle reassembly untuk packet besar
42 return;
43 }
44
45 if (icmp->type == 8) {
46 auto nb = create_netbuff();
47
48 // Alokasi space untuk ICMP payload + header
49 struct icmp_echo* icmp_reply =
50 (struct icmp_echo*) netbuff_put(nb, icmp_len);
51
52 // Copy data dari ICMP request
53 memcopy(icmp_reply, icmp, icmp_len);
54
55 // Ubah jadi Echo Reply
56 icmp_reply->type = 0;
57 icmp_reply->checksum = 0;
58 icmp_reply->checksum = checksum16_adc(
59 (uint16_t*) (void*) icmp_reply, icmp_len);
60
61 // Kirim ke layer IP
62 ipv4_send(dev, nb, ip->src_ip, 1,
63 mac_dst); // 1 adalah protokol ICMP
64
65 free_netbuff(nb);
66 }
67}
void handle_icmp(netdev_t *dev, struct ipv4_header *ip, uint8_t mac_dst[6])
Definition icmp.c:11
void ipv4_send(netdev_t *dev, struct netbuff *nb, uint32_t dst_ip, uint8_t protocol, uint8_t mac_dest[6])
Definition ipv4.c:32
void free_netbuff(struct netbuff *netbuff)
Definition netbuff.c:56
void * netbuff_put(struct netbuff *nb, size_t len)
Definition netbuff.c:42
struct netbuff * create_netbuff()
Definition netbuff.c:19
struct netdev netdev_t
Definition netdev.h:19
char * vxInetNtoa(uint32_t ip, char *buffer)
Definition netutils.c:78
uint16_t checksum16_adc(const uint16_t *data, size_t length)
Definition netutils.c:26
static uint16_t vxNtohs(uint16_t netshort)
Definition netutils.h:10
#define LOG2_WARN(mod, fmt,...)
Definition serial.h:40
void memcopy(void *dest, void *src, size_t size)
uint16_t checksum
Definition icmp.h:17
uint8_t type
Definition icmp.h:15
uint8_t type
Definition icmp.h:9
uint16_t total_length
Definition ipv4.h:11
uint32_t src_ip
Definition ipv4.h:17
uint16_t flags_fragment
Definition ipv4.h:13
uint8_t version_ihl
Definition ipv4.h:9
unsigned short uint16_t
Definition type.h:13
unsigned char uint8_t
Definition type.h:7