[1493811] | 1 | /*
|
---|
[f05edcb] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[1493811] | 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>
|
---|
[081971b] | 40 | #include <macros.h>
|
---|
[1493811] | 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. */
|
---|
[b7fd2a0] | 49 | errno_t eth_pdu_encode(eth_frame_t *frame, void **rdata, size_t *rsize)
|
---|
[1493811] | 50 | {
|
---|
| 51 | void *data;
|
---|
| 52 | size_t size;
|
---|
| 53 | eth_header_t *hdr;
|
---|
| 54 |
|
---|
[081971b] | 55 | size = max(sizeof(eth_header_t) + frame->size, ETH_FRAME_MIN_SIZE);
|
---|
[1493811] | 56 |
|
---|
| 57 | data = calloc(size, 1);
|
---|
| 58 | if (data == NULL)
|
---|
| 59 | return ENOMEM;
|
---|
| 60 |
|
---|
| 61 | hdr = (eth_header_t *)data;
|
---|
[b4edc96] | 62 | eth_addr_encode(&frame->src, hdr->src);
|
---|
| 63 | eth_addr_encode(&frame->dest, hdr->dest);
|
---|
[1493811] | 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 |
|
---|
[a1a101d] | 69 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoded Ethernet frame (%zu bytes)", size);
|
---|
[1cc8b42] | 70 |
|
---|
[1493811] | 71 | *rdata = data;
|
---|
| 72 | *rsize = size;
|
---|
| 73 | return EOK;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /** Decode Ethernet PDU. */
|
---|
[b7fd2a0] | 77 | errno_t eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
|
---|
[1493811] | 78 | {
|
---|
| 79 | eth_header_t *hdr;
|
---|
| 80 |
|
---|
[a1a101d] | 81 | log_msg(LOG_DEFAULT, LVL_DEBUG, "eth_pdu_decode()");
|
---|
[1493811] | 82 |
|
---|
| 83 | if (size < sizeof(eth_header_t)) {
|
---|
[a1a101d] | 84 | log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
|
---|
[1493811] | 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 |
|
---|
[b4edc96] | 95 | eth_addr_decode(hdr->src, &frame->src);
|
---|
| 96 | eth_addr_decode(hdr->dest, &frame->dest);
|
---|
[1493811] | 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 |
|
---|
[a1a101d] | 102 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoded Ethernet frame payload (%zu bytes)", frame->size);
|
---|
[df15e5f] | 103 |
|
---|
[1493811] | 104 | return EOK;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[87e5658c] | 107 | /** Encode ARP PDU. */
|
---|
[b7fd2a0] | 108 | errno_t arp_pdu_encode(arp_eth_packet_t *packet, void **rdata, size_t *rsize)
|
---|
[87e5658c] | 109 | {
|
---|
| 110 | void *data;
|
---|
| 111 | size_t size;
|
---|
| 112 | arp_eth_packet_fmt_t *pfmt;
|
---|
| 113 | uint16_t fopcode;
|
---|
| 114 |
|
---|
[a1a101d] | 115 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_encode()");
|
---|
[87e5658c] | 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) {
|
---|
[3bacee1] | 126 | case aop_request:
|
---|
| 127 | fopcode = AOP_REQUEST;
|
---|
| 128 | break;
|
---|
| 129 | case aop_reply:
|
---|
| 130 | fopcode = AOP_REPLY;
|
---|
| 131 | break;
|
---|
[87e5658c] | 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);
|
---|
[b4edc96] | 142 | eth_addr_encode(&packet->sender_hw_addr, pfmt->sender_hw_addr);
|
---|
[87e5658c] | 143 | pfmt->sender_proto_addr =
|
---|
[a2e3ee6] | 144 | host2uint32_t_be(packet->sender_proto_addr);
|
---|
[b4edc96] | 145 | eth_addr_encode(&packet->target_hw_addr, pfmt->target_hw_addr);
|
---|
[87e5658c] | 146 | pfmt->target_proto_addr =
|
---|
[a2e3ee6] | 147 | host2uint32_t_be(packet->target_proto_addr);
|
---|
[87e5658c] | 148 |
|
---|
| 149 | *rdata = data;
|
---|
| 150 | *rsize = size;
|
---|
| 151 | return EOK;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /** Decode ARP PDU. */
|
---|
[b7fd2a0] | 155 | errno_t arp_pdu_decode(void *data, size_t size, arp_eth_packet_t *packet)
|
---|
[87e5658c] | 156 | {
|
---|
| 157 | arp_eth_packet_fmt_t *pfmt;
|
---|
| 158 |
|
---|
[a1a101d] | 159 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_decode()");
|
---|
[87e5658c] | 160 |
|
---|
| 161 | if (size < sizeof(arp_eth_packet_fmt_t)) {
|
---|
[a1a101d] | 162 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU too short (%zu)", size);
|
---|
[87e5658c] | 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) {
|
---|
[a1a101d] | 169 | log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address space != %u (%" PRIu16 ")",
|
---|
[87e5658c] | 170 | AHRD_ETHERNET, uint16_t_be2host(pfmt->hw_addr_space));
|
---|
| 171 | return EINVAL;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[1cc8b42] | 174 | if (uint16_t_be2host(pfmt->proto_addr_space) != 0x0800) {
|
---|
[a1a101d] | 175 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address space != %u (%" PRIu16 ")",
|
---|
[87e5658c] | 176 | ETYPE_IP, uint16_t_be2host(pfmt->proto_addr_space));
|
---|
| 177 | return EINVAL;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if (pfmt->hw_addr_size != ETH_ADDR_SIZE) {
|
---|
[a1a101d] | 181 | log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address size != %zu (%zu)",
|
---|
[87e5658c] | 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) {
|
---|
[a1a101d] | 187 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address size != %zu (%zu)",
|
---|
[87e5658c] | 188 | (size_t)IPV4_ADDR_SIZE, (size_t)pfmt->proto_addr_size);
|
---|
| 189 | return EINVAL;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | switch (uint16_t_be2host(pfmt->opcode)) {
|
---|
[3bacee1] | 193 | case AOP_REQUEST:
|
---|
| 194 | packet->opcode = aop_request;
|
---|
| 195 | break;
|
---|
| 196 | case AOP_REPLY:
|
---|
| 197 | packet->opcode = aop_reply;
|
---|
| 198 | break;
|
---|
[87e5658c] | 199 | default:
|
---|
[a1a101d] | 200 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Invalid ARP opcode (%" PRIu16 ")",
|
---|
[87e5658c] | 201 | uint16_t_be2host(pfmt->opcode));
|
---|
| 202 | return EINVAL;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[b4edc96] | 205 | eth_addr_decode(pfmt->sender_hw_addr, &packet->sender_hw_addr);
|
---|
[a2e3ee6] | 206 | packet->sender_proto_addr =
|
---|
[87e5658c] | 207 | uint32_t_be2host(pfmt->sender_proto_addr);
|
---|
[b4edc96] | 208 | eth_addr_decode(pfmt->target_hw_addr, &packet->target_hw_addr);
|
---|
[a2e3ee6] | 209 | packet->target_proto_addr =
|
---|
[87e5658c] | 210 | uint32_t_be2host(pfmt->target_proto_addr);
|
---|
[a1a101d] | 211 | log_msg(LOG_DEFAULT, LVL_DEBUG, "packet->tpa = %x\n", pfmt->target_proto_addr);
|
---|
[87e5658c] | 212 |
|
---|
| 213 | return EOK;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[1493811] | 216 | /** @}
|
---|
| 217 | */
|
---|