AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ether.c
Go to the documentation of this file.
1
8
9#include <core/system.h>
10#include <core/string.h>
11#include <net/byteorder.h>
12#include <net/inet.h>
13
21void ether_input(struct netif *netif, struct netbuf *nb)
22{
23 struct ether_hdr *eh = netbuf_pull(nb, sizeof(*eh));
24
25 if (!eh) {
26 netif->stats.rx_errors++;
27 netbuf_free(nb);
28 return;
29 }
30
31 switch (ntohs(eh->type)) {
32 case ETHER_TYPE_ARP:
33 arp_input(netif, nb);
34 return;
35 case ETHER_TYPE_IPV4:
36 ipv4_input(netif, nb, eh->src);
37 return;
38 }
39
40 netif->stats.rx_dropped++;
41 netbuf_free(nb);
42}
43
54int ether_output(struct netif *netif, const uint8_t dst[6], uint16_t type, struct netbuf *nb)
55{
56 struct ether_hdr *eh = netbuf_push(nb, sizeof(*eh));
57
58 memcpy(eh->dst, dst, 6);
59 memcpy(eh->src, netif->mac, 6);
60 eh->type = htons(type);
61
62 int err = netif_output(netif, nb);
63 netbuf_free(nb);
64 return err;
65}
66
76uint16_t inet_checksum(const void *data, size_t len)
77{
78 const uint8_t *p = data;
79 uint32_t sum = 0;
80
81 for (; len > 1; len -= 2, p += 2)
82 sum += (uint32_t) p[0] << 8 | p[1];
83
84 if (len)
85 sum += (uint32_t) p[0] << 8;
86
87 while (sum >> 16)
88 sum = (sum & 0xffff) + (sum >> 16);
89
90 return htons(~sum & 0xffff);
91}
92
106uint16_t inet_checksum_pseudo(uint32_t src, uint32_t dst, uint8_t proto, const void *data, size_t len)
107{
108 struct {
110 uint8_t zero, proto;
111 uint16_t len;
112 } __packed pseudo = {src, dst, 0, proto, htons(len)};
113
114 const uint8_t *p = (const uint8_t *) &pseudo;
115 uint32_t sum = 0;
116
117 for (size_t i = 0; i < sizeof(pseudo); i += 2)
118 sum += (uint32_t) p[i] << 8 | p[i + 1];
119
120 for (p = data; len > 1; len -= 2, p += 2)
121 sum += (uint32_t) p[0] << 8 | p[1];
122
123 if (len)
124 sum += (uint32_t) p[0] << 8;
125
126 while (sum >> 16)
127 sum = (sum & 0xffff) + (sum >> 16);
128
129 return htons(~sum & 0xffff);
130}
#define htons(x)
Definition byteorder.h:10
#define ntohs(x)
Definition byteorder.h:17
#define __packed
Definition system.h:30
int ether_output(struct netif *netif, const uint8_t dst[6], uint16_t type, struct netbuf *nb)
Sends a frame: adds its Ethernet header and gives it to the interface.
Definition ether.c:54
uint16_t inet_checksum(const void *data, size_t len)
Computes the Internet checksum (RFC 1071): the ones' complement of the ones' complement sum of the da...
Definition ether.c:76
#define ETHER_TYPE_ARP
Definition inet.h:18
void ether_input(struct netif *netif, struct netbuf *nb)
Takes in a frame that an interface received, and passes it to ARP or IPv4.
Definition ether.c:21
void ipv4_input(struct netif *netif, struct netbuf *nb, const uint8_t src_mac[6])
Takes in an IPv4 packet that an interface received, and passes it to its protocol.
Definition ipv4.c:84
uint16_t inet_checksum_pseudo(uint32_t src, uint32_t dst, uint8_t proto, const void *data, size_t len)
Computes the Internet checksum of data with the IPv4 pseudo-header before it (the addresses,...
Definition ether.c:106
#define ETHER_TYPE_IPV4
Definition inet.h:17
void arp_input(struct netif *netif, struct netbuf *nb)
Takes in an ARP packet: the sender's address is recorded, and a request for our address is answered.
Definition arp.c:244
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
Definition libc.c:11
static void * netbuf_push(struct netbuf *nb, size_t size)
Makes room for a header of size bytes before the data, and returns it.
Definition netbuf.h:52
static void * netbuf_pull(struct netbuf *nb, size_t size)
Takes the header of size bytes off the front of the packet, and returns it, or NULL if the packet is ...
Definition netbuf.h:64
void netbuf_free(struct netbuf *nb)
Frees a netbuf.
Definition netbuf.c:99
int netif_output(struct netif *netif, struct netbuf *nb)
Sends a frame through an interface's driver.
Definition netif.c:358
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned short int uint16_t
Definition stddef.h:21
An Ethernet frame's header.
Definition inet.h:21
uint8_t src[6]
Definition inet.h:23
uint8_t dst[6]
Definition inet.h:22
uint16_t type
Definition inet.h:24
A packet (net/netbuf.c).
Definition netbuf.h:16
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition netif.h:16
struct netif::@020302303320120142126121344055047003105154122231 stats
uint8_t mac[6]
Definition netif.h:18