source: mainline/uspace/srv/inet/pdu.c@ 66a272f8

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

ICMP echo replying.

  • Property mode set to 100644
File size: 5.1 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 <align.h>
38#include <bitops.h>
39#include <byteorder.h>
40#include <errno.h>
41#include <fibril_synch.h>
42#include <io/log.h>
43#include <mem.h>
44#include <stdlib.h>
45
46#include "inet.h"
47#include "inet_std.h"
48#include "pdu.h"
49
50static FIBRIL_MUTEX_INITIALIZE(ip_ident_lock);
51static uint16_t ip_ident = 0;
52
53/** One's complement addition.
54 *
55 * Result is a + b + carry.
56 */
57static uint16_t inet_ocadd16(uint16_t a, uint16_t b)
58{
59 uint32_t s;
60
61 s = (uint32_t)a + (uint32_t)b;
62 return (s & 0xffff) + (s >> 16);
63}
64
65uint16_t inet_checksum_calc(uint16_t ivalue, void *data, size_t size)
66{
67 uint16_t sum;
68 uint16_t w;
69 size_t words, i;
70 uint8_t *bdata;
71
72 sum = ~ivalue;
73 words = size / 2;
74 bdata = (uint8_t *)data;
75
76 for (i = 0; i < words; i++) {
77 w = ((uint16_t)bdata[2*i] << 8) | bdata[2*i + 1];
78 sum = inet_ocadd16(sum, w);
79 }
80
81 if (size % 2 != 0) {
82 w = ((uint16_t)bdata[2*words] << 8);
83 sum = inet_ocadd16(sum, w);
84 }
85
86 return ~sum;
87}
88
89/** Encode Internet PDU.
90 */
91int inet_pdu_encode(inet_packet_t *packet, void **rdata, size_t *rsize)
92{
93 void *data;
94 size_t size;
95 ip_header_t *hdr;
96 size_t hdr_size;
97 size_t data_offs;
98 uint16_t chksum;
99 uint16_t ident;
100
101 fibril_mutex_lock(&ip_ident_lock);
102 ident = ++ip_ident;
103 fibril_mutex_unlock(&ip_ident_lock);
104
105 hdr_size = sizeof(ip_header_t);
106 size = hdr_size + packet->size;
107 data_offs = ROUND_UP(hdr_size, 4);
108
109 data = calloc(size, 1);
110 if (data == NULL)
111 return ENOMEM;
112
113 hdr = (ip_header_t *)data;
114 hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t));
115 hdr->tos = packet->tos;
116 hdr->tot_len = host2uint16_t_be(size);
117 hdr->id = host2uint16_t_be(ident);
118 hdr->flags_foff = host2uint16_t_be(packet->df ?
119 BIT_V(uint16_t, FF_FLAG_DF) : 0);
120 hdr->ttl = packet->ttl;
121 hdr->proto = packet->proto;
122 hdr->chksum = 0;
123 hdr->src_addr = host2uint32_t_be(packet->src.ipv4);
124 hdr->dest_addr = host2uint32_t_be(packet->dest.ipv4);
125
126 chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *)hdr, hdr_size);
127 hdr->chksum = host2uint16_t_be(chksum);
128
129 memcpy((uint8_t *)data + data_offs, packet->data, packet->size);
130
131 *rdata = data;
132 *rsize = size;
133 return EOK;
134}
135
136int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet)
137{
138 ip_header_t *hdr;
139 size_t tot_len;
140 size_t data_offs;
141 uint8_t version;
142 uint16_t ident;
143
144 log_msg(LVL_DEBUG, "inet_pdu_decode()");
145
146 if (size < sizeof(ip_header_t)) {
147 log_msg(LVL_DEBUG, "PDU too short (%zu)", size);
148 return EINVAL;
149 }
150
151 hdr = (ip_header_t *)data;
152
153 version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h, VI_VERSION_l,
154 hdr->ver_ihl);
155 if (version != 4) {
156 log_msg(LVL_DEBUG, "Version (%d) != 4", version);
157 return EINVAL;
158 }
159
160 tot_len = uint16_t_be2host(hdr->tot_len);
161 if (tot_len < sizeof(ip_header_t)) {
162 log_msg(LVL_DEBUG, "Total Length too small (%zu)", tot_len);
163 return EINVAL;
164 }
165
166 if (tot_len > size) {
167 log_msg(LVL_DEBUG, "Total Length = %zu > PDU size = %zu",
168 tot_len, size);
169 return EINVAL;
170 }
171
172 ident = uint16_t_be2host(hdr->id);
173 (void)ident;
174 /* XXX Flags */
175 /* XXX Fragment offset */
176 /* XXX Checksum */
177
178 packet->src.ipv4 = uint32_t_be2host(hdr->src_addr);
179 packet->dest.ipv4 = uint32_t_be2host(hdr->dest_addr);
180 packet->tos = hdr->tos;
181 packet->proto = hdr->proto;
182 packet->ttl = hdr->ttl;
183 packet->df = (uint16_t_be2host(hdr->tos) & BIT_V(uint16_t, FF_FLAG_DF))
184 ? 1 : 0;
185
186 /* XXX IP options */
187 data_offs = sizeof(uint32_t) * BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h,
188 VI_IHL_l, hdr->ver_ihl);
189
190 packet->size = tot_len - data_offs;
191 packet->data = calloc(packet->size, 1);
192 if (packet->data == NULL) {
193 log_msg(LVL_WARN, "Out of memory.");
194 return ENOMEM;
195 }
196
197 memcpy(packet->data, (uint8_t *)data + data_offs, packet->size);
198
199 return EOK;
200}
201
202/** @}
203 */
Note: See TracBrowser for help on using the repository browser.