source: mainline/uspace/srv/net/ethip/pdu.c@ 0d1a8fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0d1a8fd was 96c0b7b, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Remove debug dumps of frame contents.

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