-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathethernet.c
More file actions
71 lines (60 loc) · 2.02 KB
/
ethernet.c
File metadata and controls
71 lines (60 loc) · 2.02 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
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <net/arp.h>
#include <net/ipv4.h>
#include <net/logging.h>
#include <stdlib.h>
#include <string.h>
void ethernet_receive_frame(net_interface_t* interface,
uint8_t* data,
uint32_t len)
{
ethernet_header_t frame_header = { 0 };
memcpy(&frame_header, data, sizeof(ethernet_header_t));
frame_header.ethertype = ntohs(frame_header.ethertype);
NET_DEBUG(
"received frame from: %02x:%02x:%02x:%02x:%02x:%02x on interface=%d",
frame_header.src_mac[0],
frame_header.src_mac[1],
frame_header.src_mac[2],
frame_header.src_mac[3],
frame_header.src_mac[4],
frame_header.src_mac[5],
interface->id);
switch (frame_header.ethertype) {
case ETHERTYPE_ARP:
arp_receive_packet(interface,
&data[sizeof(ethernet_header_t)],
len - sizeof(ethernet_header_t));
break;
case ETHERTYPE_IPV4:
ipv4_receive_packet(interface,
&data[sizeof(ethernet_header_t)],
len - sizeof(ethernet_header_t));
break;
default:
NET_DEBUG("unsupported ethernet frame: type=0x%04x",
frame_header.ethertype);
}
// This is because `data` gets allocated in the driver code.
free(data);
}
void ethernet_send_frame(net_interface_t* interface,
uint8_t dst_mac[6],
uint16_t ethertype,
uint8_t* data,
uint32_t len)
{
ethernet_header_t header = { .ethertype = htons(ethertype) };
memcpy(header.src_mac, interface->mac, 6);
memcpy(header.dst_mac, dst_mac, 6);
uint32_t frame_len = sizeof(ethernet_header_t) + len;
if (frame_len < 64) {
frame_len = 64;
}
uint8_t* frame = (uint8_t*)calloc(1, frame_len);
memcpy(frame, &header, sizeof(ethernet_header_t));
memcpy(frame + sizeof(ethernet_header_t), data, len);
interface->driver->transmit(frame, frame_len);
free(frame);
}