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

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

Move network-related servers to srv/net.

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