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

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

accept multicast datagrams (thx Antonin Steinhauser)

  • 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/** Encode Ethernet PDU. */
49int 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
55 size = max(sizeof(eth_header_t) + frame->size, ETH_FRAME_MIN_SIZE);
56
57 data = calloc(size, 1);
58 if (data == NULL)
59 return ENOMEM;
60
61 hdr = (eth_header_t *)data;
62 addr48(frame->src, hdr->src);
63 addr48(frame->dest, hdr->dest);
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
69 log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoded Ethernet frame (%zu bytes)", size);
70
71 *rdata = data;
72 *rsize = size;
73 return EOK;
74}
75
76/** Decode Ethernet PDU. */
77int eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
78{
79 eth_header_t *hdr;
80
81 log_msg(LOG_DEFAULT, LVL_DEBUG, "eth_pdu_decode()");
82
83 if (size < sizeof(eth_header_t)) {
84 log_msg(LOG_DEFAULT, LVL_DEBUG, "PDU too short (%zu)", size);
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
95 addr48(hdr->src, frame->src);
96 addr48(hdr->dest, frame->dest);
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
102 log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoded Ethernet frame payload (%zu bytes)", frame->size);
103
104 return EOK;
105}
106
107/** Encode ARP PDU. */
108int 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
115 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_encode()");
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);
138 addr48(packet->sender_hw_addr, pfmt->sender_hw_addr);
139 pfmt->sender_proto_addr =
140 host2uint32_t_be(packet->sender_proto_addr);
141 addr48(packet->target_hw_addr, pfmt->target_hw_addr);
142 pfmt->target_proto_addr =
143 host2uint32_t_be(packet->target_proto_addr);
144
145 *rdata = data;
146 *rsize = size;
147 return EOK;
148}
149
150/** Decode ARP PDU. */
151int arp_pdu_decode(void *data, size_t size, arp_eth_packet_t *packet)
152{
153 arp_eth_packet_fmt_t *pfmt;
154
155 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_pdu_decode()");
156
157 if (size < sizeof(arp_eth_packet_fmt_t)) {
158 log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU too short (%zu)", size);
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) {
165 log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address space != %u (%" PRIu16 ")",
166 AHRD_ETHERNET, uint16_t_be2host(pfmt->hw_addr_space));
167 return EINVAL;
168 }
169
170 if (uint16_t_be2host(pfmt->proto_addr_space) != 0x0800) {
171 log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address space != %u (%" PRIu16 ")",
172 ETYPE_IP, uint16_t_be2host(pfmt->proto_addr_space));
173 return EINVAL;
174 }
175
176 if (pfmt->hw_addr_size != ETH_ADDR_SIZE) {
177 log_msg(LOG_DEFAULT, LVL_DEBUG, "HW address size != %zu (%zu)",
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) {
183 log_msg(LOG_DEFAULT, LVL_DEBUG, "Proto address size != %zu (%zu)",
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:
192 log_msg(LOG_DEFAULT, LVL_DEBUG, "Invalid ARP opcode (%" PRIu16 ")",
193 uint16_t_be2host(pfmt->opcode));
194 return EINVAL;
195 }
196
197 addr48(pfmt->sender_hw_addr, packet->sender_hw_addr);
198 packet->sender_proto_addr =
199 uint32_t_be2host(pfmt->sender_proto_addr);
200 addr48(pfmt->target_hw_addr, packet->target_hw_addr);
201 packet->target_proto_addr =
202 uint32_t_be2host(pfmt->target_proto_addr);
203 log_msg(LOG_DEFAULT, LVL_DEBUG, "packet->tpa = %x\n", pfmt->target_proto_addr);
204
205 return EOK;
206}
207
208/** @}
209 */
Note: See TracBrowser for help on using the repository browser.