1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup inet
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <byteorder.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <macros.h>
|
---|
41 | #include <mem.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 |
|
---|
44 | #include "ethip.h"
|
---|
45 | #include "std.h"
|
---|
46 | #include "pdu.h"
|
---|
47 |
|
---|
48 | /** Encode Ethernet PDU. */
|
---|
49 | errno_t eth_pdu_encode(eth_frame_t *frame, void **rdata, size_t *rsize)
|
---|
50 | {
|
---|
51 | void *data;
|
---|
52 | size_t size;
|
---|
53 | eth_header_t *hdr;
|
---|
54 |
|
---|
55 | size = max(sizeof(eth_header_t) + frame->size, ETH_FRAME_MIN_SIZE);
|
---|
56 |
|
---|
57 | data = calloc(size, 1);
|
---|
58 | if (data == NULL)
|
---|
59 | return ENOMEM;
|
---|
60 |
|
---|
61 | hdr = (eth_header_t *)data;
|
---|
62 | eth_addr_encode(&frame->src, hdr->src);
|
---|
63 | eth_addr_encode(&frame->dest, hdr->dest);
|
---|
64 | hdr->etype_len = host2uint16_t_be(frame->etype_len);
|
---|
65 |
|
---|
66 | memcpy((uint8_t *)data + sizeof(eth_header_t), frame->data,
|
---|
67 | frame->size);
|
---|
68 |
|
---|
69 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoded Ethernet frame (%zu bytes)", size);
|
---|
70 |
|
---|
71 | *rdata = data;
|
---|
72 | *rsize = size;
|
---|
73 | return EOK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Decode Ethernet PDU. */
|
---|
77 | errno_t eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
|
---|
78 | {
|
---|
79 | eth_header_t *hdr;
|
---|
80 |
|
---|
81 | log_msg(LOG_DEFAULT, LVL_DEBUG, "eth_pdu_decode()");
|
---|
82 |
|
---|
83 | if (size < sizeof(eth_header_t)) {
|
---|
84 | log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
|
---|
85 | return EINVAL;
|
---|
86 | }
|
---|
87 |
|
---|
88 | hdr = (eth_header_t *)data;
|
---|
89 |
|
---|
90 | frame->size = size - sizeof(eth_header_t);
|
---|
91 | frame->data = calloc(frame->size, 1);
|
---|
92 | if (frame->data == NULL)
|
---|
93 | return ENOMEM;
|
---|
94 |
|
---|
95 | eth_addr_decode(hdr->src, &frame->src);
|
---|
96 | eth_addr_decode(hdr->dest, &frame->dest);
|
---|
97 | frame->etype_len = uint16_t_be2host(hdr->etype_len);
|
---|
98 |
|
---|
99 | memcpy(frame->data, (uint8_t *)data + sizeof(eth_header_t),
|
---|
100 | frame->size);
|
---|
101 |
|
---|
102 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoded Ethernet frame payload (%zu bytes)", frame->size);
|
---|
103 |
|
---|
104 | return EOK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** Encode ARP PDU. */
|
---|
108 | errno_t arp_pdu_encode(arp_eth_packet_t *packet, void **rdata, size_t *rsize)
|
---|
109 | {
|
---|
110 | void *data;
|
---|
111 | size_t size;
|
---|
112 | arp_eth_packet_fmt_t *pfmt;
|
---|
113 | uint16_t fopcode;
|
---|
114 |
|
---|
115 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_encode()");
|
---|
116 |
|
---|
117 | size = sizeof(arp_eth_packet_fmt_t);
|
---|
118 |
|
---|
119 | data = calloc(size, 1);
|
---|
120 | if (data == NULL)
|
---|
121 | return ENOMEM;
|
---|
122 |
|
---|
123 | pfmt = (arp_eth_packet_fmt_t *)data;
|
---|
124 |
|
---|
125 | switch (packet->opcode) {
|
---|
126 | case aop_request:
|
---|
127 | fopcode = AOP_REQUEST;
|
---|
128 | break;
|
---|
129 | case aop_reply:
|
---|
130 | fopcode = AOP_REPLY;
|
---|
131 | break;
|
---|
132 | default:
|
---|
133 | assert(false);
|
---|
134 | fopcode = 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | pfmt->hw_addr_space = host2uint16_t_be(AHRD_ETHERNET);
|
---|
138 | pfmt->proto_addr_space = host2uint16_t_be(ETYPE_IP);
|
---|
139 | pfmt->hw_addr_size = ETH_ADDR_SIZE;
|
---|
140 | pfmt->proto_addr_size = IPV4_ADDR_SIZE;
|
---|
141 | pfmt->opcode = host2uint16_t_be(fopcode);
|
---|
142 | eth_addr_encode(&packet->sender_hw_addr, pfmt->sender_hw_addr);
|
---|
143 | pfmt->sender_proto_addr =
|
---|
144 | host2uint32_t_be(packet->sender_proto_addr);
|
---|
145 | eth_addr_encode(&packet->target_hw_addr, pfmt->target_hw_addr);
|
---|
146 | pfmt->target_proto_addr =
|
---|
147 | host2uint32_t_be(packet->target_proto_addr);
|
---|
148 |
|
---|
149 | *rdata = data;
|
---|
150 | *rsize = size;
|
---|
151 | return EOK;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /** Decode ARP PDU. */
|
---|
155 | errno_t arp_pdu_decode(void *data, size_t size, arp_eth_packet_t *packet)
|
---|
156 | {
|
---|
157 | arp_eth_packet_fmt_t *pfmt;
|
---|
158 |
|
---|
159 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_decode()");
|
---|
160 |
|
---|
161 | if (size < sizeof(arp_eth_packet_fmt_t)) {
|
---|
162 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU too short (%zu)", size);
|
---|
163 | return EINVAL;
|
---|
164 | }
|
---|
165 |
|
---|
166 | pfmt = (arp_eth_packet_fmt_t *)data;
|
---|
167 |
|
---|
168 | if (uint16_t_be2host(pfmt->hw_addr_space) != AHRD_ETHERNET) {
|
---|
169 | log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address space != %u (%" PRIu16 ")",
|
---|
170 | AHRD_ETHERNET, uint16_t_be2host(pfmt->hw_addr_space));
|
---|
171 | return EINVAL;
|
---|
172 | }
|
---|
173 |
|
---|
174 | if (uint16_t_be2host(pfmt->proto_addr_space) != 0x0800) {
|
---|
175 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address space != %u (%" PRIu16 ")",
|
---|
176 | ETYPE_IP, uint16_t_be2host(pfmt->proto_addr_space));
|
---|
177 | return EINVAL;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (pfmt->hw_addr_size != ETH_ADDR_SIZE) {
|
---|
181 | log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address size != %zu (%zu)",
|
---|
182 | (size_t)ETH_ADDR_SIZE, (size_t)pfmt->hw_addr_size);
|
---|
183 | return EINVAL;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (pfmt->proto_addr_size != IPV4_ADDR_SIZE) {
|
---|
187 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address size != %zu (%zu)",
|
---|
188 | (size_t)IPV4_ADDR_SIZE, (size_t)pfmt->proto_addr_size);
|
---|
189 | return EINVAL;
|
---|
190 | }
|
---|
191 |
|
---|
192 | switch (uint16_t_be2host(pfmt->opcode)) {
|
---|
193 | case AOP_REQUEST:
|
---|
194 | packet->opcode = aop_request;
|
---|
195 | break;
|
---|
196 | case AOP_REPLY:
|
---|
197 | packet->opcode = aop_reply;
|
---|
198 | break;
|
---|
199 | default:
|
---|
200 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Invalid ARP opcode (%" PRIu16 ")",
|
---|
201 | uint16_t_be2host(pfmt->opcode));
|
---|
202 | return EINVAL;
|
---|
203 | }
|
---|
204 |
|
---|
205 | eth_addr_decode(pfmt->sender_hw_addr, &packet->sender_hw_addr);
|
---|
206 | packet->sender_proto_addr =
|
---|
207 | uint32_t_be2host(pfmt->sender_proto_addr);
|
---|
208 | eth_addr_decode(pfmt->target_hw_addr, &packet->target_hw_addr);
|
---|
209 | packet->target_proto_addr =
|
---|
210 | uint32_t_be2host(pfmt->target_proto_addr);
|
---|
211 | log_msg(LOG_DEFAULT, LVL_DEBUG, "packet->tpa = %x\n", pfmt->target_proto_addr);
|
---|
212 |
|
---|
213 | return EOK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** @}
|
---|
217 | */
|
---|