[1493811] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 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>
|
---|
[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. */
|
---|
| 49 | int 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 |
|
---|
[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;
|
---|
[02a09ed] | 62 | addr48(frame->src, hdr->src);
|
---|
| 63 | addr48(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. */
|
---|
| 77 | int eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
|
---|
| 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 |
|
---|
[02a09ed] | 95 | addr48(hdr->src, frame->src);
|
---|
| 96 | addr48(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. */
|
---|
| 108 | int 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 |
|
---|
[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) {
|
---|
| 126 | case aop_request: fopcode = AOP_REQUEST; break;
|
---|
| 127 | case aop_reply: fopcode = AOP_REPLY; break;
|
---|
| 128 | default:
|
---|
| 129 | assert(false);
|
---|
| 130 | fopcode = 0;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | pfmt->hw_addr_space = host2uint16_t_be(AHRD_ETHERNET);
|
---|
| 134 | pfmt->proto_addr_space = host2uint16_t_be(ETYPE_IP);
|
---|
| 135 | pfmt->hw_addr_size = ETH_ADDR_SIZE;
|
---|
| 136 | pfmt->proto_addr_size = IPV4_ADDR_SIZE;
|
---|
| 137 | pfmt->opcode = host2uint16_t_be(fopcode);
|
---|
[02a09ed] | 138 | addr48(packet->sender_hw_addr, pfmt->sender_hw_addr);
|
---|
[87e5658c] | 139 | pfmt->sender_proto_addr =
|
---|
[a2e3ee6] | 140 | host2uint32_t_be(packet->sender_proto_addr);
|
---|
[02a09ed] | 141 | addr48(packet->target_hw_addr, pfmt->target_hw_addr);
|
---|
[87e5658c] | 142 | pfmt->target_proto_addr =
|
---|
[a2e3ee6] | 143 | host2uint32_t_be(packet->target_proto_addr);
|
---|
[87e5658c] | 144 |
|
---|
| 145 | *rdata = data;
|
---|
| 146 | *rsize = size;
|
---|
| 147 | return EOK;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** Decode ARP PDU. */
|
---|
| 151 | int arp_pdu_decode(void *data, size_t size, arp_eth_packet_t *packet)
|
---|
| 152 | {
|
---|
| 153 | arp_eth_packet_fmt_t *pfmt;
|
---|
| 154 |
|
---|
[a1a101d] | 155 | log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_decode()");
|
---|
[87e5658c] | 156 |
|
---|
| 157 | if (size < sizeof(arp_eth_packet_fmt_t)) {
|
---|
[a1a101d] | 158 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU too short (%zu)", size);
|
---|
[87e5658c] | 159 | return EINVAL;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | pfmt = (arp_eth_packet_fmt_t *)data;
|
---|
| 163 |
|
---|
| 164 | if (uint16_t_be2host(pfmt->hw_addr_space) != AHRD_ETHERNET) {
|
---|
[a1a101d] | 165 | log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address space != %u (%" PRIu16 ")",
|
---|
[87e5658c] | 166 | AHRD_ETHERNET, uint16_t_be2host(pfmt->hw_addr_space));
|
---|
| 167 | return EINVAL;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[1cc8b42] | 170 | if (uint16_t_be2host(pfmt->proto_addr_space) != 0x0800) {
|
---|
[a1a101d] | 171 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address space != %u (%" PRIu16 ")",
|
---|
[87e5658c] | 172 | ETYPE_IP, uint16_t_be2host(pfmt->proto_addr_space));
|
---|
| 173 | return EINVAL;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | if (pfmt->hw_addr_size != ETH_ADDR_SIZE) {
|
---|
[a1a101d] | 177 | log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address size != %zu (%zu)",
|
---|
[87e5658c] | 178 | (size_t)ETH_ADDR_SIZE, (size_t)pfmt->hw_addr_size);
|
---|
| 179 | return EINVAL;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | if (pfmt->proto_addr_size != IPV4_ADDR_SIZE) {
|
---|
[a1a101d] | 183 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address size != %zu (%zu)",
|
---|
[87e5658c] | 184 | (size_t)IPV4_ADDR_SIZE, (size_t)pfmt->proto_addr_size);
|
---|
| 185 | return EINVAL;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | switch (uint16_t_be2host(pfmt->opcode)) {
|
---|
| 189 | case AOP_REQUEST: packet->opcode = aop_request; break;
|
---|
| 190 | case AOP_REPLY: packet->opcode = aop_reply; break;
|
---|
| 191 | default:
|
---|
[a1a101d] | 192 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Invalid ARP opcode (%" PRIu16 ")",
|
---|
[87e5658c] | 193 | uint16_t_be2host(pfmt->opcode));
|
---|
| 194 | return EINVAL;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[02a09ed] | 197 | addr48(pfmt->sender_hw_addr, packet->sender_hw_addr);
|
---|
[a2e3ee6] | 198 | packet->sender_proto_addr =
|
---|
[87e5658c] | 199 | uint32_t_be2host(pfmt->sender_proto_addr);
|
---|
[02a09ed] | 200 | addr48(pfmt->target_hw_addr, packet->target_hw_addr);
|
---|
[a2e3ee6] | 201 | packet->target_proto_addr =
|
---|
[87e5658c] | 202 | uint32_t_be2host(pfmt->target_proto_addr);
|
---|
[a1a101d] | 203 | log_msg(LOG_DEFAULT, LVL_DEBUG, "packet->tpa = %x\n", pfmt->target_proto_addr);
|
---|
[87e5658c] | 204 |
|
---|
| 205 | return EOK;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[1493811] | 208 | /** @}
|
---|
| 209 | */
|
---|