source: mainline/uspace/srv/net/ethip/pdu.c@ 4daee7a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4daee7a was 70253688, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Merge mainline changes

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[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#define MAC48_BYTES 6
49
50/** Encode Ethernet PDU. */
51int eth_pdu_encode(eth_frame_t *frame, void **rdata, size_t *rsize)
52{
53 void *data;
54 size_t size;
55 eth_header_t *hdr;
56
[081971b]57 size = max(sizeof(eth_header_t) + frame->size, ETH_FRAME_MIN_SIZE);
[1493811]58
59 data = calloc(size, 1);
60 if (data == NULL)
61 return ENOMEM;
62
63 hdr = (eth_header_t *)data;
64 mac48_encode(&frame->src, hdr->src);
65 mac48_encode(&frame->dest, hdr->dest);
66 hdr->etype_len = host2uint16_t_be(frame->etype_len);
67
68 memcpy((uint8_t *)data + sizeof(eth_header_t), frame->data,
69 frame->size);
70
[70253688]71 log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoding Ethernet frame "
[f0a2720]72 "src=%" PRIx64 " dest=%" PRIx64 " etype=%x",
73 frame->src.addr, frame->dest.addr, frame->etype_len);
[a1a101d]74 log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoded Ethernet frame (%zu bytes)", size);
[1cc8b42]75
[1493811]76 *rdata = data;
77 *rsize = size;
78 return EOK;
79}
80
81/** Decode Ethernet PDU. */
82int eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
83{
84 eth_header_t *hdr;
85
[a1a101d]86 log_msg(LOG_DEFAULT, LVL_DEBUG, "eth_pdu_decode()");
[1493811]87
88 if (size < sizeof(eth_header_t)) {
[a1a101d]89 log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
[1493811]90 return EINVAL;
91 }
92
93 hdr = (eth_header_t *)data;
94
95 frame->size = size - sizeof(eth_header_t);
96 frame->data = calloc(frame->size, 1);
97 if (frame->data == NULL)
98 return ENOMEM;
99
100 mac48_decode(hdr->src, &frame->src);
101 mac48_decode(hdr->dest, &frame->dest);
102 frame->etype_len = uint16_t_be2host(hdr->etype_len);
103
104 memcpy(frame->data, (uint8_t *)data + sizeof(eth_header_t),
105 frame->size);
106
[70253688]107 log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoding Ethernet frame "
[f0a2720]108 "src=%" PRIx64 " dest=%" PRIx64 " etype=%x",
109 frame->src.addr, frame->dest.addr, frame->etype_len);
[a1a101d]110 log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoded Ethernet frame payload (%zu bytes)", frame->size);
[df15e5f]111
[1493811]112 return EOK;
113}
114
[56792a2]115void mac48_encode(mac48_addr_t *addr, void *buf)
[1493811]116{
117 uint64_t val;
118 uint8_t *bbuf = (uint8_t *)buf;
119 int i;
120
121 val = addr->addr;
[081971b]122 for (i = 0; i < MAC48_BYTES; i++)
[df15e5f]123 bbuf[i] = (val >> (8 * (MAC48_BYTES - i - 1))) & 0xff;
[1493811]124}
125
[56792a2]126void mac48_decode(void *data, mac48_addr_t *addr)
[1493811]127{
128 uint64_t val;
129 uint8_t *bdata = (uint8_t *)data;
130 int i;
131
132 val = 0;
133 for (i = 0; i < MAC48_BYTES; i++)
[df15e5f]134 val |= (uint64_t)bdata[i] << (8 * (MAC48_BYTES - i - 1));
[1493811]135
136 addr->addr = val;
137}
138
[87e5658c]139/** Encode ARP PDU. */
140int arp_pdu_encode(arp_eth_packet_t *packet, void **rdata, size_t *rsize)
141{
142 void *data;
143 size_t size;
144 arp_eth_packet_fmt_t *pfmt;
145 uint16_t fopcode;
146
[a1a101d]147 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_encode()");
[87e5658c]148
149 size = sizeof(arp_eth_packet_fmt_t);
150
151 data = calloc(size, 1);
152 if (data == NULL)
153 return ENOMEM;
154
155 pfmt = (arp_eth_packet_fmt_t *)data;
156
157 switch (packet->opcode) {
158 case aop_request: fopcode = AOP_REQUEST; break;
159 case aop_reply: fopcode = AOP_REPLY; break;
160 default:
161 assert(false);
162 fopcode = 0;
163 }
164
165 pfmt->hw_addr_space = host2uint16_t_be(AHRD_ETHERNET);
166 pfmt->proto_addr_space = host2uint16_t_be(ETYPE_IP);
167 pfmt->hw_addr_size = ETH_ADDR_SIZE;
168 pfmt->proto_addr_size = IPV4_ADDR_SIZE;
169 pfmt->opcode = host2uint16_t_be(fopcode);
170 mac48_encode(&packet->sender_hw_addr, pfmt->sender_hw_addr);
171 pfmt->sender_proto_addr =
172 host2uint32_t_be(packet->sender_proto_addr.ipv4);
173 mac48_encode(&packet->target_hw_addr, pfmt->target_hw_addr);
174 pfmt->target_proto_addr =
175 host2uint32_t_be(packet->target_proto_addr.ipv4);
176
177 *rdata = data;
178 *rsize = size;
179 return EOK;
180}
181
182/** Decode ARP PDU. */
183int arp_pdu_decode(void *data, size_t size, arp_eth_packet_t *packet)
184{
185 arp_eth_packet_fmt_t *pfmt;
186
[a1a101d]187 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_decode()");
[87e5658c]188
189 if (size < sizeof(arp_eth_packet_fmt_t)) {
[a1a101d]190 log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU too short (%zu)", size);
[87e5658c]191 return EINVAL;
192 }
193
194 pfmt = (arp_eth_packet_fmt_t *)data;
195
196 if (uint16_t_be2host(pfmt->hw_addr_space) != AHRD_ETHERNET) {
[a1a101d]197 log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address space != %u (%" PRIu16 ")",
[87e5658c]198 AHRD_ETHERNET, uint16_t_be2host(pfmt->hw_addr_space));
199 return EINVAL;
200 }
201
[1cc8b42]202 if (uint16_t_be2host(pfmt->proto_addr_space) != 0x0800) {
[a1a101d]203 log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address space != %u (%" PRIu16 ")",
[87e5658c]204 ETYPE_IP, uint16_t_be2host(pfmt->proto_addr_space));
205 return EINVAL;
206 }
207
208 if (pfmt->hw_addr_size != ETH_ADDR_SIZE) {
[a1a101d]209 log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address size != %zu (%zu)",
[87e5658c]210 (size_t)ETH_ADDR_SIZE, (size_t)pfmt->hw_addr_size);
211 return EINVAL;
212 }
213
214 if (pfmt->proto_addr_size != IPV4_ADDR_SIZE) {
[a1a101d]215 log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address size != %zu (%zu)",
[87e5658c]216 (size_t)IPV4_ADDR_SIZE, (size_t)pfmt->proto_addr_size);
217 return EINVAL;
218 }
219
220 switch (uint16_t_be2host(pfmt->opcode)) {
221 case AOP_REQUEST: packet->opcode = aop_request; break;
222 case AOP_REPLY: packet->opcode = aop_reply; break;
223 default:
[a1a101d]224 log_msg(LOG_DEFAULT, LVL_DEBUG, "Invalid ARP opcode (%" PRIu16 ")",
[87e5658c]225 uint16_t_be2host(pfmt->opcode));
226 return EINVAL;
227 }
228
229 mac48_decode(pfmt->sender_hw_addr, &packet->sender_hw_addr);
230 packet->sender_proto_addr.ipv4 =
231 uint32_t_be2host(pfmt->sender_proto_addr);
232 mac48_decode(pfmt->target_hw_addr, &packet->target_hw_addr);
233 packet->target_proto_addr.ipv4 =
234 uint32_t_be2host(pfmt->target_proto_addr);
[a1a101d]235 log_msg(LOG_DEFAULT, LVL_DEBUG, "packet->tpa = %x\n", pfmt->target_proto_addr);
[87e5658c]236
237 return EOK;
238}
239
240
[1493811]241/** @}
242 */
Note: See TracBrowser for help on using the repository browser.