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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d24ad3 was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

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